01: /*
02: * MultiLineToolTipUI.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.gui.components;
13:
14: import java.awt.Dimension;
15: import java.awt.FontMetrics;
16: import java.awt.Graphics;
17:
18: import javax.swing.JComponent;
19: import javax.swing.JToolTip;
20: import javax.swing.SwingUtilities;
21: import javax.swing.plaf.basic.BasicToolTipUI;
22:
23: import workbench.util.StringUtil;
24:
25: public class MultiLineToolTipUI extends BasicToolTipUI {
26: private final String[] emptyLines = new String[] { StringUtil.EMPTY_STRING };
27: private int maxWidth = 0;
28: private Dimension prefSize = new Dimension();
29: private String[] displayLines;
30:
31: public void paint(Graphics g, JComponent c) {
32: FontMetrics metrics = g.getFontMetrics();
33: Dimension size = c.getSize();
34: g.setColor(c.getBackground());
35: g.fillRect(0, 0, size.width, size.height);
36: g.setColor(c.getForeground());
37: if (this .displayLines != null) {
38: int h = metrics.getHeight();
39: int count = displayLines.length;
40: for (int i = 0; i < count; i++) {
41: g.drawString(displayLines[i], 3, (h * (i + 1)) - 2);
42: }
43: }
44: }
45:
46: public Dimension getPreferredSize(JComponent c) {
47: FontMetrics metrics = c.getFontMetrics(c.getFont());
48: String tipText = ((JToolTip) c).getTipText();
49: if (tipText == null) {
50: this .displayLines = emptyLines;
51: } else {
52: displayLines = StringUtil.PATTERN_CRLF.split(tipText);//
53: this .maxWidth = -1;
54: for (int i = 0; i < displayLines.length; i++) {
55: int width = SwingUtilities.computeStringWidth(metrics,
56: displayLines[i]);
57: this .maxWidth = (this .maxWidth < width) ? width
58: : this .maxWidth;
59: }
60: }
61: int height = metrics.getHeight() * displayLines.length;
62: prefSize.setSize(maxWidth + 6, height + 2);
63: return prefSize;
64: }
65:
66: }
|