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.Font; |
46 |
import java.awt.Insets; |
import java.awt.Insets; |
47 |
|
import java.awt.event.KeyEvent; |
48 |
import java.awt.Rectangle; |
import java.awt.Rectangle; |
49 |
|
import java.awt.Toolkit; |
50 |
|
|
51 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
52 |
import javax.swing.JToolTip; |
import javax.swing.JToolTip; |
53 |
|
import javax.swing.JMenuItem; |
54 |
|
import javax.swing.KeyStroke; |
55 |
import javax.swing.SwingConstants; |
import javax.swing.SwingConstants; |
56 |
import javax.swing.SwingUtilities; |
import javax.swing.SwingUtilities; |
57 |
import javax.swing.UIDefaults; |
import javax.swing.UIDefaults; |
58 |
import javax.swing.UIManager; |
import javax.swing.UIManager; |
|
import javax.swing.border.Border; |
|
59 |
import javax.swing.plaf.ComponentUI; |
import javax.swing.plaf.ComponentUI; |
60 |
import javax.swing.plaf.ToolTipUI; |
import javax.swing.plaf.ToolTipUI; |
61 |
|
|
65 |
public class BasicToolTipUI extends ToolTipUI |
public class BasicToolTipUI extends ToolTipUI |
66 |
{ |
{ |
67 |
|
|
68 |
/** The shared instance of BasicToolTipUI used for all ToolTips. */ |
/** The shared instance of BasicToolTipUI used for all ToolTips. */ |
69 |
private static BasicToolTipUI shared; |
private static BasicToolTipUI shared; |
70 |
|
|
71 |
|
/** The tooltip's text */ |
72 |
|
private String text; |
73 |
|
|
74 |
|
/** The accelerator's text */ |
75 |
|
private String accText; |
76 |
|
|
77 |
|
/** The accelerator's deliminator */ |
78 |
|
private String accDeliminator; |
79 |
|
|
80 |
|
/** The accelerator's font */ |
81 |
|
private Font accFont; |
82 |
|
|
83 |
|
/** The accelerator's foreground color */ |
84 |
|
private Color accFore; |
85 |
|
|
86 |
|
|
87 |
/** |
/** |
88 |
* Creates a new BasicToolTipUI object. |
* Creates a new BasicToolTipUI object. |
141 |
public Dimension getPreferredSize(JComponent c) |
public Dimension getPreferredSize(JComponent c) |
142 |
{ |
{ |
143 |
JToolTip tip = (JToolTip) c; |
JToolTip tip = (JToolTip) c; |
144 |
|
FontMetrics fm; |
145 |
|
Toolkit g = tip.getToolkit(); |
146 |
|
text = tip.getTipText(); |
147 |
|
|
148 |
|
// accelerator |
149 |
|
JComponent component = tip.getComponent(); |
150 |
|
if (component instanceof JMenuItem) |
151 |
|
{ |
152 |
|
JMenuItem item = (JMenuItem) component; |
153 |
|
KeyStroke acc = item.getAccelerator(); |
154 |
|
String tipText = item.getToolTipText(); |
155 |
|
if (tipText != null && acc != null) |
156 |
|
{ |
157 |
|
UIDefaults defaults = UIManager.getLookAndFeelDefaults(); |
158 |
|
accFore = defaults.getColor("MenuItem.acceleratorForeground"); |
159 |
|
accDeliminator = defaults.getString("MenuItem.acceleratorDelimiter"); |
160 |
|
accFont = defaults.getFont("MenuItem.acceleratorFont"); |
161 |
|
accText = getAcceleratorText(acc); |
162 |
|
text = tipText + " " + accText; |
163 |
|
} |
164 |
|
else |
165 |
|
{ |
166 |
|
accFore = null; |
167 |
|
accDeliminator = null; |
168 |
|
accFont = null; |
169 |
|
accText = null; |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
Rectangle vr = new Rectangle(); |
Rectangle vr = new Rectangle(); |
174 |
Rectangle ir = new Rectangle(); |
Rectangle ir = new Rectangle(); |
175 |
Rectangle tr = new Rectangle(); |
Rectangle tr = new Rectangle(); |
176 |
Insets insets = tip.getInsets(); |
Insets insets = tip.getInsets(); |
177 |
FontMetrics fm = tip.getToolkit().getFontMetrics(tip.getFont()); |
fm = g.getFontMetrics(tip.getFont()); |
178 |
SwingUtilities.layoutCompoundLabel(tip, fm, tip.getTipText(), null, |
SwingUtilities.layoutCompoundLabel(tip, fm, text, null, |
179 |
SwingConstants.CENTER, |
SwingConstants.CENTER, |
180 |
SwingConstants.CENTER, |
SwingConstants.CENTER, |
181 |
SwingConstants.CENTER, |
SwingConstants.CENTER, |
183 |
return new Dimension(insets.left + tr.width + insets.right, |
return new Dimension(insets.left + tr.width + insets.right, |
184 |
insets.top + tr.height + insets.bottom); |
insets.top + tr.height + insets.bottom); |
185 |
} |
} |
186 |
|
|
187 |
|
/** |
188 |
|
* Return text representation of the specified accelerator |
189 |
|
* |
190 |
|
* @param accelerator |
191 |
|
* Accelerator for which to return string representation |
192 |
|
* @return $String$ Text representation of the given accelerator |
193 |
|
*/ |
194 |
|
private String getAcceleratorText(KeyStroke accelerator) |
195 |
|
{ |
196 |
|
// convert keystroke into string format |
197 |
|
String modifiersText = ""; |
198 |
|
int modifiers = accelerator.getModifiers(); |
199 |
|
char keyChar = accelerator.getKeyChar(); |
200 |
|
int keyCode = accelerator.getKeyCode(); |
201 |
|
|
202 |
|
if (modifiers != 0) |
203 |
|
modifiersText = KeyEvent.getKeyModifiersText(modifiers) |
204 |
|
+ accDeliminator; |
205 |
|
|
206 |
|
if (keyCode == KeyEvent.VK_UNDEFINED) |
207 |
|
return modifiersText + keyChar; |
208 |
|
else |
209 |
|
return modifiersText + KeyEvent.getKeyText(keyCode); |
210 |
|
} |
211 |
|
|
212 |
/** |
/** |
213 |
* This method installs the defaults for the given JComponent. |
* This method installs the defaults for the given JComponent. |
214 |
* |
* |
255 |
JToolTip tip = (JToolTip) c; |
JToolTip tip = (JToolTip) c; |
256 |
|
|
257 |
String text = tip.getTipText(); |
String text = tip.getTipText(); |
258 |
|
Toolkit t = tip.getToolkit(); |
259 |
if (text == null) |
if (text == null) |
260 |
return; |
return; |
261 |
|
|
263 |
vr = SwingUtilities.calculateInnerArea(tip, vr); |
vr = SwingUtilities.calculateInnerArea(tip, vr); |
264 |
Rectangle ir = new Rectangle(); |
Rectangle ir = new Rectangle(); |
265 |
Rectangle tr = new Rectangle(); |
Rectangle tr = new Rectangle(); |
266 |
FontMetrics fm = tip.getToolkit().getFontMetrics(tip.getFont()); |
FontMetrics fm = t.getFontMetrics(tip.getFont()); |
267 |
SwingUtilities.layoutCompoundLabel(tip, fm, tip.getTipText(), null, |
int ascent = fm.getAscent(); |
268 |
|
SwingUtilities.layoutCompoundLabel(tip, fm, text, null, |
269 |
SwingConstants.CENTER, |
SwingConstants.CENTER, |
270 |
SwingConstants.CENTER, |
SwingConstants.CENTER, |
271 |
SwingConstants.CENTER, |
SwingConstants.CENTER, |
272 |
SwingConstants.CENTER, vr, ir, tr, 0); |
SwingConstants.CENTER, vr, ir, tr, 0); |
|
|
|
273 |
Color saved = g.getColor(); |
Color saved = g.getColor(); |
274 |
g.setColor(Color.BLACK); |
g.setColor(Color.BLACK); |
275 |
|
|
276 |
g.drawString(text, vr.x, vr.y + fm.getAscent()); |
g.drawString(text, vr.x, vr.y + ascent); |
277 |
|
|
278 |
|
// paint accelerator |
279 |
|
if (accText != null) |
280 |
|
{ |
281 |
|
g.setColor(accFore); |
282 |
|
int textWidth = fm.stringWidth(text + " "); |
283 |
|
|
284 |
|
fm = t.getFontMetrics(accFont); |
285 |
|
int width = fm.stringWidth(accText); |
286 |
|
g.drawString(accText, textWidth, vr.y + ascent); |
287 |
|
} |
288 |
|
|
289 |
g.setColor(saved); |
g.setColor(saved); |
290 |
} |
} |
291 |
|
|
292 |
/** |
/** |