Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FigGeneralization |
|
| 2.4444444444444446;2.444 |
1 | /* $Id: FigGeneralization.java 17865 2010-01-12 20:45:26Z 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) 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.ui; | |
40 | ||
41 | import java.awt.Font; | |
42 | import java.awt.Graphics; | |
43 | import java.awt.Rectangle; | |
44 | import java.beans.PropertyChangeEvent; | |
45 | ||
46 | import org.argouml.model.AttributeChangeEvent; | |
47 | import org.argouml.model.Model; | |
48 | import org.argouml.uml.diagram.DiagramSettings; | |
49 | import org.tigris.gef.presentation.ArrowHeadTriangle; | |
50 | import org.tigris.gef.presentation.Fig; | |
51 | import org.tigris.gef.presentation.FigText; | |
52 | ||
53 | /** | |
54 | * The Fig for a Generalization. | |
55 | * | |
56 | * @author abonner@ics.uci.edu, jaap.branderhorst@xs4all.nl | |
57 | */ | |
58 | public class FigGeneralization extends FigEdgeModelElement { | |
59 | ||
60 | private static final int TEXT_HEIGHT = 20; | |
61 | ||
62 | private static final int DISCRIMINATOR_WIDTH = 90; | |
63 | ||
64 | /* | |
65 | * The serialVersionUID (generated by Eclipse) | |
66 | */ | |
67 | private static final long serialVersionUID = 3983170503390943894L; | |
68 | ||
69 | ||
70 | /** | |
71 | * Group for the FigTexts concerning the name and stereotype of the | |
72 | * association itself. | |
73 | */ | |
74 | private FigTextGroup middleGroup; | |
75 | ||
76 | /** | |
77 | * Text box for discriminator. | |
78 | */ | |
79 | private FigText discriminator; | |
80 | ||
81 | private ArrowHeadTriangle endArrow; | |
82 | ||
83 | private void initialize() { | |
84 | // UML spec for Generalizations doesn't call for name | |
85 | ||
86 | 0 | discriminator.setFilled(false); |
87 | 0 | discriminator.setLineWidth(0); |
88 | 0 | discriminator.setReturnAction(FigText.END_EDITING); |
89 | 0 | discriminator.setTabAction(FigText.END_EDITING); |
90 | ||
91 | 0 | middleGroup.addFig(discriminator); |
92 | 0 | middleGroup.addFig(getStereotypeFig()); |
93 | ||
94 | 0 | addPathItem(middleGroup, |
95 | new PathItemPlacement(this, middleGroup, 50, -10)); | |
96 | ||
97 | 0 | endArrow = new ArrowHeadTriangle(); |
98 | 0 | endArrow.setFillColor(FILL_COLOR); |
99 | 0 | setDestArrowHead(endArrow); |
100 | ||
101 | 0 | setBetweenNearestPoints(true); |
102 | 0 | } |
103 | ||
104 | /** | |
105 | * Construct a new generalization edge with the given model element as the | |
106 | * owner. | |
107 | * | |
108 | * @param owner owning model element | |
109 | * @param settings render settings | |
110 | */ | |
111 | public FigGeneralization(Object owner, DiagramSettings settings) { | |
112 | 0 | super(owner, settings); |
113 | 0 | discriminator = new ArgoFigText(owner, new Rectangle(X0, Y0, |
114 | DISCRIMINATOR_WIDTH, TEXT_HEIGHT), settings, false); | |
115 | 0 | middleGroup = new FigTextGroup(owner, settings); |
116 | ||
117 | ||
118 | 0 | initialize(); |
119 | 0 | fixup(owner); |
120 | 0 | addListener(owner); |
121 | 0 | } |
122 | ||
123 | /* | |
124 | * @see org.argouml.uml.diagram.ui.FigEdgeModelElement#canEdit(org.tigris.gef.presentation.Fig) | |
125 | */ | |
126 | @Override | |
127 | protected boolean canEdit(Fig f) { | |
128 | 0 | return false; |
129 | } | |
130 | ||
131 | /* | |
132 | * @see org.argouml.uml.diagram.ui.FigEdgeModelElement#modelChanged(java.beans.PropertyChangeEvent) | |
133 | */ | |
134 | @Override | |
135 | protected void modelChanged(PropertyChangeEvent e) { | |
136 | // Name & stereotypes get updated by superclass | |
137 | 0 | super.modelChanged(e); |
138 | // Update the discriminator if it changed | |
139 | 0 | if (e instanceof AttributeChangeEvent |
140 | && "discriminator".equals(e.getPropertyName())) { | |
141 | 0 | updateDiscriminatorText(); |
142 | } | |
143 | 0 | } |
144 | ||
145 | /* | |
146 | * @see org.argouml.uml.diagram.ui.FigEdgeModelElement#updateListeners(java.lang.Object, java.lang.Object) | |
147 | */ | |
148 | @Override | |
149 | protected void updateListeners(Object oldOwner, Object newOwner) { | |
150 | 0 | if (oldOwner != null) { |
151 | 0 | removeElementListener(oldOwner); |
152 | } | |
153 | 0 | if (newOwner != null) { |
154 | 0 | addListener(newOwner); |
155 | } | |
156 | 0 | } |
157 | ||
158 | private void addListener(Object owner) { | |
159 | 0 | addElementListener(owner, new String[] {"remove", "discriminator"}); |
160 | 0 | } |
161 | ||
162 | /** | |
163 | * Updates the discriminator text. Called if the model is changed | |
164 | * and on construction time. | |
165 | */ | |
166 | public void updateDiscriminatorText() { | |
167 | 0 | Object generalization = getOwner(); |
168 | 0 | if (generalization == null) { |
169 | 0 | return; |
170 | } | |
171 | 0 | String disc = |
172 | (String) Model.getFacade().getDiscriminator(generalization); | |
173 | 0 | if (disc == null) { |
174 | 0 | disc = ""; |
175 | } | |
176 | 0 | discriminator.setFont(getSettings().getFont(Font.PLAIN)); |
177 | 0 | discriminator.setText(disc); |
178 | 0 | } |
179 | ||
180 | /* | |
181 | * @see org.tigris.gef.presentation.Fig#paint(java.awt.Graphics) | |
182 | */ | |
183 | @Override | |
184 | public void paint(Graphics g) { | |
185 | 0 | endArrow.setLineColor(getLineColor()); |
186 | 0 | super.paint(g); |
187 | 0 | } |
188 | ||
189 | /** | |
190 | * Fix up bad figs. | |
191 | * <p> | |
192 | * Due to errors in earlier releases of argouml it can happen that there is | |
193 | * a generalization without a child or parent. | |
194 | * <p> | |
195 | * TODO: Move into XSL. We should not remove from the graph model while | |
196 | * we're writing to it or we have a possible cause of concurrent | |
197 | * modification exception. | |
198 | * | |
199 | * @param owner | |
200 | */ | |
201 | private void fixup(Object owner) { | |
202 | 0 | if (Model.getFacade().isAGeneralization(owner)) { |
203 | 0 | Object subType = Model.getFacade().getSpecific(owner); |
204 | 0 | Object superType = Model.getFacade().getGeneral(owner); |
205 | ||
206 | 0 | if (subType == null || superType == null) { |
207 | // TODO: We should warn the user we have removed something - tfm | |
208 | 0 | removeFromDiagram(); |
209 | 0 | return; |
210 | } | |
211 | 0 | updateDiscriminatorText(); // show it |
212 | 0 | } else if (owner != null) { |
213 | 0 | throw new IllegalStateException( |
214 | "FigGeneralization has an illegal owner of " | |
215 | + owner.getClass().getName()); | |
216 | } | |
217 | 0 | } |
218 | } |