38 |
|
|
39 |
package javax.swing.plaf.metal; |
package javax.swing.plaf.metal; |
40 |
|
|
41 |
import java.util.HashMap; |
import java.awt.Container; |
42 |
|
import java.awt.Dimension; |
43 |
|
import java.awt.Graphics; |
44 |
|
import java.awt.LayoutManager; |
45 |
|
import java.awt.Rectangle; |
46 |
|
import java.awt.event.MouseEvent; |
47 |
|
import java.beans.PropertyChangeEvent; |
48 |
|
import java.beans.PropertyChangeListener; |
49 |
|
|
50 |
|
import javax.swing.CellRendererPane; |
51 |
|
import javax.swing.ComboBoxEditor; |
52 |
|
import javax.swing.JButton; |
53 |
|
import javax.swing.JComboBox; |
54 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
55 |
import javax.swing.plaf.ComponentUI; |
import javax.swing.plaf.ComponentUI; |
56 |
import javax.swing.plaf.basic.BasicComboBoxUI; |
import javax.swing.plaf.basic.BasicComboBoxUI; |
57 |
|
import javax.swing.plaf.basic.BasicComboPopup; |
58 |
|
import javax.swing.plaf.basic.ComboPopup; |
59 |
|
|
60 |
|
|
61 |
|
/** |
62 |
|
* A UI delegate for the {@link JComboBox} component. |
63 |
|
*/ |
64 |
public class MetalComboBoxUI |
public class MetalComboBoxUI |
65 |
extends BasicComboBoxUI |
extends BasicComboBoxUI |
66 |
{ |
{ |
67 |
|
/** |
68 |
|
* A layout manager that arranges the editor component (if active) and the |
69 |
|
* button that make up the combo box. |
70 |
|
*/ |
71 |
|
public class MetalComboBoxLayoutManager |
72 |
|
extends BasicComboBoxUI.ComboBoxLayoutManager |
73 |
|
{ |
74 |
|
/** |
75 |
|
* Creates a new instance of the layout manager. |
76 |
|
*/ |
77 |
|
public MetalComboBoxLayoutManager() |
78 |
|
{ |
79 |
|
} |
80 |
|
|
81 |
|
/** |
82 |
|
* Arranges the editor (if visible) and button that comprise the combo |
83 |
|
* box. |
84 |
|
* |
85 |
|
* @param parent the parent. |
86 |
|
*/ |
87 |
|
public void layoutContainer(Container parent) |
88 |
|
{ |
89 |
|
JComboBox cb = (JComboBox) parent; |
90 |
|
if (!cb.isEditable()) |
91 |
|
{ |
92 |
|
Rectangle bounds = parent.getBounds(); |
93 |
|
arrowButton.setBounds(0, 0, bounds.width, bounds.height); |
94 |
|
} |
95 |
|
else |
96 |
|
superLayout(parent); |
97 |
|
} |
98 |
|
|
99 |
|
/** |
100 |
|
* Calls the <code>layoutContainer(Container)</code> method in the super |
101 |
|
* class. |
102 |
|
* |
103 |
|
* @param parent the container. |
104 |
|
*/ |
105 |
|
public void superLayout(Container parent) |
106 |
|
{ |
107 |
|
super.layoutContainer(parent); |
108 |
|
} |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
|
* A listener used to handle property changes in the {@link JComboBox} |
113 |
|
* component, to ensure that the UI delegate accurately reflects the current |
114 |
|
* state in the rendering onscreen. |
115 |
|
*/ |
116 |
|
public class MetalPropertyChangeListener |
117 |
|
extends BasicComboBoxUI.PropertyChangeHandler |
118 |
|
{ |
119 |
|
/** |
120 |
|
* Creates a new listener. |
121 |
|
*/ |
122 |
|
public MetalPropertyChangeListener() |
123 |
|
{ |
124 |
|
} |
125 |
|
|
126 |
|
/** |
127 |
|
* Handles a property change event, updating the UI components as |
128 |
|
* appropriate. |
129 |
|
* |
130 |
|
* @param e the event. |
131 |
|
*/ |
132 |
|
public void propertyChange(PropertyChangeEvent e) |
133 |
|
{ |
134 |
|
if (e.getPropertyName().equals("editable")) |
135 |
|
editablePropertyChanged(e); |
136 |
|
super.propertyChange(e); |
137 |
|
} |
138 |
|
} |
139 |
|
|
140 |
/** The UI instances for JComboBoxes. */ |
/** |
141 |
private static HashMap instances = null; |
* A popup menu for the combo-box. |
142 |
|
* |
143 |
|
* @see #createPopup() |
144 |
|
* |
145 |
|
* @deprecated 1.4 |
146 |
|
*/ |
147 |
|
public class MetalComboPopup extends BasicComboPopup |
148 |
|
{ |
149 |
|
/** |
150 |
|
* Creates a new popup. |
151 |
|
* |
152 |
|
* @param cBox the combo box. |
153 |
|
*/ |
154 |
|
public MetalComboPopup(JComboBox cBox) |
155 |
|
{ |
156 |
|
super(cBox); |
157 |
|
} |
158 |
|
|
159 |
|
public void delegateFocus(MouseEvent e) |
160 |
|
{ |
161 |
|
super.delegateFocus(e); |
162 |
|
} |
163 |
|
} |
164 |
|
|
165 |
/** |
/** |
166 |
* Constructs a new instance of MetalComboBoxUI. |
* Constructs a new instance of MetalComboBoxUI. |
167 |
*/ |
*/ |
179 |
*/ |
*/ |
180 |
public static ComponentUI createUI(JComponent component) |
public static ComponentUI createUI(JComponent component) |
181 |
{ |
{ |
182 |
if (instances == null) |
return new MetalComboBoxUI(); |
183 |
instances = new HashMap(); |
} |
184 |
|
|
185 |
Object o = instances.get(component); |
/** |
186 |
MetalComboBoxUI instance; |
* Creates an editor for the combo box. |
187 |
if (o == null) |
* |
188 |
|
* @return An editor. |
189 |
|
*/ |
190 |
|
protected ComboBoxEditor createEditor() |
191 |
|
{ |
192 |
|
return new MetalComboBoxEditor.UIResource(); |
193 |
|
} |
194 |
|
|
195 |
|
/** |
196 |
|
* Creates a popup for the combo box. |
197 |
|
* |
198 |
|
* @return A popup. |
199 |
|
*/ |
200 |
|
protected ComboPopup createPopup() |
201 |
|
{ |
202 |
|
return new MetalComboPopup(comboBox); |
203 |
|
} |
204 |
|
|
205 |
|
/** |
206 |
|
* Creates a new button for use in rendering the JComboBox. |
207 |
|
* |
208 |
|
* @return A button. |
209 |
|
*/ |
210 |
|
protected JButton createArrowButton() |
211 |
|
{ |
212 |
|
return new MetalComboBoxButton(comboBox, new MetalComboBoxIcon(), |
213 |
|
new CellRendererPane(), listBox); |
214 |
|
} |
215 |
|
|
216 |
|
/** |
217 |
|
* Creates a new property change listener. |
218 |
|
* |
219 |
|
* @return A new property change listener. |
220 |
|
*/ |
221 |
|
public PropertyChangeListener createPropertyChangeListener() |
222 |
|
{ |
223 |
|
return new MetalPropertyChangeListener(); |
224 |
|
} |
225 |
|
|
226 |
|
public void paint(Graphics g, JComponent c) |
227 |
|
{ |
228 |
|
// do nothing, the button and text field are painted elsewhere |
229 |
|
} |
230 |
|
|
231 |
|
/** |
232 |
|
* Updates the button and text field to reflect a change in the 'editable' |
233 |
|
* property. |
234 |
|
* |
235 |
|
* @param e the event. |
236 |
|
* |
237 |
|
* @deprecated 1.4 |
238 |
|
*/ |
239 |
|
protected void editablePropertyChanged(PropertyChangeEvent e) |
240 |
|
{ |
241 |
|
if (arrowButton instanceof MetalComboBoxButton) |
242 |
{ |
{ |
243 |
instance = new MetalComboBoxUI(); |
MetalComboBoxButton b = (MetalComboBoxButton) arrowButton; |
244 |
instances.put(component, instance); |
b.setIconOnly(comboBox.isEditable()); |
245 |
|
} |
246 |
|
if (comboBox.isEditable()) |
247 |
|
{ |
248 |
|
arrowButton.setText(null); |
249 |
|
if (editor != null) |
250 |
|
editor.setVisible(true); |
251 |
} |
} |
252 |
else |
else |
253 |
instance = (MetalComboBoxUI) o; |
{ |
254 |
|
arrowButton.setText(comboBox.getSelectedItem().toString()); |
255 |
return instance; |
if (editor != null) |
256 |
|
editor.setVisible(true); |
257 |
|
} |
258 |
|
} |
259 |
|
|
260 |
|
/** |
261 |
|
* Creates a new layout manager for the UI delegate. |
262 |
|
* |
263 |
|
* @return A new layout manager. |
264 |
|
*/ |
265 |
|
protected LayoutManager createLayoutManager() |
266 |
|
{ |
267 |
|
return new MetalComboBoxLayoutManager(); |
268 |
|
} |
269 |
|
|
270 |
|
/** |
271 |
|
* Not used in Classpath. |
272 |
|
* |
273 |
|
* @deprecated 1.4 |
274 |
|
*/ |
275 |
|
protected void removeListeners() |
276 |
|
{ |
277 |
|
// no longer used in JDK 1.4 |
278 |
|
} |
279 |
|
|
280 |
|
/** |
281 |
|
* Returns the minimum size for the combo. |
282 |
|
* |
283 |
|
* @param c the component |
284 |
|
* |
285 |
|
* @return The minimum size for the combo box. |
286 |
|
*/ |
287 |
|
public Dimension getMinimumSize(JComponent c) |
288 |
|
{ |
289 |
|
// FIXME: this needs work |
290 |
|
Dimension result = super.getMinimumSize(c); |
291 |
|
result.height = result.height + 9; |
292 |
|
return result; |
293 |
} |
} |
294 |
|
|
295 |
} |
} |