/[classpath]/classpath/javax/swing/plaf/basic/BasicLabelUI.java
ViewVC logotype

Diff of /classpath/javax/swing/plaf/basic/BasicLabelUI.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.10.2.1 by gnu_andrew, Tue Aug 2 20:12:38 2005 UTC revision 1.10.2.2 by gnu_andrew, Tue Aug 16 16:22:38 2005 UTC
# Line 1  Line 1 
1  /* BasicLabelUI.java  /* BasicLabelUI.java
2     Copyright (C) 2002, 2004 Free Software Foundation, Inc.   Copyright (C) 2002, 2004 Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.   This file is part of GNU Classpath.
5    
6  GNU Classpath is free software; you can redistribute it and/or modify   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   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)   the Free Software Foundation; either version 2, or (at your option)
9  any later version.   any later version.
10    
11  GNU Classpath is distributed in the hope that it will be useful, but   GNU Classpath is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of   WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  General Public License for more details.   General Public License for more details.
15    
16  You should have received a copy of the GNU General Public License   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   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   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301 USA.   02110-1301 USA.
20    
21  Linking this library statically or dynamically with other modules is   Linking this library statically or dynamically with other modules is
22  making a combined work based on this library.  Thus, the terms and   making a combined work based on this library.  Thus, the terms and
23  conditions of the GNU General Public License cover the whole   conditions of the GNU General Public License cover the whole
24  combination.   combination.
25    
26  As a special exception, the copyright holders of this library give you   As a special exception, the copyright holders of this library give you
27  permission to link this library with independent modules to produce an   permission to link this library with independent modules to produce an
28  executable, regardless of the license terms of these independent   executable, regardless of the license terms of these independent
29  modules, and to copy and distribute the resulting executable under   modules, and to copy and distribute the resulting executable under
30  terms of your choice, provided that you also meet, for each linked   terms of your choice, provided that you also meet, for each linked
31  independent module, the terms and conditions of the license of that   independent module, the terms and conditions of the license of that
32  module.  An independent module is a module which is not derived from   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   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   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   obligated to do so.  If you do not wish to do so, delete this
36  exception statement from your version. */   exception statement from your version. */
37    
38  package javax.swing.plaf.basic;  package javax.swing.plaf.basic;
39    
# Line 56  import javax.swing.UIManager; Line 56  import javax.swing.UIManager;
56  import javax.swing.plaf.ComponentUI;  import javax.swing.plaf.ComponentUI;
57  import javax.swing.plaf.LabelUI;  import javax.swing.plaf.LabelUI;
58    
   
59  /**  /**
60   * This is the Basic Look and Feel class for the JLabel.  One BasicLabelUI   * This is the Basic Look and Feel class for the JLabel.  One BasicLabelUI
61   * object is used to paint all JLabels that utilize the Basic Look and Feel.   * object is used to paint all JLabels that utilize the Basic Look and Feel.
62   */   */
63  public class BasicLabelUI extends LabelUI implements PropertyChangeListener  public class BasicLabelUI
64        extends LabelUI
65        implements PropertyChangeListener
66  {  {
67    /** The labelUI that is shared by all labels. */    /** The labelUI that is shared by all labels. */
68    protected static BasicLabelUI labelUI;    protected static BasicLabelUI labelUI;
# Line 99  public class BasicLabelUI extends LabelU Line 100  public class BasicLabelUI extends LabelU
100     *     *
101     * @return The preferred size.     * @return The preferred size.
102     */     */
103    public Dimension getPreferredSize(JComponent c)    public Dimension getPreferredSize(JComponent c)
104    {    {
105      JLabel lab = (JLabel)c;      JLabel lab = (JLabel) c;
106      Rectangle vr = new Rectangle();      Rectangle vr = new Rectangle();
107      Rectangle ir = new Rectangle();      Rectangle ir = new Rectangle();
108      Rectangle tr = new Rectangle();      Rectangle tr = new Rectangle();
109      Insets insets = lab.getInsets();            Insets insets = lab.getInsets();
110      FontMetrics fm = lab.getToolkit().getFontMetrics(lab.getFont());      FontMetrics fm = lab.getToolkit().getFontMetrics(lab.getFont());
111      layoutCL(lab, fm, lab.getText(), lab.getIcon(), vr, ir, tr);      layoutCL(lab, fm, lab.getText(), lab.getIcon(), vr, ir, tr);
112      Rectangle cr = tr.union(ir);      Rectangle cr = tr.union(ir);
113      return new Dimension(insets.left + cr.width + insets.right,      return new Dimension(insets.left + cr.width + insets.right, insets.top
114                           insets.top + cr.height + insets.bottom);          + cr.height + insets.bottom);
115        
116    }      }
117    
118    /**    /**
119     * This method returns the minimum size of the {@link JComponent} given. If     * This method returns the minimum size of the {@link JComponent} given. If
# Line 144  public class BasicLabelUI extends LabelU Line 145  public class BasicLabelUI extends LabelU
145    
146    /**    /**
147     * The method that paints the label according to its current state.     * The method that paints the label according to its current state.
148     *     *
149     * @param g The {@link Graphics} object to paint with.     * @param g The {@link Graphics} object to paint with.
150     * @param c The {@link JComponent} to paint.     * @param c The {@link JComponent} to paint.
151     */     */
# Line 169  public class BasicLabelUI extends LabelU Line 170  public class BasicLabelUI extends LabelU
170        vr.width = 0;        vr.width = 0;
171      if (vr.height < 0)      if (vr.height < 0)
172        vr.height = 0;        vr.height = 0;
173          
174      Icon icon = (b.isEnabled()) ? b.getIcon() : b.getDisabledIcon();      Icon icon = (b.isEnabled()) ? b.getIcon() : b.getDisabledIcon();
175    
176      String text = layoutCL(b, fm, b.getText(), icon, vr, ir, tr);      String text = layoutCL(b, fm, b.getText(), icon, vr, ir, tr);
177        
178      if (icon != null)      if (icon != null)
179        icon.paintIcon(b, g, ir.x, ir.y);        icon.paintIcon(b, g, ir.x, ir.y);        
180      if (text != null && ! text.equals(""))      if (text != null && !text.equals(""))
181        {
182          g.setColor(b.getBackground());
183    
184          if (b.isOpaque())
185            g.fillRect(vr.x, vr.y, vr.width, vr.height);
186          else
187        {        {
188          if (b.isEnabled())          g.fillRect(tr.x, tr.y, tr.width, tr.height);
189            paintEnabledText(b, g, text, tr.x, tr.y + fm.getAscent());          if (b.getBorder() != null)
190          else            b.getBorder().paintBorder(b, g, tr.x, tr.y, tr.width, tr.height);
191            paintDisabledText(b, g, text, tr.x, tr.y + fm.getAscent());          b.setBorder(null);
192        }        }
193    
194          if (b.isEnabled())
195            paintEnabledText(b, g, text, tr.x, tr.y + fm.getAscent());
196          else
197            paintDisabledText(b, g, text, tr.x, tr.y + fm.getAscent());
198        }
199    
200      g.setFont(saved_font);      g.setFont(saved_font);
201    }    }
202    
203    /**    /**
204     * This method is simply calls SwingUtilities's layoutCompoundLabel.     * This method is simply calls SwingUtilities's layoutCompoundLabel.
205     *     *
206     * @param label The label to lay out.     * @param label The label to lay out.
207     * @param fontMetrics The FontMetrics for the font used.     * @param fontMetrics The FontMetrics for the font used.
208     * @param text The text to paint.     * @param text The text to paint.
# Line 196  public class BasicLabelUI extends LabelU Line 210  public class BasicLabelUI extends LabelU
210     * @param viewR The entire viewable rectangle.     * @param viewR The entire viewable rectangle.
211     * @param iconR The icon bounds rectangle.     * @param iconR The icon bounds rectangle.
212     * @param textR The text bounds rectangle.     * @param textR The text bounds rectangle.
213     *     *
214     * @return A possibly clipped version of the text.     * @return A possibly clipped version of the text.
215     */     */
216    protected String layoutCL(JLabel label, FontMetrics fontMetrics,    protected String layoutCL(JLabel label, FontMetrics fontMetrics, String text,
217                              String text, Icon icon, Rectangle viewR,        Icon icon, Rectangle viewR, Rectangle iconR, Rectangle textR)
                             Rectangle iconR, Rectangle textR)  
218    {    {
219      return SwingUtilities.layoutCompoundLabel(label, fontMetrics, text, icon,      return SwingUtilities.layoutCompoundLabel(label, fontMetrics, text, icon,
220                                                label.getVerticalAlignment(),          label.getVerticalAlignment(), label.getHorizontalAlignment(), label
221                                                label.getHorizontalAlignment(),              .getVerticalTextPosition(), label.getHorizontalTextPosition(),
222                                                label.getVerticalTextPosition(),          viewR, iconR, textR, label.getIconTextGap());
                                               label.getHorizontalTextPosition(),  
                                               viewR, iconR, textR,  
                                               label.getIconTextGap());  
223    }    }
224    
225    /**    /**
# Line 225  public class BasicLabelUI extends LabelU Line 235  public class BasicLabelUI extends LabelU
235     * @param textY The y coordinate of the start of the baseline.     * @param textY The y coordinate of the start of the baseline.
236     */     */
237    protected void paintDisabledText(JLabel l, Graphics g, String s, int textX,    protected void paintDisabledText(JLabel l, Graphics g, String s, int textX,
238                                     int textY)        int textY)
239    {    {
240      Color saved_color = g.getColor();      Color saved_color = g.getColor();
241    
# Line 235  public class BasicLabelUI extends LabelU Line 245  public class BasicLabelUI extends LabelU
245    
246      if (mnemIndex != -1)      if (mnemIndex != -1)
247        BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,        BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,
248                                                     textY);            textY);
249      else      else
250        g.drawString(s, textX, textY);        g.drawString(s, textX, textY);
251    
252      g.setColor(l.getBackground().darker());      g.setColor(l.getBackground().darker());
253      if (mnemIndex != -1)      if (mnemIndex != -1)
254        BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX + 1,        BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX + 1,
255                                                     textY + 1);            textY + 1);
256      else      else
257        g.drawString(s, textX + 1, textY + 1);        g.drawString(s, textX + 1, textY + 1);
258    
# Line 260  public class BasicLabelUI extends LabelU Line 270  public class BasicLabelUI extends LabelU
270     * @param textY The y coordinate of the start of the baseline.     * @param textY The y coordinate of the start of the baseline.
271     */     */
272    protected void paintEnabledText(JLabel l, Graphics g, String s, int textX,    protected void paintEnabledText(JLabel l, Graphics g, String s, int textX,
273                                    int textY)        int textY)
274    {    {
275      Color saved_color = g.getColor();      Color saved_color = g.getColor();
276      g.setColor(l.getForeground());      g.setColor(l.getForeground());
# Line 269  public class BasicLabelUI extends LabelU Line 279  public class BasicLabelUI extends LabelU
279    
280      if (mnemIndex != -1)      if (mnemIndex != -1)
281        BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,        BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,
282                                                     textY);            textY);
283      else      else
284        g.drawString(s, textX, textY);        g.drawString(s, textX, textY);
285    
# Line 287  public class BasicLabelUI extends LabelU Line 297  public class BasicLabelUI extends LabelU
297    {    {
298      super.installUI(c);      super.installUI(c);
299      if (c instanceof JLabel)      if (c instanceof JLabel)
300        {      {
301          JLabel l = (JLabel) c;        JLabel l = (JLabel) c;
302    
303          installComponents(l);        installComponents(l);
304          installDefaults(l);        installDefaults(l);
305          installListeners(l);        installListeners(l);
306          installKeyboardActions(l);        installKeyboardActions(l);
307        }      }
308    }    }
309    
310    /**    /**
# Line 308  public class BasicLabelUI extends LabelU Line 318  public class BasicLabelUI extends LabelU
318    {    {
319      super.uninstallUI(c);      super.uninstallUI(c);
320      if (c instanceof JLabel)      if (c instanceof JLabel)
321        {      {
322          JLabel l = (JLabel) c;        JLabel l = (JLabel) c;
323    
324          uninstallKeyboardActions(l);        uninstallKeyboardActions(l);
325          uninstallListeners(l);        uninstallListeners(l);
326          uninstallDefaults(l);        uninstallDefaults(l);
327          uninstallComponents(l);        uninstallComponents(l);
328        }      }
329    }    }
330    
331    /**    /**

Legend:
Removed from v.1.10.2.1  
changed lines
  Added in v.1.10.2.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26