maemigh
07-12-2007, 02:31 PM
I searched a way to create multiline tooltips where I can use a '\n' in the string, but I was not able to find anything. So I modified ToolTipRenderer.draw() to support it. This may not be the best way to code it and there might be some bugs, but it seems to work for what I need it to do, so I thought I would share it with everyone and maybe someone will be interested in improving upon it :)
public void draw(String str, int x, int y)
{
if (str == null)
{
String message = WorldWind.retrieveErrMsg("nullValue.StringIsNull");
WorldWind.logger().log(FINE, message);
throw new IllegalArgumentException(message);
}
Color fg;
Color bg;
Insets insets;
GL gl = GLU.getCurrentGL();
if (this.useSystemLookAndFeel)
{
insets = toolTipBorder.getBorderInsets(null);
fg = toolTipFg;
bg = toolTipBg;
}
else
{
if (this.foreground != null)
{
fg = this.foreground;
}
else
{
gl.glGetFloatv(GL.GL_CURRENT_COLOR, compArray, 0);
fg = new Color(compArray[0], compArray[1], compArray[2], compArray[3]);
}
if (this.background != null)
{
bg = this.background;
}
else
{
if (compArray == null)
compArray = new float[4];
Color.RGBtoHSB(fg.getRed(), fg.getGreen(), fg.getBlue(), compArray);
bg = Color.getHSBColor(0, 0, (compArray[2] + 0.5f) % 1f);
}
if (this.insets != null)
insets = this.insets;
else
insets = new Insets(1, 1, 1, 1);
}
String formattedString[] = str.split("\\\\n");
Rectangle2D formattedStringBounds[] = new Rectangle2D[formattedString.length];
double largestWidth = 0;
double totalHeight = 0;
// Calculate the total size of background box from each of the strings
for (int i=0; i<formattedString.length; i++) {
formattedStringBounds[i] = this.textRenderer.getBounds(formattedStr ing[i]);
if (formattedStringBounds[i].getWidth() > largestWidth) {
largestWidth = formattedStringBounds[i].getWidth();
}
totalHeight += formattedStringBounds[i].getHeight();
}
Rectangle2D ttBounds = new Rectangle2D.Double(
x, y,
largestWidth + insets.left + insets.right,
totalHeight + insets.bottom + insets.top);
ttBounds = ensureVisibleBounds(ttBounds, this.orthoWidth, this.orthoHeight);
// Draw the background rectangle
this.setDrawColor(bg);
gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL);
gl.glRectd(ttBounds.getMinX(), ttBounds.getMinY(), ttBounds.getMaxX(), ttBounds.getMaxY());
this.setDrawColor(fg);
gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
gl.glLineWidth(this.borderWidth);
gl.glRectd(ttBounds.getMinX(), ttBounds.getMinY(), ttBounds.getMaxX(), ttBounds.getMaxY());
this.textRenderer.setColor(fg);
gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL);
this.textRenderer.begin3DRendering();
// Draw the text for each string
double accumHeight = 0;
for (int i=formattedString.length-1; i>=0; i--) {
double strX = ttBounds.getX() + insets.left - formattedStringBounds[i].getX();
double strY = ttBounds.getY() + insets.bottom + formattedStringBounds[i].getY() + formattedStringBounds[i].getHeight() + accumHeight;
accumHeight += formattedStringBounds[i].getHeight();
this.textRenderer.draw(formattedString[i], (int) strX, (int) strY);
}
this.textRenderer.end3DRendering();
}
public void draw(String str, int x, int y)
{
if (str == null)
{
String message = WorldWind.retrieveErrMsg("nullValue.StringIsNull");
WorldWind.logger().log(FINE, message);
throw new IllegalArgumentException(message);
}
Color fg;
Color bg;
Insets insets;
GL gl = GLU.getCurrentGL();
if (this.useSystemLookAndFeel)
{
insets = toolTipBorder.getBorderInsets(null);
fg = toolTipFg;
bg = toolTipBg;
}
else
{
if (this.foreground != null)
{
fg = this.foreground;
}
else
{
gl.glGetFloatv(GL.GL_CURRENT_COLOR, compArray, 0);
fg = new Color(compArray[0], compArray[1], compArray[2], compArray[3]);
}
if (this.background != null)
{
bg = this.background;
}
else
{
if (compArray == null)
compArray = new float[4];
Color.RGBtoHSB(fg.getRed(), fg.getGreen(), fg.getBlue(), compArray);
bg = Color.getHSBColor(0, 0, (compArray[2] + 0.5f) % 1f);
}
if (this.insets != null)
insets = this.insets;
else
insets = new Insets(1, 1, 1, 1);
}
String formattedString[] = str.split("\\\\n");
Rectangle2D formattedStringBounds[] = new Rectangle2D[formattedString.length];
double largestWidth = 0;
double totalHeight = 0;
// Calculate the total size of background box from each of the strings
for (int i=0; i<formattedString.length; i++) {
formattedStringBounds[i] = this.textRenderer.getBounds(formattedStr ing[i]);
if (formattedStringBounds[i].getWidth() > largestWidth) {
largestWidth = formattedStringBounds[i].getWidth();
}
totalHeight += formattedStringBounds[i].getHeight();
}
Rectangle2D ttBounds = new Rectangle2D.Double(
x, y,
largestWidth + insets.left + insets.right,
totalHeight + insets.bottom + insets.top);
ttBounds = ensureVisibleBounds(ttBounds, this.orthoWidth, this.orthoHeight);
// Draw the background rectangle
this.setDrawColor(bg);
gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL);
gl.glRectd(ttBounds.getMinX(), ttBounds.getMinY(), ttBounds.getMaxX(), ttBounds.getMaxY());
this.setDrawColor(fg);
gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
gl.glLineWidth(this.borderWidth);
gl.glRectd(ttBounds.getMinX(), ttBounds.getMinY(), ttBounds.getMaxX(), ttBounds.getMaxY());
this.textRenderer.setColor(fg);
gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL);
this.textRenderer.begin3DRendering();
// Draw the text for each string
double accumHeight = 0;
for (int i=formattedString.length-1; i>=0; i--) {
double strX = ttBounds.getX() + insets.left - formattedStringBounds[i].getX();
double strY = ttBounds.getY() + insets.bottom + formattedStringBounds[i].getY() + formattedStringBounds[i].getHeight() + accumHeight;
accumHeight += formattedStringBounds[i].getHeight();
this.textRenderer.draw(formattedString[i], (int) strX, (int) strY);
}
this.textRenderer.end3DRendering();
}