Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UMLSearchableComboBox |
|
| 3.75;3.75 |
1 | /* $Id: UMLSearchableComboBox.java 17881 2010-01-12 21:09:28Z linus $ | |
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 javax.swing.Action; | |
42 | import javax.swing.ComboBoxModel; | |
43 | ||
44 | import org.argouml.model.Model; | |
45 | ||
46 | /** | |
47 | * A searchable combobox. Searchable means that the user only has to type the | |
48 | * starting of a modelelement name to select that modelelement. The first | |
49 | * modelelement that conforms to the typed text is selected. | |
50 | * @author jaap.branderhorst@xs4all.nl | |
51 | * @since Jan 5, 2003 | |
52 | */ | |
53 | public class UMLSearchableComboBox extends UMLEditableComboBox { | |
54 | ||
55 | /** | |
56 | * Constructor for UMLSearchableComboBox. | |
57 | * @param model the model | |
58 | * @param selectAction the action for selection | |
59 | * @param showIcon true if we show an icon in the list | |
60 | */ | |
61 | public UMLSearchableComboBox(UMLComboBoxModel2 model, | |
62 | Action selectAction, boolean showIcon) { | |
63 | 2289 | super(model, selectAction, showIcon); |
64 | 2289 | } |
65 | ||
66 | /** | |
67 | * Constructor for UMLSearchableComboBox. | |
68 | * @param arg0 the model | |
69 | * @param selectAction the action for selection | |
70 | */ | |
71 | public UMLSearchableComboBox(UMLComboBoxModel2 arg0, | |
72 | Action selectAction) { | |
73 | 0 | this(arg0, selectAction, true); |
74 | 0 | } |
75 | ||
76 | /** | |
77 | * Searches amongst the modelelements (the elements in the listmodel) for an | |
78 | * item that conforms to the parameter item. If such an element is a | |
79 | * ModelElement, the name should start with the item (which is a String). | |
80 | * Otherwise the text that is shown in the combobox should start with the | |
81 | * text. As the element is found, this is made to the selected item. | |
82 | * | |
83 | * {@inheritDoc} | |
84 | */ | |
85 | protected void doOnEdit(Object item) { | |
86 | 0 | Object element = search(item); |
87 | 0 | if (element != null) { |
88 | 0 | setSelectedItem(element); |
89 | } | |
90 | 0 | } |
91 | ||
92 | /** | |
93 | * Does the actual searching. Returns the item found or null if there is no | |
94 | * item found. | |
95 | * @param item the string entered by the user | |
96 | * @return Object the found object from the list, or null if none found | |
97 | */ | |
98 | protected Object search(Object item) { | |
99 | 0 | String text = (String) item; |
100 | 0 | ComboBoxModel model = getModel(); |
101 | 0 | for (int i = 0; i < model.getSize(); i++) { |
102 | 0 | Object element = model.getElementAt(i); |
103 | 0 | if (Model.getFacade().isAModelElement(element)) { |
104 | 0 | if (getRenderer() instanceof UMLListCellRenderer2) { |
105 | 0 | String labelText = ((UMLListCellRenderer2) getRenderer()) |
106 | .makeText(element); | |
107 | 0 | if (labelText != null && labelText.startsWith(text)) { |
108 | 0 | return element; |
109 | } | |
110 | } | |
111 | 0 | if (Model.getFacade().isAModelElement(element)) { |
112 | 0 | Object/*MModelElement*/ elem = element; |
113 | 0 | String name = Model.getFacade().getName(elem); |
114 | 0 | if (name != null && name.startsWith(text)) { |
115 | 0 | return element; |
116 | } | |
117 | } | |
118 | } | |
119 | ||
120 | } | |
121 | 0 | return null; |
122 | } | |
123 | ||
124 | } |