1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52:
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:
70: public class BasicLabelUI extends LabelUI implements PropertyChangeListener
71: {
72:
73: protected static BasicLabelUI labelUI;
74:
75:
79: private Rectangle vr;
80: private Rectangle ir;
81: private Rectangle tr;
82:
83:
86: private Insets cachedInsets;
87:
88:
91: public BasicLabelUI()
92: {
93: super();
94: vr = new Rectangle();
95: ir = new Rectangle();
96: tr = new Rectangle();
97: }
98:
99:
108: public static ComponentUI createUI(JComponent c)
109: {
110: if (labelUI == null)
111: labelUI = new BasicLabelUI();
112: return labelUI;
113: }
114:
115:
124: public Dimension getPreferredSize(JComponent c)
125: {
126: JLabel lab = (JLabel) c;
127: Insets insets = lab.getInsets();
128: int insetsX = insets.left + insets.right;
129: int insetsY = insets.top + insets.bottom;
130: Icon icon = lab.getIcon();
131: String text = lab.getText();
132: Dimension ret;
133: if (icon == null && text == null)
134: ret = new Dimension(insetsX, insetsY);
135: else if (icon != null && text == null)
136: ret = new Dimension(icon.getIconWidth() + insetsX,
137: icon.getIconHeight() + insetsY);
138: else
139: {
140: FontMetrics fm = getFontMetrics(lab);
141: ir.x = 0;
142: ir.y = 0;
143: ir.width = 0;
144: ir.height = 0;
145: tr.x = 0;
146: tr.y = 0;
147: tr.width = 0;
148: tr.height = 0;
149: vr.x = 0;
150: vr.y = 0;
151: vr.width = Short.MAX_VALUE;
152: vr.height = Short.MAX_VALUE;
153: layoutCL(lab, fm, text, icon, vr, ir, tr);
154: Rectangle cr = SwingUtilities.computeUnion(tr.x, tr.y, tr.width,
155: tr.height, ir);
156: ret = new Dimension(cr.width + insetsX, cr.height + insetsY);
157: }
158: return ret;
159: }
160:
161:
170: public Dimension getMinimumSize(JComponent c)
171: {
172: return getPreferredSize(c);
173: }
174:
175:
184: public Dimension getMaximumSize(JComponent c)
185: {
186: return getPreferredSize(c);
187: }
188:
189:
195: public void paint(Graphics g, JComponent c)
196: {
197: JLabel b = (JLabel) c;
198: Icon icon = (b.isEnabled()) ? b.getIcon() : b.getDisabledIcon();
199: String text = b.getText();
200: if (icon != null || (text != null && ! text.equals("")))
201: {
202: FontMetrics fm = getFontMetrics(b);
203: Insets i = c.getInsets(cachedInsets);
204: vr.x = i.left;
205: vr.y = i.right;
206: vr.width = c.getWidth() - i.left - i.right;
207: vr.height = c.getHeight() - i.top - i.bottom;
208: ir.x = 0;
209: ir.y = 0;
210: ir.width = 0;
211: ir.height = 0;
212: tr.x = 0;
213: tr.y = 0;
214: tr.width = 0;
215: tr.height = 0;
216:
217: text = layoutCL(b, fm, text, icon, vr, ir, tr);
218:
219: if (icon != null)
220: icon.paintIcon(b, g, ir.x, ir.y);
221:
222: if (text != null && ! text.equals(""))
223: {
224: Object htmlRenderer = b.getClientProperty(BasicHTML.propertyKey);
225: if (htmlRenderer == null)
226: {
227: if (b.isEnabled())
228: paintEnabledText(b, g, text, tr.x, tr.y + fm.getAscent());
229: else
230: paintDisabledText(b, g, text, tr.x, tr.y + fm.getAscent());
231: }
232: else
233: {
234: ((View) htmlRenderer).paint(g, tr);
235: }
236: }
237: }
238: }
239:
240:
253: protected String layoutCL(JLabel label, FontMetrics fontMetrics, String text,
254: Icon icon, Rectangle viewR, Rectangle iconR, Rectangle textR)
255: {
256: return SwingUtilities.layoutCompoundLabel(label, fontMetrics, text, icon,
257: label.getVerticalAlignment(), label.getHorizontalAlignment(), label
258: .getVerticalTextPosition(), label.getHorizontalTextPosition(),
259: viewR, iconR, textR, label.getIconTextGap());
260: }
261:
262:
274: protected void paintDisabledText(JLabel l, Graphics g, String s, int textX,
275: int textY)
276: {
277: g.setColor(l.getBackground().brighter());
278:
279: int mnemIndex = l.getDisplayedMnemonicIndex();
280:
281: if (mnemIndex != -1)
282: BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,
283: textY);
284: else
285: g.drawString(s, textX, textY);
286:
287: g.setColor(l.getBackground().darker());
288: if (mnemIndex != -1)
289: BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX + 1,
290: textY + 1);
291: else
292: g.drawString(s, textX + 1, textY + 1);
293: }
294:
295:
305: protected void paintEnabledText(JLabel l, Graphics g, String s, int textX,
306: int textY)
307: {
308: g.setColor(l.getForeground());
309:
310: int mnemIndex = l.getDisplayedMnemonicIndex();
311:
312: if (mnemIndex != -1)
313: BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,
314: textY);
315: else
316: g.drawString(s, textX, textY);
317: }
318:
319:
326: public void installUI(JComponent c)
327: {
328: super.installUI(c);
329: if (c instanceof JLabel)
330: {
331: JLabel l = (JLabel) c;
332:
333: installComponents(l);
334: installDefaults(l);
335: installListeners(l);
336: installKeyboardActions(l);
337: }
338: }
339:
340:
347: public void uninstallUI(JComponent c)
348: {
349: super.uninstallUI(c);
350: if (c instanceof JLabel)
351: {
352: JLabel l = (JLabel) c;
353:
354: uninstallKeyboardActions(l);
355: uninstallListeners(l);
356: uninstallDefaults(l);
357: uninstallComponents(l);
358: }
359: }
360:
361:
366: protected void installComponents(JLabel c)
367: {
368: BasicHTML.updateRenderer(c, c.getText());
369: }
370:
371:
376: protected void uninstallComponents(JLabel c)
377: {
378: c.putClientProperty(BasicHTML.propertyKey, null);
379: c.putClientProperty(BasicHTML.documentBaseKey, null);
380: }
381:
382:
388: protected void installDefaults(JLabel c)
389: {
390: LookAndFeel.installColorsAndFont(c, "Label.background", "Label.foreground",
391: "Label.font");
392:
393:
394: }
395:
396:
402: protected void uninstallDefaults(JLabel c)
403: {
404: c.setForeground(null);
405: c.setBackground(null);
406: c.setFont(null);
407: }
408:
409:
414: protected void installKeyboardActions(JLabel l)
415: {
416: Component c = l.getLabelFor();
417: if (c != null)
418: {
419: int mnemonic = l.getDisplayedMnemonic();
420: if (mnemonic > 0)
421: {
422:
423: InputMap keyMap = new InputMap();
424: keyMap.put(KeyStroke.getKeyStroke(mnemonic, KeyEvent.VK_ALT),
425: "press");
426: SwingUtilities.replaceUIInputMap(l,
427: JComponent.WHEN_IN_FOCUSED_WINDOW, keyMap);
428:
429:
430: ActionMap map = new ActionMap();
431: map.put("press", new AbstractAction() {
432: public void actionPerformed(ActionEvent event)
433: {
434: JLabel label = (JLabel) event.getSource();
435: Component c = label.getLabelFor();
436: if (c != null)
437: c.requestFocus();
438: }
439: });
440: SwingUtilities.replaceUIActionMap(l, map);
441: }
442: }
443: }
444:
445:
450: protected void uninstallKeyboardActions(JLabel l)
451: {
452: SwingUtilities.replaceUIActionMap(l, null);
453: SwingUtilities.replaceUIInputMap(l, JComponent.WHEN_IN_FOCUSED_WINDOW,
454: null);
455: }
456:
457:
463: protected void installListeners(JLabel c)
464: {
465: c.addPropertyChangeListener(this);
466: }
467:
468:
474: protected void uninstallListeners(JLabel c)
475: {
476: c.removePropertyChangeListener(this);
477: }
478:
479:
485: public void propertyChange(PropertyChangeEvent e)
486: {
487: if (e.getPropertyName().equals("text"))
488: {
489: String text = (String) e.getNewValue();
490: JLabel l = (JLabel) e.getSource();
491: BasicHTML.updateRenderer(l, text);
492: }
493: else if (e.getPropertyName().equals("displayedMnemonic"))
494: {
495:
496: JLabel label = (JLabel) e.getSource();
497: if (label.getLabelFor() != null)
498: {
499: int oldMnemonic = ((Integer) e.getOldValue()).intValue();
500: int newMnemonic = ((Integer) e.getNewValue()).intValue();
501: InputMap keyMap = label.getInputMap(
502: JComponent.WHEN_IN_FOCUSED_WINDOW);
503: keyMap.put(KeyStroke.getKeyStroke(oldMnemonic,
504: KeyEvent.ALT_DOWN_MASK), null);
505: keyMap.put(KeyStroke.getKeyStroke(newMnemonic,
506: KeyEvent.ALT_DOWN_MASK), "press");
507: }
508: }
509: else if (e.getPropertyName().equals("labelFor"))
510: {
511: JLabel label = (JLabel) e.getSource();
512: InputMap keyMap = label.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
513: int mnemonic = label.getDisplayedMnemonic();
514: if (mnemonic > 0)
515: keyMap.put(KeyStroke.getKeyStroke(mnemonic, KeyEvent.ALT_DOWN_MASK),
516: "press");
517: }
518: }
519:
520:
531: private FontMetrics getFontMetrics(JLabel l)
532: {
533: Font font = l.getFont();
534: FontMetrics fm = l.getFontMetrics(font);
535: if (fm == null)
536: {
537: Toolkit tk = Toolkit.getDefaultToolkit();
538: fm = tk.getFontMetrics(font);
539: }
540: return fm;
541: }
542: }