xref: /AOO41X/main/odk/examples/java/Inspector/HideableTreeModel.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir 
2*cdf0e10cSrcweir // *** HideableTreeModel ***
3*cdf0e10cSrcweir import java.awt.*;
4*cdf0e10cSrcweir import java.awt.event.*;
5*cdf0e10cSrcweir import java.util.ArrayList;
6*cdf0e10cSrcweir import java.util.Enumeration;
7*cdf0e10cSrcweir import java.util.Vector;
8*cdf0e10cSrcweir import javax.swing.*;
9*cdf0e10cSrcweir import javax.swing.event.*;
10*cdf0e10cSrcweir import javax.swing.tree.*;
11*cdf0e10cSrcweir 
12*cdf0e10cSrcweir 
13*cdf0e10cSrcweir public class HideableTreeModel implements TreeModel {
14*cdf0e10cSrcweir 
15*cdf0e10cSrcweir 	private Vector modelListeners = new Vector();
16*cdf0e10cSrcweir 	private Object root = null;
17*cdf0e10cSrcweir 
18*cdf0e10cSrcweir 
19*cdf0e10cSrcweir         public HideableTreeModel(TreeNode _root) {
20*cdf0e10cSrcweir             super();
21*cdf0e10cSrcweir             setRoot(_root);
22*cdf0e10cSrcweir 	}
23*cdf0e10cSrcweir 
24*cdf0e10cSrcweir 
25*cdf0e10cSrcweir 	public Object getRoot() {
26*cdf0e10cSrcweir 		return this.root;
27*cdf0e10cSrcweir 	}
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir         protected void setRoot(Object r) {
31*cdf0e10cSrcweir             this.root = r;
32*cdf0e10cSrcweir 	}
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir         public Object[] getPathToRoot(Object node) {
36*cdf0e10cSrcweir             return getPathToRoot(node, 0);
37*cdf0e10cSrcweir 	}
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir         private Object[] getPathToRoot(Object node, int i) {
41*cdf0e10cSrcweir             Object anode[];
42*cdf0e10cSrcweir             if(node == null) {
43*cdf0e10cSrcweir                 if(i == 0) {
44*cdf0e10cSrcweir                     return null;
45*cdf0e10cSrcweir                 }
46*cdf0e10cSrcweir                 anode = new Object[i];
47*cdf0e10cSrcweir             } else {
48*cdf0e10cSrcweir                 i++;
49*cdf0e10cSrcweir                 if(node == getRoot()) {
50*cdf0e10cSrcweir                     anode = new Object[i];
51*cdf0e10cSrcweir                 } else {
52*cdf0e10cSrcweir                     anode = getPathToRoot(getParent(node), i);
53*cdf0e10cSrcweir                 }
54*cdf0e10cSrcweir                 anode[anode.length - i] = node;
55*cdf0e10cSrcweir             }
56*cdf0e10cSrcweir             return anode;
57*cdf0e10cSrcweir 	}
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir         public void addTreeModelListener(TreeModelListener l) {
61*cdf0e10cSrcweir             modelListeners.addElement(l);
62*cdf0e10cSrcweir 	}
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir         public void removeTreeModelListener(TreeModelListener l) {
66*cdf0e10cSrcweir             modelListeners.removeElement(l);
67*cdf0e10cSrcweir 	}
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 	public void reload() {
71*cdf0e10cSrcweir             reload(getRoot());
72*cdf0e10cSrcweir 	}
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir         public void reload(Object node) {
76*cdf0e10cSrcweir             if(node != null) {
77*cdf0e10cSrcweir                 TreePath tp = new TreePath(getPathToRoot(node));
78*cdf0e10cSrcweir                 fireTreeStructureChanged(new TreeModelEvent(this, tp));
79*cdf0e10cSrcweir             }
80*cdf0e10cSrcweir 	}
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir         public void valueForPathChanged(TreePath path, Object newValue) {
84*cdf0e10cSrcweir             nodeChanged(path.getLastPathComponent());
85*cdf0e10cSrcweir 	}
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 	public void nodeInserted(Object node, Object child) {
88*cdf0e10cSrcweir             nodeInserted(node, child, -1);
89*cdf0e10cSrcweir 	}
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir         public void nodeInserted(Object node, Object child, int index) {
93*cdf0e10cSrcweir             if(index < 0) {
94*cdf0e10cSrcweir                 index = getIndexOfChild(node, child);
95*cdf0e10cSrcweir             }
96*cdf0e10cSrcweir             if(node != null && child != null && index >= 0) {
97*cdf0e10cSrcweir                 TreePath tp = new TreePath(getPathToRoot(node));
98*cdf0e10cSrcweir                 int[] ai = { index };
99*cdf0e10cSrcweir                 Object[] ac = { child };
100*cdf0e10cSrcweir                 fireTreeNodesInserted(new TreeModelEvent(this, tp, ai, ac));
101*cdf0e10cSrcweir             }
102*cdf0e10cSrcweir 	}
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir         public void nodeRemoved(Object node, Object child, int index) {
106*cdf0e10cSrcweir             if(node != null && child != null && index >= 0) {
107*cdf0e10cSrcweir                 TreePath tp = new TreePath(getPathToRoot(node));
108*cdf0e10cSrcweir                 int[] ai = { index };
109*cdf0e10cSrcweir                 Object[] ac = { child };
110*cdf0e10cSrcweir                 fireTreeNodesRemoved(new TreeModelEvent(this, tp, ai, ac));
111*cdf0e10cSrcweir             }
112*cdf0e10cSrcweir 	}
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir         public void nodeChanged(Object node) {
116*cdf0e10cSrcweir             if(node != null) {
117*cdf0e10cSrcweir                 TreePath tp = new TreePath(getPathToRoot(node));
118*cdf0e10cSrcweir                 fireTreeNodesChanged(new TreeModelEvent(this, tp, null, null));
119*cdf0e10cSrcweir             }
120*cdf0e10cSrcweir 	}
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir         protected void fireTreeNodesChanged(TreeModelEvent event) {
124*cdf0e10cSrcweir             for(int i = 0; i < modelListeners.size(); i++) {
125*cdf0e10cSrcweir                 ((TreeModelListener)modelListeners.elementAt(i)).treeNodesChanged(event);
126*cdf0e10cSrcweir             }
127*cdf0e10cSrcweir 	}
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir         protected void fireTreeNodesInserted(TreeModelEvent event) {
131*cdf0e10cSrcweir             for(int i = 0; i < modelListeners.size(); i++) {
132*cdf0e10cSrcweir                 ((TreeModelListener)modelListeners.elementAt(i)).treeNodesInserted(event);
133*cdf0e10cSrcweir             }
134*cdf0e10cSrcweir 	}
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir         protected void fireTreeNodesRemoved(TreeModelEvent event) {
138*cdf0e10cSrcweir             for(int i = 0; i < modelListeners.size(); i++) {
139*cdf0e10cSrcweir                 ((TreeModelListener)modelListeners.elementAt(i)).treeNodesRemoved(event);
140*cdf0e10cSrcweir             }
141*cdf0e10cSrcweir 	}
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 	protected void fireTreeStructureChanged(TreeModelEvent event) {
144*cdf0e10cSrcweir             for(int i = 0; i < modelListeners.size(); i++) {
145*cdf0e10cSrcweir                 ((TreeModelListener)modelListeners.elementAt(i)).treeStructureChanged(event);
146*cdf0e10cSrcweir             }
147*cdf0e10cSrcweir 	}
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir         public ArrayList getExpandedPaths(JTree tree) {
151*cdf0e10cSrcweir 		ArrayList expandedPaths = new ArrayList();
152*cdf0e10cSrcweir 		addExpandedPaths(tree, tree.getPathForRow(0), expandedPaths);
153*cdf0e10cSrcweir 		return expandedPaths;
154*cdf0e10cSrcweir 	}
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir         private void addExpandedPaths(JTree tree, TreePath path, ArrayList pathlist) {
158*cdf0e10cSrcweir             Enumeration aEnum = tree.getExpandedDescendants(path);
159*cdf0e10cSrcweir             while(aEnum.hasMoreElements()) {
160*cdf0e10cSrcweir                 TreePath tp = (TreePath) aEnum.nextElement();
161*cdf0e10cSrcweir                 pathlist.add(tp);
162*cdf0e10cSrcweir                 addExpandedPaths(tree, tp, pathlist);
163*cdf0e10cSrcweir             }
164*cdf0e10cSrcweir 	}
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir         public void expandPaths(JTree tree, ArrayList pathlist) {
168*cdf0e10cSrcweir             for(int i = 0; i < pathlist.size(); i++) {
169*cdf0e10cSrcweir                 tree.expandPath((TreePath)pathlist.get(i));
170*cdf0e10cSrcweir             }
171*cdf0e10cSrcweir 	}
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         public boolean isLeaf(Object _oNode) {
175*cdf0e10cSrcweir             if(_oNode instanceof TreeNode) {
176*cdf0e10cSrcweir                 return ((TreeNode) _oNode).isLeaf();
177*cdf0e10cSrcweir             }
178*cdf0e10cSrcweir             return true;
179*cdf0e10cSrcweir 	}
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 	public Object getParent(Object node) {
184*cdf0e10cSrcweir             if(node != getRoot() && (node instanceof TreeNode)) {
185*cdf0e10cSrcweir                 return ((TreeNode)node).getParent();
186*cdf0e10cSrcweir             }
187*cdf0e10cSrcweir             return null;
188*cdf0e10cSrcweir 	}
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir         public boolean isNodeVisible(Object node) {
192*cdf0e10cSrcweir             if(node != getRoot()) {
193*cdf0e10cSrcweir                 if(node instanceof HideableMutableTreeNode) {
194*cdf0e10cSrcweir                         return ((HideableMutableTreeNode)node).isVisible();
195*cdf0e10cSrcweir                 }
196*cdf0e10cSrcweir             }
197*cdf0e10cSrcweir             return true;
198*cdf0e10cSrcweir 	}
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir         public boolean setNodeVisible(Object node, boolean v) {
202*cdf0e10cSrcweir             // can't hide root
203*cdf0e10cSrcweir             if(node != getRoot()) {
204*cdf0e10cSrcweir                 if(node instanceof HideableMutableTreeNode) {
205*cdf0e10cSrcweir                     HideableMutableTreeNode n = (HideableMutableTreeNode)node;
206*cdf0e10cSrcweir                     if(v != n.isVisible()) {
207*cdf0e10cSrcweir                         TreeNode parent = n.getParent();
208*cdf0e10cSrcweir                         if(v) {
209*cdf0e10cSrcweir                             // need to get index after showing...
210*cdf0e10cSrcweir                             n.setVisible(v);
211*cdf0e10cSrcweir                             int index = getIndexOfChild(parent, n);
212*cdf0e10cSrcweir                             nodeInserted(parent, n, index);
213*cdf0e10cSrcweir                         } else {
214*cdf0e10cSrcweir                             // need to get index before hiding...
215*cdf0e10cSrcweir                             int index = getIndexOfChild(parent, n);
216*cdf0e10cSrcweir                             n.setVisible(v);
217*cdf0e10cSrcweir                             nodeRemoved(parent, n, index);
218*cdf0e10cSrcweir                         }
219*cdf0e10cSrcweir                     }
220*cdf0e10cSrcweir                     return true;
221*cdf0e10cSrcweir                 }
222*cdf0e10cSrcweir             }
223*cdf0e10cSrcweir             return false;
224*cdf0e10cSrcweir 	}
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir         public boolean isPathToNodeVisible(Object node) {
228*cdf0e10cSrcweir             Object[] path = getPathToRoot(node);
229*cdf0e10cSrcweir             for(int i = 0; i < path.length; i++) {
230*cdf0e10cSrcweir                 if(!isNodeVisible(path[i])) {
231*cdf0e10cSrcweir                     return false;
232*cdf0e10cSrcweir                 }
233*cdf0e10cSrcweir             }
234*cdf0e10cSrcweir             return true;
235*cdf0e10cSrcweir 	}
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir         public void ensurePathToNodeVisible(Object node) {
239*cdf0e10cSrcweir             Object[] path = getPathToRoot(node);
240*cdf0e10cSrcweir             for(int i = 0; i < path.length; i++) {
241*cdf0e10cSrcweir                 setNodeVisible(path[i], true);
242*cdf0e10cSrcweir             }
243*cdf0e10cSrcweir 	}
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir         public Object getChild(Object parent, int index) {
247*cdf0e10cSrcweir             if(parent instanceof TreeNode) {
248*cdf0e10cSrcweir                 TreeNode p = (TreeNode) parent;
249*cdf0e10cSrcweir                 for(int i = 0, j = -1; i < p.getChildCount(); i++) {
250*cdf0e10cSrcweir                     TreeNode pc = (TreeNode)p.getChildAt(i);
251*cdf0e10cSrcweir                     if(isNodeVisible(pc)) {
252*cdf0e10cSrcweir                         j++;
253*cdf0e10cSrcweir                     }
254*cdf0e10cSrcweir                     if(j == index) {
255*cdf0e10cSrcweir                         return pc;
256*cdf0e10cSrcweir                     }
257*cdf0e10cSrcweir                 }
258*cdf0e10cSrcweir             }
259*cdf0e10cSrcweir             return null;
260*cdf0e10cSrcweir 	}
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir         public int getChildCount(Object parent) {
264*cdf0e10cSrcweir             int count = 0;
265*cdf0e10cSrcweir             if(parent instanceof TreeNode) {
266*cdf0e10cSrcweir                 TreeNode p = (TreeNode) parent;
267*cdf0e10cSrcweir                 for(int i = 0; i < p.getChildCount(); i++) {
268*cdf0e10cSrcweir                     TreeNode pc = (TreeNode)p.getChildAt(i);
269*cdf0e10cSrcweir                     if(isNodeVisible(pc)) {
270*cdf0e10cSrcweir                         count++;
271*cdf0e10cSrcweir                     }
272*cdf0e10cSrcweir                 }
273*cdf0e10cSrcweir             }
274*cdf0e10cSrcweir             return count;
275*cdf0e10cSrcweir 	}
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir         public int getIndexOfChild(Object parent, Object child) {
279*cdf0e10cSrcweir             int index = -1;
280*cdf0e10cSrcweir             if(parent instanceof TreeNode && child instanceof TreeNode) {
281*cdf0e10cSrcweir                 TreeNode p = (TreeNode)parent;
282*cdf0e10cSrcweir                 TreeNode c = (TreeNode)child;
283*cdf0e10cSrcweir                 if(isNodeVisible(c)) {
284*cdf0e10cSrcweir                     index = 0;
285*cdf0e10cSrcweir                     for(int i = 0; i < p.getChildCount(); i++) {
286*cdf0e10cSrcweir                         TreeNode pc = (TreeNode)p.getChildAt(i);
287*cdf0e10cSrcweir                         if(pc.equals(c)) {
288*cdf0e10cSrcweir                             return index;
289*cdf0e10cSrcweir                         }
290*cdf0e10cSrcweir                         if(isNodeVisible(pc)) {
291*cdf0e10cSrcweir                             index++;
292*cdf0e10cSrcweir                         }
293*cdf0e10cSrcweir                     }
294*cdf0e10cSrcweir                 }
295*cdf0e10cSrcweir             }
296*cdf0e10cSrcweir             return index;
297*cdf0e10cSrcweir 	}
298*cdf0e10cSrcweir }