1 | /** | | 1 | /** |
2 | * In the selectionChanged method not only the selection of this | | 2 | * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent) |
3 | * diagram is set but also the selection in the projectbrowser. | | 3 | * |
4 | * | | 4 | * Change in explorer tree selection -> set target in target |
5 | * @param gse The event. | | 5 | * manager. |
6 | */ | | 6 | */ |
7 | public void selectionChanged(GraphSelectionEvent gse) { | | 7 | public void valueChanged(TreeSelectionEvent e) { |
8 | if (!updatingSelection) { | | 8 | |
9 | updatingSelection = true; | | 9 | if (!updatingSelectionViaTreeSelection) { |
10 | List<Fig> selections = gse.getSelections(); | | 10 | updatingSelectionViaTreeSelection = true; |
11 | ActionCut.getInstance().setEnabled( | | 11 | |
12 | selections != null && !selections.isEmpty()); | | 12 | // get the elements |
13 | | | 13 | TreePath[] addedOrRemovedPaths = e.getPaths(); |
14 | // TODO: If ActionCopy is no longer a singleton, how shall | | 14 | TreePath[] selectedPaths = getSelectionPaths(); |
15 | // this work? | | 15 | List elementsAsList = new ArrayList(); |
16 | ActionCopy.getInstance() | | 16 | for (int i = 0; selectedPaths != null |
17 | .setEnabled(selections != null && !selections.isEmpty()); | | 17 | && i < selectedPaths.length; i++) { |
18 | /* | | 18 | Object element = ((DefaultMutableTreeNode) selectedPaths[i] |
19 | * ActionPaste.getInstance().setEnabled( Globals.clipBoard | | 19 | .getLastPathComponent()).getUserObject(); |
20 | * != null && !Globals.clipBoard.isEmpty()); | | 20 | elementsAsList.add(element); |
21 | */ | | 21 | // scan the visible rows for duplicates of |
22 | // the old selection | | 22 | // this elem and select them |
23 | List currentSelection = | | 23 | int rows = getRowCount(); |
24 | TargetManager.getInstance().getTargets(); | | 24 | for (int row = 0; row < rows; row++) { |
25 | | | 25 | Object rowItem = ((DefaultMutableTreeNode) getPathForRow( |
26 | List removedTargets = new ArrayList(currentSelection); | | 26 | row).getLastPathComponent()).getUserObject(); |
27 | List addedTargets = new ArrayList(); | | 27 | if (rowItem == element && !(isRowSelected(row))) { |
28 | for (Object selection : selections) { | | 28 | addSelectionRow(row); |
29 | Object owner = TargetManager.getInstance().getOwner(selection); | | 29 | } |
30 | if (currentSelection.contains(owner)) { | | 30 | } |
31 | removedTargets.remove(owner); // remains selected | | 31 | } |
32 | } else { | | 32 | |
33 | // add to selection | | 33 | // check which targetmanager method to call |
34 | addedTargets.add(owner); | | 34 | boolean callSetTarget = true; |
35 | } | | 35 | List addedElements = new ArrayList(); |
36 | } | | 36 | for (int i = 0; i < addedOrRemovedPaths.length; i++) { |
37 | if (addedTargets.size() == 1 | | 37 | Object element = ((DefaultMutableTreeNode) addedOrRemovedPaths[i] |
38 | && removedTargets.size() == currentSelection.size() | | 38 | .getLastPathComponent()).getUserObject(); |
39 | && removedTargets.size() != 0) { | | 39 | if (!e.isAddedPath(i)) { |
40 | // Optimize for the normal case to minimize target changes | | 40 | callSetTarget = false; |
41 | TargetManager.getInstance().setTarget(addedTargets.get(0)); | | 41 | break; |
42 | } else { | | 42 | } |
43 | for (Object o : removedTargets) { | | 43 | addedElements.add(element); |
44 | TargetManager.getInstance().removeTarget(o); | | 44 | } |
45 | } | | 45 | |
46 | for (Object o : addedTargets) { | | 46 | if (callSetTarget |
47 | TargetManager.getInstance().addTarget(o); | | 47 | && addedElements.size() == elementsAsList.size() |
48 | } | | 48 | && elementsAsList.containsAll(addedElements)) { |
49 | } | | 49 | TargetManager.getInstance().setTargets(elementsAsList); |
50 | updatingSelection = false; | | 50 | } else { |
51 | } | | 51 | // we must call the correct method on targetmanager |
52 | | | 52 | // for each added or removed target |
53 | } | | 53 | List removedTargets = new ArrayList(); |
| | | 54 | List addedTargets = new ArrayList(); |
| | | 55 | for (int i = 0; i < addedOrRemovedPaths.length; i++) { |
| | | 56 | Object element = ((DefaultMutableTreeNode) addedOrRemovedPaths[i] |
| | | 57 | .getLastPathComponent()).getUserObject(); |
| | | 58 | if (e.isAddedPath(i)) { |
| | | 59 | addedTargets.add(element); |
| | | 60 | } else { |
| | | 61 | removedTargets.add(element); |
| | | 62 | } |
| | | 63 | } |
| | | 64 | // we can't remove the targets in one go, we have to |
| | | 65 | // do it one by one. |
| | | 66 | if (!removedTargets.isEmpty()) { |
| | | 67 | Iterator it = removedTargets.iterator(); |
| | | 68 | while (it.hasNext()) { |
| | | 69 | TargetManager.getInstance().removeTarget(it.next()); |
| | | 70 | } |
| | | 71 | } |
| | | 72 | if (!addedTargets.isEmpty()) { |
| | | 73 | Iterator it = addedTargets.iterator(); |
| | | 74 | while (it.hasNext()) { |
| | | 75 | TargetManager.getInstance().addTarget(it.next()); |
| | | 76 | } |
| | | 77 | } |
| | | 78 | } |
| | | 79 | |
| | | 80 | updatingSelectionViaTreeSelection = false; |
| | | 81 | } |
| | | 82 | } |