1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45:
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53:
54:
57: public class MetalComboBoxButton
58: extends JButton
59: {
60:
61:
62: protected JComboBox comboBox;
63:
64:
65: protected JList listBox;
66:
67:
70: protected CellRendererPane rendererPane;
71:
72:
73: protected Icon comboIcon;
74:
75:
76: protected boolean iconOnly;
77:
78:
87: public MetalComboBoxButton(JComboBox cb, Icon i, CellRendererPane pane,
88: JList list)
89: {
90: this(cb, i, cb.isEditable(), pane, list);
91: }
92:
93:
104: public MetalComboBoxButton(JComboBox cb, Icon i, boolean onlyIcon,
105: CellRendererPane pane, JList list)
106: {
107: super();
108: if (cb == null)
109: throw new NullPointerException("Null 'cb' argument");
110: comboBox = cb;
111: comboIcon = i;
112: iconOnly = onlyIcon;
113: listBox = list;
114: rendererPane = pane;
115: setRolloverEnabled(false);
116: setEnabled(comboBox.isEnabled());
117: setFocusable(comboBox.isEnabled());
118: }
119:
120:
125: public final JComboBox getComboBox()
126: {
127: return comboBox;
128: }
129:
130:
135: public final void setComboBox(JComboBox cb)
136: {
137: comboBox = cb;
138: }
139:
140:
146: public final Icon getComboIcon()
147: {
148: return comboIcon;
149: }
150:
151:
156: public final void setComboIcon(Icon i)
157: {
158: comboIcon = i;
159: }
160:
161:
167: public final boolean isIconOnly()
168: {
169: return iconOnly;
170: }
171:
172:
178: public final void setIconOnly(boolean isIconOnly)
179: {
180: iconOnly = isIconOnly;
181: }
182:
183:
189: public boolean isFocusTraversable()
190: {
191: return false;
192: }
193:
194:
199: public void setEnabled(boolean enabled)
200: {
201: super.setEnabled(enabled);
202: if (enabled)
203: {
204: setBackground(comboBox.getBackground());
205: setForeground(comboBox.getForeground());
206: }
207: else
208: {
209: setBackground(UIManager.getColor("ComboBox.disabledBackground"));
210: setForeground(UIManager.getColor("ComboBox.disabledForeground"));
211: }
212: }
213:
214:
219: public void paintComponent(Graphics g)
220: {
221: super.paintComponent(g);
222: Insets insets = this.getInsets();
223: int w = getWidth() - (insets.left + insets.right);
224: int h = getHeight() - (insets.top + insets.bottom);
225: if (h > 0 && w > 0)
226: {
227: int x1 = insets.left;
228: int y1 = insets.top;
229: int x2 = x1 + (w - 1);
230: int y2 = y1 + (h - 1);
231: int iconWidth = 0;
232: int iconX = x2;
233: if (comboIcon != null)
234: {
235: iconWidth = comboIcon.getIconWidth();
236: int iconHeight = comboIcon.getIconHeight();
237: int iconY;
238: if (iconOnly)
239: {
240: iconX = getWidth() / 2 - iconWidth / 2;
241: iconY = getHeight() / 2 - iconHeight / 2;
242: }
243: else
244: {
245: iconX = x1 + (w - 1) - iconWidth;
246: iconY = y1 + (y2 - y1) / 2 - iconHeight / 2;
247: }
248: comboIcon.paintIcon(this, g, iconX, iconY);
249: if (this.hasFocus())
250: {
251: g.setColor(MetalLookAndFeel.getFocusColor());
252: g.drawRect(x1 - 1, y1 - 1, w + 3, h + 1);
253: }
254: }
255: if (! iconOnly && comboBox != null)
256: {
257: ListCellRenderer renderer = comboBox.getRenderer();
258: boolean pressed = this.getModel().isPressed();
259: Component comp = renderer.getListCellRendererComponent(listBox,
260: comboBox.getSelectedItem(), -1, false, false);
261: comp.setFont(rendererPane.getFont());
262:
263: if ((model.isArmed() && model.isPressed())
264: || (comboBox.isFocusOwner() && !comboBox.isPopupVisible()))
265: {
266: if (isOpaque())
267: {
268: comp.setBackground(UIManager.getColor("Button.select"));
269: comp.setForeground(comboBox.getForeground());
270: }
271: }
272: else if (! comboBox.isEnabled())
273: {
274: if (this.isOpaque())
275: {
276: Color dbg =
277: UIManager.getColor("ComboBox.disabledBackground");
278: comp.setBackground(dbg);
279: Color dfg =
280: UIManager.getColor("ComboBox.disabledForeground");
281: comp.setForeground(dfg);
282: }
283: }
284: else
285: {
286: comp.setForeground(comboBox.getForeground());
287: comp.setBackground(comboBox.getBackground());
288: }
289: int wr = w - (insets.right + iconWidth);
290: rendererPane.paintComponent(g, comp, this, x1, y1, wr, h);
291: }
292: }
293: }
294: }