Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / plaf / basic / BasicLabelUI.java
blobb278f1f593ba3be36a40cbd1dd71b00ad516c34b
1 /* BasicLabelUI.java
2 Copyright (C) 2002, 2004 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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.Color;
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.beans.PropertyChangeEvent;
48 import java.beans.PropertyChangeListener;
50 import javax.swing.Icon;
51 import javax.swing.JComponent;
52 import javax.swing.JLabel;
53 import javax.swing.SwingUtilities;
54 import javax.swing.UIDefaults;
55 import javax.swing.UIManager;
56 import javax.swing.plaf.ComponentUI;
57 import javax.swing.plaf.LabelUI;
60 /**
61 * This is the Basic Look and Feel class for the JLabel. One BasicLabelUI
62 * object is used to paint all JLabels that utilize the Basic Look and Feel.
64 public class BasicLabelUI extends LabelUI implements PropertyChangeListener
66 /** The labelUI that is shared by all labels. */
67 protected static BasicLabelUI labelUI;
69 /**
70 * Creates a new BasicLabelUI object.
72 public BasicLabelUI()
74 super();
77 /**
78 * Creates and returns a UI for the label. Since one UI is shared by all
79 * labels, this means creating only if necessary and returning the shared
80 * UI.
82 * @param c The {@link JComponent} that a UI is being created for.
84 * @return A label UI for the Basic Look and Feel.
86 public static ComponentUI createUI(JComponent c)
88 if (labelUI == null)
89 labelUI = new BasicLabelUI();
90 return labelUI;
93 /**
94 * Returns the preferred size of this component as calculated by the
95 * {@link layoutCL} method.
97 * @param c This {@link JComponent} to get a preferred size for.
99 * @return The preferred size.
101 public Dimension getPreferredSize(JComponent c)
103 JLabel lab = (JLabel)c;
104 Rectangle vr = new Rectangle();
105 Rectangle ir = new Rectangle();
106 Rectangle tr = new Rectangle();
107 Insets insets = lab.getInsets();
108 FontMetrics fm = lab.getToolkit().getFontMetrics(lab.getFont());
109 layoutCL(lab, fm, lab.getText(), lab.getIcon(), vr, ir, tr);
110 Rectangle cr = tr.union(ir);
111 return new Dimension(insets.left + cr.width + insets.right,
112 insets.top + cr.height + insets.bottom);
117 * This method returns the minimum size of the {@link JComponent} given. If
118 * this method returns null, then it is up to the Layout Manager to give
119 * this component a minimum size.
121 * @param c The {@link JComponent} to get a minimum size for.
123 * @return The minimum size.
125 public Dimension getMinimumSize(JComponent c)
127 return getPreferredSize(c);
131 * This method returns the maximum size of the {@link JComponent} given. If
132 * this method returns null, then it is up to the Layout Manager to give
133 * this component a maximum size.
135 * @param c The {@link JComponent} to get a maximum size for.
137 * @return The maximum size.
139 public Dimension getMaximumSize(JComponent c)
141 return getPreferredSize(c);
145 * The method that paints the label according to its current state.
147 * @param g The {@link Graphics} object to paint with.
148 * @param c The {@link JComponent} to paint.
150 public void paint(Graphics g, JComponent c)
152 JLabel b = (JLabel) c;
154 Font saved_font = g.getFont();
156 Rectangle tr = new Rectangle();
157 Rectangle ir = new Rectangle();
158 Rectangle vr = new Rectangle();
160 Font f = c.getFont();
162 g.setFont(f);
163 FontMetrics fm = g.getFontMetrics(f);
165 vr = SwingUtilities.calculateInnerArea(c, vr);
167 if (vr.width < 0)
168 vr.width = 0;
169 if (vr.height < 0)
170 vr.height = 0;
172 Icon icon = (b.isEnabled()) ? b.getIcon() : b.getDisabledIcon();
174 String text = layoutCL(b, fm, b.getText(), icon, vr, ir, tr);
176 if (icon != null)
177 icon.paintIcon(b, g, ir.x, ir.y);
178 if (text != null && ! text.equals(""))
180 if (b.isEnabled())
181 paintEnabledText(b, g, text, tr.x, tr.y + fm.getAscent());
182 else
183 paintDisabledText(b, g, text, tr.x, tr.y + fm.getAscent());
185 g.setFont(saved_font);
189 * This method is simply calls SwingUtilities's layoutCompoundLabel.
191 * @param label The label to lay out.
192 * @param fontMetrics The FontMetrics for the font used.
193 * @param text The text to paint.
194 * @param icon The icon to draw.
195 * @param viewR The entire viewable rectangle.
196 * @param iconR The icon bounds rectangle.
197 * @param textR The text bounds rectangle.
199 * @return A possibly clipped version of the text.
201 protected String layoutCL(JLabel label, FontMetrics fontMetrics,
202 String text, Icon icon, Rectangle viewR,
203 Rectangle iconR, Rectangle textR)
205 return SwingUtilities.layoutCompoundLabel(label, fontMetrics, text, icon,
206 label.getVerticalAlignment(),
207 label.getHorizontalAlignment(),
208 label.getVerticalTextPosition(),
209 label.getHorizontalTextPosition(),
210 viewR, iconR, textR,
211 label.getIconTextGap());
215 * Paints the text if the label is disabled. By default, this paints the
216 * clipped text returned by layoutCompoundLabel using the
217 * background.brighter() color. It also paints the same text using the
218 * background.darker() color one pixel to the right and one pixel down.
220 * @param l The {@link JLabel} being painted.
221 * @param g The {@link Graphics} object to paint with.
222 * @param s The String to paint.
223 * @param textX The x coordinate of the start of the baseline.
224 * @param textY The y coordinate of the start of the baseline.
226 protected void paintDisabledText(JLabel l, Graphics g, String s, int textX,
227 int textY)
229 Color saved_color = g.getColor();
231 g.setColor(l.getBackground().brighter());
233 int mnemIndex = l.getDisplayedMnemonicIndex();
235 if (mnemIndex != -1)
236 BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,
237 textY);
238 else
239 g.drawString(s, textX, textY);
241 g.setColor(l.getBackground().darker());
242 if (mnemIndex != -1)
243 BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX + 1,
244 textY + 1);
245 else
246 g.drawString(s, textX + 1, textY + 1);
248 g.setColor(saved_color);
252 * Paints the text if the label is enabled. The text is painted using the
253 * foreground color.
255 * @param l The {@link JLabel} being painted.
256 * @param g The {@link Graphics} object to paint with.
257 * @param s The String to paint.
258 * @param textX The x coordinate of the start of the baseline.
259 * @param textY The y coordinate of the start of the baseline.
261 protected void paintEnabledText(JLabel l, Graphics g, String s, int textX,
262 int textY)
264 Color saved_color = g.getColor();
265 g.setColor(l.getForeground());
267 int mnemIndex = l.getDisplayedMnemonicIndex();
269 if (mnemIndex != -1)
270 BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,
271 textY);
272 else
273 g.drawString(s, textX, textY);
275 g.setColor(saved_color);
279 * This method installs the UI for the given {@link JComponent}. This
280 * method will install the component, defaults, listeners, and keyboard
281 * actions.
283 * @param c The {@link JComponent} that this UI is being installed on.
285 public void installUI(JComponent c)
287 super.installUI(c);
288 if (c instanceof JLabel)
290 JLabel l = (JLabel) c;
292 installComponents(l);
293 installDefaults(l);
294 installListeners(l);
295 installKeyboardActions(l);
300 * This method uninstalls the UI for the given {@link JComponent}. This
301 * method will uninstall the component, defaults, listeners, and keyboard
302 * actions.
304 * @param c The {@link JComponent} that this UI is being installed on.
306 public void uninstallUI(JComponent c)
308 super.uninstallUI(c);
309 if (c instanceof JLabel)
311 JLabel l = (JLabel) c;
313 uninstallKeyboardActions(l);
314 uninstallListeners(l);
315 uninstallDefaults(l);
316 uninstallComponents(l);
321 * This method installs the components for this {@link JLabel}.
323 * @param c The {@link JLabel} to install components for.
325 protected void installComponents(JLabel c)
327 //FIXME: fix javadoc + implement.
331 * This method uninstalls the components for this {@link JLabel}.
333 * @param c The {@link JLabel} to uninstall components for.
335 protected void uninstallComponents(JLabel c)
337 //FIXME: fix javadoc + implement.
341 * This method installs the defaults that are defined in the Basic look and
342 * feel for this {@link JLabel}.
344 * @param c The {@link JLabel} to install defaults for.
346 protected void installDefaults(JLabel c)
348 UIDefaults defaults = UIManager.getLookAndFeelDefaults();
350 c.setForeground(defaults.getColor("Label.foreground"));
351 c.setBackground(defaults.getColor("Label.background"));
352 c.setFont(defaults.getFont("Label.font"));
353 c.setBorder(defaults.getBorder("Label.border"));
354 c.setOpaque(true);
355 //XXX: There are properties we don't use called disabledForeground
356 //and disabledShadow.
360 * This method uninstalls the defaults that are defined in the Basic look
361 * and feel for this {@link JLabel}.
363 * @param c The {@link JLabel} to uninstall defaults for.
365 protected void uninstallDefaults(JLabel c)
367 c.setForeground(null);
368 c.setBackground(null);
369 c.setFont(null);
370 c.setBorder(null);
374 * This method installs the keyboard actions for the given {@link JLabel}.
376 * @param l The {@link JLabel} to install keyboard actions for.
378 protected void installKeyboardActions(JLabel l)
380 //FIXME: implement.
384 * This method uninstalls the keyboard actions for the given {@link JLabel}.
386 * @param l The {@link JLabel} to uninstall keyboard actions for.
388 protected void uninstallKeyboardActions(JLabel l)
390 //FIXME: implement.
394 * This method installs the listeners for the given {@link JLabel}. The UI
395 * delegate only listens to the label.
397 * @param c The {@link JLabel} to install listeners for.
399 protected void installListeners(JLabel c)
401 c.addPropertyChangeListener(this);
405 * This method uninstalls the listeners for the given {@link JLabel}. The UI
406 * delegate only listens to the label.
408 * @param c The {@link JLabel} to uninstall listeners for.
410 protected void uninstallListeners(JLabel c)
412 c.removePropertyChangeListener(this);
416 * This method is called whenever any JLabel's that use this UI has one of
417 * their properties change.
419 * @param e The {@link PropertyChangeEvent} that describes the change.
421 public void propertyChange(PropertyChangeEvent e)
423 JLabel c = (JLabel) e.getSource();
424 c.revalidate();
425 c.repaint();