Merge with trank @ 137446
[official-gcc.git] / libjava / classpath / javax / swing / plaf / basic / BasicLabelUI.java
blob045740df6095c053752d564e02d34e720a2cb26f
1 /* BasicLabelUI.java
2 Copyright (C) 2002, 2004, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.swing.plaf.basic;
40 import java.awt.Component;
41 import java.awt.Dimension;
42 import java.awt.Font;
43 import java.awt.FontMetrics;
44 import java.awt.Graphics;
45 import java.awt.Insets;
46 import java.awt.Rectangle;
47 import java.awt.Toolkit;
48 import java.awt.event.ActionEvent;
49 import java.awt.event.KeyEvent;
50 import java.beans.PropertyChangeEvent;
51 import java.beans.PropertyChangeListener;
53 import javax.swing.AbstractAction;
54 import javax.swing.ActionMap;
55 import javax.swing.Icon;
56 import javax.swing.InputMap;
57 import javax.swing.JComponent;
58 import javax.swing.JLabel;
59 import javax.swing.KeyStroke;
60 import javax.swing.LookAndFeel;
61 import javax.swing.SwingUtilities;
62 import javax.swing.plaf.ComponentUI;
63 import javax.swing.plaf.LabelUI;
64 import javax.swing.text.View;
66 /**
67 * This is the Basic Look and Feel class for the JLabel. One BasicLabelUI
68 * object is used to paint all JLabels that utilize the Basic Look and Feel.
70 public class BasicLabelUI extends LabelUI implements PropertyChangeListener
72 /** The labelUI that is shared by all labels. */
73 protected static BasicLabelUI labelUI;
75 /**
76 * These fields hold the rectangles for the whole label,
77 * the icon and the text.
79 private Rectangle vr;
80 private Rectangle ir;
81 private Rectangle tr;
83 /**
84 * A cached Insets object for reuse in the label layout methods.
86 private Insets cachedInsets;
88 /**
89 * Creates a new BasicLabelUI object.
91 public BasicLabelUI()
93 super();
94 vr = new Rectangle();
95 ir = new Rectangle();
96 tr = new Rectangle();
99 /**
100 * Creates and returns a UI for the label. Since one UI is shared by all
101 * labels, this means creating only if necessary and returning the shared
102 * UI.
104 * @param c The {@link JComponent} that a UI is being created for.
106 * @return A label UI for the Basic Look and Feel.
108 public static ComponentUI createUI(JComponent c)
110 if (labelUI == null)
111 labelUI = new BasicLabelUI();
112 return labelUI;
116 * Returns the preferred size of this component as calculated by the
117 * {@link #layoutCL(JLabel, FontMetrics, String, Icon, Rectangle, Rectangle,
118 * Rectangle)} method.
120 * @param c This {@link JComponent} to get a preferred size for.
122 * @return The preferred size.
124 public Dimension getPreferredSize(JComponent c)
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
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);
158 return ret;
162 * This method returns the minimum size of the {@link JComponent} given. If
163 * this method returns null, then it is up to the Layout Manager to give
164 * this component a minimum size.
166 * @param c The {@link JComponent} to get a minimum size for.
168 * @return The minimum size.
170 public Dimension getMinimumSize(JComponent c)
172 return getPreferredSize(c);
176 * This method returns the maximum size of the {@link JComponent} given. If
177 * this method returns null, then it is up to the Layout Manager to give
178 * this component a maximum size.
180 * @param c The {@link JComponent} to get a maximum size for.
182 * @return The maximum size.
184 public Dimension getMaximumSize(JComponent c)
186 return getPreferredSize(c);
190 * The method that paints the label according to its current state.
192 * @param g The {@link Graphics} object to paint with.
193 * @param c The {@link JComponent} to paint.
195 public void paint(Graphics g, JComponent c)
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("")))
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;
217 text = layoutCL(b, fm, text, icon, vr, ir, tr);
219 if (icon != null)
220 icon.paintIcon(b, g, ir.x, ir.y);
222 if (text != null && ! text.equals(""))
224 Object htmlRenderer = b.getClientProperty(BasicHTML.propertyKey);
225 if (htmlRenderer == null)
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());
232 else
234 ((View) htmlRenderer).paint(g, tr);
241 * This method is simply calls SwingUtilities's layoutCompoundLabel.
243 * @param label The label to lay out.
244 * @param fontMetrics The FontMetrics for the font used.
245 * @param text The text to paint.
246 * @param icon The icon to draw.
247 * @param viewR The entire viewable rectangle.
248 * @param iconR The icon bounds rectangle.
249 * @param textR The text bounds rectangle.
251 * @return A possibly clipped version of the text.
253 protected String layoutCL(JLabel label, FontMetrics fontMetrics, String text,
254 Icon icon, Rectangle viewR, Rectangle iconR, Rectangle textR)
256 return SwingUtilities.layoutCompoundLabel(label, fontMetrics, text, icon,
257 label.getVerticalAlignment(), label.getHorizontalAlignment(), label
258 .getVerticalTextPosition(), label.getHorizontalTextPosition(),
259 viewR, iconR, textR, label.getIconTextGap());
263 * Paints the text if the label is disabled. By default, this paints the
264 * clipped text returned by layoutCompoundLabel using the
265 * background.brighter() color. It also paints the same text using the
266 * background.darker() color one pixel to the right and one pixel down.
268 * @param l The {@link JLabel} being painted.
269 * @param g The {@link Graphics} object to paint with.
270 * @param s The String to paint.
271 * @param textX The x coordinate of the start of the baseline.
272 * @param textY The y coordinate of the start of the baseline.
274 protected void paintDisabledText(JLabel l, Graphics g, String s, int textX,
275 int textY)
277 g.setColor(l.getBackground().brighter());
279 int mnemIndex = l.getDisplayedMnemonicIndex();
281 if (mnemIndex != -1)
282 BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,
283 textY);
284 else
285 g.drawString(s, textX, textY);
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);
296 * Paints the text if the label is enabled. The text is painted using the
297 * foreground color.
299 * @param l The {@link JLabel} being painted.
300 * @param g The {@link Graphics} object to paint with.
301 * @param s The String to paint.
302 * @param textX The x coordinate of the start of the baseline.
303 * @param textY The y coordinate of the start of the baseline.
305 protected void paintEnabledText(JLabel l, Graphics g, String s, int textX,
306 int textY)
308 g.setColor(l.getForeground());
310 int mnemIndex = l.getDisplayedMnemonicIndex();
312 if (mnemIndex != -1)
313 BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,
314 textY);
315 else
316 g.drawString(s, textX, textY);
320 * This method installs the UI for the given {@link JComponent}. This
321 * method will install the component, defaults, listeners, and keyboard
322 * actions.
324 * @param c The {@link JComponent} that this UI is being installed on.
326 public void installUI(JComponent c)
328 super.installUI(c);
329 if (c instanceof JLabel)
331 JLabel l = (JLabel) c;
333 installComponents(l);
334 installDefaults(l);
335 installListeners(l);
336 installKeyboardActions(l);
341 * This method uninstalls the UI for the given {@link JComponent}. This
342 * method will uninstall the component, defaults, listeners, and keyboard
343 * actions.
345 * @param c The {@link JComponent} that this UI is being installed on.
347 public void uninstallUI(JComponent c)
349 super.uninstallUI(c);
350 if (c instanceof JLabel)
352 JLabel l = (JLabel) c;
354 uninstallKeyboardActions(l);
355 uninstallListeners(l);
356 uninstallDefaults(l);
357 uninstallComponents(l);
362 * This method installs the components for this {@link JLabel}.
364 * @param c The {@link JLabel} to install components for.
366 protected void installComponents(JLabel c)
368 BasicHTML.updateRenderer(c, c.getText());
372 * This method uninstalls the components for this {@link JLabel}.
374 * @param c The {@link JLabel} to uninstall components for.
376 protected void uninstallComponents(JLabel c)
378 c.putClientProperty(BasicHTML.propertyKey, null);
379 c.putClientProperty(BasicHTML.documentBaseKey, null);
383 * This method installs the defaults that are defined in the Basic look and
384 * feel for this {@link JLabel}.
386 * @param c The {@link JLabel} to install defaults for.
388 protected void installDefaults(JLabel c)
390 LookAndFeel.installColorsAndFont(c, "Label.background", "Label.foreground",
391 "Label.font");
392 //XXX: There are properties we don't use called disabledForeground
393 //and disabledShadow.
397 * This method uninstalls the defaults that are defined in the Basic look
398 * and feel for this {@link JLabel}.
400 * @param c The {@link JLabel} to uninstall defaults for.
402 protected void uninstallDefaults(JLabel c)
404 c.setForeground(null);
405 c.setBackground(null);
406 c.setFont(null);
410 * Installs the keyboard actions for the given {@link JLabel}.
412 * @param l The {@link JLabel} to install keyboard actions for.
414 protected void installKeyboardActions(JLabel l)
416 Component c = l.getLabelFor();
417 if (c != null)
419 int mnemonic = l.getDisplayedMnemonic();
420 if (mnemonic > 0)
422 // add a keystroke for the given mnemonic mapping to 'press';
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);
429 // add an action to focus the component when 'press' happens
430 ActionMap map = new ActionMap();
431 map.put("press", new AbstractAction() {
432 public void actionPerformed(ActionEvent event)
434 JLabel label = (JLabel) event.getSource();
435 Component c = label.getLabelFor();
436 if (c != null)
437 c.requestFocus();
440 SwingUtilities.replaceUIActionMap(l, map);
446 * This method uninstalls the keyboard actions for the given {@link JLabel}.
448 * @param l The {@link JLabel} to uninstall keyboard actions for.
450 protected void uninstallKeyboardActions(JLabel l)
452 SwingUtilities.replaceUIActionMap(l, null);
453 SwingUtilities.replaceUIInputMap(l, JComponent.WHEN_IN_FOCUSED_WINDOW,
454 null);
458 * This method installs the listeners for the given {@link JLabel}. The UI
459 * delegate only listens to the label.
461 * @param c The {@link JLabel} to install listeners for.
463 protected void installListeners(JLabel c)
465 c.addPropertyChangeListener(this);
469 * This method uninstalls the listeners for the given {@link JLabel}. The UI
470 * delegate only listens to the label.
472 * @param c The {@link JLabel} to uninstall listeners for.
474 protected void uninstallListeners(JLabel c)
476 c.removePropertyChangeListener(this);
480 * This method is called whenever any JLabel's that use this UI has one of
481 * their properties change.
483 * @param e The {@link PropertyChangeEvent} that describes the change.
485 public void propertyChange(PropertyChangeEvent e)
487 if (e.getPropertyName().equals("text"))
489 String text = (String) e.getNewValue();
490 JLabel l = (JLabel) e.getSource();
491 BasicHTML.updateRenderer(l, text);
493 else if (e.getPropertyName().equals("displayedMnemonic"))
495 // update the key to action mapping
496 JLabel label = (JLabel) e.getSource();
497 if (label.getLabelFor() != null)
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");
509 else if (e.getPropertyName().equals("labelFor"))
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");
521 * Fetches a font metrics object for the specified label. This first
522 * tries to get it from the label object itself by calling
523 * {@link Component#getFontMetrics(Font)}, and if that does not work
524 * (for instance, when we are in the initialization and have no parent yet),
525 * it asks the Toolkit for a font metrics object.
527 * @param l the label
529 * @return a suitable font metrics object
531 private FontMetrics getFontMetrics(JLabel l)
533 Font font = l.getFont();
534 FontMetrics fm = l.getFontMetrics(font);
535 if (fm == null)
537 Toolkit tk = Toolkit.getDefaultToolkit();
538 fm = tk.getFontMetrics(font);
540 return fm;