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

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

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

revision 1.2.2.4 by gnu_andrew, Tue Aug 2 20:12:37 2005 UTC revision 1.2.2.5 by gnu_andrew, Tue Sep 20 18:46:33 2005 UTC
# Line 1  Line 1 
1  /* BasicComboBoxRenderer.java --  /* BasicComboBoxRenderer.java --
2     Copyright (C) 2004  Free Software Foundation, Inc.     Copyright (C) 2004, 2005  Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 40  package javax.swing.plaf.basic; Line 40  package javax.swing.plaf.basic;
40    
41  import java.awt.Component;  import java.awt.Component;
42  import java.awt.Dimension;  import java.awt.Dimension;
43    import java.awt.FontMetrics;
44  import java.io.Serializable;  import java.io.Serializable;
45    
46    import javax.swing.JComboBox;
47  import javax.swing.JLabel;  import javax.swing.JLabel;
48  import javax.swing.JList;  import javax.swing.JList;
49  import javax.swing.ListCellRenderer;  import javax.swing.ListCellRenderer;
50  import javax.swing.SwingConstants;  import javax.swing.SwingConstants;
51  import javax.swing.UIDefaults;  import javax.swing.SwingUtilities;
 import javax.swing.UIManager;  
52  import javax.swing.border.Border;  import javax.swing.border.Border;
53  import javax.swing.border.EmptyBorder;  import javax.swing.border.EmptyBorder;
54    
55  /**  /**
56   * This class is renderer for the combo box.   * A renderer for a {@link JComboBox}.
57   *   *
58   * @author Olga Rodimina   * @author Olga Rodimina
59   */   */
60  public class BasicComboBoxRenderer extends JLabel implements ListCellRenderer,  public class BasicComboBoxRenderer
61                                                               Serializable    extends JLabel
62      implements ListCellRenderer, Serializable
63  {  {
64    /**    /**
65     * This border is used whenever renderer doesn't have a focus.     * A shared border instance for all renderers.
66     */     */
67    protected static Border noFocusBorder = new EmptyBorder(0, 0, 0, 0);    protected static Border noFocusBorder = new EmptyBorder(0, 0, 0, 0);
68    
69    /**    /**
70     * Creates a new BasicComboBoxRenderer object.     * Creates a new <code>BasicComboBoxRenderer</code> object.
71     */     */
72    public BasicComboBoxRenderer()    public BasicComboBoxRenderer()
73    {    {
74      setHorizontalAlignment(SwingConstants.LEFT);      setHorizontalAlignment(SwingConstants.LEFT);
75        setBorder(noFocusBorder);
76    }    }
77    
78    /**    /**
# Line 83  public class BasicComboBoxRenderer exten Line 86  public class BasicComboBoxRenderer exten
86    }    }
87    
88    /**    /**
89     * getListCellRendererComponent     * Returns a component that has been configured to display the given
90       * <code>value</code>.
91     *     *
92     * @param list List of items for which to the background and foreground     * @param list List of items for which to the background and foreground
93     *        colors     *        colors
# Line 100  public class BasicComboBoxRenderer exten Line 104  public class BasicComboBoxRenderer exten
104                                                  boolean cellHasFocus)                                                  boolean cellHasFocus)
105    {    {
106      String s = value.toString();      String s = value.toString();
107      setText(s);      
108        // String maybe larger than comboBox.
109        FontMetrics fm = getToolkit().getFontMetrics(list.getFont());
110        int strWidth = SwingUtilities.computeStringWidth(fm, s);
111        int cbWidth = getSize().width;
112        if (cbWidth != 0 && strWidth > cbWidth)
113          {
114            char[] str = s.toCharArray();
115            int currWidth = 0;
116            int i = 0;
117            String postStr = "... ";
118            cbWidth -= SwingUtilities.computeStringWidth(fm, postStr);
119            while (i < str.length && currWidth < cbWidth)
120              {
121                ++i;
122                currWidth = SwingUtilities.computeStringWidth(fm, new String(str, 0, i));
123              }
124            setText(new String(str, 0, i)  + postStr);
125          }
126        else  
127          setText(s);
128        
129      setOpaque(true);      setOpaque(true);
130    
131      UIDefaults defaults = UIManager.getLookAndFeelDefaults();      if (isSelected || cellHasFocus)
   
     if (isSelected)  
132        {        {
133          setBackground(list.getSelectionBackground());          setBackground(list.getSelectionBackground());
134          setForeground(list.getSelectionForeground());          setForeground(list.getSelectionForeground());
135        }        }
136      else      else
137        {        {
138          setBackground(list.getBackground());          setBackground(list.getBackground());
139          setForeground(list.getForeground());          setForeground(list.getForeground());
140        }        }
141    
142      setEnabled(list.isEnabled());      setEnabled(list.isEnabled());
143      setFont(list.getFont());      setFont(list.getFont());
   
     // Use focusCellHighlightBorder when renderer has focus and  
     // noFocusBorder otherwise  
     if (cellHasFocus)  
       setBorder(UIManager.getBorder("List.focusCellHighlightBorder"));  
     else  
       setBorder(noFocusBorder);  
   
144      return this;      return this;
145    }    }
146    
147      /**
148       * A subclass of {@link BasicComboBoxRenderer} that implements the
149       * {@link javax.swing.plaf.UIResource} interface.
150       */
151    public static class UIResource extends BasicComboBoxRenderer    public static class UIResource extends BasicComboBoxRenderer
152      implements javax.swing.plaf.UIResource      implements javax.swing.plaf.UIResource
153    {    {
154      /**      /**
155       * Creates a new UIResource object.       * Creates a new <code>UIResource</code> object.
156       */       */
157      public UIResource()      public UIResource()
158      {      {

Legend:
Removed from v.1.2.2.4  
changed lines
  Added in v.1.2.2.5

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