1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
package org.argouml.uml.ui; |
41 | |
|
42 | |
import java.awt.BorderLayout; |
43 | |
import java.awt.Dimension; |
44 | |
import java.awt.Insets; |
45 | |
import java.awt.event.ActionEvent; |
46 | |
import java.awt.event.ActionListener; |
47 | |
import java.beans.PropertyChangeEvent; |
48 | |
import java.util.ArrayList; |
49 | |
import java.util.Collection; |
50 | |
import java.util.Iterator; |
51 | |
|
52 | |
import javax.swing.BorderFactory; |
53 | |
import javax.swing.Box; |
54 | |
import javax.swing.BoxLayout; |
55 | |
import javax.swing.ImageIcon; |
56 | |
import javax.swing.JButton; |
57 | |
import javax.swing.JLabel; |
58 | |
import javax.swing.JList; |
59 | |
import javax.swing.JPanel; |
60 | |
import javax.swing.JScrollPane; |
61 | |
import javax.swing.event.ListSelectionEvent; |
62 | |
import javax.swing.event.ListSelectionListener; |
63 | |
|
64 | |
import org.argouml.configuration.Configuration; |
65 | |
import org.argouml.i18n.Translator; |
66 | |
import org.argouml.kernel.ProjectManager; |
67 | |
import org.argouml.model.AddAssociationEvent; |
68 | |
import org.argouml.model.AttributeChangeEvent; |
69 | |
import org.argouml.model.Model; |
70 | |
import org.argouml.model.RemoveAssociationEvent; |
71 | |
import org.argouml.swingext.SpacerPanel; |
72 | |
import org.argouml.swingext.UpArrowIcon; |
73 | |
import org.argouml.ui.TabModelTarget; |
74 | |
import org.argouml.ui.targetmanager.TargetManager; |
75 | |
import org.argouml.uml.StereotypeUtility; |
76 | |
import org.tigris.gef.presentation.Fig; |
77 | |
import org.tigris.swidgets.Horizontal; |
78 | |
import org.tigris.swidgets.Vertical; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | 0 | public class TabStereotype extends PropPanel implements TabModelTarget { |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
private static final int INSET_PX = 3; |
93 | |
|
94 | 900 | private static String orientation = |
95 | |
Configuration.getString(Configuration |
96 | |
.makeKey("layout", "tabstereotype")); |
97 | |
|
98 | |
private UMLModelElementStereotypeListModel selectedListModel; |
99 | |
private UMLModelStereotypeListModel availableListModel; |
100 | |
|
101 | |
private JScrollPane selectedScroll; |
102 | |
private JScrollPane availableScroll; |
103 | |
private JPanel panel; |
104 | |
private JButton addStButton; |
105 | |
private JButton removeStButton; |
106 | |
private JPanel xferButtons; |
107 | |
private JList selectedList; |
108 | |
private JList availableList; |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
public TabStereotype() { |
114 | 900 | super(Translator.localize("tab.stereotype"), (ImageIcon) null); |
115 | 900 | setOrientation((orientation |
116 | |
.equals("West") || orientation.equals("East")) ? Vertical |
117 | |
.getInstance() : Horizontal.getInstance()); |
118 | 900 | setIcon(new UpArrowIcon()); |
119 | 900 | setLayout(new BorderLayout()); |
120 | 900 | remove(getTitleLabel()); |
121 | |
|
122 | 900 | panel = makePanel(); |
123 | 900 | add(panel); |
124 | 900 | } |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
private JPanel makePanel() { |
132 | |
|
133 | 900 | selectedListModel = new UMLModelElementStereotypeListModel(); |
134 | 900 | selectedList = new UMLStereotypeList(selectedListModel); |
135 | 900 | selectedScroll = new JScrollPane(selectedList); |
136 | 900 | selectedScroll.setBorder(BorderFactory.createEmptyBorder( |
137 | |
INSET_PX, INSET_PX, INSET_PX, INSET_PX)); |
138 | 900 | selectedScroll.setColumnHeaderView(new JLabel( |
139 | |
Translator.localize("label.applied-stereotypes"))); |
140 | |
|
141 | 900 | availableListModel = new UMLModelStereotypeListModel(); |
142 | 900 | availableList = new UMLStereotypeList(availableListModel); |
143 | 900 | availableScroll = new JScrollPane(availableList); |
144 | 900 | availableScroll.setBorder(BorderFactory.createEmptyBorder( |
145 | |
INSET_PX, INSET_PX, INSET_PX, INSET_PX)); |
146 | 900 | availableScroll.setColumnHeaderView(new JLabel( |
147 | |
Translator.localize("label.available-stereotypes"))); |
148 | |
|
149 | |
|
150 | 900 | addStButton = new JButton(">>"); |
151 | 900 | addStButton.setToolTipText(Translator.localize("button.add-stereo")); |
152 | 900 | removeStButton = new JButton("<<"); |
153 | 900 | removeStButton.setToolTipText(Translator.localize( |
154 | |
"button.remove-stereo")); |
155 | 900 | addStButton.setEnabled(false); |
156 | 900 | removeStButton.setEnabled(false); |
157 | 900 | addStButton.setMargin(new Insets(2, 15, 2, 15)); |
158 | 900 | removeStButton.setMargin(new Insets(2, 15, 2, 15)); |
159 | 900 | addStButton.setPreferredSize(addStButton.getMinimumSize()); |
160 | 900 | removeStButton.setPreferredSize(removeStButton.getMinimumSize()); |
161 | |
|
162 | |
|
163 | |
BoxLayout box; |
164 | 900 | xferButtons = new JPanel(); |
165 | 900 | box = new BoxLayout(xferButtons, BoxLayout.Y_AXIS); |
166 | 900 | xferButtons.setLayout(box); |
167 | 900 | xferButtons.add(new SpacerPanel()); |
168 | 900 | xferButtons.add(addStButton); |
169 | 900 | xferButtons.add(new SpacerPanel()); |
170 | 900 | xferButtons.add(removeStButton); |
171 | 900 | Dimension dmax = box.maximumLayoutSize(xferButtons); |
172 | 900 | Dimension dmin = box.minimumLayoutSize(xferButtons); |
173 | 900 | xferButtons.setMaximumSize(new Dimension(dmin.width, dmax.height)); |
174 | |
|
175 | |
|
176 | 900 | addStButton.addActionListener(new AddRemoveListener()); |
177 | 900 | removeStButton.addActionListener(new AddRemoveListener()); |
178 | 900 | availableList.addListSelectionListener( |
179 | |
new AvailableListSelectionListener()); |
180 | 900 | selectedList.addListSelectionListener( |
181 | |
new SelectedListSelectionListener()); |
182 | |
|
183 | |
|
184 | 900 | JPanel thePanel = new JPanel(); |
185 | 900 | thePanel.setLayout(new BoxLayout(thePanel, BoxLayout.X_AXIS)); |
186 | 900 | thePanel.setBorder(BorderFactory.createEmptyBorder( |
187 | |
INSET_PX, INSET_PX, INSET_PX, INSET_PX)); |
188 | 900 | thePanel.add(availableScroll); |
189 | 900 | thePanel.add(xferButtons); |
190 | 900 | thePanel.add(Box.createRigidArea(new Dimension(5, 1))); |
191 | 900 | thePanel.add(selectedScroll); |
192 | |
|
193 | 900 | return thePanel; |
194 | |
} |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
public boolean shouldBeEnabled() { |
204 | 0 | Object target = getTarget(); |
205 | 0 | return shouldBeEnabled(target); |
206 | |
} |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
@Override |
212 | |
public boolean shouldBeEnabled(Object target) { |
213 | 1327 | if (target instanceof Fig) { |
214 | 0 | target = ((Fig) target).getOwner(); |
215 | |
} |
216 | 1327 | return Model.getFacade().isAModelElement(target); |
217 | |
} |
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
@Override |
223 | |
public void setTarget(Object theTarget) { |
224 | 0 | super.setTarget(theTarget); |
225 | 0 | if (isVisible()) { |
226 | 0 | Object me = getModelElement(); |
227 | 0 | if (me != null) { |
228 | 0 | selectedListModel.setTarget(me); |
229 | 0 | validate(); |
230 | |
} |
231 | |
} |
232 | 0 | } |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
private void doAddStereotype() { |
239 | 0 | Object stereotype = availableList.getSelectedValue(); |
240 | 0 | Object modelElement = TargetManager.getInstance().getModelTarget(); |
241 | 0 | if (modelElement == null) { |
242 | 0 | return; |
243 | |
} |
244 | 0 | Model.getCoreHelper().addStereotype(modelElement, stereotype); |
245 | 0 | ProjectManager.getManager().updateRoots(); |
246 | 0 | } |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
private void doRemoveStereotype() { |
253 | 0 | Object stereotype = selectedList.getSelectedValue(); |
254 | 0 | Object modelElement = TargetManager.getInstance().getModelTarget(); |
255 | 0 | if (modelElement == null) { |
256 | 0 | return; |
257 | |
} |
258 | |
|
259 | 0 | if (Model.getFacade().getStereotypes(modelElement) |
260 | |
.contains(stereotype)) { |
261 | 0 | Model.getCoreHelper().removeStereotype(modelElement, stereotype); |
262 | 0 | ProjectManager.getManager().updateRoots(); |
263 | |
} |
264 | 0 | } |
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
private static class UMLModelStereotypeListModel |
271 | |
extends UMLStereotypeListModel { |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
public UMLModelStereotypeListModel() { |
277 | 900 | super("stereotype"); |
278 | 900 | } |
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
protected void buildModelList() { |
284 | 0 | removeAllElements(); |
285 | 0 | if (Model.getFacade().isAModelElement(getTarget())) { |
286 | |
Collection s; |
287 | 0 | s = StereotypeUtility.getAvailableStereotypes(getTarget()); |
288 | |
|
289 | 0 | s.removeAll(Model.getFacade().getStereotypes(getTarget())); |
290 | 0 | addAll(s); |
291 | |
} |
292 | 0 | } |
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
private boolean isValidEventRemove(PropertyChangeEvent e) { |
302 | 0 | boolean valid = false; |
303 | 0 | if (!(getChangedElement(e) instanceof Collection)) { |
304 | |
|
305 | 0 | if ((e.getNewValue() != null && e.getOldValue() == null) |
306 | |
|| isValidElement(getChangedElement(e))) { |
307 | 0 | valid = true; |
308 | |
} |
309 | |
} else { |
310 | 0 | Collection col = (Collection) getChangedElement(e); |
311 | 0 | Iterator it = col.iterator(); |
312 | 0 | if (!col.isEmpty()) { |
313 | 0 | valid = true; |
314 | 0 | while (it.hasNext()) { |
315 | 0 | Object o = it.next(); |
316 | 0 | if (!isValidElement(o)) { |
317 | 0 | valid = false; |
318 | 0 | break; |
319 | |
} |
320 | 0 | } |
321 | |
} else { |
322 | 0 | if (e.getOldValue() instanceof Collection |
323 | |
&& !((Collection) e.getOldValue()).isEmpty()) { |
324 | 0 | valid = true; |
325 | |
} |
326 | |
} |
327 | |
} |
328 | 0 | return valid; |
329 | |
} |
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
public void propertyChange(PropertyChangeEvent e) { |
343 | 0 | if (e instanceof AttributeChangeEvent) { |
344 | |
|
345 | 0 | } else if (e instanceof AddAssociationEvent) { |
346 | |
|
347 | 0 | boolean valid = false; |
348 | 0 | if (!(getChangedElement(e) instanceof Collection)) { |
349 | 0 | valid = contains(getChangedElement(e)); |
350 | |
} else { |
351 | 0 | Collection col = (Collection) getChangedElement(e); |
352 | 0 | Iterator it = col.iterator(); |
353 | 0 | valid = true; |
354 | 0 | while (it.hasNext()) { |
355 | 0 | Object o = it.next(); |
356 | 0 | if (!contains(o)) { |
357 | 0 | valid = false; |
358 | 0 | break; |
359 | |
} |
360 | 0 | } |
361 | |
} |
362 | 0 | if (valid) { |
363 | 0 | Object o = getChangedElement(e); |
364 | 0 | if (o instanceof Collection) { |
365 | 0 | Iterator it = ((Collection) o).iterator(); |
366 | 0 | while (it.hasNext()) { |
367 | 0 | Object o3 = it.next(); |
368 | 0 | removeElement(o3); |
369 | 0 | } |
370 | 0 | } else { |
371 | 0 | removeElement(o); |
372 | |
} |
373 | |
} |
374 | |
|
375 | |
|
376 | |
|
377 | 0 | } else if (e instanceof RemoveAssociationEvent) { |
378 | 0 | if (isValidEventRemove(e)) { |
379 | 0 | Object o = getChangedElement(e); |
380 | 0 | if (o instanceof Collection) { |
381 | 0 | ArrayList tempList = new ArrayList((Collection) o); |
382 | 0 | Iterator it = tempList.iterator(); |
383 | 0 | while (it.hasNext()) { |
384 | 0 | Object o2 = it.next(); |
385 | 0 | addElement(o2); |
386 | 0 | } |
387 | 0 | } else { |
388 | 0 | addElement(o); |
389 | |
} |
390 | |
} |
391 | |
|
392 | |
} |
393 | 0 | } |
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
protected boolean isValidElement(Object element) { |
399 | 0 | return Model.getFacade().isAStereotype(element); |
400 | |
} |
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
private static final long serialVersionUID = 7247425177890724453L; |
406 | |
} |
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | 3600 | private class AddRemoveListener implements ActionListener { |
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
public void actionPerformed(ActionEvent e) { |
417 | |
|
418 | 0 | Object src = e.getSource(); |
419 | 0 | if (src == addStButton) { |
420 | 0 | doAddStereotype(); |
421 | 0 | } else if (src == removeStButton) { |
422 | 0 | doRemoveStereotype(); |
423 | |
} |
424 | 0 | } |
425 | |
} |
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | 1800 | private class AvailableListSelectionListener |
431 | |
implements ListSelectionListener { |
432 | |
|
433 | |
|
434 | |
|
435 | |
public void valueChanged(ListSelectionEvent lse) { |
436 | 0 | if (lse.getValueIsAdjusting()) { |
437 | 0 | return; |
438 | |
} |
439 | |
|
440 | 0 | Object selRule = availableList.getSelectedValue(); |
441 | 0 | addStButton.setEnabled(selRule != null); |
442 | 0 | } |
443 | |
} |
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | 1800 | private class SelectedListSelectionListener |
449 | |
implements ListSelectionListener { |
450 | |
|
451 | |
|
452 | |
|
453 | |
public void valueChanged(ListSelectionEvent lse) { |
454 | 0 | if (lse.getValueIsAdjusting()) { |
455 | 0 | return; |
456 | |
} |
457 | |
|
458 | 0 | Object selRule = selectedList.getSelectedValue(); |
459 | 0 | removeStButton.setEnabled(selRule != null); |
460 | 0 | } |
461 | |
} |
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
private static final long serialVersionUID = -4741653225927138553L; |
467 | |
} |