Source for javax.swing.plaf.metal.MetalComboBoxButton

   1: /* MetalComboBoxButton.java
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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.
  20: 
  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.
  25: 
  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. */
  37: 
  38: 
  39: package javax.swing.plaf.metal;
  40: 
  41: import java.awt.Component;
  42: import java.awt.Graphics;
  43: import java.awt.Insets;
  44: import java.awt.Rectangle;
  45: 
  46: import javax.swing.CellRendererPane;
  47: import javax.swing.Icon;
  48: import javax.swing.JButton;
  49: import javax.swing.JComboBox;
  50: import javax.swing.JList;
  51: import javax.swing.SwingUtilities;
  52: 
  53: /**
  54:  * A button used by the {@link MetalComboBoxUI} class.
  55:  */
  56: public class MetalComboBoxButton extends JButton {
  57: 
  58:   /** A reference to the JComboBox that the button belongs to. */
  59:   protected JComboBox comboBox;
  60: 
  61:   /** A reference to the JList. */
  62:   protected JList listBox;
  63:   
  64:   /** ??? */
  65:   protected CellRendererPane rendererPane;
  66:   
  67:   /** The button icon. */
  68:   protected Icon comboIcon;
  69:   
  70:   /** Display just the icon, or the icon plus the label. */
  71:   protected boolean iconOnly;
  72:   
  73:   /**
  74:    * Creates a new button.
  75:    * 
  76:    * @param cb  the combo that the button is used for (<code>null</code> not 
  77:    *            permitted).
  78:    * @param i  the icon displayed on the button.
  79:    * @param pane  the rendering pane.
  80:    * @param list  the list.
  81:    */
  82:   public MetalComboBoxButton(JComboBox cb, Icon i, CellRendererPane pane,
  83:       JList list)
  84:   {
  85:     this(cb, i, cb.isEditable(), pane, list);  
  86:   }
  87:   
  88:   /**
  89:    * Creates a new button.
  90:    * 
  91:    * @param cb  the combo that the button is used for (<code>null</code> not 
  92:    *            permitted).
  93:    * @param i  the icon displayed on the button.
  94:    * @parma onlyIcon  a flag that specifies whether the button displays only an
  95:    *                  icon, or text as well.
  96:    * @param pane  the rendering pane.
  97:    * @param list  the list.
  98:    */
  99:   public MetalComboBoxButton(JComboBox cb, Icon i, boolean onlyIcon,
 100:       CellRendererPane pane, JList list)
 101:   {
 102:     super();
 103:     if (cb == null)
 104:       throw new NullPointerException("Null 'cb' argument");
 105:     comboBox = cb;
 106:     comboIcon = i;
 107:     iconOnly = onlyIcon;
 108:     listBox = list;
 109:     rendererPane = pane;
 110:   }
 111:   
 112:   /**
 113:    * Returns the combo box that the button is used with.
 114:    * 
 115:    * @return The combo box.
 116:    */
 117:   public final JComboBox getComboBox()
 118:   {
 119:     return comboBox;
 120:   }
 121:   
 122:   /**
 123:    * Sets the combo box that the button is used with.
 124:    * 
 125:    * @param cb  the combo box.
 126:    */
 127:   public final void setComboBox(JComboBox cb)
 128:   {
 129:     comboBox = cb;
 130:   }
 131:   
 132:   /**
 133:    * Returns the icon displayed by the button.  By default, this will be an
 134:    * instance of {@link MetalComboBoxIcon}.
 135:    * 
 136:    * @return The icon displayed by the button.
 137:    */
 138:   public final Icon getComboIcon()
 139:   {
 140:     return comboIcon;
 141:   }
 142:   
 143:   /**
 144:    * Sets the icon displayed by the button.
 145:    * 
 146:    * @param i  the icon.
 147:    */
 148:   public final void setComboIcon(Icon i)
 149:   {
 150:     comboIcon = i;
 151:   }
 152:   
 153:   /**
 154:    * Returns a flag that controls whether the button displays an icon only,
 155:    * or text as well.
 156:    * 
 157:    * @return A boolean.
 158:    */
 159:   public final boolean isIconOnly()
 160:   {
 161:     return iconOnly;
 162:   }
 163:   
 164:   /**
 165:    * Sets the flag that controls whether the button displays an icon only,
 166:    * or text as well.
 167:    * 
 168:    * @param isIconOnly  the flag.
 169:    */
 170:   public final void setIconOnly(boolean isIconOnly)
 171:   {
 172:     iconOnly = isIconOnly;
 173:   }
 174:   
 175:   /**
 176:    * Returns <code>false</code>, to indicate that this component is not part
 177:    * of the focus traversal group.
 178:    * 
 179:    * @return <code>false</code>
 180:    */
 181:   public boolean isFocusTraversable()
 182:   {
 183:     return false;
 184:   }
 185:   
 186:   /**
 187:    * Enables or disables the button.
 188:    * 
 189:    * @param enabled  the new status.
 190:    */
 191:   public void setEnabled(boolean enabled)
 192:   {
 193:     super.setEnabled(enabled);
 194:     // TODO: figure out what this might need to be used for
 195:     // perhaps it has something to do with the button's icon and/or border?
 196:   }
 197:   
 198:   /**
 199:    * Paints the component.
 200:    * 
 201:    * @param g  the graphics device.
 202:    */
 203:   public void paintComponent(Graphics g)
 204:   {
 205:     super.paintComponent(g);
 206:     if (iconOnly)
 207:       {
 208:         Rectangle bounds = getBounds();
 209:         int x = (bounds.width - comboIcon.getIconWidth()) / 2;
 210:         int y = (bounds.height - comboIcon.getIconHeight()) / 2;
 211:         comboIcon.paintIcon(comboBox, g, x, y);  
 212:       }
 213:     else
 214:       {
 215:         Object selected = comboBox.getModel().getSelectedItem();
 216:         if (selected == null)
 217:           selected = "";
 218:         Rectangle bounds = comboBox.getBounds();
 219:         Rectangle innerArea = SwingUtilities.calculateInnerArea(this, null);
 220:         Insets insets = comboBox.getInsets();
 221:         Rectangle renderArea = new Rectangle(innerArea.x, innerArea.y, 
 222:             innerArea.width - comboIcon.getIconWidth() - 4, innerArea.height);
 223:         Component cellRenderer 
 224:             = comboBox.getRenderer().getListCellRendererComponent(this.listBox,
 225:                     selected, comboBox.getSelectedIndex(), false, false);
 226:         cellRenderer.setBackground(comboBox.getBackground());
 227:         cellRenderer.setEnabled(comboBox.isEnabled());
 228:         rendererPane.paintComponent(g, cellRenderer, this, renderArea);
 229:         if (comboBox.hasFocus())
 230:           {
 231:             g.setColor(MetalLookAndFeel.getFocusColor());
 232:             g.drawRect(innerArea.x, innerArea.y - 1, innerArea.width - 1, 
 233:                     innerArea.height);
 234:           }
 235:         int iconX = bounds.width - insets.right - comboIcon.getIconWidth() - 7;
 236:         int iconY = insets.top 
 237:             + (bounds.height - comboIcon.getIconHeight()) / 2; 
 238:         comboIcon.paintIcon(comboBox, g, iconX, iconY);
 239:       }
 240:   }
 241: }