1 /* MetalComboBoxUI.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)
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
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
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.Color;
42 import java.awt.Container;
43 import java.awt.Dimension;
44 import java.awt.Graphics;
45 import java.awt.Insets;
46 import java.awt.LayoutManager;
47 import java.awt.event.MouseEvent;
48 import java.beans.PropertyChangeEvent;
49 import java.beans.PropertyChangeListener;
51 import javax.swing.ComboBoxEditor;
52 import javax.swing.Icon;
53 import javax.swing.JButton;
54 import javax.swing.JComboBox;
55 import javax.swing.JComponent;
56 import javax.swing.plaf.ComponentUI;
57 import javax.swing.plaf.basic.BasicComboBoxUI;
58 import javax.swing.plaf.basic.BasicComboPopup;
59 import javax.swing.plaf.basic.ComboPopup;
63 * A UI delegate for the {@link JComboBox} component.
65 public class MetalComboBoxUI extends BasicComboBoxUI
68 * A layout manager that arranges the editor component (if active) and the
69 * button that make up the combo box.
71 public class MetalComboBoxLayoutManager
72 extends BasicComboBoxUI.ComboBoxLayoutManager
75 * Creates a new instance of the layout manager.
77 public MetalComboBoxLayoutManager()
79 // Nothing to do here.
83 * Arranges the editor (if visible) and button that comprise the combo
86 * @param parent the parent.
88 public void layoutContainer(Container parent)
90 layoutComboBox(parent, this);
94 * Calls the <code>layoutContainer(Container)</code> method in the super
97 * @param parent the container.
99 public void superLayout(Container parent)
101 super.layoutContainer(parent);
106 * A listener used to handle property changes in the {@link JComboBox}
107 * component, to ensure that the UI delegate accurately reflects the current
108 * state in the rendering onscreen.
110 public class MetalPropertyChangeListener
111 extends BasicComboBoxUI.PropertyChangeHandler
114 * Creates a new listener.
116 public MetalPropertyChangeListener()
118 // Nothing to do here.
122 * Handles a property change event, updating the UI components as
125 * @param e the event.
127 public void propertyChange(PropertyChangeEvent e)
129 super.propertyChange(e);
130 String name = e.getPropertyName();
131 if (name.equals("editable"))
132 editablePropertyChanged(e);
133 else if (name.equals("enabled"))
135 if (arrowButton instanceof MetalComboBoxButton)
137 arrowButton.setFocusable(!comboBox.isEditable()
138 && comboBox.isEnabled());
142 else if (name.equals("background"))
144 Color c = (Color) e.getNewValue();
145 arrowButton.setBackground(c);
146 listBox.setBackground(c);
148 else if (name.equals("foreground"))
150 Color c = (Color) e.getNewValue();
151 arrowButton.setForeground(c);
152 listBox.setForeground(c);
158 * A popup menu for the combo-box.
160 * @see #createPopup()
164 public class MetalComboPopup extends BasicComboPopup
167 * Creates a new popup.
169 * @param cBox the combo box.
171 public MetalComboPopup(JComboBox cBox)
176 public void delegateFocus(MouseEvent e)
178 super.delegateFocus(e);
183 * Constructs a new instance of MetalComboBoxUI.
185 public MetalComboBoxUI()
191 * Returns an instance of MetalComboBoxUI.
193 * @param component the component for which we return an UI instance
195 * @return an instance of MetalComboBoxUI
197 public static ComponentUI createUI(JComponent component)
199 return new MetalComboBoxUI();
203 * Creates an editor for the combo box.
207 protected ComboBoxEditor createEditor()
209 return new MetalComboBoxEditor.UIResource();
213 * Creates a popup for the combo box.
217 protected ComboPopup createPopup()
219 return super.createPopup();
223 * Creates a new button for use in rendering the JComboBox.
227 protected JButton createArrowButton()
229 JButton button = new MetalComboBoxButton(comboBox, new MetalComboBoxIcon(),
230 currentValuePane, listBox);
231 button.setMargin(new Insets(0, 1, 1, 3));
236 * Creates a new property change listener.
238 * @return A new property change listener.
240 public PropertyChangeListener createPropertyChangeListener()
242 return new MetalPropertyChangeListener();
245 public void paint(Graphics g, JComponent c)
247 // do nothing, the button and text field are painted elsewhere
251 * Updates the button and text field to reflect a change in the 'editable'
254 * @param e the event.
258 protected void editablePropertyChanged(PropertyChangeEvent e)
260 if (arrowButton instanceof MetalComboBoxButton)
262 MetalComboBoxButton b = (MetalComboBoxButton) arrowButton;
263 b.setIconOnly(comboBox.isEditable());
264 b.setFocusable(!comboBox.isEditable() && comboBox.isEnabled());
270 * Creates a new layout manager for the UI delegate.
272 * @return A new layout manager.
274 protected LayoutManager createLayoutManager()
276 return new MetalComboBoxLayoutManager();
280 * Not used in Classpath.
284 protected void removeListeners()
286 // no longer used in JDK 1.4
290 * Returns the minimum size for the combo.
292 * @param c the component
294 * @return The minimum size for the combo box.
296 public Dimension getMinimumSize(JComponent c)
298 if (!isMinimumSizeDirty)
299 return new Dimension(cachedMinimumSize);
302 if (!comboBox.isEditable() && arrowButton != null
303 && arrowButton instanceof MetalComboBoxButton)
305 MetalComboBoxButton b = (MetalComboBoxButton) arrowButton;
306 d = getDisplaySize();
307 Insets arrowInsets = b.getInsets();
308 Insets comboInsets = comboBox.getInsets();
309 Icon icon = b.getComboIcon();
310 d.width += comboInsets.left + comboInsets.right;
311 d.width += arrowInsets.left + arrowInsets.right;
312 d.width += arrowInsets.right + icon.getIconWidth();
313 d.height += comboInsets.top + comboInsets.bottom;
314 d.height += arrowInsets.top + arrowInsets.bottom;
316 else if (comboBox.isEditable() && arrowButton != null && editor != null)
318 d = super.getMinimumSize(c);
319 Insets arrowMargin = arrowButton.getMargin();
320 d.height += arrowMargin.top + arrowMargin.bottom;
321 d.width += arrowMargin.left + arrowMargin.right;
325 d = super.getMinimumSize(c);
327 cachedMinimumSize.setSize(d.width, d.height);
328 isMinimumSizeDirty = false;
329 return new Dimension(cachedMinimumSize);
333 * Configures the editor for this combo box.
335 public void configureEditor()
337 super.configureEditor();
338 if (popupKeyListener != null)
339 editor.removeKeyListener(popupKeyListener);
340 if (focusListener != null)
341 editor.addFocusListener(focusListener);
345 * Unconfigures the editor for this combo box.
347 public void unconfigureEditor()
349 super.unconfigureEditor();
350 if (focusListener != null)
351 editor.removeFocusListener(focusListener);
355 * Lays out the ComboBox
357 public void layoutComboBox(Container parent,
358 MetalComboBoxUI.MetalComboBoxLayoutManager manager)
360 if (comboBox.isEditable())
361 manager.superLayout(parent);
362 else if (arrowButton != null)
364 Insets comboInsets = comboBox.getInsets();
365 int width = comboBox.getWidth();
366 int height = comboBox.getHeight();
367 arrowButton.setBounds(comboInsets.left, comboInsets.top,
368 width - (comboInsets.left + comboInsets.right),
369 height - (comboInsets.top + comboInsets.bottom));