Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UMLEditableComboBox |
|
| 1.3181818181818181;1.318 | ||||
UMLEditableComboBox$UMLComboBoxEditor |
|
| 1.3181818181818181;1.318 | ||||
UMLEditableComboBox$UMLComboBoxEditor$UMLImagePanel |
|
| 1.3181818181818181;1.318 |
1 | /* $Id: UMLEditableComboBox.java 18735 2010-09-13 11:37:06Z bobtarling $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * tfmorris | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2006 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.uml.ui; | |
40 | ||
41 | import java.awt.BorderLayout; | |
42 | import java.awt.Component; | |
43 | import java.awt.event.ActionEvent; | |
44 | import java.awt.event.ActionListener; | |
45 | import java.awt.event.FocusEvent; | |
46 | import java.awt.event.FocusListener; | |
47 | ||
48 | import javax.swing.Action; | |
49 | import javax.swing.BorderFactory; | |
50 | import javax.swing.ComboBoxEditor; | |
51 | import javax.swing.Icon; | |
52 | import javax.swing.JLabel; | |
53 | import javax.swing.JPanel; | |
54 | import javax.swing.JTextField; | |
55 | import javax.swing.border.BevelBorder; | |
56 | import javax.swing.plaf.basic.BasicComboBoxEditor; | |
57 | ||
58 | import org.argouml.application.helpers.ResourceLoaderWrapper; | |
59 | ||
60 | /** | |
61 | * An editable combobox. Upon pressing enter the text entered by the user is | |
62 | * sent as an actioncommand to the actionlistener (this). The item that's being | |
63 | * edited is sent to the method doIt after that. The developer should implement | |
64 | * this method | |
65 | * @author jaap.branderhorst@xs4all.nl | |
66 | * @since Jan 4, 2003 | |
67 | * @deprecated in 0.31.5 by Bob Tarling. Property panel controls are now | |
68 | * internal to the property panel component | |
69 | */ | |
70 | public abstract class UMLEditableComboBox extends UMLComboBox2 implements | |
71 | FocusListener { | |
72 | ||
73 | /** | |
74 | * The comboboxeditor for editable uml comboboxes. This has to be changed | |
75 | * since it controls the rendering of the textfield where the user can edit | |
76 | * the list elements. Setitem has to give the correct value. Furthermore, | |
77 | * the standard comboboxeditor (BasicComboBoxEditor) does not support | |
78 | * showing icons. | |
79 | * | |
80 | * @author jaap.branderhorst@xs4all.nl | |
81 | * @since Jan 5, 2003 | |
82 | */ | |
83 | protected class UMLComboBoxEditor extends BasicComboBoxEditor { | |
84 | ||
85 | /** | |
86 | * A panel which helps us to show the editable textfield for this | |
87 | * combobox (including the Icon). | |
88 | * | |
89 | * @author jaap.branderhorst@xs4all.nl | |
90 | * @since Jan 5, 2003 | |
91 | */ | |
92 | private class UMLImagePanel extends JPanel { | |
93 | ||
94 | /** | |
95 | * The label that shows the icon. | |
96 | */ | |
97 | 2289 | private JLabel imageIconLabel = new JLabel(); |
98 | /** | |
99 | * The textfield the user can edit. | |
100 | */ | |
101 | private JTextField theTextField; | |
102 | ||
103 | /** | |
104 | * Constructs a UMLImagePanel | |
105 | * @param textField The textfield the user can edit | |
106 | * @param showIcon boolean which must be true if an icon is to be | |
107 | * shown. | |
108 | */ | |
109 | 2289 | public UMLImagePanel(JTextField textField, boolean showIcon) { |
110 | 2289 | setLayout(new BorderLayout()); |
111 | 2289 | theTextField = textField; |
112 | 2289 | setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); |
113 | 2289 | if (showIcon) { |
114 | // we don't want to show some nasty gray background | |
115 | // color, now do we? | |
116 | 2289 | imageIconLabel.setOpaque(true); |
117 | 2289 | imageIconLabel.setBackground(theTextField.getBackground()); |
118 | 2289 | add(imageIconLabel, BorderLayout.WEST); |
119 | } | |
120 | 2289 | add(theTextField, BorderLayout.CENTER); |
121 | 2289 | theTextField.addFocusListener(UMLEditableComboBox.this); |
122 | 2289 | } |
123 | ||
124 | public void setText(String text) { | |
125 | 0 | theTextField.setText(text); |
126 | 0 | } |
127 | ||
128 | public String getText() { | |
129 | 0 | return theTextField.getText(); |
130 | } | |
131 | ||
132 | /** | |
133 | * Sets the icon. Calls repaint to redraw the panel | |
134 | * @param i The icon to be shown. | |
135 | */ | |
136 | public void setIcon(Icon i) { | |
137 | 2949 | if (i != null) { |
138 | 2949 | imageIconLabel.setIcon(i); |
139 | // necessary to create distance between | |
140 | // the textfield and the icon. | |
141 | 2949 | imageIconLabel.setBorder(BorderFactory |
142 | .createEmptyBorder(0, 2, 0, 2)); | |
143 | ||
144 | } else { | |
145 | 0 | imageIconLabel.setIcon(null); |
146 | 0 | imageIconLabel.setBorder(null); |
147 | } | |
148 | 2949 | imageIconLabel.invalidate(); |
149 | 2949 | validate(); |
150 | 2949 | repaint(); |
151 | 2949 | } |
152 | ||
153 | public void selectAll() { | |
154 | 0 | theTextField.selectAll(); |
155 | 0 | } |
156 | ||
157 | public void addActionListener(ActionListener l) { | |
158 | 6867 | theTextField.addActionListener(l); |
159 | 6867 | } |
160 | ||
161 | public void removeActionListener(ActionListener l) { | |
162 | 2289 | theTextField.removeActionListener(l); |
163 | 2289 | } |
164 | ||
165 | } | |
166 | ||
167 | private UMLImagePanel panel; | |
168 | ||
169 | /** | |
170 | * True if an icon should be shown. | |
171 | */ | |
172 | private boolean theShowIcon; | |
173 | ||
174 | ||
175 | /** | |
176 | * Constructor for UMLComboBoxEditor. | |
177 | * | |
178 | * @param showIcon true if an icon is to be shown | |
179 | */ | |
180 | 2289 | public UMLComboBoxEditor(boolean showIcon) { |
181 | 2289 | super(); |
182 | 2289 | panel = new UMLImagePanel(editor, showIcon); |
183 | 2289 | setShowIcon(showIcon); |
184 | 2289 | } |
185 | ||
186 | /* | |
187 | * @see javax.swing.ComboBoxEditor#setItem(java.lang.Object) | |
188 | */ | |
189 | public void setItem(Object anObject) { | |
190 | 5238 | if (((UMLComboBoxModel2) getModel()).contains(anObject)) { |
191 | 2949 | editor.setText(((UMLListCellRenderer2) getRenderer()) |
192 | .makeText(anObject)); | |
193 | 2949 | if (theShowIcon && (anObject != null)) |
194 | 2949 | panel.setIcon(ResourceLoaderWrapper.getInstance() |
195 | .lookupIcon(anObject)); | |
196 | } else | |
197 | 2289 | super.setItem(anObject); |
198 | ||
199 | 5238 | } |
200 | ||
201 | /** | |
202 | * Returns the showIcon. | |
203 | * @return boolean | |
204 | */ | |
205 | public boolean isShowIcon() { | |
206 | 0 | return theShowIcon; |
207 | } | |
208 | ||
209 | /** | |
210 | * Sets the showIcon. | |
211 | * @param showIcon The showIcon to set | |
212 | */ | |
213 | public void setShowIcon(boolean showIcon) { | |
214 | 2289 | theShowIcon = showIcon; |
215 | 2289 | } |
216 | ||
217 | /* | |
218 | * @see javax.swing.ComboBoxEditor#getEditorComponent() | |
219 | */ | |
220 | public Component getEditorComponent() { | |
221 | 3015 | return panel; |
222 | } | |
223 | ||
224 | /* | |
225 | * @see javax.swing.ComboBoxEditor#addActionListener(java.awt.event.ActionListener) | |
226 | */ | |
227 | public void addActionListener(ActionListener l) { | |
228 | 6867 | panel.addActionListener(l); |
229 | 6867 | } |
230 | ||
231 | ||
232 | ||
233 | /* | |
234 | * @see javax.swing.ComboBoxEditor#removeActionListener(java.awt.event.ActionListener) | |
235 | */ | |
236 | public void removeActionListener(ActionListener l) { | |
237 | 2289 | panel.removeActionListener(l); |
238 | 2289 | } |
239 | ||
240 | /* | |
241 | * @see javax.swing.ComboBoxEditor#selectAll() | |
242 | */ | |
243 | public void selectAll() { | |
244 | 0 | super.selectAll(); |
245 | 0 | } |
246 | ||
247 | /* | |
248 | * @see javax.swing.ComboBoxEditor#getItem() | |
249 | */ | |
250 | public Object getItem() { | |
251 | 0 | return panel.getText(); |
252 | } | |
253 | ||
254 | } | |
255 | ||
256 | /* | |
257 | * @see org.argouml.uml.ui.UMLComboBox2#UMLComboBox2( UMLComboBoxModel2, | |
258 | * Action, boolean) | |
259 | */ | |
260 | public UMLEditableComboBox(UMLComboBoxModel2 model, Action selectAction, | |
261 | boolean showIcon) { | |
262 | 2289 | super(model, selectAction, showIcon); |
263 | 2289 | setEditable(true); |
264 | 2289 | setEditor(new UMLComboBoxEditor(showIcon)); |
265 | 2289 | getEditor().addActionListener(this); |
266 | 2289 | } |
267 | ||
268 | /* | |
269 | * @see org.argouml.uml.ui.UMLComboBox2#UMLComboBox2( UMLComboBoxModel2, | |
270 | * Action) | |
271 | */ | |
272 | public UMLEditableComboBox(UMLComboBoxModel2 arg0, Action selectAction) { | |
273 | 0 | this(arg0, selectAction, true); |
274 | 0 | } |
275 | ||
276 | /* | |
277 | * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) | |
278 | * TODO: From ComboBox javadoc - "This method is public as an | |
279 | * implementation side effect. do not call or override." | |
280 | * We should find some other way to implement this. | |
281 | */ | |
282 | public void actionPerformed(ActionEvent e) { | |
283 | 0 | super.actionPerformed(e); |
284 | 0 | if (e.getSource() instanceof JTextField) { |
285 | 0 | Object oldValue = getSelectedItem(); |
286 | 0 | ComboBoxEditor editor = getEditor(); |
287 | 0 | Object item = editor.getItem(); |
288 | 0 | doOnEdit(item); |
289 | // next statement is necessary to update the textfield | |
290 | // if the selection is equal to what was allready | |
291 | // selected | |
292 | 0 | if (oldValue == getSelectedItem()) |
293 | 0 | getEditor().setItem(getSelectedItem()); |
294 | } | |
295 | 0 | } |
296 | ||
297 | /** | |
298 | * This method is called after the user has edited the editable textfield | |
299 | * and has press enter. ActionPerformed determines that the action is about | |
300 | * editing the textfield and calls this method afterwards. | |
301 | * @param item The item in the comboboxeditor. In this case it's the text of | |
302 | * the editable textfield. | |
303 | */ | |
304 | protected abstract void doOnEdit(Object item); | |
305 | ||
306 | /* | |
307 | * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent) | |
308 | */ | |
309 | public final void focusGained(FocusEvent arg0) { | |
310 | // ignored | |
311 | 0 | } |
312 | ||
313 | /* | |
314 | * TODO: This is a temporary method of making sure the model is updated | |
315 | * on loss of focus of a combo box. In the long term we should attempt to | |
316 | * update the model on each keypress. | |
317 | * | |
318 | * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent) | |
319 | */ | |
320 | public final void focusLost(FocusEvent arg0) { | |
321 | 0 | doOnEdit(getEditor().getItem()); |
322 | 0 | } |
323 | } |