Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UMLModelElementTaggedValueProxy |
|
| 1.3846153846153846;1.385 |
1 | /* $Id: UMLModelElementTaggedValueProxy.java 17881 2010-01-12 21:09:28Z 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 | * tfmorris | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 2006 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; | |
40 | ||
41 | import java.beans.PropertyChangeEvent; | |
42 | ||
43 | import javax.swing.event.DocumentListener; | |
44 | import javax.swing.event.UndoableEditListener; | |
45 | import javax.swing.text.AttributeSet; | |
46 | import javax.swing.text.BadLocationException; | |
47 | import javax.swing.text.Element; | |
48 | import javax.swing.text.Position; | |
49 | import javax.swing.text.Segment; | |
50 | ||
51 | import org.argouml.model.AddAssociationEvent; | |
52 | import org.argouml.model.Model; | |
53 | import org.argouml.model.ModelEventPump; | |
54 | import org.argouml.model.RemoveAssociationEvent; | |
55 | import org.argouml.ui.targetmanager.TargetEvent; | |
56 | import org.tigris.gef.presentation.Fig; | |
57 | ||
58 | /** | |
59 | * This class provides a text field that can be used to access | |
60 | * tagged values of a ModelElement object. Because TaggedValues | |
61 | * are separated ModelElements themselves, it acts as a proxy | |
62 | * intermediary to an instance of UMLPlainTextDocument and handles | |
63 | * the indirection. | |
64 | * UMLModelElementTaggedValueDocument is especially useful when | |
65 | * using LabelledLayout. | |
66 | * | |
67 | * @since 15 Feb 2006 | |
68 | * @author Tom Morris (tfmorris@gmail.com) | |
69 | */ | |
70 | public class UMLModelElementTaggedValueProxy implements UMLDocument { | |
71 | ||
72 | /** | |
73 | * The target of the propertypanel that's behind this property. | |
74 | */ | |
75 | 0 | private Object panelTarget = null; |
76 | ||
77 | /** | |
78 | * The name (tagType) of the type (TagDefinition) of the TaggedValue | |
79 | * that this document shows. | |
80 | */ | |
81 | 0 | private String tagName = null; |
82 | private static final String EVENT_NAME = "taggedValue"; | |
83 | ||
84 | private UMLModelElementTaggedValueDocument document; | |
85 | ||
86 | /** | |
87 | * Creates a UMLPlainTextDocument object that represents a tagged value of | |
88 | * an ModelElement object. | |
89 | * | |
90 | * @param taggedValue the tagged value | |
91 | */ | |
92 | 0 | public UMLModelElementTaggedValueProxy(String taggedValue) { |
93 | 0 | tagName = taggedValue; |
94 | 0 | document = new UMLModelElementTaggedValueDocument(""); |
95 | 0 | } |
96 | ||
97 | /* | |
98 | * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent) | |
99 | */ | |
100 | public void propertyChange(PropertyChangeEvent evt) { | |
101 | 0 | if (evt instanceof AddAssociationEvent) { |
102 | 0 | Object tv = evt.getNewValue(); |
103 | 0 | Object td = Model.getFacade().getTagDefinition(tv); |
104 | 0 | String name = (String) Model.getFacade().getType(td); |
105 | 0 | if (tagName != null && tagName.equals(name)) { |
106 | 0 | document.setTarget(tv); |
107 | } | |
108 | 0 | } else if (evt instanceof RemoveAssociationEvent) { |
109 | 0 | Object tv = evt.getOldValue(); |
110 | 0 | Object td = Model.getFacade().getTagDefinition(tv); |
111 | 0 | String name = (String) Model.getFacade().getType(td); |
112 | 0 | if (tagName != null && tagName.equals(name)) { |
113 | 0 | document.setTarget(null); |
114 | } | |
115 | 0 | } else { |
116 | 0 | document.propertyChange(evt); |
117 | } | |
118 | 0 | } |
119 | ||
120 | /** | |
121 | * Sets the tagged value to given String. | |
122 | * | |
123 | * @param text the property | |
124 | */ | |
125 | protected void setProperty(String text) { | |
126 | 0 | document.setProperty(text); |
127 | 0 | } |
128 | ||
129 | /** | |
130 | * | |
131 | * @return the value of the tagged value | |
132 | */ | |
133 | protected String getProperty() { | |
134 | 0 | return document.getProperty(); |
135 | } | |
136 | ||
137 | /** | |
138 | * Returns the target. | |
139 | * @return Object | |
140 | */ | |
141 | public final Object getTarget() { | |
142 | 0 | return panelTarget; |
143 | } | |
144 | ||
145 | /** | |
146 | * Sets the target. | |
147 | * @param target The target to set | |
148 | */ | |
149 | public final void setTarget(Object target) { | |
150 | 0 | target = target instanceof Fig ? ((Fig) target).getOwner() : target; |
151 | 0 | if (Model.getFacade().isAModelElement(target)) { |
152 | 0 | if (target != panelTarget) { |
153 | 0 | ModelEventPump eventPump = Model.getPump(); |
154 | 0 | if (panelTarget != null) { |
155 | 0 | eventPump.removeModelEventListener(this, panelTarget, |
156 | EVENT_NAME); | |
157 | } | |
158 | 0 | panelTarget = target; |
159 | 0 | eventPump.addModelEventListener(this, panelTarget, EVENT_NAME); |
160 | // TODO: see if the new target has a TV that we can proxy | |
161 | 0 | document.setTarget(Model.getFacade().getTaggedValue( |
162 | panelTarget, tagName)); | |
163 | } | |
164 | } | |
165 | 0 | } |
166 | ||
167 | /////////////////////////////////////////////////////////////////////// | |
168 | // Proxy methods for real UMLModelElementTaggedValue | |
169 | /////////////////////////////////////////////////////////////////////// | |
170 | ||
171 | /* | |
172 | * @see org.argouml.ui.targetmanager.TargetListener#targetAdded(org.argouml.ui.targetmanager.TargetEvent) | |
173 | */ | |
174 | public void targetAdded(TargetEvent e) { | |
175 | 0 | setTarget(e.getNewTarget()); |
176 | 0 | } |
177 | ||
178 | /* | |
179 | * @see org.argouml.ui.targetmanager.TargetListener#targetRemoved(org.argouml.ui.targetmanager.TargetEvent) | |
180 | */ | |
181 | public void targetRemoved(TargetEvent e) { | |
182 | 0 | setTarget(e.getNewTarget()); |
183 | 0 | } |
184 | ||
185 | /* | |
186 | * @see org.argouml.ui.targetmanager.TargetListener#targetSet(org.argouml.ui.targetmanager.TargetEvent) | |
187 | */ | |
188 | public void targetSet(TargetEvent e) { | |
189 | 0 | setTarget(e.getNewTarget()); |
190 | 0 | } |
191 | ||
192 | /* | |
193 | * @see javax.swing.text.Document#insertString( | |
194 | * int, java.lang.String, javax.swing.text.AttributeSet) | |
195 | */ | |
196 | public void insertString(int offset, String str, AttributeSet a) | |
197 | throws BadLocationException { | |
198 | 0 | document.insertString(offset, str, a); |
199 | 0 | } |
200 | ||
201 | /* | |
202 | * @see javax.swing.text.Document#remove(int, int) | |
203 | */ | |
204 | public void remove(int offs, int len) throws BadLocationException { | |
205 | 0 | document.remove(offs, len); |
206 | 0 | } |
207 | ||
208 | /* | |
209 | * @see javax.swing.text.Document#getDefaultRootElement() | |
210 | */ | |
211 | public Element getDefaultRootElement() { | |
212 | 0 | return document.getDefaultRootElement(); |
213 | } | |
214 | ||
215 | /* | |
216 | * @see javax.swing.text.Document#getLength() | |
217 | */ | |
218 | public int getLength() { | |
219 | 0 | return document.getLength(); |
220 | } | |
221 | ||
222 | /* | |
223 | * @see javax.swing.text.Document#render(Runnable r) | |
224 | */ | |
225 | public void render(Runnable r) { | |
226 | 0 | document.render(r); |
227 | 0 | } |
228 | ||
229 | /* | |
230 | * @see javax.swing.text.Document#getText(int, int) | |
231 | */ | |
232 | public String getText(int offset, int length) throws BadLocationException { | |
233 | 0 | return document.getText(offset, length); |
234 | } | |
235 | ||
236 | public void addDocumentListener(DocumentListener listener) { | |
237 | 0 | document.addDocumentListener(listener); |
238 | 0 | } |
239 | ||
240 | public void removeDocumentListener(DocumentListener listener) { | |
241 | 0 | document.removeDocumentListener(listener); |
242 | 0 | } |
243 | ||
244 | public void addUndoableEditListener(UndoableEditListener listener) { | |
245 | 0 | document.addUndoableEditListener(listener); |
246 | 0 | } |
247 | ||
248 | public void removeUndoableEditListener(UndoableEditListener listener) { | |
249 | 0 | document.removeUndoableEditListener(listener); |
250 | 0 | } |
251 | ||
252 | public Element[] getRootElements() { | |
253 | 0 | return document.getRootElements(); |
254 | } | |
255 | ||
256 | public Position getEndPosition() { | |
257 | 0 | return document.getEndPosition(); |
258 | } | |
259 | ||
260 | public Position getStartPosition() { | |
261 | 0 | return document.getStartPosition(); |
262 | } | |
263 | ||
264 | public Position createPosition(int offs) throws BadLocationException { | |
265 | 0 | return document.createPosition(offs); |
266 | } | |
267 | ||
268 | public void getText(int offset, int length, Segment txt) | |
269 | throws BadLocationException { | |
270 | 0 | document.getText(offset, length, txt); |
271 | 0 | } |
272 | ||
273 | public Object getProperty(Object key) { | |
274 | 0 | return document.getProperty(key); |
275 | } | |
276 | ||
277 | public void putProperty(Object key, Object value) { | |
278 | 0 | document.putProperty(key, value); |
279 | 0 | } |
280 | ||
281 | ||
282 | } |