Commit | Line | Data |
---|---|---|
2a03ecc0 NR |
1 | package be.nikiroo.fanfix_swing.gui.utils; |
2 | ||
3 | import java.util.ArrayList; | |
4 | import java.util.LinkedList; | |
5 | import java.util.List; | |
6 | ||
7 | import javax.swing.JTree; | |
8 | import javax.swing.tree.TreeModel; | |
9 | import javax.swing.tree.TreeNode; | |
10 | import javax.swing.tree.TreePath; | |
11 | ||
12 | public class TreeSnapshot { | |
13 | private interface NodeAction { | |
14 | public void run(TreeNode node); | |
15 | } | |
16 | ||
17 | private JTree tree; | |
18 | private TreePath[] selectionPaths; | |
19 | private List<TreePath> expanded; | |
20 | ||
21 | public TreeSnapshot(JTree tree) { | |
22 | this.tree = tree; | |
23 | ||
24 | selectionPaths = tree.getSelectionPaths(); | |
25 | if (selectionPaths == null) { | |
26 | selectionPaths = new TreePath[0]; | |
27 | } | |
28 | ||
29 | expanded = new ArrayList<TreePath>(); | |
30 | forEach(tree, new NodeAction() { | |
31 | @Override | |
32 | public void run(TreeNode node) { | |
33 | TreePath path = nodeToPath(node); | |
34 | if (path != null) { | |
35 | if (TreeSnapshot.this.tree.isExpanded(path)) { | |
36 | expanded.add(path); | |
37 | } | |
38 | } | |
39 | } | |
40 | }); | |
41 | } | |
42 | ||
43 | public void apply() { | |
44 | applyTo(tree); | |
45 | } | |
46 | ||
47 | public void applyTo(JTree tree) { | |
48 | final List<TreePath> newExpanded = new ArrayList<TreePath>(); | |
49 | final List<TreePath> newSlectionPaths = new ArrayList<TreePath>(); | |
50 | ||
51 | forEach(tree, new NodeAction() { | |
52 | @Override | |
53 | public void run(TreeNode newNode) { | |
54 | TreePath newPath = nodeToPath(newNode); | |
55 | if (newPath != null) { | |
56 | for (TreePath path : selectionPaths) { | |
57 | if (newPath.toString().equals(path.toString())) { | |
58 | newSlectionPaths.add(newPath); | |
59 | if (expanded.contains(path)) { | |
60 | newExpanded.add(newPath); | |
61 | } | |
62 | } | |
63 | } | |
64 | } | |
65 | } | |
66 | }); | |
67 | ||
68 | for (TreePath newPath : newExpanded) { | |
69 | tree.expandPath(newPath); | |
70 | } | |
71 | ||
72 | tree.setSelectionPaths(newSlectionPaths.toArray(new TreePath[0])); | |
73 | } | |
74 | ||
75 | private void forEach(JTree tree, NodeAction action) { | |
76 | forEach(tree.getModel(), tree.getModel().getRoot(), action); | |
77 | } | |
78 | ||
79 | private void forEach(TreeModel model, Object parent, NodeAction action) { | |
80 | if (!(parent instanceof TreeNode)) | |
81 | return; | |
82 | ||
83 | TreeNode node = (TreeNode) parent; | |
84 | ||
85 | action.run(node); | |
86 | int count = model.getChildCount(node); | |
87 | for (int i = 0; i < count; i++) { | |
88 | Object child = model.getChild(node, i); | |
89 | forEach(model, child, action); | |
90 | } | |
91 | } | |
92 | ||
93 | private static TreePath nodeToPath(TreeNode node) { | |
94 | List<Object> nodes = new LinkedList<Object>(); | |
95 | if (node != null) { | |
96 | nodes.add(node); | |
97 | node = node.getParent(); | |
98 | while (node != null) { | |
99 | nodes.add(0, node); | |
100 | node = node.getParent(); | |
101 | } | |
102 | } | |
103 | ||
104 | return nodes.isEmpty() ? null : new TreePath(nodes.toArray()); | |
105 | } | |
106 | ||
107 | @Override | |
108 | public String toString() { | |
109 | StringBuilder builder = new StringBuilder(); | |
110 | builder.append("Tree Snapshot of: ").append(tree).append("\n"); | |
111 | builder.append("Selected paths:\n"); | |
112 | for (TreePath path : selectionPaths) { | |
113 | builder.append("\t").append(path).append("\n"); | |
114 | } | |
115 | builder.append("Expanded paths:\n"); | |
116 | for (TreePath epath : expanded) { | |
117 | builder.append("\t").append(epath).append("\n"); | |
118 | } | |
119 | ||
120 | return builder.toString(); | |
121 | } | |
122 | } |