Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ObjectFlowStateStateNotationUml |
|
| 4.0;4 |
1 | /* $Id: ObjectFlowStateStateNotationUml.java 18852 2010-11-20 19:27:11Z mvw $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009-2010 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 | * Michiel van der Wulp | |
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.ArrayList; | |
43 | import java.util.Collection; | |
44 | import java.util.Iterator; | |
45 | import java.util.StringTokenizer; | |
46 | ||
47 | import org.argouml.application.events.ArgoEventPump; | |
48 | import org.argouml.application.events.ArgoEventTypes; | |
49 | import org.argouml.application.events.ArgoHelpEvent; | |
50 | import org.argouml.i18n.Translator; | |
51 | import org.argouml.kernel.ProjectManager; | |
52 | import org.argouml.model.Model; | |
53 | import org.argouml.notation.NotationSettings; | |
54 | import org.argouml.notation.providers.ObjectFlowStateStateNotation; | |
55 | ||
56 | /** | |
57 | * Notation for the State of an ObjectFlowState. | |
58 | * | |
59 | * @author Michiel van der Wulp | |
60 | */ | |
61 | public class ObjectFlowStateStateNotationUml extends | |
62 | ObjectFlowStateStateNotation { | |
63 | ||
64 | /** | |
65 | * The constructor. | |
66 | * | |
67 | * @param objectflowstate | |
68 | * the ObjectFlowState represented by this notation | |
69 | */ | |
70 | public ObjectFlowStateStateNotationUml(Object objectflowstate) { | |
71 | 0 | super(objectflowstate); |
72 | 0 | } |
73 | ||
74 | /* | |
75 | * @see org.argouml.notation.providers.NotationProvider#getParsingHelp() | |
76 | */ | |
77 | public String getParsingHelp() { | |
78 | 0 | return "parsing.help.fig-objectflowstate2"; |
79 | } | |
80 | ||
81 | /* | |
82 | * @see org.argouml.notation.providers.NotationProvider#parse(java.lang.Object, java.lang.String) | |
83 | */ | |
84 | public void parse(Object modelElement, String text) { | |
85 | try { | |
86 | 0 | parseObjectFlowState2(modelElement, text); |
87 | 0 | } catch (ParseException pe) { |
88 | 0 | String msg = "statusmsg.bar.error.parsing.objectflowstate"; |
89 | 0 | Object[] args = {pe.getLocalizedMessage(), |
90 | Integer.valueOf(pe.getErrorOffset()), }; | |
91 | 0 | ArgoEventPump.fireEvent(new ArgoHelpEvent( |
92 | ArgoEventTypes.HELP_CHANGED, this, | |
93 | Translator.messageFormat(msg, args))); | |
94 | 0 | } |
95 | 0 | } |
96 | ||
97 | /** | |
98 | * Do the actual parsing. | |
99 | * | |
100 | * @param objectFlowState | |
101 | * the given element to be altered | |
102 | * @param s | |
103 | * the new string | |
104 | * @return the altered ObjectFlowState | |
105 | * @throws ParseException | |
106 | * when the given text was rejected | |
107 | */ | |
108 | protected Object parseObjectFlowState2(Object objectFlowState, String s) | |
109 | throws ParseException { | |
110 | 0 | s = s.trim(); |
111 | /* Let's not be picky about the brackets - just remove them: */ | |
112 | 0 | if (s.startsWith("[")) { |
113 | 0 | s = s.substring(1); |
114 | } | |
115 | 0 | if (s.endsWith("]")) { |
116 | 0 | s = s.substring(0, s.length() - 1); |
117 | } | |
118 | 0 | s = s.trim(); |
119 | 0 | Object c = Model.getFacade().getType(objectFlowState); // get the |
120 | // classifier | |
121 | 0 | if (c != null) { |
122 | 0 | if (Model.getFacade().isAClassifierInState(c)) { |
123 | 0 | Object classifier = Model.getFacade().getType(c); |
124 | 0 | if ((s == null) || "".equals(s)) { |
125 | // the State of a ClassifierInState is removed, | |
126 | // so let's reduce it to a Classifier. | |
127 | 0 | Model.getCoreHelper().setType(objectFlowState, classifier); |
128 | 0 | delete(c); |
129 | 0 | Model.getCoreHelper().setType(objectFlowState, classifier); |
130 | 0 | return objectFlowState; // the model is changed - job done |
131 | } | |
132 | 0 | Collection states = |
133 | new ArrayList(Model.getFacade() | |
134 | .getInStates(c)); | |
135 | 0 | Collection statesToBeRemoved = new ArrayList(states); |
136 | 0 | Collection namesToBeAdded = new ArrayList(); // Strings |
137 | 0 | StringTokenizer tokenizer = new StringTokenizer(s, ","); |
138 | 0 | while (tokenizer.hasMoreTokens()) { |
139 | 0 | String nextToken = tokenizer.nextToken().trim(); |
140 | 0 | boolean found = false; |
141 | 0 | Iterator i = states.iterator(); |
142 | 0 | while (i.hasNext()) { |
143 | 0 | Object state = i.next(); |
144 | 0 | if (Model.getFacade().getName(state) == nextToken) { |
145 | 0 | found = true; |
146 | 0 | statesToBeRemoved.remove(state); |
147 | } | |
148 | 0 | } |
149 | 0 | if (!found) { |
150 | 0 | namesToBeAdded.add(nextToken); |
151 | } | |
152 | 0 | } |
153 | /* Remove the states that did not match. */ | |
154 | 0 | states.removeAll(statesToBeRemoved); |
155 | ||
156 | 0 | Iterator i = namesToBeAdded.iterator(); |
157 | 0 | while (i.hasNext()) { |
158 | 0 | String name = (String) i.next(); |
159 | /* | |
160 | * Now we have to see if any state in any statemachine of | |
161 | * classifier is named [name]. If so, then we only have to | |
162 | * link the state to c. | |
163 | */ | |
164 | 0 | Object state = |
165 | Model.getActivityGraphsHelper() | |
166 | .findStateByName(classifier, name); | |
167 | 0 | if (state != null) { |
168 | 0 | states.add(state); |
169 | // the model is changed - our job is done | |
170 | } else { | |
171 | // no state named s is found, so we have to | |
172 | // reject the user's input | |
173 | 0 | String msg = |
174 | "parsing.error.object-flow-state.state-not-found"; | |
175 | 0 | Object[] args = {s}; |
176 | 0 | throw new ParseException(Translator.localize(msg, args), |
177 | 0); | |
178 | } | |
179 | 0 | } |
180 | ||
181 | /* Finally, do the adaptations: */ | |
182 | 0 | Model.getActivityGraphsHelper().setInStates(c, states); |
183 | ||
184 | 0 | } else { // then c is a "normal" Classifier |
185 | 0 | Collection statesToBeAdded = new ArrayList(); // UML states |
186 | ||
187 | 0 | StringTokenizer tokenizer = new StringTokenizer(s, ","); |
188 | 0 | while (tokenizer.hasMoreTokens()) { |
189 | 0 | String nextToken = tokenizer.nextToken().trim(); |
190 | 0 | Object state = |
191 | Model.getActivityGraphsHelper() | |
192 | .findStateByName(c, nextToken); | |
193 | 0 | if (state != null) { |
194 | 0 | statesToBeAdded.add(state); |
195 | } else { | |
196 | // no state with the given name is found, so we have to | |
197 | // reject the complete user's input | |
198 | 0 | String msg = |
199 | "parsing.error.object-flow-state.state-not-found"; | |
200 | 0 | Object[] args = {s}; |
201 | 0 | throw new ParseException(Translator.localize(msg, args), |
202 | 0); | |
203 | } | |
204 | 0 | } |
205 | ||
206 | // let's create a new ClassifierInState with the correct links | |
207 | 0 | Object cis = |
208 | Model.getActivityGraphsFactory() | |
209 | .buildClassifierInState(c, statesToBeAdded); | |
210 | 0 | Model.getCoreHelper().setType(objectFlowState, cis); |
211 | // the model is changed - our job is done | |
212 | 0 | } |
213 | } else { | |
214 | // if no classifier has been set, then entering a state is | |
215 | // not useful, so the user's input has to be rejected. | |
216 | 0 | String msg = |
217 | "parsing.error.object-flow-state.classifier-not-found"; | |
218 | 0 | throw new ParseException(Translator.localize(msg), |
219 | 0); | |
220 | } | |
221 | 0 | return objectFlowState; |
222 | } | |
223 | ||
224 | /** | |
225 | * This deletes modelelements, and swallows null without barking. | |
226 | * | |
227 | * @author mvw@tigris.org | |
228 | * @param obj | |
229 | * the modelelement to be deleted | |
230 | */ | |
231 | private void delete(Object obj) { | |
232 | 0 | if (obj != null) { |
233 | 0 | ProjectManager.getManager().getCurrentProject().moveToTrash(obj); |
234 | } | |
235 | 0 | } |
236 | ||
237 | private String toString(Object modelElement) { | |
238 | 0 | StringBuilder theNewText = new StringBuilder(""); |
239 | 0 | Object cis = Model.getFacade().getType(modelElement); |
240 | 0 | if (Model.getFacade().isAClassifierInState(cis)) { |
241 | 0 | theNewText.append("[ "); |
242 | 0 | theNewText.append(NotationUtilityUml.formatNameList( |
243 | Model.getFacade().getInStates(cis))); | |
244 | 0 | theNewText.append(" ]"); |
245 | } | |
246 | 0 | return theNewText.toString(); |
247 | } | |
248 | ||
249 | @Override | |
250 | public String toString(Object modelElement, NotationSettings settings) { | |
251 | 0 | return toString(modelElement); |
252 | } | |
253 | } |