Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
NotationNameImpl |
|
| 1.6842105263157894;1.684 |
1 | /* $Id: NotationNameImpl.java 17825 2010-01-12 18:49:29Z 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 | * mvw | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2007 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.notation; | |
40 | ||
41 | import java.util.ArrayList; | |
42 | import java.util.Collections; | |
43 | import java.util.List; | |
44 | import java.util.ListIterator; | |
45 | ||
46 | import javax.swing.Icon; | |
47 | ||
48 | import org.apache.log4j.Logger; | |
49 | import org.argouml.application.events.ArgoEventPump; | |
50 | import org.argouml.application.events.ArgoEventTypes; | |
51 | import org.argouml.application.events.ArgoNotationEvent; | |
52 | ||
53 | /** | |
54 | * This class provides definition and manipulation of notation names. | |
55 | * All notation names will be accessed using the | |
56 | * {@link NotationName} wrapper. | |
57 | * | |
58 | * Not mutable! | |
59 | * | |
60 | * @author Thierry Lach | |
61 | * @since 0.9.4 | |
62 | */ | |
63 | class NotationNameImpl | |
64 | implements NotationName { | |
65 | ||
66 | /** | |
67 | * Logger. | |
68 | */ | |
69 | 900 | private static final Logger LOG = Logger.getLogger(NotationNameImpl.class); |
70 | ||
71 | private String name; | |
72 | private String version; | |
73 | private Icon icon; | |
74 | ||
75 | /** The one and only list of notations available | |
76 | * in the running ArgoUML application. */ | |
77 | 900 | private static ArrayList<NotationName> notations = |
78 | new ArrayList<NotationName>(); | |
79 | ||
80 | /** | |
81 | * A notation without a version or icon. | |
82 | * | |
83 | * @param theName the name of the notation | |
84 | */ | |
85 | protected NotationNameImpl(String theName) { | |
86 | 0 | this(theName, null, null); |
87 | 0 | } |
88 | ||
89 | /** | |
90 | * A notation without a version and with an icon. | |
91 | * | |
92 | * @param theName the name of the notation | |
93 | * @param theIcon the icon for of the notation | |
94 | */ | |
95 | protected NotationNameImpl(String theName, Icon theIcon) { | |
96 | 0 | this(theName, null, theIcon); |
97 | 0 | } |
98 | ||
99 | /** | |
100 | * A notation with a version and no icon. | |
101 | * | |
102 | * @param theName the name of the notation | |
103 | * @param theVersion the version of the notation | |
104 | */ | |
105 | protected NotationNameImpl(String theName, String theVersion) { | |
106 | 0 | this(theName, theVersion, null); |
107 | 0 | } |
108 | ||
109 | /** | |
110 | * A notation with a version and an icon. | |
111 | * | |
112 | * @param myName the name of the notation | |
113 | * @param myVersion the version of the notation | |
114 | * @param myIcon the icon of the notation | |
115 | */ | |
116 | 1800 | protected NotationNameImpl(String myName, String myVersion, Icon myIcon) { |
117 | 1800 | name = myName; |
118 | 1800 | version = myVersion; |
119 | 1800 | icon = myIcon; |
120 | 1800 | } |
121 | ||
122 | /** | |
123 | * Accessor for the language name. | |
124 | * | |
125 | * @see org.argouml.notation.NotationName#getName() | |
126 | */ | |
127 | public String getName() { | |
128 | 0 | return name; |
129 | } | |
130 | ||
131 | /** | |
132 | * Accessor for the language version. | |
133 | * | |
134 | * @see org.argouml.notation.NotationName#getVersion() | |
135 | */ | |
136 | public String getVersion() { | |
137 | 0 | return version; |
138 | } | |
139 | ||
140 | /** | |
141 | * Gets a textual title for the notation suitable for use | |
142 | * in a combo box or other such visual location. | |
143 | * | |
144 | * @see org.argouml.notation.NotationName#getTitle() | |
145 | */ | |
146 | public String getTitle() { | |
147 | 426 | String myName = name; |
148 | 426 | if (myName.equalsIgnoreCase("uml")) { |
149 | 284 | myName = myName.toUpperCase(); |
150 | } | |
151 | ||
152 | 426 | if (version == null || version.equals("")) { |
153 | 142 | return myName; |
154 | } | |
155 | 284 | return myName + " " + version; |
156 | } | |
157 | ||
158 | /** | |
159 | * Returns an icon for the notation, or null if no icon is available. | |
160 | * | |
161 | * @see org.argouml.notation.NotationName#getIcon() | |
162 | */ | |
163 | public Icon getIcon() { | |
164 | 88 | return icon; |
165 | } | |
166 | ||
167 | /* | |
168 | * @see org.argouml.notation.NotationName#getConfigurationValue() | |
169 | */ | |
170 | public String getConfigurationValue() { | |
171 | 8763 | return getNotationNameString(name, version); |
172 | } | |
173 | ||
174 | /* | |
175 | * @see java.lang.Object#toString() | |
176 | */ | |
177 | public String toString() { | |
178 | 379 | return getTitle(); |
179 | } | |
180 | ||
181 | /** | |
182 | * @param k1 first part of the given name | |
183 | * @param k2 2nd part of the given name | |
184 | * @return the notation name string | |
185 | */ | |
186 | static String getNotationNameString(String k1, String k2) { | |
187 | 11463 | if (k2 == null) { |
188 | 925 | return k1; |
189 | } | |
190 | 10538 | if (k2.equals("")) { |
191 | 0 | return k1; |
192 | } | |
193 | 10538 | return k1 + " " + k2; |
194 | } | |
195 | ||
196 | private static void fireEvent(int eventType, NotationName nn) { | |
197 | 1800 | ArgoEventPump.fireEvent(new ArgoNotationEvent(eventType, nn)); |
198 | 1800 | } |
199 | ||
200 | /** | |
201 | * Create a NotationName with or without a version. | |
202 | * The NotationName is only created if there is no such notation before. | |
203 | * | |
204 | * @param k1 the 1st part of the notation name | |
205 | * @param k2 the 2nd part of the notation name | |
206 | * @param icon the icon for the notation | |
207 | * @return the newly created or the old NotationName | |
208 | */ | |
209 | static NotationName makeNotation(String k1, String k2, Icon icon) { | |
210 | 2700 | NotationName nn = null; |
211 | 2700 | nn = findNotation(getNotationNameString(k1, k2)); |
212 | 2700 | if (nn == null) { |
213 | 1800 | nn = new NotationNameImpl(k1, k2, icon); |
214 | 1800 | notations.add(nn); |
215 | 1800 | fireEvent(ArgoEventTypes.NOTATION_ADDED, nn); |
216 | } | |
217 | 2700 | return nn; |
218 | } | |
219 | ||
220 | static boolean removeNotation(NotationName theNotation) { | |
221 | 0 | return notations.remove(theNotation); |
222 | } | |
223 | ||
224 | /** | |
225 | * Get all of the registered notations. | |
226 | * | |
227 | * @return a List with all notations | |
228 | */ | |
229 | static List<NotationName> getAvailableNotations() { | |
230 | 143 | return Collections.unmodifiableList(notations); |
231 | } | |
232 | ||
233 | /** | |
234 | * Finds a NotationName matching the string matching | |
235 | * the name of the notation. Returns null if no match. | |
236 | * | |
237 | * @param s the name string | |
238 | * @return the notationName or null | |
239 | */ | |
240 | static NotationName findNotation(String s) { | |
241 | 6605 | ListIterator iterator = notations.listIterator(); |
242 | 7507 | while (iterator.hasNext()) { |
243 | try { | |
244 | 5707 | NotationName nn = (NotationName) iterator.next(); |
245 | 5707 | if (s.equals(nn.getConfigurationValue())) { |
246 | 4805 | return nn; |
247 | } | |
248 | 0 | } catch (Exception e) { |
249 | // TODO: Document why we catch this. | |
250 | 0 | LOG.error("Unexpected exception", e); |
251 | 902 | } |
252 | } | |
253 | 1800 | return null; |
254 | } | |
255 | ||
256 | /* | |
257 | * @see org.argouml.notation.NotationName#sameNotationAs(org.argouml.notation.NotationName) | |
258 | */ | |
259 | public boolean sameNotationAs(NotationName nn) { | |
260 | 44 | return this.getConfigurationValue().equals(nn.getConfigurationValue()); |
261 | } | |
262 | ||
263 | /** | |
264 | * Finds a NotationName matching the language with no version. | |
265 | * Returns null if no match. | |
266 | * | |
267 | * @param k1 the notation name string | |
268 | * @return the notation name | |
269 | */ | |
270 | static NotationName getNotation(String k1) { | |
271 | 0 | return findNotation(getNotationNameString(k1, null)); |
272 | } | |
273 | ||
274 | /** | |
275 | * Finds a NotationName matching the language and version. | |
276 | * Returns null if no match. | |
277 | * | |
278 | * @param k1 the 1st part of the notation name | |
279 | * @param k2 the 2nd part of the notation name | |
280 | * @return the notation name | |
281 | */ | |
282 | static NotationName getNotation(String k1, String k2) { | |
283 | 0 | return findNotation(getNotationNameString(k1, k2)); |
284 | } | |
285 | ||
286 | } |