/[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.14 by trebligd, Fri Jul 8 15:24:08 2005 UTC revision 1.15 by langel, Tue Aug 9 16:11:55 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          if (b.isEnabled())  
183            paintEnabledText(b, g, text, tr.x, tr.y + fm.getAscent());        g.setColor(b.getBackground());
184          else        if (b.isOpaque())
185            paintDisabledText(b, g, text, tr.x, tr.y + fm.getAscent());          g.fillRect(vr.x, vr.y, vr.width, vr.height);
186        }        else
187            g.fillRect(tr.x, tr.y, tr.width, tr.height);
188    
189          if (b.isEnabled())
190            paintEnabledText(b, g, text, tr.x, tr.y + fm.getAscent());
191          else
192            paintDisabledText(b, g, text, tr.x, tr.y + fm.getAscent());
193        }
194      g.setFont(saved_font);      g.setFont(saved_font);
195    }    }
196    
197    /**    /**
198     * This method is simply calls SwingUtilities's layoutCompoundLabel.     * This method is simply calls SwingUtilities's layoutCompoundLabel.
199     *     *
200     * @param label The label to lay out.     * @param label The label to lay out.
201     * @param fontMetrics The FontMetrics for the font used.     * @param fontMetrics The FontMetrics for the font used.
202     * @param text The text to paint.     * @param text The text to paint.
# Line 196  public class BasicLabelUI extends LabelU Line 204  public class BasicLabelUI extends LabelU
204     * @param viewR The entire viewable rectangle.     * @param viewR The entire viewable rectangle.
205     * @param iconR The icon bounds rectangle.     * @param iconR The icon bounds rectangle.
206     * @param textR The text bounds rectangle.     * @param textR The text bounds rectangle.
207     *     *
208     * @return A possibly clipped version of the text.     * @return A possibly clipped version of the text.
209     */     */
210    protected String layoutCL(JLabel label, FontMetrics fontMetrics,    protected String layoutCL(JLabel label, FontMetrics fontMetrics, String text,
211                              String text, Icon icon, Rectangle viewR,        Icon icon, Rectangle viewR, Rectangle iconR, Rectangle textR)
                             Rectangle iconR, Rectangle textR)  
212    {    {
213      return SwingUtilities.layoutCompoundLabel(label, fontMetrics, text, icon,      return SwingUtilities.layoutCompoundLabel(label, fontMetrics, text, icon,
214                                                label.getVerticalAlignment(),          label.getVerticalAlignment(), label.getHorizontalAlignment(), label
215                                                label.getHorizontalAlignment(),              .getVerticalTextPosition(), label.getHorizontalTextPosition(),
216                                                label.getVerticalTextPosition(),          viewR, iconR, textR, label.getIconTextGap());
                                               label.getHorizontalTextPosition(),  
                                               viewR, iconR, textR,  
                                               label.getIconTextGap());  
217    }    }
218    
219    /**    /**
# Line 225  public class BasicLabelUI extends LabelU Line 229  public class BasicLabelUI extends LabelU
229     * @param textY The y coordinate of the start of the baseline.     * @param textY The y coordinate of the start of the baseline.
230     */     */
231    protected void paintDisabledText(JLabel l, Graphics g, String s, int textX,    protected void paintDisabledText(JLabel l, Graphics g, String s, int textX,
232                                     int textY)        int textY)
233    {    {
234      Color saved_color = g.getColor();      Color saved_color = g.getColor();
235    
# Line 235  public class BasicLabelUI extends LabelU Line 239  public class BasicLabelUI extends LabelU
239    
240      if (mnemIndex != -1)      if (mnemIndex != -1)
241        BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,        BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,
242                                                     textY);            textY);
243      else      else
244        g.drawString(s, textX, textY);        g.drawString(s, textX, textY);
245    
246      g.setColor(l.getBackground().darker());      g.setColor(l.getBackground().darker());
247      if (mnemIndex != -1)      if (mnemIndex != -1)
248        BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX + 1,        BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX + 1,
249                                                     textY + 1);            textY + 1);
250      else      else
251        g.drawString(s, textX + 1, textY + 1);        g.drawString(s, textX + 1, textY + 1);
252    
# Line 260  public class BasicLabelUI extends LabelU Line 264  public class BasicLabelUI extends LabelU
264     * @param textY The y coordinate of the start of the baseline.     * @param textY The y coordinate of the start of the baseline.
265     */     */
266    protected void paintEnabledText(JLabel l, Graphics g, String s, int textX,    protected void paintEnabledText(JLabel l, Graphics g, String s, int textX,
267                                    int textY)        int textY)
268    {    {
269      Color saved_color = g.getColor();      Color saved_color = g.getColor();
270      g.setColor(l.getForeground());      g.setColor(l.getForeground());
# Line 269  public class BasicLabelUI extends LabelU Line 273  public class BasicLabelUI extends LabelU
273    
274      if (mnemIndex != -1)      if (mnemIndex != -1)
275        BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,        BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,
276                                                     textY);            textY);
277      else      else
278        g.drawString(s, textX, textY);        g.drawString(s, textX, textY);
279    
# Line 287  public class BasicLabelUI extends LabelU Line 291  public class BasicLabelUI extends LabelU
291    {    {
292      super.installUI(c);      super.installUI(c);
293      if (c instanceof JLabel)      if (c instanceof JLabel)
294        {      {
295          JLabel l = (JLabel) c;        JLabel l = (JLabel) c;
296    
297          installComponents(l);        installComponents(l);
298          installDefaults(l);        installDefaults(l);
299          installListeners(l);        installListeners(l);
300          installKeyboardActions(l);        installKeyboardActions(l);
301        }      }
302    }    }
303    
304    /**    /**
# Line 308  public class BasicLabelUI extends LabelU Line 312  public class BasicLabelUI extends LabelU
312    {    {
313      super.uninstallUI(c);      super.uninstallUI(c);
314      if (c instanceof JLabel)      if (c instanceof JLabel)
315        {      {
316          JLabel l = (JLabel) c;        JLabel l = (JLabel) c;
317    
318          uninstallKeyboardActions(l);        uninstallKeyboardActions(l);
319          uninstallListeners(l);        uninstallListeners(l);
320          uninstallDefaults(l);        uninstallDefaults(l);
321          uninstallComponents(l);        uninstallComponents(l);
322        }      }
323    }    }
324    
325    /**    /**

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.15

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