Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AssociationRoleNotationUml |
|
| 6.333333333333333;6.333 |
1 | /* $Id: AssociationRoleNotationUml.java 17828 2010-01-12 18:55:12Z 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) 2006-2009 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.providers.uml; | |
40 | ||
41 | import java.text.ParseException; | |
42 | import java.util.Collection; | |
43 | import java.util.Iterator; | |
44 | ||
45 | import org.argouml.application.events.ArgoEventPump; | |
46 | import org.argouml.application.events.ArgoEventTypes; | |
47 | import org.argouml.application.events.ArgoHelpEvent; | |
48 | import org.argouml.i18n.Translator; | |
49 | import org.argouml.model.Model; | |
50 | import org.argouml.notation.NotationSettings; | |
51 | import org.argouml.notation.providers.AssociationRoleNotation; | |
52 | import org.argouml.util.MyTokenizer; | |
53 | ||
54 | /** | |
55 | * The UML notation for an AssociationRole. | |
56 | * | |
57 | * @author michiel | |
58 | */ | |
59 | public class AssociationRoleNotationUml extends AssociationRoleNotation { | |
60 | ||
61 | /** | |
62 | * The constructor. | |
63 | * | |
64 | * @param role the given association-role | |
65 | */ | |
66 | public AssociationRoleNotationUml(Object role) { | |
67 | 0 | super(role); |
68 | 0 | } |
69 | ||
70 | /* | |
71 | * @see org.argouml.notation.providers.NotationProvider#getParsingHelp() | |
72 | */ | |
73 | public String getParsingHelp() { | |
74 | 0 | return "parsing.help.fig-association-role"; |
75 | } | |
76 | ||
77 | /* | |
78 | * @see org.argouml.notation.providers.NotationProvider#parse(java.lang.Object, java.lang.String) | |
79 | */ | |
80 | public void parse(Object modelElement, String text) { | |
81 | try { | |
82 | 0 | parseRole(modelElement, text); |
83 | 0 | } catch (ParseException pe) { |
84 | 0 | String msg = "statusmsg.bar.error.parsing.association-role"; |
85 | 0 | Object[] args = { |
86 | pe.getLocalizedMessage(), | |
87 | Integer.valueOf(pe.getErrorOffset()), | |
88 | }; | |
89 | 0 | ArgoEventPump.fireEvent(new ArgoHelpEvent( |
90 | ArgoEventTypes.HELP_CHANGED, this, | |
91 | Translator.messageFormat(msg, args))); | |
92 | 0 | } |
93 | 0 | } |
94 | ||
95 | /** | |
96 | * Parse the string that represents an AssociationRole: <pre> | |
97 | * ["/" name] [":" name_of_the_base_association] | |
98 | * </pre> | |
99 | * | |
100 | * @param role The AssociationRole <em>text</em> describes. | |
101 | * @param text A String on the above format. | |
102 | * @throws ParseException | |
103 | * when it detects an error in the role string. See also | |
104 | * ParseError.getErrorOffset(). | |
105 | */ | |
106 | protected void parseRole(Object role, String text) | |
107 | throws ParseException { | |
108 | String token; | |
109 | 0 | boolean hasColon = false; |
110 | 0 | boolean hasSlash = false; |
111 | 0 | String rolestr = null; |
112 | 0 | String basestr = null; |
113 | ||
114 | 0 | MyTokenizer st = new MyTokenizer(text, " ,\t,/,:"); |
115 | ||
116 | 0 | while (st.hasMoreTokens()) { |
117 | 0 | token = st.nextToken(); |
118 | 0 | if (" ".equals(token) || "\t".equals(token)) { |
119 | /* Do nothing. */ | |
120 | 0 | } else if ("/".equals(token)) { |
121 | 0 | hasSlash = true; |
122 | 0 | hasColon = false; |
123 | ||
124 | 0 | } else if (":".equals(token)) { |
125 | 0 | hasColon = true; |
126 | 0 | hasSlash = false; |
127 | ||
128 | 0 | } else if (hasColon) { |
129 | 0 | if (basestr != null) { |
130 | 0 | String msg = |
131 | "parsing.error.association-role.association-extra-text"; | |
132 | 0 | throw new ParseException(Translator.localize(msg), st |
133 | .getTokenIndex()); | |
134 | } | |
135 | 0 | basestr = token; |
136 | 0 | } else if (hasSlash) { |
137 | 0 | if (rolestr != null) { |
138 | 0 | String msg = |
139 | "parsing.error.association-role.association-extra-text"; | |
140 | 0 | throw new ParseException(Translator.localize(msg), st |
141 | .getTokenIndex()); | |
142 | } | |
143 | 0 | rolestr = token; |
144 | } else { | |
145 | 0 | String msg = |
146 | "parsing.error.association-role.association-extra-text"; | |
147 | 0 | throw new ParseException(Translator.localize(msg), |
148 | st.getTokenIndex()); | |
149 | } | |
150 | } | |
151 | ||
152 | 0 | if (basestr == null) { |
153 | /* If no base was typed, then only set the name: */ | |
154 | 0 | if (rolestr != null) { |
155 | 0 | Model.getCoreHelper().setName(role, rolestr.trim()); |
156 | } | |
157 | 0 | return; |
158 | } | |
159 | /* If the base was not changed, then only set the name: */ | |
160 | 0 | Object currentBase = Model.getFacade().getBase(role); |
161 | 0 | if (currentBase != null) { |
162 | 0 | String currentBaseStr = Model.getFacade().getName(currentBase); |
163 | 0 | if (currentBaseStr == null) { |
164 | /* TODO: Is this needed? */ | |
165 | 0 | currentBaseStr = ""; |
166 | } | |
167 | 0 | if (currentBaseStr.equals(basestr)) { |
168 | 0 | if (rolestr != null) { |
169 | 0 | Model.getCoreHelper().setName(role, rolestr.trim()); |
170 | } | |
171 | 0 | return; |
172 | } | |
173 | } | |
174 | 0 | Collection c = |
175 | Model.getCollaborationsHelper().getAllPossibleBases(role); | |
176 | 0 | Iterator i = c.iterator(); |
177 | 0 | while (i.hasNext()) { |
178 | 0 | Object candidate = i.next(); |
179 | 0 | if (basestr.equals(Model.getFacade().getName(candidate))) { |
180 | 0 | if (Model.getFacade().getBase(role) != candidate) { |
181 | /* If the base is already set to this assoc, | |
182 | * then do not set it again. | |
183 | * This check is needed, otherwise the setbase() | |
184 | * below gives an exception.*/ | |
185 | 0 | Model.getCollaborationsHelper().setBase(role, candidate); |
186 | } | |
187 | /* Only set the name if the base was found: */ | |
188 | 0 | if (rolestr != null) { |
189 | 0 | Model.getCoreHelper().setName(role, rolestr.trim()); |
190 | } | |
191 | 0 | return; |
192 | } | |
193 | 0 | } |
194 | 0 | String msg = "parsing.error.association-role.base-not-found"; |
195 | 0 | throw new ParseException(Translator.localize(msg), 0); |
196 | } | |
197 | ||
198 | private String toString(final Object modelElement) { | |
199 | //get the associationRole name | |
200 | 0 | String name = Model.getFacade().getName(modelElement); |
201 | 0 | if (name == null) { |
202 | 0 | name = ""; |
203 | } | |
204 | 0 | if (name.length() > 0) { |
205 | 0 | name = "/" + name; |
206 | } | |
207 | //get the base association name | |
208 | 0 | Object assoc = Model.getFacade().getBase(modelElement); |
209 | 0 | if (assoc != null) { |
210 | 0 | String baseName = Model.getFacade().getName(assoc); |
211 | 0 | if (baseName != null && baseName.length() > 0) { |
212 | 0 | name = name + ":" + baseName; |
213 | } | |
214 | } | |
215 | 0 | return name; |
216 | } | |
217 | ||
218 | /* | |
219 | * Generate the name of an association role of the form: | |
220 | * ["/" name] [":" name_of_the_base_association] | |
221 | * <p> | |
222 | * Remark: | |
223 | * So, if both names are empty, then nothing is shown! | |
224 | * See issue 2712. | |
225 | */ | |
226 | @Override | |
227 | public String toString(final Object modelElement, | |
228 | final NotationSettings settings) { | |
229 | 0 | return toString(modelElement); |
230 | } | |
231 | ||
232 | } |