Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FigClassifierRole |
|
| 1.7777777777777777;1.778 |
1 | /* $Id: FigClassifierRole.java 18726 2010-09-10 08:37:15Z 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) 1996-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.collaboration.ui; | |
40 | ||
41 | import java.awt.Color; | |
42 | import java.awt.Dimension; | |
43 | import java.awt.Rectangle; | |
44 | import java.util.Iterator; | |
45 | ||
46 | import org.argouml.model.AddAssociationEvent; | |
47 | import org.argouml.model.AttributeChangeEvent; | |
48 | import org.argouml.model.UmlChangeEvent; | |
49 | import org.argouml.notation.NotationProviderFactory2; | |
50 | import org.argouml.uml.diagram.DiagramSettings; | |
51 | import org.argouml.uml.diagram.ui.FigNodeModelElement; | |
52 | import org.tigris.gef.base.Selection; | |
53 | import org.tigris.gef.presentation.Fig; | |
54 | import org.tigris.gef.presentation.FigRect; | |
55 | import org.tigris.gef.presentation.FigText; | |
56 | ||
57 | /** | |
58 | * Class to display graphics for a UML classifier role in a collaboration | |
59 | * diagram.<p> | |
60 | * | |
61 | * Stereotypes (if there are any) and name are displayed in the center of the | |
62 | * box. | |
63 | * | |
64 | * @author 10 Apr 2002. Jeremy Bennett (mail@jeremybennett.com). Modifications | |
65 | * to ensure stereotypes are handled correctly. | |
66 | * | |
67 | * @author agauthie | |
68 | */ | |
69 | public class FigClassifierRole extends FigNodeModelElement { | |
70 | ||
71 | private static final int DEFAULT_HEIGHT = 50; | |
72 | ||
73 | private static final int DEFAULT_WIDTH = 90; | |
74 | ||
75 | /** | |
76 | * The minimum padding top and bottom. | |
77 | */ | |
78 | private static final int PADDING = 5; | |
79 | ||
80 | /** | |
81 | * The fig that is used for the complete classifier role. | |
82 | * Identical in size to {@link FigNodeModelElement#bigPort}.<p> | |
83 | */ | |
84 | private FigRect cover; | |
85 | ||
86 | /** | |
87 | * Construct a FigClassifierRole. | |
88 | * | |
89 | * @param owner owning UML element | |
90 | * @param bounds position and size (size is ignored) | |
91 | * @param settings render settings | |
92 | */ | |
93 | public FigClassifierRole(Object owner, Rectangle bounds, | |
94 | DiagramSettings settings) { | |
95 | 0 | super(owner, bounds, settings); |
96 | 0 | initClassifierRoleFigs(); |
97 | 0 | if (bounds != null) { |
98 | 0 | setLocation(bounds.x, bounds.y); |
99 | } | |
100 | 0 | } |
101 | ||
102 | @Override | |
103 | protected Fig createBigPortFig() { | |
104 | // The big port. Color of the big port is irrelevant | |
105 | 0 | return new FigRect(X0, Y0, DEFAULT_WIDTH, DEFAULT_HEIGHT, |
106 | DEBUG_COLOR, DEBUG_COLOR); | |
107 | } | |
108 | ||
109 | /** | |
110 | * There should be no size calculations here, | |
111 | * since not all attributes are set yet. | |
112 | */ | |
113 | private void initClassifierRoleFigs() { | |
114 | // The cover. | |
115 | 0 | cover = new FigRect(X0, Y0, DEFAULT_WIDTH, DEFAULT_HEIGHT, LINE_COLOR, |
116 | FILL_COLOR); | |
117 | ||
118 | // The stereotype. Width is the same as the cover, height is its default | |
119 | // (since the font is not yet set). The text should be centered. | |
120 | ||
121 | 0 | getStereotypeFig().setLineWidth(0); |
122 | 0 | getStereotypeFig().setVisible(true); |
123 | //getStereotypeFig().setFilled(false); | |
124 | 0 | getStereotypeFig().setFillColor(DEBUG_COLOR); |
125 | 0 | getStereotypeFig().setBounds(X0, Y0, |
126 | DEFAULT_WIDTH, getStereotypeFig().getHeight()); | |
127 | ||
128 | // The name. Width is the same as the cover, height is the default. | |
129 | // The text of the name will be centered by | |
130 | // default. In the same place as the stereotype, since at this stage | |
131 | // the stereotype is not displayed. Being a classifier role it is | |
132 | // underlined | |
133 | ||
134 | 0 | getNameFig().setLineWidth(0); |
135 | 0 | getNameFig().setReturnAction(FigText.END_EDITING); |
136 | 0 | getNameFig().setFilled(false); |
137 | 0 | getNameFig().setUnderline(true); |
138 | ||
139 | 0 | getNameFig().setBounds(X0, Y0, |
140 | DEFAULT_WIDTH, getStereotypeFig().getHeight()); | |
141 | ||
142 | // add Figs to the FigNode in back-to-front order | |
143 | ||
144 | 0 | addFig(getBigPort()); |
145 | 0 | addFig(cover); |
146 | 0 | addFig(getStereotypeFig()); |
147 | 0 | addFig(getNameFig()); |
148 | 0 | } |
149 | ||
150 | /* | |
151 | * The NotationProvider for the ClassifierRole. <p> | |
152 | * | |
153 | * The syntax is for UML is: | |
154 | * <pre> | |
155 | * baselist := [base] [, base]* | |
156 | * classifierRole := [name] [/ role] [: baselist] | |
157 | * </pre></p> | |
158 | * | |
159 | * The <code>name</code> is the Instance name, not used currently. | |
160 | * See ClassifierRoleNotationUml for details.<p> | |
161 | * | |
162 | * This syntax is compatible with the UML 1.4 specification. | |
163 | */ | |
164 | @Override | |
165 | protected int getNotationProviderType() { | |
166 | 0 | return NotationProviderFactory2.TYPE_CLASSIFIERROLE; |
167 | } | |
168 | ||
169 | /** | |
170 | * Version of the clone to ensure all sub-figs are copied.<p> | |
171 | * | |
172 | * Uses the generic superclass clone which gives a list of all the | |
173 | * figs. Then initialize our instance variables from this list.<p> | |
174 | * | |
175 | * @return A new copy of the the current fig. | |
176 | */ | |
177 | @Override | |
178 | public Object clone() { | |
179 | 0 | FigClassifierRole figClone = (FigClassifierRole) super.clone(); |
180 | 0 | Iterator it = figClone.getFigs().iterator(); |
181 | ||
182 | 0 | figClone.setBigPort((FigRect) it.next()); |
183 | 0 | figClone.cover = (FigRect) it.next(); |
184 | 0 | it.next(); |
185 | 0 | figClone.setNameFig((FigText) it.next()); |
186 | ||
187 | 0 | return figClone; |
188 | } | |
189 | ||
190 | /** | |
191 | * Update the stereotype text.<p> | |
192 | * | |
193 | * If the stereotype text is non-existent, we must make sure it is | |
194 | * marked not displayed, and update the display accordingly.<p> | |
195 | * | |
196 | * Similarly if there is text, we must make sure it is marked | |
197 | * displayed.<p> | |
198 | */ | |
199 | @Override | |
200 | protected void updateStereotypeText() { | |
201 | 0 | Rectangle rect = getBounds(); |
202 | ||
203 | 0 | int stereotypeHeight = 0; |
204 | 0 | if (getStereotypeFig().isVisible()) { |
205 | 0 | stereotypeHeight = getStereotypeFig().getHeight(); |
206 | } | |
207 | 0 | int heightWithoutStereo = getHeight() - stereotypeHeight; |
208 | ||
209 | 0 | getStereotypeFig().populate(); |
210 | ||
211 | 0 | stereotypeHeight = 0; |
212 | 0 | if (getStereotypeFig().isVisible()) { |
213 | 0 | stereotypeHeight = getStereotypeFig().getHeight(); |
214 | } | |
215 | ||
216 | 0 | int minWidth = this.getMinimumSize().width; |
217 | 0 | if (minWidth > rect.width) { |
218 | 0 | rect.width = minWidth; |
219 | } | |
220 | ||
221 | 0 | setBounds( |
222 | rect.x, | |
223 | rect.y, | |
224 | rect.width, | |
225 | heightWithoutStereo + stereotypeHeight); | |
226 | 0 | calcBounds(); |
227 | 0 | } |
228 | ||
229 | /* | |
230 | * @see org.tigris.gef.presentation.Fig#setLineColor(java.awt.Color) | |
231 | */ | |
232 | @Override | |
233 | public void setLineColor(Color col) { | |
234 | 0 | cover.setLineColor(col); |
235 | 0 | } |
236 | ||
237 | /* | |
238 | * @see org.tigris.gef.presentation.Fig#getLineColor() | |
239 | */ | |
240 | @Override | |
241 | public Color getLineColor() { | |
242 | 0 | return cover.getLineColor(); |
243 | } | |
244 | ||
245 | /* | |
246 | * @see org.tigris.gef.presentation.Fig#setFillColor(java.awt.Color) | |
247 | */ | |
248 | @Override | |
249 | public void setFillColor(Color col) { | |
250 | 0 | cover.setFillColor(col); |
251 | 0 | } |
252 | ||
253 | /* | |
254 | * @see org.tigris.gef.presentation.Fig#getFillColor() | |
255 | */ | |
256 | @Override | |
257 | public Color getFillColor() { | |
258 | 0 | return cover.getFillColor(); |
259 | } | |
260 | ||
261 | /* | |
262 | * @see org.tigris.gef.presentation.Fig#setFilled(boolean) | |
263 | */ | |
264 | @Override | |
265 | public void setFilled(boolean f) { | |
266 | 0 | cover.setFilled(f); |
267 | 0 | } |
268 | ||
269 | ||
270 | @Override | |
271 | public boolean isFilled() { | |
272 | 0 | return cover.isFilled(); |
273 | } | |
274 | ||
275 | /* | |
276 | * @see org.tigris.gef.presentation.Fig#setLineWidth(int) | |
277 | */ | |
278 | @Override | |
279 | public void setLineWidth(int w) { | |
280 | 0 | cover.setLineWidth(w); |
281 | 0 | } |
282 | ||
283 | /* | |
284 | * @see org.tigris.gef.presentation.Fig#getLineWidth() | |
285 | */ | |
286 | @Override | |
287 | public int getLineWidth() { | |
288 | 0 | return cover.getLineWidth(); |
289 | } | |
290 | ||
291 | ||
292 | /** | |
293 | * Work out the minimum size that this Fig can be.<p> | |
294 | * | |
295 | * This should be the size of the stereotype + name + padding. However | |
296 | * we allow for the possible case that the cover or big port could be | |
297 | * bigger still.<p> | |
298 | * | |
299 | * @return The minimum size of this fig. | |
300 | */ | |
301 | @Override | |
302 | public Dimension getMinimumSize() { | |
303 | ||
304 | 0 | Dimension stereoMin = getStereotypeFig().getMinimumSize(); |
305 | 0 | Dimension nameMin = getNameFig().getMinimumSize(); |
306 | ||
307 | 0 | Dimension newMin = new Dimension(nameMin.width, nameMin.height); |
308 | ||
309 | 0 | if (!(stereoMin.height == 0 && stereoMin.width == 0)) { |
310 | 0 | newMin.width = Math.max(newMin.width, stereoMin.width); |
311 | 0 | newMin.height += stereoMin.height; |
312 | } | |
313 | ||
314 | 0 | newMin.height += PADDING; |
315 | ||
316 | 0 | return newMin; |
317 | } | |
318 | ||
319 | ||
320 | /** | |
321 | * Override setBounds to keep shapes looking right.<p> | |
322 | * | |
323 | * Set the bounds of all components of the Fig. The stereotype (if any) | |
324 | * and name are centred in the fig.<p> | |
325 | * | |
326 | * We allow for the requested bounds being too small, and impose our | |
327 | * minimum size if necessary.<p> | |
328 | * | |
329 | * @param x X coordinate of upper left corner | |
330 | * | |
331 | * @param y Y coordinate of upper left corner | |
332 | * | |
333 | * @param w width of bounding box | |
334 | * | |
335 | * @param h height of bounding box | |
336 | * | |
337 | * @author 10 Apr 2002. Jeremy Bennett (mail@jeremybennett.com). Patch to | |
338 | * allow for stereotype as well. | |
339 | */ | |
340 | @Override | |
341 | protected void setStandardBounds(int x, int y, int w, int h) { | |
342 | ||
343 | // In the rather unlikely case that we have no name, we give up. | |
344 | ||
345 | 0 | if (getNameFig() == null) { |
346 | 0 | return; |
347 | } | |
348 | ||
349 | // Remember where we are at present, so we can tell GEF later. Then | |
350 | // check we are as big as the minimum size | |
351 | ||
352 | 0 | Rectangle oldBounds = getBounds(); |
353 | 0 | Dimension minSize = getMinimumSize(); |
354 | ||
355 | 0 | int newW = (minSize.width > w) ? minSize.width : w; |
356 | 0 | int newH = (minSize.height > h) ? minSize.height : h; |
357 | ||
358 | 0 | Dimension stereoMin = getStereotypeFig().getMinimumSize(); |
359 | 0 | Dimension nameMin = getNameFig().getMinimumSize(); |
360 | ||
361 | // Work out the padding each side, depending on whether the stereotype | |
362 | // is displayed and set bounds accordingly | |
363 | ||
364 | 0 | int extraEach = (newH - nameMin.height - stereoMin.height) / 2; |
365 | 0 | if (!(stereoMin.height == 0 && stereoMin.width == 0)) { |
366 | /* At least one stereotype is visible */ | |
367 | 0 | getStereotypeFig().setBounds(x, y + extraEach, newW, |
368 | getStereotypeFig().getHeight()); | |
369 | } | |
370 | 0 | getNameFig().setBounds(x, y + stereoMin.height + extraEach, newW, |
371 | nameMin.height); | |
372 | ||
373 | // Set the bounds of the bigPort and cover | |
374 | ||
375 | 0 | getBigPort().setBounds(x, y, newW, newH); |
376 | 0 | cover.setBounds(x, y, newW, newH); |
377 | ||
378 | // Record the changes in the instance variables of our parent, tell GEF | |
379 | // and trigger the edges to reconsider themselves. | |
380 | ||
381 | 0 | _x = x; |
382 | 0 | _y = y; |
383 | 0 | _w = newW; |
384 | 0 | _h = newH; |
385 | ||
386 | 0 | firePropChange("bounds", oldBounds, getBounds()); |
387 | 0 | updateEdges(); |
388 | 0 | } |
389 | ||
390 | @Override | |
391 | protected void updateLayout(UmlChangeEvent event) { | |
392 | 0 | super.updateLayout(event); |
393 | 0 | if (event instanceof AddAssociationEvent |
394 | || event instanceof AttributeChangeEvent) { | |
395 | // TODO: We need to be more specific here about what to build | |
396 | 0 | renderingChanged(); |
397 | // TODO: Is this really needed? | |
398 | 0 | damage(); |
399 | } | |
400 | 0 | } |
401 | ||
402 | /* | |
403 | * @see org.tigris.gef.presentation.Fig#makeSelection() | |
404 | */ | |
405 | @Override | |
406 | public Selection makeSelection() { | |
407 | 0 | return new SelectionClassifierRole(this); |
408 | } | |
409 | ||
410 | } |