KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > synth > SynthComboBoxUI


1 /*
2  * @(#)SynthComboBoxUI.java 1.15 03/12/19
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.synth;
9
10 import java.awt.*;
11 import java.awt.event.*;
12 import java.lang.reflect.*;
13 import javax.swing.*;
14 import javax.accessibility.*;
15 import javax.swing.FocusManager JavaDoc;
16 import javax.swing.plaf.*;
17 import javax.swing.border.*;
18 import javax.swing.text.*;
19 import javax.swing.event.*;
20 import javax.swing.plaf.basic.*;
21 import java.beans.PropertyChangeListener JavaDoc;
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import sun.awt.AppContext;
24 import sun.swing.plaf.synth.SynthUI;
25
26 /**
27  * Synth's ComboBoxUI.
28  *
29  * @version 1.15, 12/19/03
30  * @author Scott Violet
31  */

32 class SynthComboBoxUI extends BasicComboBoxUI implements
33                               PropertyChangeListener JavaDoc, SynthUI {
34     private SynthStyle JavaDoc style;
35     private boolean useListColors;
36
37     public static ComponentUI createUI(JComponent c) {
38         return new SynthComboBoxUI JavaDoc();
39     }
40
41     protected void installDefaults() {
42         updateStyle(comboBox);
43     }
44
45     private void updateStyle(JComboBox comboBox) {
46         SynthStyle JavaDoc oldStyle = style;
47         SynthContext JavaDoc context = getContext(comboBox, ENABLED);
48
49         style = SynthLookAndFeel.updateStyle(context, this);
50         if (style != oldStyle) {
51             useListColors = style.getBoolean(context,
52                                   "ComboBox.rendererUseListColors", true);
53             if (oldStyle != null) {
54                 uninstallKeyboardActions();
55                 installKeyboardActions();
56             }
57         }
58         context.dispose();
59     }
60
61     protected void installListeners() {
62         comboBox.addPropertyChangeListener(this);
63         super.installListeners();
64     }
65
66     protected void uninstallDefaults() {
67         SynthContext JavaDoc context = getContext(comboBox, ENABLED);
68
69         style.uninstallDefaults(context);
70         context.dispose();
71         style = null;
72     }
73
74     protected void uninstallListeners() {
75         comboBox.removePropertyChangeListener(this);
76         super.uninstallListeners();
77     }
78
79     public SynthContext JavaDoc getContext(JComponent c) {
80         return getContext(c, getComponentState(c));
81     }
82
83     private SynthContext JavaDoc getContext(JComponent c, int state) {
84         return SynthContext.getContext(SynthContext JavaDoc.class, c,
85                     SynthLookAndFeel.getRegion(c), style, state);
86     }
87
88     private Region JavaDoc getRegion(JComponent c) {
89         return SynthLookAndFeel.getRegion(c);
90     }
91
92     private int getComponentState(JComponent c) {
93         return SynthLookAndFeel.getComponentState(c);
94     }
95
96     protected ComboPopup createPopup() {
97         SynthComboPopup JavaDoc popup = new SynthComboPopup JavaDoc( comboBox );
98         return popup;
99     }
100
101     protected ListCellRenderer createRenderer() {
102         return new SynthComboBoxRenderer();
103     }
104
105     protected ComboBoxEditor createEditor() {
106         return new SynthComboBoxEditor();
107     }
108
109     //
110
// end UI Initialization
111
//======================
112

113
114     public void propertyChange(PropertyChangeEvent JavaDoc e) {
115         if (SynthLookAndFeel.shouldUpdateStyle(e)) {
116             updateStyle(comboBox);
117         }
118     }
119
120     protected JButton createArrowButton() {
121         SynthArrowButton JavaDoc button = new SynthArrowButton JavaDoc(SwingConstants.SOUTH);
122         button.setName("ComboBox.arrowButton");
123         return button;
124     }
125
126     //=================================
127
// begin ComponentUI Implementation
128

129     public void update(Graphics g, JComponent c) {
130         SynthContext JavaDoc context = getContext(c);
131
132         SynthLookAndFeel.update(context, g);
133         context.getPainter().paintComboBoxBackground(context, g, 0, 0,
134                                                   c.getWidth(), c.getHeight());
135         paint(context, g);
136         context.dispose();
137     }
138
139     public void paint(Graphics g, JComponent c) {
140         SynthContext JavaDoc context = getContext(c);
141
142         paint(context, g);
143         context.dispose();
144     }
145
146     protected void paint(SynthContext JavaDoc context, Graphics g) {
147         hasFocus = comboBox.hasFocus();
148         if ( !comboBox.isEditable() ) {
149             Rectangle r = rectangleForCurrentValue();
150             paintCurrentValue(g,r,hasFocus);
151         }
152     }
153
154     public void paintBorder(SynthContext JavaDoc context, Graphics g, int x,
155                             int y, int w, int h) {
156         context.getPainter().paintComboBoxBorder(context, g, x, y, w, h);
157     }
158
159
160     /**
161      * Paints the currently selected item.
162      */

163     public void paintCurrentValue(Graphics g,Rectangle bounds,boolean hasFocus) {
164         ListCellRenderer renderer = comboBox.getRenderer();
165         Component c;
166
167         if ( hasFocus && !isPopupVisible(comboBox) ) {
168             c = renderer.getListCellRendererComponent( listBox,
169                                                        comboBox.getSelectedItem(),
170                                                        -1,
171                                                        false,
172                                                        false );
173         }
174         else {
175             c = renderer.getListCellRendererComponent( listBox,
176                                                        comboBox.getSelectedItem(),
177                                                        -1,
178                                                        false,
179                                                        false );
180         }
181         // Fix for 4238829: should lay out the JPanel.
182
boolean shouldValidate = false;
183         if (c instanceof JPanel) {
184             shouldValidate = true;
185         }
186
187         if (c instanceof UIResource) {
188             c.setName("ComboBox.renderer");
189             currentValuePane.paintComponent(g,c,comboBox,bounds.x,bounds.y,
190                                         bounds.width,bounds.height, shouldValidate);
191         }
192         else {
193             currentValuePane.paintComponent(g,c,comboBox,bounds.x,bounds.y,
194                                         bounds.width,bounds.height, shouldValidate);
195         }
196     }
197
198     /**
199      * From BasicComboBoxRenderer v 1.18.
200      */

201     private class SynthComboBoxRenderer extends JLabel implements ListCellRenderer, UIResource {
202         public SynthComboBoxRenderer() {
203             super();
204             setText(" ");
205         }
206
207         public String JavaDoc getName() {
208             // As SynthComboBoxRenderer's are asked for a size BEFORE they
209
// are parented getName is overriden to force the name to be
210
// ComboBox.renderer if it isn't set. If we didn't do this the
211
// wrong style could be used for size calculations.
212
String JavaDoc name = super.getName();
213             if (name == null) {
214                 return "ComboBox.renderer";
215             }
216             return name;
217         }
218
219         public Component getListCellRendererComponent(JList list, Object JavaDoc value,
220                          int index, boolean isSelected, boolean cellHasFocus) {
221             setName("ComboBox.listRenderer");
222             SynthLookAndFeel.resetSelectedUI();
223             if (isSelected) {
224                 setBackground(list.getSelectionBackground());
225                 setForeground(list.getSelectionForeground());
226                 if (!useListColors) {
227                     SynthLookAndFeel.setSelectedUI(
228                          (SynthLabelUI JavaDoc)SynthLookAndFeel.getUIOfType(getUI(),
229                          SynthLabelUI JavaDoc.class), isSelected, cellHasFocus,
230                          list.isEnabled());
231                 }
232             }
233             else {
234                 setBackground(list.getBackground());
235                 setForeground(list.getForeground());
236             }
237
238             setFont(list.getFont());
239
240             if (value instanceof Icon) {
241                 setIcon((Icon)value);
242                 setText("");
243             }
244             else {
245                 String JavaDoc text = (value == null) ? " " : value.toString();
246
247                 if ("".equals(text)) {
248                     text = " ";
249                 }
250                 setText(text);
251             }
252             return this;
253         }
254
255         public void paint(Graphics g) {
256             super.paint(g);
257             SynthLookAndFeel.resetSelectedUI();
258         }
259     }
260
261
262     /**
263      * From BasicCombBoxEditor v 1.24.
264      */

265     private static class SynthComboBoxEditor implements
266                               ComboBoxEditor, UIResource {
267         protected JTextField editor;
268         private Object JavaDoc oldValue;
269
270         public SynthComboBoxEditor() {
271             editor = new JTextField("",9);
272             editor.setName("ComboBox.textField");
273         }
274
275         public Component getEditorComponent() {
276             return editor;
277         }
278
279         /**
280          * Sets the item that should be edited.
281          *
282          * @param anObject the displayed value of the editor
283          */

284         public void setItem(Object JavaDoc anObject) {
285             String JavaDoc text;
286
287             if ( anObject != null ) {
288                 text = anObject.toString();
289                 oldValue = anObject;
290             } else {
291                 text = "";
292             }
293             // workaround for 4530952
294
if (!text.equals(editor.getText())) {
295                 editor.setText(text);
296             }
297         }
298
299         public Object JavaDoc getItem() {
300             Object JavaDoc newValue = editor.getText();
301         
302             if (oldValue != null && !(oldValue instanceof String JavaDoc)) {
303                 // The original value is not a string. Should return the value in it's
304
// original type.
305
if (newValue.equals(oldValue.toString())) {
306                     return oldValue;
307                 } else {
308                     // Must take the value from the editor and get the value and cast it to the new type.
309
Class JavaDoc cls = oldValue.getClass();
310                     try {
311                         Method method = cls.getMethod("valueOf", new Class JavaDoc[]{String JavaDoc.class});
312                         newValue = method.invoke(oldValue, new Object JavaDoc[] { editor.getText()});
313                     } catch (Exception JavaDoc ex) {
314                         // Fail silently and return the newValue (a String object)
315
}
316                 }
317             }
318             return newValue;
319         }
320
321         public void selectAll() {
322             editor.selectAll();
323             editor.requestFocus();
324         }
325
326         public void addActionListener(ActionListener l) {
327             editor.addActionListener(l);
328         }
329
330         public void removeActionListener(ActionListener l) {
331             editor.removeActionListener(l);
332         }
333     }
334 }
335
Popular Tags