Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UMLModelElementNamespaceComboBoxModel |
|
| 3.142857142857143;3.143 |
1 | /* $Id: UMLModelElementNamespaceComboBoxModel.java 17996 2010-02-13 19:42:04Z tfmorris $ | |
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 | * mvw | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2008 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.foundation.core; | |
40 | ||
41 | import java.util.ArrayList; | |
42 | import java.util.Collection; | |
43 | import java.util.Set; | |
44 | import java.util.TreeSet; | |
45 | ||
46 | import org.apache.log4j.Logger; | |
47 | import org.argouml.kernel.ProjectManager; | |
48 | import org.argouml.model.Model; | |
49 | import org.argouml.model.UmlChangeEvent; | |
50 | import org.argouml.uml.diagram.ui.UMLDiagram; | |
51 | import org.argouml.uml.ui.UMLComboBoxModel2; | |
52 | import org.argouml.uml.util.PathComparator; | |
53 | ||
54 | /** | |
55 | * A model for a namespace combo box. | |
56 | * | |
57 | * @since Oct 10, 2002 | |
58 | * @author jaap.branderhorst@xs4all.nl, alexb | |
59 | */ | |
60 | public class UMLModelElementNamespaceComboBoxModel extends UMLComboBoxModel2 { | |
61 | ||
62 | /** | |
63 | * Logger. | |
64 | */ | |
65 | 0 | private static final Logger LOG = |
66 | Logger.getLogger(UMLModelElementNamespaceComboBoxModel.class); | |
67 | ||
68 | /** | |
69 | * The UID. | |
70 | */ | |
71 | private static final long serialVersionUID = -775116993155949065L; | |
72 | ||
73 | /** | |
74 | * Constructor for UMLModelElementNamespaceComboBoxModel. | |
75 | */ | |
76 | public UMLModelElementNamespaceComboBoxModel() { | |
77 | 0 | super("namespace", true); |
78 | 0 | Model.getPump().addClassModelEventListener(this, |
79 | Model.getMetaTypes().getNamespace(), "ownedElement"); | |
80 | 0 | } |
81 | ||
82 | /* | |
83 | * @see org.argouml.uml.ui.UMLComboBoxModel2#isValidElement(Object) | |
84 | */ | |
85 | protected boolean isValidElement(Object o) { | |
86 | 0 | return Model.getFacade().isANamespace(o) |
87 | && Model.getCoreHelper().isValidNamespace(getTarget(), o); | |
88 | } | |
89 | ||
90 | /* | |
91 | * @see org.argouml.uml.ui.UMLComboBoxModel2#buildModelList() | |
92 | */ | |
93 | @Override | |
94 | protected void buildMinimalModelList() { | |
95 | 0 | Object target = getTarget(); |
96 | 0 | Collection c = new ArrayList(1); |
97 | ||
98 | 0 | if (target != null) { |
99 | 0 | Object namespace = null; |
100 | 0 | if (target instanceof UMLDiagram) { |
101 | 0 | namespace = ((UMLDiagram) target).getNamespace(); |
102 | 0 | } else if (Model.getFacade().isAElement(target)) { |
103 | 0 | namespace = Model.getFacade().getNamespace(target); |
104 | } | |
105 | 0 | if (namespace != null && !c.contains(namespace)) { |
106 | 0 | c.add(namespace); |
107 | } | |
108 | } | |
109 | 0 | setElements(c); |
110 | 0 | setModelInvalid(); |
111 | 0 | } |
112 | ||
113 | /* | |
114 | * @see org.argouml.uml.ui.UMLComboBoxModel2#buildModelList() | |
115 | */ | |
116 | protected void buildModelList() { | |
117 | 0 | Set<Object> elements = new TreeSet<Object>(new PathComparator()); |
118 | ||
119 | 0 | Object model = |
120 | ProjectManager.getManager().getCurrentProject().getRoot(); | |
121 | 0 | Object target = getTarget(); |
122 | 0 | elements.addAll( |
123 | Model.getCoreHelper().getAllPossibleNamespaces(target, model)); | |
124 | ||
125 | /* These next lines for the case that the current namespace | |
126 | * is not a valid one... Which of course should not happen, | |
127 | * but it does - see the project attached to issue 3772. | |
128 | */ | |
129 | /* TODO: Enhance the isValidNamespace function so | |
130 | * that this never happens. | |
131 | */ | |
132 | 0 | if (target != null) { |
133 | 0 | Object namespace = Model.getFacade().getNamespace(target); |
134 | 0 | if (namespace != null && !elements.contains(namespace)) { |
135 | 0 | elements.add(namespace); |
136 | 0 | LOG.warn("The current namespace is not a valid one!"); |
137 | } | |
138 | } | |
139 | ||
140 | // Our comparator will throw an InvalidElementException if the old | |
141 | // list contains deleted elements (eg after a new project is loaded) | |
142 | // so remove all the old contents first | |
143 | 0 | removeAllElements(); |
144 | 0 | addAll(elements); |
145 | 0 | } |
146 | ||
147 | /* | |
148 | * @see org.argouml.uml.ui.UMLComboBoxModel2#getSelectedModelElement() | |
149 | */ | |
150 | protected Object getSelectedModelElement() { | |
151 | 0 | Object target = getTarget(); |
152 | 0 | if (getTarget() != null && Model.getFacade().isANamedElement(target)) { |
153 | 0 | return Model.getFacade().getNamespace(getTarget()); |
154 | } | |
155 | 0 | return null; |
156 | } | |
157 | ||
158 | /* | |
159 | * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent) | |
160 | */ | |
161 | @Override | |
162 | public void modelChanged(UmlChangeEvent evt) { | |
163 | /* | |
164 | * Rebuild the list from scratch to be sure it's correct. | |
165 | */ | |
166 | 0 | Object t = getTarget(); |
167 | 0 | if (t != null |
168 | && evt.getSource() == t | |
169 | && evt.getNewValue() != null) { | |
170 | 0 | buildMinimalModelList(); |
171 | /* In some cases (see issue 3780) the list remains the same, but | |
172 | * the selected item differs. Without the next step, | |
173 | * the combo would not be refreshed. | |
174 | */ | |
175 | 0 | setSelectedItem(getSelectedModelElement()); |
176 | } | |
177 | 0 | } |
178 | ||
179 | @Override | |
180 | protected boolean isLazy() { | |
181 | 0 | return true; |
182 | } | |
183 | } |