Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FigSingleLineTextWithNotation |
|
| 1.4782608695652173;1.478 |
1 | /* $Id: FigSingleLineTextWithNotation.java 18859 2010-11-29 21:37:03Z 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) 2008-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.uml.diagram.ui; | |
40 | ||
41 | import java.awt.Rectangle; | |
42 | ||
43 | import org.argouml.application.events.ArgoEventPump; | |
44 | import org.argouml.application.events.ArgoEventTypes; | |
45 | import org.argouml.application.events.ArgoHelpEvent; | |
46 | import org.argouml.application.events.ArgoNotationEvent; | |
47 | import org.argouml.application.events.ArgoNotationEventListener; | |
48 | import org.argouml.i18n.Translator; | |
49 | import org.argouml.model.UmlChangeEvent; | |
50 | import org.argouml.notation.Notation; | |
51 | import org.argouml.notation.NotationName; | |
52 | import org.argouml.notation.NotationProvider; | |
53 | import org.argouml.notation.NotationProviderFactory2; | |
54 | import org.argouml.notation.NotationRenderer; | |
55 | import org.argouml.notation.NotationSettings; | |
56 | import org.argouml.uml.diagram.DiagramSettings; | |
57 | ||
58 | /** | |
59 | * A single line FigText that uses the Notation subsystem facilities | |
60 | * for its text generation. | |
61 | * This FigText is editable by the user on the diagram. | |
62 | * | |
63 | * @author Michiel | |
64 | */ | |
65 | 0 | public class FigSingleLineTextWithNotation extends FigSingleLineText |
66 | implements ArgoNotationEventListener, NotationRenderer { | |
67 | ||
68 | /** | |
69 | * @param owner the owning UML object | |
70 | * @param bounds the initial position and size | |
71 | * @param settings the diagram settings applicable for this Fig | |
72 | * @param expandOnly true if the Fig won't shrink if its contents shrink | |
73 | */ | |
74 | public FigSingleLineTextWithNotation(Object owner, Rectangle bounds, | |
75 | DiagramSettings settings, boolean expandOnly) { | |
76 | 0 | super(owner, bounds, settings, expandOnly); |
77 | 0 | initNotationProviders(); |
78 | 0 | } |
79 | ||
80 | /** | |
81 | * @param owner the owning UML object | |
82 | * @param bounds the initial position and size | |
83 | * @param settings the diagram settings applicable for this Fig | |
84 | * @param expandOnly true if the Fig won't shrink if its contents shrink | |
85 | * @param property name of property to listen to | |
86 | */ | |
87 | public FigSingleLineTextWithNotation(Object owner, Rectangle bounds, | |
88 | DiagramSettings settings, boolean expandOnly, String property) { | |
89 | 0 | super(owner, bounds, settings, expandOnly, property); |
90 | 0 | initNotationProviders(); |
91 | 0 | } |
92 | ||
93 | /** | |
94 | * @param owner the owning UML object | |
95 | * @param bounds the initial position and size | |
96 | * @param settings the diagram settings applicable for this Fig | |
97 | * @param expandOnly true if the Fig won't shrink if its contents shrink | |
98 | * @param allProperties names of properties to listen to | |
99 | */ | |
100 | public FigSingleLineTextWithNotation(Object owner, Rectangle bounds, | |
101 | DiagramSettings settings, boolean expandOnly, | |
102 | String[] allProperties) { | |
103 | 0 | super(owner, bounds, settings, expandOnly, allProperties); |
104 | 0 | initNotationProviders(); |
105 | 0 | } |
106 | ||
107 | /** | |
108 | * The notation provider for the text shown in this compartment. | |
109 | */ | |
110 | private NotationProvider notationProvider; | |
111 | ||
112 | @Override | |
113 | public void removeFromDiagram() { | |
114 | 0 | ArgoEventPump.removeListener(ArgoEventTypes.ANY_NOTATION_EVENT, this); |
115 | 0 | notationProvider.cleanListener(); |
116 | 0 | super.removeFromDiagram(); |
117 | 0 | } |
118 | ||
119 | /** | |
120 | * This is a template method called by the ArgoUML framework as the result | |
121 | * of a change to a model element. Do not call this method directly | |
122 | * yourself. | |
123 | * <p>Override this in any subclasses in order to redisplay the Fig | |
124 | * due to change of any model element that this Fig is listening to.</p> | |
125 | * <p>This method is guaranteed by the framework to be running on the | |
126 | * Swing/AWT thread.</p> | |
127 | * | |
128 | * @param event the UmlChangeEvent that caused the change | |
129 | */ | |
130 | protected void updateLayout(UmlChangeEvent event) { | |
131 | 0 | assert event != null; |
132 | ||
133 | 0 | if (notationProvider != null |
134 | && (!"remove".equals(event.getPropertyName()) | |
135 | || event.getSource() != getOwner())) { // not??? | |
136 | 0 | this.setText(notationProvider.toString(getOwner(), |
137 | getNotationSettings())); | |
138 | 0 | damage(); |
139 | } | |
140 | 0 | } |
141 | ||
142 | /** | |
143 | * Overrule this for subclasses | |
144 | * that need a different NotationProvider. | |
145 | * | |
146 | * @return the type of the notation provider | |
147 | */ | |
148 | protected int getNotationProviderType() { | |
149 | 0 | return NotationProviderFactory2.TYPE_NAME; |
150 | } | |
151 | ||
152 | /** | |
153 | * @return Returns the notationProvider for the text in this compartment. | |
154 | */ | |
155 | public NotationProvider getNotationProvider() { | |
156 | 0 | return notationProvider; |
157 | } | |
158 | ||
159 | /** | |
160 | * @param np The notationProvider to set. | |
161 | */ | |
162 | void setNotationProvider(NotationProvider np) { | |
163 | 0 | if (notationProvider != null) { |
164 | 0 | notationProvider.cleanListener(); |
165 | } | |
166 | 0 | this.notationProvider = np; |
167 | 0 | } |
168 | ||
169 | protected void initNotationProviders() { | |
170 | 0 | if (notationProvider != null) { |
171 | 0 | notationProvider.cleanListener(); |
172 | } | |
173 | 0 | if (getOwner() != null) { |
174 | 0 | NotationName notation = Notation.findNotation( |
175 | getNotationSettings().getNotationLanguage()); | |
176 | 0 | notationProvider = |
177 | NotationProviderFactory2.getInstance().getNotationProvider( | |
178 | getNotationProviderType(), getOwner(), this, notation); | |
179 | } | |
180 | 0 | } |
181 | ||
182 | /** | |
183 | * Show the help-text for parsing, and initialise the text. | |
184 | */ | |
185 | protected void textEditStarted() { | |
186 | 0 | String s = getNotationProvider().getParsingHelp(); |
187 | 0 | showHelp(s); |
188 | 0 | setText(); |
189 | 0 | } |
190 | ||
191 | /** | |
192 | * Parse the edited text to adapt the UML model. | |
193 | */ | |
194 | protected void textEdited() { | |
195 | 0 | notationProvider.parse(getOwner(), getText()); |
196 | 0 | setText(); |
197 | 0 | } |
198 | ||
199 | /** | |
200 | * @deprecated for 0.27.3 by tfmorris. Diagrams are responsible for | |
201 | * updating their contained Figs with any notation changes. | |
202 | * MVW: There is no replacement yet! | |
203 | * | |
204 | * @param e the event | |
205 | * @see org.argouml.application.events.ArgoNotationEventListener#notationAdded(org.argouml.application.events.ArgoNotationEvent) | |
206 | */ | |
207 | @Deprecated | |
208 | public void notationAdded(ArgoNotationEvent e) { | |
209 | // Do nothing | |
210 | 0 | } |
211 | ||
212 | /** | |
213 | * @deprecated for 0.27.3 by tfmorris. Diagrams are responsible for | |
214 | * updating their contained Figs with any notation changes. | |
215 | * MVW: There is no replacement yet! | |
216 | * | |
217 | * @param e the event | |
218 | * @see org.argouml.application.events.ArgoNotationEventListener#notationChanged(org.argouml.application.events.ArgoNotationEvent) | |
219 | */ | |
220 | @Deprecated | |
221 | public void notationChanged(ArgoNotationEvent e) { | |
222 | 0 | renderingChanged(); |
223 | 0 | } |
224 | ||
225 | /** | |
226 | * @deprecated for 0.27.3 by tfmorris. Diagrams are responsible for | |
227 | * updating their contained Figs with any notation changes. | |
228 | * MVW: There is no replacement yet! | |
229 | * | |
230 | * @param e the event | |
231 | * @see org.argouml.application.events.ArgoNotationEventListener#notationProviderAdded(org.argouml.application.events.ArgoNotationEvent) | |
232 | */ | |
233 | @Deprecated | |
234 | public void notationProviderAdded(ArgoNotationEvent e) { | |
235 | // Do nothing | |
236 | 0 | } |
237 | ||
238 | /** | |
239 | * @deprecated for 0.27.3 by tfmorris. Diagrams are responsible for | |
240 | * updating their contained Figs with any notation changes. | |
241 | * MVW: There is no replacement yet! | |
242 | * | |
243 | * @param e the event | |
244 | * @see org.argouml.application.events.ArgoNotationEventListener#notationProviderRemoved(org.argouml.application.events.ArgoNotationEvent) | |
245 | */ | |
246 | @Deprecated | |
247 | public void notationProviderRemoved(ArgoNotationEvent e) { | |
248 | // Do nothing | |
249 | 0 | } |
250 | ||
251 | /** | |
252 | * @deprecated for 0.27.3 by tfmorris. Diagrams are responsible for | |
253 | * updating their contained Figs with any notation changes. | |
254 | * MVW: There is no replacement yet! | |
255 | * | |
256 | * @param e the event | |
257 | * @see org.argouml.application.events.ArgoNotationEventListener#notationRemoved(org.argouml.application.events.ArgoNotationEvent) | |
258 | */ | |
259 | @Deprecated | |
260 | public void notationRemoved(ArgoNotationEvent e) { | |
261 | // Do nothing | |
262 | 0 | } |
263 | ||
264 | public void renderingChanged() { | |
265 | 0 | initNotationProviders(); |
266 | 0 | super.renderingChanged(); |
267 | 0 | } |
268 | ||
269 | @Override | |
270 | protected void setText() { | |
271 | 0 | assert getOwner() != null; |
272 | 0 | assert notationProvider != null; |
273 | 0 | setText(notationProvider.toString(getOwner(), getNotationSettings())); |
274 | 0 | } |
275 | ||
276 | /** | |
277 | * Utility function to localize the given string with help text, and show it | |
278 | * in the status bar of the ArgoUML window. This function is used in favour | |
279 | * of the inline call to enable later improvements; e.g. it would be | |
280 | * possible to show a help-balloon. | |
281 | * <p> | |
282 | * TODO: This code is also present in other root Figs... | |
283 | * | |
284 | * @param s the given string to be localized and shown | |
285 | */ | |
286 | protected void showHelp(String s) { | |
287 | 0 | ArgoEventPump.fireEvent(new ArgoHelpEvent( |
288 | ArgoEventTypes.HELP_CHANGED, this, | |
289 | Translator.localize(s))); | |
290 | 0 | } |
291 | ||
292 | protected NotationSettings getNotationSettings() { | |
293 | 0 | return getSettings().getNotationSettings(); |
294 | } | |
295 | ||
296 | public void notationRenderingChanged(NotationProvider np, String rendering) { | |
297 | 0 | if (notationProvider == np) { |
298 | 0 | setText(rendering); |
299 | 0 | damage(); |
300 | } | |
301 | 0 | } |
302 | ||
303 | public NotationSettings getNotationSettings(NotationProvider np) { | |
304 | 0 | if (notationProvider == np) { |
305 | 0 | return getNotationSettings(); |
306 | } | |
307 | 0 | return null; |
308 | } | |
309 | ||
310 | public Object getOwner(NotationProvider np) { | |
311 | 0 | if (notationProvider == np) { |
312 | 0 | return getOwner(); |
313 | } | |
314 | 0 | return null; |
315 | } | |
316 | } |