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.ui.explorer; |
40 | |
|
41 | |
import java.awt.Graphics; |
42 | |
import java.awt.Image; |
43 | |
import java.awt.event.MouseAdapter; |
44 | |
import java.awt.event.MouseEvent; |
45 | |
import java.util.ArrayList; |
46 | |
import java.util.Collection; |
47 | |
import java.util.Enumeration; |
48 | |
import java.util.HashSet; |
49 | |
import java.util.Iterator; |
50 | |
import java.util.List; |
51 | |
import java.util.Set; |
52 | |
|
53 | |
import javax.swing.JPopupMenu; |
54 | |
import javax.swing.JTree; |
55 | |
import javax.swing.event.TreeExpansionEvent; |
56 | |
import javax.swing.event.TreeExpansionListener; |
57 | |
import javax.swing.event.TreeSelectionEvent; |
58 | |
import javax.swing.event.TreeSelectionListener; |
59 | |
import javax.swing.event.TreeWillExpandListener; |
60 | |
import javax.swing.tree.DefaultMutableTreeNode; |
61 | |
import javax.swing.tree.TreePath; |
62 | |
|
63 | |
import org.argouml.application.helpers.ResourceLoaderWrapper; |
64 | |
import org.argouml.kernel.Project; |
65 | |
import org.argouml.kernel.ProjectManager; |
66 | |
import org.argouml.kernel.ProjectSettings; |
67 | |
import org.argouml.model.Model; |
68 | |
import org.argouml.ui.DisplayTextTree; |
69 | |
import org.argouml.ui.ProjectActions; |
70 | |
import org.argouml.ui.targetmanager.TargetEvent; |
71 | |
import org.argouml.ui.targetmanager.TargetListener; |
72 | |
import org.argouml.ui.targetmanager.TargetManager; |
73 | |
import org.tigris.gef.presentation.Fig; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | 5239 | public class ExplorerTree extends DisplayTextTree { |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | 900 | private Image bgImage = null; |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
private boolean updatingSelection; |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
private boolean updatingSelectionViaTreeSelection; |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public ExplorerTree() { |
109 | 900 | super(); |
110 | |
|
111 | 900 | Project p = ProjectManager.getManager().getCurrentProject(); |
112 | 900 | this.setModel(new ExplorerTreeModel(p, this)); |
113 | 900 | if (Model.getFacade().getUmlVersion().charAt(0) == '2') { |
114 | 0 | bgImage = ResourceLoaderWrapper.lookupIconResource( |
115 | |
"uml2explorerbg").getImage(); |
116 | |
} |
117 | 900 | if (p != null) { |
118 | 0 | ProjectSettings ps = p.getProjectSettings(); |
119 | 0 | setShowStereotype(ps.getShowStereotypesValue()); |
120 | |
} |
121 | 900 | this.addMouseListener(new ExplorerMouseListener(this)); |
122 | 900 | this.addTreeSelectionListener(new ExplorerTreeSelectionListener()); |
123 | 900 | this.addTreeWillExpandListener(new ExplorerTreeWillExpandListener()); |
124 | 900 | this.addTreeExpansionListener(new ExplorerTreeExpansionListener()); |
125 | |
|
126 | 900 | TargetManager.getInstance().addTargetListener( |
127 | |
new ExplorerTargetListener()); |
128 | 900 | } |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
class ExplorerMouseListener extends MouseAdapter { |
135 | |
|
136 | |
private JTree mLTree; |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | 900 | public ExplorerMouseListener(JTree newtree) { |
144 | 900 | super(); |
145 | 900 | mLTree = newtree; |
146 | 900 | } |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
@Override |
154 | |
public void mousePressed(MouseEvent me) { |
155 | 0 | if (me.isPopupTrigger()) { |
156 | 0 | me.consume(); |
157 | 0 | showPopupMenu(me); |
158 | |
} |
159 | 0 | } |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
@Override |
171 | |
public void mouseReleased(MouseEvent me) { |
172 | 0 | if (me.isPopupTrigger()) { |
173 | 0 | me.consume(); |
174 | 0 | showPopupMenu(me); |
175 | |
} |
176 | 0 | } |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
@Override |
184 | |
public void mouseClicked(MouseEvent me) { |
185 | 0 | if (me.isPopupTrigger()) { |
186 | 0 | me.consume(); |
187 | 0 | showPopupMenu(me); |
188 | |
} |
189 | 0 | if (me.getClickCount() >= 2) { |
190 | 0 | myDoubleClick(); |
191 | |
} |
192 | 0 | } |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
private void myDoubleClick() { |
198 | 0 | Object target = TargetManager.getInstance().getTarget(); |
199 | 0 | if (target != null) { |
200 | 0 | List show = new ArrayList(); |
201 | 0 | show.add(target); |
202 | 0 | ProjectActions.jumpToDiagramShowing(show); |
203 | |
} |
204 | 0 | } |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
public void showPopupMenu(MouseEvent me) { |
212 | |
|
213 | 0 | TreePath path = getPathForLocation(me.getX(), me.getY()); |
214 | 0 | if (path == null) { |
215 | 0 | return; |
216 | |
} |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | 0 | if (!isPathSelected(path)) { |
223 | |
|
224 | 0 | getSelectionModel().setSelectionPath(path); |
225 | |
} |
226 | |
|
227 | 0 | Object selectedItem = ((DefaultMutableTreeNode) path |
228 | |
.getLastPathComponent()).getUserObject(); |
229 | 0 | JPopupMenu popup = new ExplorerPopup(selectedItem, me); |
230 | |
|
231 | 0 | if (popup.getComponentCount() > 0) { |
232 | 0 | popup.show(mLTree, me.getX(), me.getY()); |
233 | |
} |
234 | 0 | } |
235 | |
|
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | 900 | class ExplorerTreeWillExpandListener implements TreeWillExpandListener { |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
public void treeWillCollapse(TreeExpansionEvent tee) { |
249 | |
|
250 | 0 | } |
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
public void treeWillExpand(TreeExpansionEvent tee) { |
261 | |
|
262 | 10 | Project p = ProjectManager.getManager().getCurrentProject(); |
263 | 10 | ProjectSettings ps = p.getProjectSettings(); |
264 | 10 | setShowStereotype(ps.getShowStereotypesValue()); |
265 | |
|
266 | 10 | if (getModel() instanceof ExplorerTreeModel) { |
267 | |
|
268 | 10 | ((ExplorerTreeModel) getModel()).updateChildren(tee.getPath()); |
269 | |
} |
270 | 10 | } |
271 | |
} |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | 900 | class ExplorerTreeExpansionListener implements TreeExpansionListener { |
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
public void treeCollapsed(TreeExpansionEvent event) { |
283 | |
|
284 | 0 | } |
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
public void treeExpanded(TreeExpansionEvent event) { |
291 | |
|
292 | |
|
293 | 10 | setSelection(TargetManager.getInstance().getTargets().toArray()); |
294 | 10 | } |
295 | |
} |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
public void refreshSelection() { |
302 | 231 | Collection targets = TargetManager.getInstance().getTargets(); |
303 | 231 | updatingSelectionViaTreeSelection = true; |
304 | 231 | setSelection(targets.toArray()); |
305 | 231 | updatingSelectionViaTreeSelection = false; |
306 | 231 | } |
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
private void setSelection(Object[] targets) { |
314 | 1442 | updatingSelectionViaTreeSelection = true; |
315 | |
|
316 | 1442 | this.clearSelection(); |
317 | 1442 | addTargetsInternal(targets); |
318 | 1442 | updatingSelectionViaTreeSelection = false; |
319 | 1442 | } |
320 | |
|
321 | |
private void addTargetsInternal(Object[] addedTargets) { |
322 | 1442 | if (addedTargets.length < 1) { |
323 | 0 | return; |
324 | |
} |
325 | 1442 | Set targets = new HashSet(); |
326 | 2884 | for (Object t : addedTargets) { |
327 | 1442 | if (t instanceof Fig) { |
328 | 0 | targets.add(((Fig) t).getOwner()); |
329 | |
} else { |
330 | 1442 | targets.add(t); |
331 | |
} |
332 | |
|
333 | 1442 | selectVisible(t); |
334 | |
} |
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | 1442 | int[] selectedRows = getSelectionRows(); |
342 | 1442 | if (selectedRows != null && selectedRows.length > 0) { |
343 | |
|
344 | |
|
345 | |
|
346 | 10 | makeVisible(getPathForRow(selectedRows[0])); |
347 | 10 | scrollRowToVisible(selectedRows[0]); |
348 | |
} |
349 | 1442 | } |
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
private void selectVisible(Object target) { |
355 | 2492 | for (int j = 0; j < getRowCount(); j++) { |
356 | 1050 | Object rowItem = ((DefaultMutableTreeNode) getPathForRow(j) |
357 | |
.getLastPathComponent()).getUserObject(); |
358 | 1050 | if (rowItem == target) { |
359 | 10 | addSelectionRow(j); |
360 | |
} |
361 | |
} |
362 | 1442 | } |
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
private void selectAll(Set targets) { |
368 | 0 | ExplorerTreeModel model = (ExplorerTreeModel) getModel(); |
369 | 0 | ExplorerTreeNode root = (ExplorerTreeNode) model.getRoot(); |
370 | 0 | selectChildren(model, root, targets); |
371 | 0 | } |
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
private void selectChildren(ExplorerTreeModel model, ExplorerTreeNode node, |
378 | |
Set targets) { |
379 | 0 | if (targets.isEmpty()) { |
380 | 0 | return; |
381 | |
} |
382 | |
|
383 | 0 | Object nodeObject = node.getUserObject(); |
384 | 0 | if (nodeObject != null) { |
385 | 0 | for (Object t : targets) { |
386 | 0 | if (t == nodeObject) { |
387 | 0 | addSelectionPath(new TreePath(node.getPath())); |
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
} |
394 | |
} |
395 | |
} |
396 | |
|
397 | 0 | model.updateChildren(new TreePath(node.getPath())); |
398 | 0 | Enumeration e = node.children(); |
399 | 0 | while (e.hasMoreElements()) { |
400 | 0 | selectChildren(model, (ExplorerTreeNode) e.nextElement(), targets); |
401 | |
} |
402 | 0 | } |
403 | |
|
404 | |
public void paint(Graphics g) { |
405 | 1422 | super.paint(g); |
406 | 1422 | if (bgImage != null) { |
407 | 0 | g.drawImage(bgImage, 0, 40, null); |
408 | |
} |
409 | 1422 | } |
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | 900 | class ExplorerTreeSelectionListener implements TreeSelectionListener { |
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
public void valueChanged(TreeSelectionEvent e) { |
424 | |
|
425 | 19 | if (!updatingSelectionViaTreeSelection) { |
426 | 9 | updatingSelectionViaTreeSelection = true; |
427 | |
|
428 | |
|
429 | 9 | TreePath[] addedOrRemovedPaths = e.getPaths(); |
430 | 9 | TreePath[] selectedPaths = getSelectionPaths(); |
431 | 9 | List elementsAsList = new ArrayList(); |
432 | 9 | for (int i = 0; selectedPaths != null |
433 | 9 | && i < selectedPaths.length; i++) { |
434 | 0 | Object element = ((DefaultMutableTreeNode) selectedPaths[i] |
435 | |
.getLastPathComponent()).getUserObject(); |
436 | 0 | elementsAsList.add(element); |
437 | |
|
438 | |
|
439 | 0 | int rows = getRowCount(); |
440 | 0 | for (int row = 0; row < rows; row++) { |
441 | 0 | Object rowItem = ((DefaultMutableTreeNode) getPathForRow( |
442 | |
row).getLastPathComponent()).getUserObject(); |
443 | 0 | if (rowItem == element && !(isRowSelected(row))) { |
444 | 0 | addSelectionRow(row); |
445 | |
} |
446 | |
} |
447 | |
} |
448 | |
|
449 | |
|
450 | 9 | boolean callSetTarget = true; |
451 | 9 | List addedElements = new ArrayList(); |
452 | 9 | for (int i = 0; i < addedOrRemovedPaths.length; i++) { |
453 | 9 | Object element = ((DefaultMutableTreeNode) addedOrRemovedPaths[i] |
454 | |
.getLastPathComponent()).getUserObject(); |
455 | 9 | if (!e.isAddedPath(i)) { |
456 | 9 | callSetTarget = false; |
457 | 9 | break; |
458 | |
} |
459 | 0 | addedElements.add(element); |
460 | |
} |
461 | |
|
462 | 9 | if (callSetTarget |
463 | |
&& addedElements.size() == elementsAsList.size() |
464 | |
&& elementsAsList.containsAll(addedElements)) { |
465 | 0 | TargetManager.getInstance().setTargets(elementsAsList); |
466 | |
} else { |
467 | |
|
468 | |
|
469 | 9 | List removedTargets = new ArrayList(); |
470 | 9 | List addedTargets = new ArrayList(); |
471 | 18 | for (int i = 0; i < addedOrRemovedPaths.length; i++) { |
472 | 9 | Object element = ((DefaultMutableTreeNode) addedOrRemovedPaths[i] |
473 | |
.getLastPathComponent()).getUserObject(); |
474 | 9 | if (e.isAddedPath(i)) { |
475 | 0 | addedTargets.add(element); |
476 | |
} else { |
477 | 9 | removedTargets.add(element); |
478 | |
} |
479 | |
} |
480 | |
|
481 | |
|
482 | 9 | if (!removedTargets.isEmpty()) { |
483 | 9 | Iterator it = removedTargets.iterator(); |
484 | 18 | while (it.hasNext()) { |
485 | 9 | TargetManager.getInstance().removeTarget(it.next()); |
486 | |
} |
487 | |
} |
488 | 9 | if (!addedTargets.isEmpty()) { |
489 | 0 | Iterator it = addedTargets.iterator(); |
490 | 0 | while (it.hasNext()) { |
491 | 0 | TargetManager.getInstance().addTarget(it.next()); |
492 | |
} |
493 | |
} |
494 | |
} |
495 | |
|
496 | 9 | updatingSelectionViaTreeSelection = false; |
497 | |
} |
498 | 19 | } |
499 | |
} |
500 | |
|
501 | 900 | class ExplorerTargetListener implements TargetListener { |
502 | |
|
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
|
508 | |
private void setTargets(Object[] targets) { |
509 | |
|
510 | 1239 | if (!updatingSelection) { |
511 | 1239 | updatingSelection = true; |
512 | 1239 | if (targets.length <= 0) { |
513 | 38 | clearSelection(); |
514 | |
} else { |
515 | 1201 | setSelection(targets); |
516 | |
} |
517 | 1239 | updatingSelection = false; |
518 | |
} |
519 | 1239 | } |
520 | |
|
521 | |
|
522 | |
|
523 | |
|
524 | |
|
525 | |
public void targetAdded(TargetEvent e) { |
526 | 0 | if (!updatingSelection) { |
527 | 0 | updatingSelection = true; |
528 | 0 | Object[] targets = e.getAddedTargets(); |
529 | |
|
530 | 0 | updatingSelectionViaTreeSelection = true; |
531 | 0 | addTargetsInternal(targets); |
532 | 0 | updatingSelectionViaTreeSelection = false; |
533 | 0 | updatingSelection = false; |
534 | |
} |
535 | |
|
536 | 0 | } |
537 | |
|
538 | |
|
539 | |
|
540 | |
|
541 | |
|
542 | |
public void targetRemoved(TargetEvent e) { |
543 | 88 | if (!updatingSelection) { |
544 | 88 | updatingSelection = true; |
545 | |
|
546 | 88 | Object[] targets = e.getRemovedTargets(); |
547 | |
|
548 | 88 | int rows = getRowCount(); |
549 | 176 | for (int i = 0; i < targets.length; i++) { |
550 | 88 | Object target = targets[i]; |
551 | 88 | if (target instanceof Fig) { |
552 | 0 | target = ((Fig) target).getOwner(); |
553 | |
} |
554 | 264 | for (int j = 0; j < rows; j++) { |
555 | 176 | Object rowItem = ((DefaultMutableTreeNode) getPathForRow( |
556 | |
j).getLastPathComponent()).getUserObject(); |
557 | 176 | if (rowItem == target) { |
558 | 0 | updatingSelectionViaTreeSelection = true; |
559 | 0 | removeSelectionRow(j); |
560 | 0 | updatingSelectionViaTreeSelection = false; |
561 | |
} |
562 | |
} |
563 | |
} |
564 | |
|
565 | 88 | if (getSelectionCount() > 0) { |
566 | 0 | scrollRowToVisible(getSelectionRows()[0]); |
567 | |
} |
568 | 88 | updatingSelection = false; |
569 | |
} |
570 | |
|
571 | 88 | } |
572 | |
|
573 | |
|
574 | |
|
575 | |
|
576 | |
|
577 | |
public void targetSet(TargetEvent e) { |
578 | 1239 | setTargets(e.getNewTargets()); |
579 | |
|
580 | 1239 | } |
581 | |
} |
582 | |
|
583 | |
|
584 | |
|
585 | |
|
586 | |
private static final long serialVersionUID = 992867483644759920L; |
587 | |
} |