KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicLabelUI


1 /*
2  * @(#)BasicLabelUI.java 1.85 06/02/16
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.basic;
9
10 import com.sun.java.swing.SwingUtilities2;
11 import sun.swing.DefaultLookup;
12 import sun.swing.UIAction;
13 import javax.swing.*;
14 import javax.swing.plaf.*;
15 import javax.swing.text.View JavaDoc;
16
17 import java.awt.event.ActionEvent JavaDoc;
18 import java.awt.event.ActionListener JavaDoc;
19 import java.awt.Component JavaDoc;
20 import java.awt.Container JavaDoc;
21 import java.awt.Dimension JavaDoc;
22 import java.awt.Rectangle JavaDoc;
23 import java.awt.Insets JavaDoc;
24 import java.awt.Color JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.Font JavaDoc;
27 import java.awt.FontMetrics JavaDoc;
28 import java.beans.PropertyChangeEvent JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30
31 /**
32  * A Windows L&F implementation of LabelUI. This implementation
33  * is completely static, i.e. there's only one UIView implementation
34  * that's shared by all JLabel objects.
35  *
36  * @version 1.85 02/16/06
37  * @author Hans Muller
38  */

39 public class BasicLabelUI extends LabelUI implements PropertyChangeListener JavaDoc
40 {
41     protected static BasicLabelUI JavaDoc labelUI = new BasicLabelUI JavaDoc();
42     private final static BasicLabelUI JavaDoc SAFE_BASIC_LABEL_UI = new BasicLabelUI JavaDoc();
43
44     static void loadActionMap(LazyActionMap JavaDoc map) {
45         map.put(new Actions(Actions.PRESS));
46     map.put(new Actions(Actions.RELEASE));
47     }
48
49     /**
50      * Forwards the call to SwingUtilities.layoutCompoundLabel().
51      * This method is here so that a subclass could do Label specific
52      * layout and to shorten the method name a little.
53      *
54      * @see SwingUtilities#layoutCompoundLabel
55      */

56     protected String JavaDoc layoutCL(
57         JLabel label,
58         FontMetrics JavaDoc fontMetrics,
59         String JavaDoc text,
60         Icon icon,
61         Rectangle JavaDoc viewR,
62         Rectangle JavaDoc iconR,
63         Rectangle JavaDoc textR)
64     {
65         return SwingUtilities.layoutCompoundLabel(
66             (JComponent) label,
67             fontMetrics,
68             text,
69             icon,
70             label.getVerticalAlignment(),
71             label.getHorizontalAlignment(),
72             label.getVerticalTextPosition(),
73             label.getHorizontalTextPosition(),
74             viewR,
75             iconR,
76             textR,
77             label.getIconTextGap());
78     }
79
80     /**
81      * Paint clippedText at textX, textY with the labels foreground color.
82      *
83      * @see #paint
84      * @see #paintDisabledText
85      */

86     protected void paintEnabledText(JLabel l, Graphics JavaDoc g, String JavaDoc s, int textX, int textY)
87     {
88         int mnemIndex = l.getDisplayedMnemonicIndex();
89         g.setColor(l.getForeground());
90         SwingUtilities2.drawStringUnderlineCharAt(l, g, s, mnemIndex,
91                                                      textX, textY);
92     }
93
94
95     /**
96      * Paint clippedText at textX, textY with background.lighter() and then
97      * shifted down and to the right by one pixel with background.darker().
98      *
99      * @see #paint
100      * @see #paintEnabledText
101      */

102     protected void paintDisabledText(JLabel l, Graphics JavaDoc g, String JavaDoc s, int textX, int textY)
103     {
104         int accChar = l.getDisplayedMnemonicIndex();
105         Color JavaDoc background = l.getBackground();
106         g.setColor(background.brighter());
107         SwingUtilities2.drawStringUnderlineCharAt(l, g, s, accChar,
108                                                    textX + 1, textY + 1);
109         g.setColor(background.darker());
110         SwingUtilities2.drawStringUnderlineCharAt(l, g, s, accChar,
111                                                    textX, textY);
112     }
113
114
115     /* These rectangles/insets are allocated once for this shared LabelUI
116      * implementation. Re-using rectangles rather than allocating
117      * them in each paint call halved the time it took paint to run.
118      */

119     private static Rectangle JavaDoc paintIconR = new Rectangle JavaDoc();
120     private static Rectangle JavaDoc paintTextR = new Rectangle JavaDoc();
121     private static Rectangle JavaDoc paintViewR = new Rectangle JavaDoc();
122     private static Insets JavaDoc paintViewInsets = new Insets JavaDoc(0, 0, 0, 0);
123     
124
125     /**
126      * Paint the label text in the foreground color, if the label
127      * is opaque then paint the entire background with the background
128      * color. The Label text is drawn by paintEnabledText() or
129      * paintDisabledText(). The locations of the label parts are computed
130      * by layoutCL.
131      *
132      * @see #paintEnabledText
133      * @see #paintDisabledText
134      * @see #layoutCL
135      */

136     public void paint(Graphics JavaDoc g, JComponent c)
137     {
138         JLabel label = (JLabel)c;
139         String JavaDoc text = label.getText();
140         Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
141
142         if ((icon == null) && (text == null)) {
143             return;
144         }
145
146         FontMetrics JavaDoc fm = SwingUtilities2.getFontMetrics(label, g);
147         Insets JavaDoc insets = c.getInsets(paintViewInsets);
148
149         paintViewR.x = insets.left;
150         paintViewR.y = insets.top;
151         paintViewR.width = c.getWidth() - (insets.left + insets.right);
152         paintViewR.height = c.getHeight() - (insets.top + insets.bottom);
153
154         paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
155         paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
156
157         String JavaDoc clippedText =
158             layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR);
159
160         if (icon != null) {
161             icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
162         }
163
164         if (text != null) {
165         View JavaDoc v = (View JavaDoc) c.getClientProperty(BasicHTML.propertyKey);
166         if (v != null) {
167         v.paint(g, paintTextR);
168         } else {
169         int textX = paintTextR.x;
170         int textY = paintTextR.y + fm.getAscent();
171         
172         if (label.isEnabled()) {
173             paintEnabledText(label, g, clippedText, textX, textY);
174         }
175         else {
176             paintDisabledText(label, g, clippedText, textX, textY);
177         }
178         }
179         }
180     }
181
182
183     /* These rectangles/insets are allocated once for this shared LabelUI
184      * implementation. Re-using rectangles rather than allocating
185      * them in each getPreferredSize call sped up the method substantially.
186      */

187     private static Rectangle JavaDoc iconR = new Rectangle JavaDoc();
188     private static Rectangle JavaDoc textR = new Rectangle JavaDoc();
189     private static Rectangle JavaDoc viewR = new Rectangle JavaDoc();
190     private static Insets JavaDoc viewInsets = new Insets JavaDoc(0, 0, 0, 0);
191
192
193     public Dimension JavaDoc getPreferredSize(JComponent c)
194     {
195         JLabel label = (JLabel)c;
196         String JavaDoc text = label.getText();
197         Icon icon = (label.isEnabled()) ? label.getIcon() :
198                                           label.getDisabledIcon();
199         Insets JavaDoc insets = label.getInsets(viewInsets);
200         Font JavaDoc font = label.getFont();
201
202         int dx = insets.left + insets.right;
203         int dy = insets.top + insets.bottom;
204
205         if ((icon == null) &&
206             ((text == null) ||
207              ((text != null) && (font == null)))) {
208             return new Dimension JavaDoc(dx, dy);
209         }
210         else if ((text == null) || ((icon != null) && (font == null))) {
211             return new Dimension JavaDoc(icon.getIconWidth() + dx,
212                                  icon.getIconHeight() + dy);
213         }
214         else {
215             FontMetrics JavaDoc fm = label.getFontMetrics(font);
216
217             iconR.x = iconR.y = iconR.width = iconR.height = 0;
218             textR.x = textR.y = textR.width = textR.height = 0;
219             viewR.x = dx;
220             viewR.y = dy;
221             viewR.width = viewR.height = Short.MAX_VALUE;
222
223             layoutCL(label, fm, text, icon, viewR, iconR, textR);
224             int x1 = Math.min(iconR.x, textR.x);
225             int x2 = Math.max(iconR.x + iconR.width, textR.x + textR.width);
226             int y1 = Math.min(iconR.y, textR.y);
227             int y2 = Math.max(iconR.y + iconR.height, textR.y + textR.height);
228             Dimension JavaDoc rv = new Dimension JavaDoc(x2 - x1, y2 - y1);
229
230             rv.width += dx;
231             rv.height += dy;
232             return rv;
233         }
234     }
235
236
237     /**
238      * @return getPreferredSize(c)
239      */

240     public Dimension JavaDoc getMinimumSize(JComponent c) {
241         Dimension JavaDoc d = getPreferredSize(c);
242     View JavaDoc v = (View JavaDoc) c.getClientProperty(BasicHTML.propertyKey);
243     if (v != null) {
244         d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
245     }
246     return d;
247     }
248
249     /**
250      * @return getPreferredSize(c)
251      */

252     public Dimension JavaDoc getMaximumSize(JComponent c) {
253         Dimension JavaDoc d = getPreferredSize(c);
254     View JavaDoc v = (View JavaDoc) c.getClientProperty(BasicHTML.propertyKey);
255     if (v != null) {
256         d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
257     }
258     return d;
259     }
260
261
262     public void installUI(JComponent c) {
263         installDefaults((JLabel)c);
264         installComponents((JLabel)c);
265         installListeners((JLabel)c);
266         installKeyboardActions((JLabel)c);
267     }
268
269     
270     public void uninstallUI(JComponent c) {
271         uninstallDefaults((JLabel)c);
272         uninstallComponents((JLabel)c);
273         uninstallListeners((JLabel)c);
274         uninstallKeyboardActions((JLabel)c);
275     }
276
277      protected void installDefaults(JLabel c){
278          LookAndFeel.installColorsAndFont(c, "Label.background", "Label.foreground", "Label.font");
279          LookAndFeel.installProperty(c, "opaque", Boolean.FALSE);
280       }
281
282     protected void installListeners(JLabel c){
283         c.addPropertyChangeListener(this);
284     }
285
286     protected void installComponents(JLabel c){
287     BasicHTML.updateRenderer(c, c.getText());
288         c.setInheritsPopupMenu(true);
289     }
290
291     protected void installKeyboardActions(JLabel l) {
292         int dka = l.getDisplayedMnemonic();
293         Component JavaDoc lf = l.getLabelFor();
294         if ((dka != 0) && (lf != null)) {
295             LazyActionMap.installLazyActionMap(l, BasicLabelUI JavaDoc.class,
296                                                "Label.actionMap");
297         InputMap inputMap = SwingUtilities.getUIInputMap
298                     (l, JComponent.WHEN_IN_FOCUSED_WINDOW);
299         if (inputMap == null) {
300         inputMap = new ComponentInputMapUIResource(l);
301         SwingUtilities.replaceUIInputMap(l,
302                 JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
303         }
304         inputMap.clear();
305         inputMap.put(KeyStroke.getKeyStroke(dka, ActionEvent.ALT_MASK,
306                           false), "press");
307         }
308     else {
309         InputMap inputMap = SwingUtilities.getUIInputMap
310                     (l, JComponent.WHEN_IN_FOCUSED_WINDOW);
311         if (inputMap != null) {
312         inputMap.clear();
313         }
314     }
315     }
316
317     protected void uninstallDefaults(JLabel c){
318     }
319
320     protected void uninstallListeners(JLabel c){
321         c.removePropertyChangeListener(this);
322     }
323
324     protected void uninstallComponents(JLabel c){
325     BasicHTML.updateRenderer(c, "");
326     }
327
328     protected void uninstallKeyboardActions(JLabel c) {
329     SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, null);
330     SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW,
331                        null);
332     SwingUtilities.replaceUIActionMap(c, null);
333     }
334
335     public static ComponentUI createUI(JComponent c) {
336         if (System.getSecurityManager() != null) {
337             return SAFE_BASIC_LABEL_UI;
338         } else {
339             return labelUI;
340         }
341     }
342
343     public void propertyChange(PropertyChangeEvent JavaDoc e) {
344     String JavaDoc name = e.getPropertyName();
345     if (name == "text" || "font" == name || "foreground" == name) {
346         // remove the old html view client property if one
347
// existed, and install a new one if the text installed
348
// into the JLabel is html source.
349
JLabel lbl = ((JLabel) e.getSource());
350         String JavaDoc text = lbl.getText();
351         BasicHTML.updateRenderer(lbl, text);
352     }
353         else if (name == "labelFor" || name == "displayedMnemonic") {
354             installKeyboardActions((JLabel) e.getSource());
355         }
356     }
357
358     // When the accelerator is pressed, temporarily make the JLabel
359
// focusTraversable by registering a WHEN_FOCUSED action for the
360
// release of the accelerator. Then give it focus so it can
361
// prevent unwanted keyTyped events from getting to other components.
362
private static class Actions extends UIAction {
363         private static final String JavaDoc PRESS = "press";
364         private static final String JavaDoc RELEASE = "release";
365
366         Actions(String JavaDoc key) {
367             super(key);
368         }
369
370         public void actionPerformed(ActionEvent JavaDoc e) {
371             JLabel label = (JLabel)e.getSource();
372             String JavaDoc key = getName();
373             if (key == PRESS) {
374                 doPress(label);
375             }
376             else if (key == RELEASE) {
377                 doRelease(label);
378             }
379         }
380
381         private void doPress(JLabel label) {
382        Component JavaDoc labelFor = label.getLabelFor();
383        if(labelFor != null && labelFor.isEnabled()) {
384           InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
385           if (inputMap == null) {
386              inputMap = new InputMapUIResource();
387          SwingUtilities.replaceUIInputMap(label, JComponent.WHEN_FOCUSED, inputMap);
388           }
389           int dka = label.getDisplayedMnemonic();
390           inputMap.put(KeyStroke.getKeyStroke(dka, ActionEvent.ALT_MASK, true), RELEASE);
391               // Need this if the accelerator is released before the ALT key
392
inputMap.put(KeyStroke.getKeyStroke(0, ActionEvent.ALT_MASK, true), RELEASE);
393           Component JavaDoc owner = label.getLabelFor();
394           label.requestFocus();
395        }
396         }
397
398         private void doRelease(JLabel label) {
399        Component JavaDoc labelFor = label.getLabelFor();
400        if(labelFor != null && labelFor.isEnabled()) {
401           InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
402           if (inputMap != null) {
403              // inputMap should never be null.
404
inputMap.remove(KeyStroke.getKeyStroke (label.getDisplayedMnemonic(), ActionEvent.ALT_MASK, true));
405              inputMap.remove(KeyStroke.getKeyStroke(0, ActionEvent.ALT_MASK, true));
406           }
407           if (labelFor instanceof Container JavaDoc &&
408           ((Container JavaDoc)labelFor).isFocusCycleRoot()) {
409           labelFor.requestFocus();
410           }
411           else {
412           BasicLookAndFeel.compositeRequestFocus(labelFor);
413           }
414        }
415         }
416     }
417 }
418
Popular Tags