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 | |
package org.argouml.uml.ui; |
40 | |
|
41 | |
import java.awt.BorderLayout; |
42 | |
import java.awt.Component; |
43 | |
import java.awt.Container; |
44 | |
import java.awt.Dimension; |
45 | |
import java.awt.Font; |
46 | |
import java.awt.GridLayout; |
47 | |
import java.awt.event.ComponentEvent; |
48 | |
import java.awt.event.ComponentListener; |
49 | |
import java.util.ArrayList; |
50 | |
import java.util.Collection; |
51 | |
import java.util.HashSet; |
52 | |
import java.util.Iterator; |
53 | |
import java.util.List; |
54 | |
|
55 | |
import javax.swing.Action; |
56 | |
import javax.swing.Icon; |
57 | |
import javax.swing.ImageIcon; |
58 | |
import javax.swing.JButton; |
59 | |
import javax.swing.JLabel; |
60 | |
import javax.swing.JPanel; |
61 | |
import javax.swing.JToolBar; |
62 | |
import javax.swing.ListModel; |
63 | |
import javax.swing.SwingConstants; |
64 | |
import javax.swing.SwingUtilities; |
65 | |
import javax.swing.border.TitledBorder; |
66 | |
import javax.swing.event.EventListenerList; |
67 | |
|
68 | |
import org.apache.log4j.Logger; |
69 | |
import org.argouml.application.api.AbstractArgoJPanel; |
70 | |
import org.argouml.application.helpers.ResourceLoaderWrapper; |
71 | |
import org.argouml.i18n.Translator; |
72 | |
import org.argouml.kernel.ProfileConfiguration; |
73 | |
import org.argouml.kernel.ProjectManager; |
74 | |
import org.argouml.model.Model; |
75 | |
import org.argouml.ui.ActionCreateContainedModelElement; |
76 | |
import org.argouml.ui.LookAndFeelMgr; |
77 | |
import org.argouml.ui.targetmanager.TargetEvent; |
78 | |
import org.argouml.ui.targetmanager.TargetListener; |
79 | |
import org.argouml.ui.targetmanager.TargettableModelView; |
80 | |
import org.tigris.gef.presentation.Fig; |
81 | |
import org.tigris.swidgets.GridLayout2; |
82 | |
import org.tigris.swidgets.Orientation; |
83 | |
import org.tigris.toolbar.ToolBarFactory; |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | 0 | public abstract class PropPanel extends AbstractArgoJPanel implements |
99 | |
UMLUserInterfaceContainer, ComponentListener { |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | 900 | private static final Logger LOG = Logger.getLogger(PropPanel.class); |
105 | |
|
106 | |
private Object target; |
107 | |
|
108 | |
private Object modelElement; |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
private EventListenerList listenerList; |
116 | |
|
117 | 4089 | private JPanel buttonPanel = new JPanel(new GridLayout()); |
118 | |
|
119 | |
private JLabel titleLabel; |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | 4089 | private List actions = new ArrayList(); |
131 | |
|
132 | 900 | private static Font stdFont = |
133 | |
LookAndFeelMgr.getInstance().getStandardFont(); |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public PropPanel(String label, ImageIcon icon) { |
142 | 4089 | super(Translator.localize(label)); |
143 | |
|
144 | 4089 | LabelledLayout layout = new LabelledLayout(); |
145 | 4089 | layout.setHgap(5); |
146 | 4089 | setLayout(layout); |
147 | |
|
148 | 4089 | if (icon != null) { |
149 | 2289 | setTitleLabel(new JLabel(Translator.localize(label), icon, |
150 | |
SwingConstants.LEFT)); |
151 | |
} else { |
152 | 1800 | setTitleLabel(new JLabel(Translator.localize(label))); |
153 | |
} |
154 | 4089 | titleLabel.setLabelFor(buttonPanel); |
155 | 4089 | add(titleLabel); |
156 | 4089 | add(buttonPanel); |
157 | |
|
158 | 4089 | addComponentListener(this); |
159 | 4089 | } |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
@Override |
166 | |
public void setOrientation(Orientation orientation) { |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | 1800 | super.setOrientation(orientation); |
175 | 1800 | } |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
protected void addAction(Action action) { |
185 | 4578 | actions.add(action); |
186 | 4578 | } |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
protected void addAction(Action action, String tooltip) { |
198 | 0 | JButton button = new TargettableButton(action); |
199 | 0 | if (tooltip != null) { |
200 | 0 | button.setToolTipText(tooltip); |
201 | |
} |
202 | 0 | button.setText(""); |
203 | 0 | button.setFocusable(false); |
204 | 0 | actions.add(button); |
205 | 0 | } |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
protected void addAction(Object[] actionArray) { |
213 | 0 | actions.add(actionArray); |
214 | 0 | } |
215 | |
|
216 | |
public void buildToolbar() { |
217 | 3208 | LOG.debug("Building toolbar"); |
218 | |
|
219 | 3208 | ToolBarFactory factory = new ToolBarFactory(getActions()); |
220 | 3208 | factory.setRollover(true); |
221 | 3208 | factory.setFloatable(false); |
222 | 3208 | JToolBar toolBar = factory.createToolBar(); |
223 | 3208 | toolBar.setName("misc.toolbar.properties"); |
224 | |
|
225 | 3208 | buttonPanel.removeAll(); |
226 | 3208 | buttonPanel.add(BorderLayout.WEST, toolBar); |
227 | |
|
228 | 3208 | buttonPanel.putClientProperty("ToolBar.toolTipSelectTool", |
229 | |
Translator.localize("action.select")); |
230 | 3208 | } |
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
protected List getActions() { |
237 | 3208 | return actions; |
238 | |
} |
239 | |
|
240 | |
private static class TargettableButton extends JButton |
241 | |
implements TargettableModelView { |
242 | |
|
243 | |
public TargettableButton(Action action) { |
244 | 0 | super(action); |
245 | 0 | } |
246 | |
|
247 | |
public TargetListener getTargettableModel() { |
248 | 0 | if (getAction() instanceof TargetListener) { |
249 | 0 | return (TargetListener) getAction(); |
250 | |
} |
251 | 0 | return null; |
252 | |
} |
253 | |
|
254 | |
} |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
public JLabel addField(String label, Component component) { |
266 | 11778 | JLabel jlabel = createLabelFor(label, component); |
267 | 11778 | component.setFont(stdFont); |
268 | 11778 | add(jlabel); |
269 | 11778 | add(component); |
270 | 11778 | if (component instanceof UMLStereotypeList) { |
271 | 0 | UMLModelElementListModel2 list = |
272 | |
(UMLModelElementListModel2) ((UMLStereotypeList) component).getModel(); |
273 | 0 | ActionCreateContainedModelElement newAction = |
274 | |
new ActionCreateContainedModelElement( |
275 | |
list.getMetaType(), |
276 | |
list.getTarget(), |
277 | |
"New..."); |
278 | |
} |
279 | 11778 | return jlabel; |
280 | |
} |
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
private JLabel createLabelFor(String label, Component comp) { |
288 | 11778 | JLabel jlabel = new JLabel(Translator.localize(label)); |
289 | 11778 | jlabel.setToolTipText(Translator.localize(label)); |
290 | 11778 | jlabel.setFont(stdFont); |
291 | 11778 | jlabel.setLabelFor(comp); |
292 | 11778 | return jlabel; |
293 | |
} |
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
public JLabel addFieldAfter(String label, Component component, |
308 | |
Component afterComponent) { |
309 | 0 | int nComponent = getComponentCount(); |
310 | 0 | for (int i = 0; i < nComponent; ++i) { |
311 | 0 | if (getComponent(i) == afterComponent) { |
312 | 0 | JLabel jlabel = createLabelFor(label, component); |
313 | 0 | component.setFont(stdFont); |
314 | 0 | add(jlabel, ++i); |
315 | 0 | add(component, ++i); |
316 | 0 | return jlabel; |
317 | |
} |
318 | |
} |
319 | 0 | throw new IllegalArgumentException("Component not found"); |
320 | |
} |
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
public JLabel addFieldBefore(String label, Component component, |
335 | |
Component beforeComponent) { |
336 | 0 | int nComponent = getComponentCount(); |
337 | 0 | for (int i = 0; i < nComponent; ++i) { |
338 | 0 | if (getComponent(i) == beforeComponent) { |
339 | 0 | JLabel jlabel = createLabelFor(label, component); |
340 | 0 | component.setFont(stdFont); |
341 | 0 | add(jlabel, i - 1); |
342 | 0 | add(component, i++); |
343 | 0 | return jlabel; |
344 | |
} |
345 | |
} |
346 | 0 | throw new IllegalArgumentException("Component not found"); |
347 | |
} |
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
protected final void addSeparator() { |
353 | 0 | add(LabelledLayout.getSeparator()); |
354 | 0 | } |
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
public void setTarget(Object t) { |
365 | 4723 | LOG.debug("setTarget called with " + t + " as parameter (not target!)"); |
366 | 4723 | t = (t instanceof Fig) ? ((Fig) t).getOwner() : t; |
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | 4723 | Runnable dispatch = null; |
372 | 4723 | if (t != target) { |
373 | |
|
374 | |
|
375 | |
|
376 | 3208 | target = t; |
377 | 3208 | modelElement = null; |
378 | 3208 | if (listenerList == null) { |
379 | 2289 | listenerList = collectTargetListeners(this); |
380 | |
} |
381 | |
|
382 | 3208 | if (Model.getFacade().isAUMLElement(target)) { |
383 | 0 | modelElement = target; |
384 | |
} |
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | 3208 | dispatch = new UMLChangeDispatch(this, |
390 | |
UMLChangeDispatch.TARGET_CHANGED_ADD); |
391 | |
|
392 | 3208 | buildToolbar(); |
393 | |
} else { |
394 | 1515 | dispatch = new UMLChangeDispatch(this, |
395 | |
UMLChangeDispatch.TARGET_REASSERTED); |
396 | |
|
397 | |
} |
398 | 4723 | SwingUtilities.invokeLater(dispatch); |
399 | |
|
400 | |
|
401 | |
|
402 | 4723 | if (titleLabel != null) { |
403 | 4723 | Icon icon = null; |
404 | 4723 | if (t != null) { |
405 | 4464 | icon = ResourceLoaderWrapper.getInstance().lookupIcon(t); |
406 | |
} |
407 | 4723 | if (icon != null) { |
408 | 4464 | titleLabel.setIcon(icon); |
409 | |
} |
410 | |
} |
411 | 4723 | } |
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | |
private EventListenerList collectTargetListeners(Container container) { |
425 | 32046 | Component[] components = container.getComponents(); |
426 | 32046 | EventListenerList list = new EventListenerList(); |
427 | 61803 | for (int i = 0; i < components.length; i++) { |
428 | 29757 | if (components[i] instanceof TargetListener) { |
429 | 2289 | list.add(TargetListener.class, (TargetListener) components[i]); |
430 | |
} |
431 | 29757 | if (components[i] instanceof TargettableModelView) { |
432 | 2289 | list.add(TargetListener.class, |
433 | |
((TargettableModelView) components[i]) |
434 | |
.getTargettableModel()); |
435 | |
} |
436 | 29757 | if (components[i] instanceof Container) { |
437 | 29757 | EventListenerList list2 = collectTargetListeners( |
438 | |
(Container) components[i]); |
439 | 29757 | Object[] objects = list2.getListenerList(); |
440 | 34335 | for (int j = 1; j < objects.length; j += 2) { |
441 | 4578 | list.add(TargetListener.class, (TargetListener) objects[j]); |
442 | |
} |
443 | |
} |
444 | |
} |
445 | 32046 | if (container instanceof PropPanel) { |
446 | |
|
447 | 2289 | for (TargetListener action : collectTargetListenerActions()) { |
448 | 2289 | list.add(TargetListener.class, action); |
449 | |
} |
450 | |
} |
451 | 32046 | return list; |
452 | |
} |
453 | |
|
454 | |
private Collection<TargetListener> collectTargetListenerActions() { |
455 | 2289 | Collection<TargetListener> set = new HashSet<TargetListener>(); |
456 | 2289 | for (Object obj : actions) { |
457 | 4578 | if (obj instanceof TargetListener) { |
458 | 2289 | set.add((TargetListener) obj); |
459 | |
} |
460 | |
} |
461 | 2289 | return set; |
462 | |
} |
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
public final Object getTarget() { |
468 | 4723 | return target; |
469 | |
} |
470 | |
|
471 | |
|
472 | |
|
473 | |
|
474 | |
public void refresh() { |
475 | 0 | SwingUtilities.invokeLater(new UMLChangeDispatch(this, 0)); |
476 | 0 | } |
477 | |
|
478 | |
|
479 | |
|
480 | |
|
481 | |
public boolean shouldBeEnabled(Object t) { |
482 | 0 | t = (t instanceof Fig) ? ((Fig) t).getOwner() : t; |
483 | 0 | return Model.getFacade().isAUMLElement(t); |
484 | |
} |
485 | |
|
486 | |
|
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
|
492 | |
protected Object getDisplayNamespace() { |
493 | 0 | Object ns = null; |
494 | 0 | Object theTarget = getTarget(); |
495 | 0 | if (Model.getFacade().isAModelElement(theTarget)) { |
496 | 0 | ns = Model.getFacade().getNamespace(theTarget); |
497 | |
} |
498 | 0 | return ns; |
499 | |
} |
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
public ProfileConfiguration getProfile() { |
505 | 0 | return ProjectManager.getManager().getCurrentProject() |
506 | |
.getProfileConfiguration(); |
507 | |
} |
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
public final Object getModelElement() { |
513 | 0 | return modelElement; |
514 | |
} |
515 | |
|
516 | |
|
517 | |
|
518 | |
|
519 | |
public String formatElement(Object element) { |
520 | 0 | return getProfile().getFormatingStrategy().formatElement(element, |
521 | |
getDisplayNamespace()); |
522 | |
} |
523 | |
|
524 | |
|
525 | |
|
526 | |
|
527 | |
public String formatNamespace(Object namespace) { |
528 | 0 | return getProfile().getFormatingStrategy().formatElement(namespace, |
529 | |
null); |
530 | |
} |
531 | |
|
532 | |
|
533 | |
|
534 | |
|
535 | |
public String formatCollection(Iterator iter) { |
536 | 0 | Object namespace = getDisplayNamespace(); |
537 | 0 | return getProfile().getFormatingStrategy().formatCollection(iter, |
538 | |
namespace); |
539 | |
} |
540 | |
|
541 | |
|
542 | |
|
543 | |
|
544 | |
|
545 | |
|
546 | |
|
547 | |
protected final Action getDeleteAction() { |
548 | 0 | return ActionDeleteModelElements.getTargetFollower(); |
549 | |
} |
550 | |
|
551 | |
|
552 | |
|
553 | |
|
554 | |
|
555 | |
|
556 | |
|
557 | |
|
558 | |
public boolean isRemovableElement() { |
559 | 0 | return ((getTarget() != null) && (getTarget() != (ProjectManager |
560 | |
.getManager().getCurrentProject().getModel()))); |
561 | |
} |
562 | |
|
563 | |
|
564 | |
|
565 | |
|
566 | |
public void targetAdded(TargetEvent e) { |
567 | 0 | if (listenerList == null) { |
568 | 0 | listenerList = collectTargetListeners(this); |
569 | |
} |
570 | 0 | setTarget(e.getNewTarget()); |
571 | 0 | if (isVisible()) { |
572 | 0 | fireTargetAdded(e); |
573 | |
} |
574 | 0 | } |
575 | |
|
576 | |
|
577 | |
|
578 | |
|
579 | |
public void targetRemoved(TargetEvent e) { |
580 | 176 | setTarget(e.getNewTarget()); |
581 | 176 | if (isVisible()) { |
582 | 176 | fireTargetRemoved(e); |
583 | |
} |
584 | 176 | } |
585 | |
|
586 | |
|
587 | |
|
588 | |
|
589 | |
public void targetSet(TargetEvent e) { |
590 | 4547 | setTarget(e.getNewTarget()); |
591 | 4547 | if (isVisible()) { |
592 | 4547 | fireTargetSet(e); |
593 | |
} |
594 | 4547 | } |
595 | |
|
596 | |
private void fireTargetSet(TargetEvent targetEvent) { |
597 | 4547 | if (listenerList == null) { |
598 | 0 | listenerList = collectTargetListeners(this); |
599 | |
} |
600 | |
|
601 | 4547 | Object[] listeners = listenerList.getListenerList(); |
602 | 18188 | for (int i = listeners.length - 2; i >= 0; i -= 2) { |
603 | 13641 | if (listeners[i] == TargetListener.class) { |
604 | 13641 | ((TargetListener) listeners[i + 1]).targetSet(targetEvent); |
605 | |
} |
606 | |
} |
607 | 4547 | } |
608 | |
|
609 | |
private void fireTargetAdded(TargetEvent targetEvent) { |
610 | 0 | if (listenerList == null) { |
611 | 0 | listenerList = collectTargetListeners(this); |
612 | |
} |
613 | |
|
614 | 0 | Object[] listeners = listenerList.getListenerList(); |
615 | |
|
616 | 0 | for (int i = listeners.length - 2; i >= 0; i -= 2) { |
617 | 0 | if (listeners[i] == TargetListener.class) { |
618 | 0 | ((TargetListener) listeners[i + 1]).targetAdded(targetEvent); |
619 | |
} |
620 | |
} |
621 | 0 | } |
622 | |
|
623 | |
private void fireTargetRemoved(TargetEvent targetEvent) { |
624 | 176 | if (listenerList == null) { |
625 | 0 | listenerList = collectTargetListeners(this); |
626 | |
} |
627 | |
|
628 | 176 | Object[] listeners = listenerList.getListenerList(); |
629 | 704 | for (int i = listeners.length - 2; i >= 0; i -= 2) { |
630 | 528 | if (listeners[i] == TargetListener.class) { |
631 | 528 | ((TargetListener) listeners[i + 1]).targetRemoved(targetEvent); |
632 | |
} |
633 | |
} |
634 | 176 | } |
635 | |
|
636 | |
|
637 | |
|
638 | |
|
639 | |
|
640 | |
protected void setTitleLabel(JLabel theTitleLabel) { |
641 | 4089 | titleLabel = theTitleLabel; |
642 | 4089 | titleLabel.setFont(stdFont); |
643 | 4089 | } |
644 | |
|
645 | |
|
646 | |
|
647 | |
|
648 | |
protected JLabel getTitleLabel() { |
649 | 900 | return titleLabel; |
650 | |
} |
651 | |
|
652 | |
protected final JPanel createBorderPanel(String title) { |
653 | 0 | return new GroupPanel(Translator.localize(title)); |
654 | |
} |
655 | |
|
656 | |
private class GroupPanel extends JPanel { |
657 | |
|
658 | 0 | public GroupPanel(String title) { |
659 | 0 | super(new GridLayout2()); |
660 | 0 | TitledBorder border = new TitledBorder(Translator.localize(title)); |
661 | 0 | border.setTitleFont(stdFont); |
662 | 0 | setBorder(border); |
663 | 0 | } |
664 | |
|
665 | |
public void setEnabled(boolean enabled) { |
666 | 0 | super.setEnabled(enabled); |
667 | 0 | for (final Component component : getComponents()) { |
668 | 0 | component.setEnabled(enabled); |
669 | |
} |
670 | 0 | } |
671 | |
} |
672 | |
|
673 | |
|
674 | |
|
675 | |
|
676 | |
|
677 | |
|
678 | |
|
679 | |
|
680 | |
protected void setButtonPanelSize(int height) { |
681 | |
|
682 | |
|
683 | |
|
684 | 900 | buttonPanel.setMinimumSize(new Dimension(0, height)); |
685 | 900 | buttonPanel.setPreferredSize(new Dimension(0, height)); |
686 | 900 | } |
687 | |
|
688 | |
|
689 | |
|
690 | |
|
691 | |
|
692 | |
|
693 | |
|
694 | |
|
695 | |
protected static ImageIcon lookupIcon(String name) { |
696 | 2289 | return ResourceLoaderWrapper.lookupIconResource(name); |
697 | |
} |
698 | |
|
699 | |
|
700 | |
|
701 | |
|
702 | |
|
703 | |
public void componentHidden(ComponentEvent e) { |
704 | |
|
705 | |
|
706 | 1800 | } |
707 | |
|
708 | |
|
709 | |
|
710 | |
|
711 | |
public void componentShown(ComponentEvent e) { |
712 | |
|
713 | |
|
714 | 0 | fireTargetSet(new TargetEvent( |
715 | |
this, TargetEvent.TARGET_SET, null, new Object[] {target})); |
716 | 0 | } |
717 | |
|
718 | |
|
719 | |
|
720 | |
|
721 | |
public void componentMoved(ComponentEvent e) { |
722 | |
|
723 | 1800 | } |
724 | |
|
725 | |
|
726 | |
|
727 | |
|
728 | |
public void componentResized(ComponentEvent e) { |
729 | |
|
730 | 3000 | } |
731 | |
} |