KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > metal > MetalComboBoxUI


1 /*
2  * @(#)MetalComboBoxUI.java 1.49 04/05/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.metal;
9
10 import java.awt.*;
11 import java.awt.event.*;
12 import javax.swing.*;
13 import javax.swing.plaf.*;
14 import javax.swing.border.*;
15 import javax.swing.plaf.basic.*;
16 import java.io.Serializable JavaDoc;
17 import java.beans.*;
18
19
20 /**
21  * Metal UI for JComboBox
22  * <p>
23  * <strong>Warning:</strong>
24  * Serialized objects of this class will not be compatible with
25  * future Swing releases. The current serialization support is
26  * appropriate for short term storage or RMI between applications running
27  * the same version of Swing. As of 1.4, support for long term storage
28  * of all JavaBeans<sup><font size="-2">TM</font></sup>
29  * has been added to the <code>java.beans</code> package.
30  * Please see {@link java.beans.XMLEncoder}.
31  *
32  * @see MetalComboBoxEditor
33  * @see MetalComboBoxButton
34  * @version 1.49 05/18/04
35  * @author Tom Santos
36  */

37 public class MetalComboBoxUI extends BasicComboBoxUI {
38
39     public static ComponentUI createUI(JComponent c) {
40         return new MetalComboBoxUI JavaDoc();
41     }
42
43     public void paint(Graphics g, JComponent c) {
44         if (MetalLookAndFeel.usingOcean()) {
45             super.paint(g, c);
46         }
47     }
48
49     /**
50      * If necessary paints the currently selected item.
51      *
52      * @param g Graphics to paint to
53      * @param bounds Region to paint current value to
54      * @param hasFocus whether or not the JComboBox has focus
55      * @throws NullPointerException if any of the arguments are null.
56      * @since 1.5
57      */

58     public void paintCurrentValue(Graphics g, Rectangle bounds,
59                                   boolean hasFocus) {
60         // This is really only called if we're using ocean.
61
if (MetalLookAndFeel.usingOcean()) {
62             bounds.x += 2;
63             bounds.y += 2;
64             bounds.width -= 3;
65             bounds.height -= 4;
66             super.paintCurrentValue(g, bounds, hasFocus);
67         }
68         else if (g == null || bounds == null) {
69             throw new NullPointerException JavaDoc(
70                 "Must supply a non-null Graphics and Rectangle");
71         }
72     }
73
74     /**
75      * If necessary paints the background of the currently selected item.
76      *
77      * @param g Graphics to paint to
78      * @param bounds Region to paint background to
79      * @param hasFocus whether or not the JComboBox has focus
80      * @throws NullPointerException if any of the arguments are null.
81      * @since 1.5
82      */

83     public void paintCurrentValueBackground(Graphics g, Rectangle bounds,
84                                             boolean hasFocus) {
85         // This is really only called if we're using ocean.
86
if (MetalLookAndFeel.usingOcean()) {
87             g.setColor(MetalLookAndFeel.getControlDarkShadow());
88             g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height - 1);
89             g.setColor(MetalLookAndFeel.getControlShadow());
90             g.drawRect(bounds.x + 1, bounds.y + 1, bounds.width - 2,
91                        bounds.height - 3);
92         }
93         else if (g == null || bounds == null) {
94             throw new NullPointerException JavaDoc(
95                 "Must supply a non-null Graphics and Rectangle");
96         }
97     }
98
99     protected ComboBoxEditor createEditor() {
100         return new MetalComboBoxEditor.UIResource JavaDoc();
101     }
102
103     protected ComboPopup createPopup() {
104         return super.createPopup();
105     }
106
107     protected JButton createArrowButton() {
108         boolean iconOnly = (comboBox.isEditable() ||
109                             MetalLookAndFeel.usingOcean());
110         JButton button = new MetalComboBoxButton JavaDoc( comboBox,
111                                                   new MetalComboBoxIcon JavaDoc(),
112                                                   iconOnly,
113                                                   currentValuePane,
114                                                   listBox );
115         button.setMargin( new Insets( 0, 1, 1, 3 ) );
116         if (MetalLookAndFeel.usingOcean()) {
117             // Disabled rollover effect.
118
button.putClientProperty(MetalBorders.NO_BUTTON_ROLLOVER,
119                                      Boolean.TRUE);
120         }
121         updateButtonForOcean(button);
122         return button;
123     }
124
125     /**
126      * Resets the necessary state on the ComboBoxButton for ocean.
127      */

128     private void updateButtonForOcean(JButton button) {
129         if (MetalLookAndFeel.usingOcean()) {
130             // Ocean renders the focus in a different way, this
131
// would be redundant.
132
button.setFocusPainted(comboBox.isEditable());
133         }
134     }
135
136     public PropertyChangeListener createPropertyChangeListener() {
137         return new MetalPropertyChangeListener();
138     }
139
140     /**
141      * This inner class is marked &quot;public&quot; due to a compiler bug.
142      * This class should be treated as a &quot;protected&quot; inner class.
143      * Instantiate it only within subclasses of <FooUI>.
144      */

145     public class MetalPropertyChangeListener extends BasicComboBoxUI.PropertyChangeHandler {
146         public void propertyChange(PropertyChangeEvent e) {
147             super.propertyChange( e );
148             String JavaDoc propertyName = e.getPropertyName();
149
150             if ( propertyName == "editable" ) {
151         MetalComboBoxButton JavaDoc button = (MetalComboBoxButton JavaDoc)arrowButton;
152         button.setIconOnly( comboBox.isEditable() ||
153                                     MetalLookAndFeel.usingOcean() );
154         comboBox.repaint();
155                 updateButtonForOcean(button);
156             } else if ( propertyName == "background" ) {
157                 Color color = (Color)e.getNewValue();
158                 arrowButton.setBackground(color);
159                 listBox.setBackground(color);
160                 
161             } else if ( propertyName == "foreground" ) {
162                 Color color = (Color)e.getNewValue();
163                 arrowButton.setForeground(color);
164                 listBox.setForeground(color);
165             }
166         }
167     }
168
169     /**
170      * As of Java 2 platform v1.4 this method is no longer used. Do not call or
171      * override. All the functionality of this method is in the
172      * MetalPropertyChangeListener.
173      *
174      * @deprecated As of Java 2 platform v1.4.
175      */

176     @Deprecated JavaDoc
177     protected void editablePropertyChanged( PropertyChangeEvent e ) { }
178
179     protected LayoutManager createLayoutManager() {
180         return new MetalComboBoxLayoutManager();
181     }
182
183     /**
184      * This inner class is marked &quot;public&quot; due to a compiler bug.
185      * This class should be treated as a &quot;protected&quot; inner class.
186      * Instantiate it only within subclasses of <FooUI>.
187      */

188     public class MetalComboBoxLayoutManager extends BasicComboBoxUI.ComboBoxLayoutManager {
189         public void layoutContainer( Container parent ) {
190             layoutComboBox( parent, this );
191         }
192         public void superLayout( Container parent ) {
193             super.layoutContainer( parent );
194         }
195     }
196
197     // This is here because of a bug in the compiler.
198
// When a protected-inner-class-savvy compiler comes out we
199
// should move this into MetalComboBoxLayoutManager.
200
public void layoutComboBox( Container parent, MetalComboBoxLayoutManager manager ) {
201         if (comboBox.isEditable() && !MetalLookAndFeel.usingOcean()) {
202             manager.superLayout( parent );
203             return;
204         }
205
206         if (arrowButton != null) {
207             if (MetalLookAndFeel.usingOcean() &&
208                                 (arrowButton instanceof MetalComboBoxButton JavaDoc)) {
209                 Icon icon = ((MetalComboBoxButton JavaDoc)arrowButton).getComboIcon();
210                 Insets buttonInsets = arrowButton.getInsets();
211                 Insets insets = comboBox.getInsets();
212                 int buttonWidth = icon.getIconWidth() + buttonInsets.left +
213                                   buttonInsets.right;
214         arrowButton.setBounds(MetalUtils.isLeftToRight(comboBox)
215                 ? (comboBox.getWidth() - insets.right - buttonWidth)
216                 : insets.left,
217                             insets.top, buttonWidth,
218                             comboBox.getHeight() - insets.top - insets.bottom);
219             }
220             else {
221                 Insets insets = comboBox.getInsets();
222                 int width = comboBox.getWidth();
223                 int height = comboBox.getHeight();
224                 arrowButton.setBounds( insets.left, insets.top,
225                                        width - (insets.left + insets.right),
226                                        height - (insets.top + insets.bottom) );
227             }
228         }
229
230         if (editor != null && MetalLookAndFeel.usingOcean()) {
231             Rectangle cvb = rectangleForCurrentValue();
232             editor.setBounds(cvb);
233         }
234     }
235
236     /**
237      * As of Java 2 platform v1.4 this method is no
238      * longer used.
239      *
240      * @deprecated As of Java 2 platform v1.4.
241      */

242     @Deprecated JavaDoc
243     protected void removeListeners() {
244         if ( propertyChangeListener != null ) {
245             comboBox.removePropertyChangeListener( propertyChangeListener );
246         }
247     }
248
249     // These two methods were overloaded and made public. This was probably a
250
// mistake in the implementation. The functionality that they used to
251
// provide is no longer necessary and should be removed. However,
252
// removing them will create an uncompatible API change.
253

254     public void configureEditor() {
255     super.configureEditor();
256     }
257
258     public void unconfigureEditor() {
259     super.unconfigureEditor();
260     }
261
262     public Dimension getMinimumSize( JComponent c ) {
263         if ( !isMinimumSizeDirty ) {
264             return new Dimension( cachedMinimumSize );
265         }
266
267         Dimension size = null;
268
269         if ( !comboBox.isEditable() &&
270              arrowButton != null &&
271              arrowButton instanceof MetalComboBoxButton JavaDoc ) {
272
273             MetalComboBoxButton JavaDoc button = (MetalComboBoxButton JavaDoc)arrowButton;
274             Insets buttonInsets = button.getInsets();
275             Insets insets = comboBox.getInsets();
276
277             size = getDisplaySize();
278             size.width += insets.left + insets.right;
279             size.width += buttonInsets.left + buttonInsets.right;
280             size.width += buttonInsets.right + button.getComboIcon().getIconWidth();
281             size.height += insets.top + insets.bottom;
282             size.height += buttonInsets.top + buttonInsets.bottom;
283         }
284         else if ( comboBox.isEditable() &&
285                   arrowButton != null &&
286                   editor != null ) {
287             size = super.getMinimumSize( c );
288             Insets margin = arrowButton.getMargin();
289             size.height += margin.top + margin.bottom;
290             size.width += margin.left + margin.right;
291         }
292         else {
293             size = super.getMinimumSize( c );
294         }
295
296         cachedMinimumSize.setSize( size.width, size.height );
297         isMinimumSizeDirty = false;
298
299         return new Dimension( cachedMinimumSize );
300     }
301
302     /**
303      * This inner class is marked &quot;public&quot; due to a compiler bug.
304      * This class should be treated as a &quot;protected&quot; inner class.
305      * Instantiate it only within subclasses of <FooUI>.
306      *
307      * This class is now obsolete and doesn't do anything and
308      * is only included for backwards API compatibility. Do not call or
309      * override.
310      *
311      * @deprecated As of Java 2 platform v1.4.
312      */

313     @Deprecated JavaDoc
314     public class MetalComboPopup extends BasicComboPopup {
315
316     public MetalComboPopup( JComboBox cBox) {
317         super( cBox );
318     }
319
320     // This method was overloaded and made public. This was probably
321
// mistake in the implementation. The functionality that they used to
322
// provide is no longer necessary and should be removed. However,
323
// removing them will create an uncompatible API change.
324

325     public void delegateFocus(MouseEvent e) {
326         super.delegateFocus(e);
327     }
328     }
329 }
330
331
332
Popular Tags