Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / javax / swing / plaf / metal / MetalComboBoxButton.java
blob6993e18e9b9fc5c546a97b9f1d2fd737a22ef82a
1 /* MetalComboBoxButton.java
2 Copyright (C) 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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. */
39 package javax.swing.plaf.metal;
41 import java.awt.Component;
42 import java.awt.Graphics;
43 import java.awt.Insets;
44 import java.awt.Rectangle;
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;
53 /**
54 * A button used by the {@link MetalComboBoxUI} class.
56 public class MetalComboBoxButton extends JButton {
58 /** A reference to the JComboBox that the button belongs to. */
59 protected JComboBox comboBox;
61 /** A reference to the JList. */
62 protected JList listBox;
64 /** ??? */
65 protected CellRendererPane rendererPane;
67 /** The button icon. */
68 protected Icon comboIcon;
70 /** Display just the icon, or the icon plus the label. */
71 protected boolean iconOnly;
73 /**
74 * Creates a new button.
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.
82 public MetalComboBoxButton(JComboBox cb, Icon i, CellRendererPane pane,
83 JList list)
85 this(cb, i, cb.isEditable(), pane, list);
88 /**
89 * Creates a new button.
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.
99 public MetalComboBoxButton(JComboBox cb, Icon i, boolean onlyIcon,
100 CellRendererPane pane, JList list)
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;
113 * Returns the combo box that the button is used with.
115 * @return The combo box.
117 public final JComboBox getComboBox()
119 return comboBox;
123 * Sets the combo box that the button is used with.
125 * @param cb the combo box.
127 public final void setComboBox(JComboBox cb)
129 comboBox = cb;
133 * Returns the icon displayed by the button. By default, this will be an
134 * instance of {@link MetalComboBoxIcon}.
136 * @return The icon displayed by the button.
138 public final Icon getComboIcon()
140 return comboIcon;
144 * Sets the icon displayed by the button.
146 * @param i the icon.
148 public final void setComboIcon(Icon i)
150 comboIcon = i;
154 * Returns a flag that controls whether the button displays an icon only,
155 * or text as well.
157 * @return A boolean.
159 public final boolean isIconOnly()
161 return iconOnly;
165 * Sets the flag that controls whether the button displays an icon only,
166 * or text as well.
168 * @param isIconOnly the flag.
170 public final void setIconOnly(boolean isIconOnly)
172 iconOnly = isIconOnly;
176 * Returns <code>false</code>, to indicate that this component is not part
177 * of the focus traversal group.
179 * @return <code>false</code>
181 public boolean isFocusTraversable()
183 return false;
187 * Enables or disables the button.
189 * @param enabled the new status.
191 public void setEnabled(boolean enabled)
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?
199 * Paints the component.
201 * @param g the graphics device.
203 public void paintComponent(Graphics g)
205 super.paintComponent(g);
206 if (iconOnly)
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);
213 else
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())
231 g.setColor(MetalLookAndFeel.getFocusColor());
232 g.drawRect(innerArea.x, innerArea.y - 1, innerArea.width - 1,
233 innerArea.height);
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);