1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51:
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65:
66:
69: public class MetalToolTipUI
70: extends BasicToolTipUI
71: {
72:
76: public static final int padSpaceBetweenStrings = 12;
77:
78:
79: private static MetalToolTipUI instance = null;
80:
81:
82: private boolean isAcceleratorHidden;
83:
84:
85: private String acceleratorString;
86:
87:
90: private String acceleratorDelimiter;
91:
92:
93: private Font acceleratorFont;
94:
95:
96: private Color acceleratorForeground;
97:
98:
99: private Border activeBorder;
100:
101:
102: private Border inactiveBorder;
103:
104:
107: public MetalToolTipUI()
108: {
109: super();
110: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
111: activeBorder = defaults.getBorder("ToolTip.border");
112: inactiveBorder = defaults.getBorder("ToolTip.borderInactive");
113: isAcceleratorHidden = defaults.getBoolean("ToolTip.hideAccelerator");
114: acceleratorFont = defaults.getFont("MenuItem.acceleratorFont");
115: acceleratorForeground = defaults.getColor("MenuItem.acceleratorForeground");
116: acceleratorDelimiter = defaults.getString("MenuItem.acceleratorDelimiter");
117: }
118:
119:
128: public static ComponentUI createUI(JComponent component)
129: {
130: if (instance == null)
131: instance = new MetalToolTipUI();
132: return instance;
133: }
134:
135:
141: public String getAcceleratorString()
142: {
143: return acceleratorString;
144: }
145:
146:
151: public void installUI(JComponent c)
152: {
153: super.installUI(c);
154: Border existingBorder = c.getBorder();
155: if (existingBorder == null || existingBorder instanceof UIResource)
156: {
157: if (c.isEnabled())
158: c.setBorder(activeBorder);
159: else
160: c.setBorder(inactiveBorder);
161: }
162: }
163:
164:
169: public void uninstallUI(JComponent c)
170: {
171: super.uninstallUI(c);
172: if (c.getBorder() instanceof UIResource)
173: c.setBorder(null);
174: }
175:
176:
183: protected boolean isAcceleratorHidden()
184: {
185: return isAcceleratorHidden;
186: }
187:
188:
195: public Dimension getPreferredSize(JComponent c)
196: {
197: if (isAcceleratorHidden())
198: return super.getPreferredSize(c);
199: else
200: {
201: Insets insets = c.getInsets();
202: JToolTip tt = (JToolTip) c;
203: String tipText = tt.getTipText();
204: if (tipText != null)
205: {
206: FontMetrics fm = c.getFontMetrics(c.getFont());
207: int prefH = fm.getHeight() + insets.top + insets.bottom;
208: int prefW = fm.stringWidth(tipText) + insets.left + insets.right;
209:
210:
211:
212: acceleratorString = fetchAcceleratorString(c);
213: if (acceleratorString != null)
214: {
215: prefW += padSpaceBetweenStrings;
216: fm = c.getFontMetrics(acceleratorFont);
217: prefW += fm.stringWidth(acceleratorString);
218: }
219: return new Dimension(prefW, prefH);
220: }
221: else return new Dimension(0, 0);
222: }
223: }
224:
225:
231: public void paint(Graphics g, JComponent c)
232: {
233: JToolTip tip = (JToolTip) c;
234:
235: String text = tip.getTipText();
236: Toolkit t = tip.getToolkit();
237: if (text == null)
238: return;
239:
240: Rectangle vr = new Rectangle();
241: vr = SwingUtilities.calculateInnerArea(tip, vr);
242: Rectangle ir = new Rectangle();
243: Rectangle tr = new Rectangle();
244: FontMetrics fm = t.getFontMetrics(tip.getFont());
245: int ascent = fm.getAscent();
246: SwingUtilities.layoutCompoundLabel(tip, fm, text, null,
247: SwingConstants.CENTER, SwingConstants.LEFT,
248: SwingConstants.CENTER, SwingConstants.CENTER, vr, ir, tr, 0);
249: Color saved = g.getColor();
250: g.setColor(Color.BLACK);
251:
252: g.drawString(text, vr.x, vr.y + ascent);
253:
254:
255: if (acceleratorString != null)
256: {
257: g.setFont(acceleratorFont);
258: g.setColor(acceleratorForeground);
259: fm = t.getFontMetrics(acceleratorFont);
260: int width = fm.stringWidth(acceleratorString);
261: g.drawString(acceleratorString, vr.x + vr.width - width - padSpaceBetweenStrings/2,
262: vr.y + vr.height - fm.getDescent());
263: }
264:
265: g.setColor(saved);
266: }
267:
268:
277: private String fetchAcceleratorString(JComponent c)
278: {
279: String result = null;
280: if (c instanceof JToolTip)
281: {
282: JToolTip toolTip = (JToolTip) c;
283: JComponent component = toolTip.getComponent();
284: KeyStroke ks = null;
285: int mne = 0;
286: if (component instanceof JMenuItem)
287: {
288: JMenuItem item = (JMenuItem) component;
289: ks = item.getAccelerator();
290: if (ks == null)
291: mne = item.getMnemonic();
292: }
293: else if (component instanceof AbstractButton)
294: {
295: AbstractButton button = (AbstractButton) component;
296: mne = button.getMnemonic();
297: }
298: if (mne > 0)
299: ks = KeyStroke.getKeyStroke(Character.toUpperCase((char) mne),
300: InputEvent.ALT_MASK, false);
301: if (ks != null)
302: result = acceleratorToString(ks);
303: }
304: return result;
305: }
306:
307:
314: private String acceleratorToString(KeyStroke accelerator)
315: {
316:
317: String modifiersText = "";
318: int modifiers = accelerator.getModifiers();
319: char keyChar = accelerator.getKeyChar();
320: int keyCode = accelerator.getKeyCode();
321:
322: if (modifiers != 0)
323: modifiersText = KeyEvent.getKeyModifiersText(modifiers)
324: + acceleratorDelimiter;
325:
326: if (keyCode == KeyEvent.VK_UNDEFINED)
327: return modifiersText + keyChar;
328: else
329: return modifiersText + KeyEvent.getKeyText(keyCode);
330: }
331:
332: }