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