1 |
/* BasicToolTipUI.java -- |
/* BasicToolTipUI.java -- |
2 |
Copyright (C) 2004 Free Software Foundation, Inc. |
Copyright (C) 2004, 2005 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
39 |
package javax.swing.plaf.basic; |
package javax.swing.plaf.basic; |
40 |
|
|
41 |
import java.awt.Color; |
import java.awt.Color; |
|
import java.awt.Component; |
|
42 |
import java.awt.Dimension; |
import java.awt.Dimension; |
43 |
import java.awt.FontMetrics; |
import java.awt.FontMetrics; |
44 |
import java.awt.Graphics; |
import java.awt.Graphics; |
45 |
import java.awt.Insets; |
import java.awt.Insets; |
46 |
import java.awt.Rectangle; |
import java.awt.Rectangle; |
47 |
|
import java.awt.Toolkit; |
48 |
|
|
49 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
50 |
import javax.swing.JToolTip; |
import javax.swing.JToolTip; |
51 |
|
import javax.swing.LookAndFeel; |
52 |
import javax.swing.SwingConstants; |
import javax.swing.SwingConstants; |
53 |
import javax.swing.SwingUtilities; |
import javax.swing.SwingUtilities; |
|
import javax.swing.UIDefaults; |
|
|
import javax.swing.UIManager; |
|
|
import javax.swing.border.Border; |
|
54 |
import javax.swing.plaf.ComponentUI; |
import javax.swing.plaf.ComponentUI; |
55 |
import javax.swing.plaf.ToolTipUI; |
import javax.swing.plaf.ToolTipUI; |
56 |
|
|
59 |
*/ |
*/ |
60 |
public class BasicToolTipUI extends ToolTipUI |
public class BasicToolTipUI extends ToolTipUI |
61 |
{ |
{ |
|
/** The default Border around the JToolTip. */ |
|
|
private static Border defaultBorder = new Border() |
|
|
{ |
|
|
// FIXME: This needs to go into Basic Look and Feel |
|
|
// defaults. |
|
|
|
|
|
/** |
|
|
* This method returns the border insets. |
|
|
* |
|
|
* @param c The Component to find Border insets for. |
|
|
* |
|
|
* @return The Border insets. |
|
|
*/ |
|
|
public Insets getBorderInsets(Component c) |
|
|
{ |
|
|
return new Insets(4, 4, 4, 4); |
|
|
} |
|
|
|
|
|
/** |
|
|
* This method returns whether the border is opaque. |
|
|
* |
|
|
* @return Whether the border is opaque. |
|
|
*/ |
|
|
public boolean isBorderOpaque() |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
|
|
|
/** |
|
|
* This method paints the border. |
|
|
* |
|
|
* @param c The Component to paint this border around. |
|
|
* @param g The Graphics object to paint with. |
|
|
* @param x The x coordinate to start painting at. |
|
|
* @param y The y coordinate to start painting at. |
|
|
* @param w The width of the Component. |
|
|
* @param h The height of the Component. |
|
|
*/ |
|
|
public void paintBorder(Component c, Graphics g, int x, int y, int w, |
|
|
int h) |
|
|
{ |
|
|
Color saved = g.getColor(); |
|
|
g.setColor(Color.BLACK); |
|
|
|
|
|
g.drawRect(0, 0, w - 1, h - 1); |
|
|
|
|
|
g.setColor(saved); |
|
|
} |
|
|
}; |
|
62 |
|
|
63 |
/** The shared instance of BasicToolTipUI used for all ToolTips. */ |
/** The shared instance of BasicToolTipUI used for all ToolTips. */ |
64 |
private static BasicToolTipUI shared; |
private static BasicToolTipUI shared; |
65 |
|
|
66 |
|
/** The tooltip's text */ |
67 |
|
private String text; |
68 |
|
|
69 |
/** |
/** |
70 |
* Creates a new BasicToolTipUI object. |
* Creates a new BasicToolTipUI object. |
76 |
|
|
77 |
/** |
/** |
78 |
* This method creates a new BasicToolTip UI for the given |
* This method creates a new BasicToolTip UI for the given |
79 |
* JComponent. |
* JComponent. |
80 |
* |
* |
81 |
* @param c The JComponent to create a UI for. |
* @param c The JComponent to create a UI for. |
82 |
* |
* |
84 |
*/ |
*/ |
85 |
public static ComponentUI createUI(JComponent c) |
public static ComponentUI createUI(JComponent c) |
86 |
{ |
{ |
87 |
if (shared == null) |
if (shared == null) |
88 |
shared = new BasicToolTipUI(); |
shared = new BasicToolTipUI(); |
89 |
return shared; |
return shared; |
90 |
} |
} |
91 |
|
|
92 |
/** |
/** |
123 |
public Dimension getPreferredSize(JComponent c) |
public Dimension getPreferredSize(JComponent c) |
124 |
{ |
{ |
125 |
JToolTip tip = (JToolTip) c; |
JToolTip tip = (JToolTip) c; |
126 |
|
FontMetrics fm; |
127 |
|
Toolkit g = tip.getToolkit(); |
128 |
|
text = tip.getTipText(); |
129 |
|
|
130 |
Rectangle vr = new Rectangle(); |
Rectangle vr = new Rectangle(); |
131 |
Rectangle ir = new Rectangle(); |
Rectangle ir = new Rectangle(); |
132 |
Rectangle tr = new Rectangle(); |
Rectangle tr = new Rectangle(); |
133 |
Insets insets = tip.getInsets(); |
Insets insets = tip.getInsets(); |
134 |
FontMetrics fm = tip.getToolkit().getFontMetrics(tip.getFont()); |
fm = g.getFontMetrics(tip.getFont()); |
135 |
SwingUtilities.layoutCompoundLabel(tip, fm, tip.getTipText(), null, |
SwingUtilities.layoutCompoundLabel(tip, fm, text, null, |
136 |
SwingConstants.CENTER, |
SwingConstants.CENTER, |
137 |
SwingConstants.CENTER, |
SwingConstants.CENTER, |
138 |
SwingConstants.CENTER, |
SwingConstants.CENTER, |
148 |
*/ |
*/ |
149 |
protected void installDefaults(JComponent c) |
protected void installDefaults(JComponent c) |
150 |
{ |
{ |
151 |
UIDefaults defaults = UIManager.getLookAndFeelDefaults(); |
LookAndFeel.installColorsAndFont(c, "ToolTip.background", |
152 |
c.setBackground(defaults.getColor("ToolTip.background")); |
"ToolTip.foreground", "ToolTip.font"); |
153 |
c.setForeground(defaults.getColor("ToolTip.foreground")); |
LookAndFeel.installBorder(c, "ToolTip.border"); |
|
c.setFont(defaults.getFont("ToolTip.font")); |
|
|
c.setBorder(defaultBorder); |
|
154 |
} |
} |
155 |
|
|
156 |
/** |
/** |
160 |
*/ |
*/ |
161 |
protected void installListeners(JComponent c) |
protected void installListeners(JComponent c) |
162 |
{ |
{ |
163 |
|
// TODO: Implement this properly. |
164 |
} |
} |
165 |
|
|
166 |
/** |
/** |
186 |
JToolTip tip = (JToolTip) c; |
JToolTip tip = (JToolTip) c; |
187 |
|
|
188 |
String text = tip.getTipText(); |
String text = tip.getTipText(); |
189 |
|
Toolkit t = tip.getToolkit(); |
190 |
if (text == null) |
if (text == null) |
191 |
return; |
return; |
192 |
|
|
194 |
vr = SwingUtilities.calculateInnerArea(tip, vr); |
vr = SwingUtilities.calculateInnerArea(tip, vr); |
195 |
Rectangle ir = new Rectangle(); |
Rectangle ir = new Rectangle(); |
196 |
Rectangle tr = new Rectangle(); |
Rectangle tr = new Rectangle(); |
197 |
FontMetrics fm = tip.getToolkit().getFontMetrics(tip.getFont()); |
FontMetrics fm = t.getFontMetrics(tip.getFont()); |
198 |
SwingUtilities.layoutCompoundLabel(tip, fm, tip.getTipText(), null, |
int ascent = fm.getAscent(); |
199 |
|
SwingUtilities.layoutCompoundLabel(tip, fm, text, null, |
200 |
SwingConstants.CENTER, |
SwingConstants.CENTER, |
201 |
SwingConstants.CENTER, |
SwingConstants.CENTER, |
202 |
SwingConstants.CENTER, |
SwingConstants.CENTER, |
203 |
SwingConstants.CENTER, vr, ir, tr, 0); |
SwingConstants.CENTER, vr, ir, tr, 0); |
|
|
|
204 |
Color saved = g.getColor(); |
Color saved = g.getColor(); |
205 |
g.setColor(Color.BLACK); |
g.setColor(Color.BLACK); |
206 |
|
|
207 |
g.drawString(text, vr.x, vr.y + fm.getAscent()); |
g.drawString(text, vr.x, vr.y + ascent); |
208 |
|
|
209 |
g.setColor(saved); |
g.setColor(saved); |
210 |
} |
} |
211 |
|
|
212 |
/** |
/** |
229 |
*/ |
*/ |
230 |
protected void uninstallListeners(JComponent c) |
protected void uninstallListeners(JComponent c) |
231 |
{ |
{ |
232 |
|
// TODO: Implement this properly. |
233 |
} |
} |
234 |
|
|
235 |
/** |
/** |