KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > tree > EnumBoundedTreeModelDecorator


1 /*
2  * Copyright (c) 1971-2003 TONBELLER AG, Bensheim.
3  * All rights reserved.
4  */

5 package com.tonbeller.wcf.tree;
6
7 import java.util.Collection JavaDoc;
8 import java.util.Comparator JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.TreeMap JavaDoc;
13
14 /**
15  * A TreeModelDecorator that limits the initial visible children to a
16  * enumerated set of nodes. When the user clicks on the refresh button,
17  * all children are displayed from the underlying TreeModel.
18  *
19  * @author av
20  * @since May 2, 2006
21  */

22 public class EnumBoundedTreeModelDecorator extends TreeModelDecorator implements TreeBounding,
23     TreeModelChangeListener {
24   /** maps parent nodes to its bounded children */
25   Map JavaDoc childrenMap;
26   TreeModelChangeSupport changeSupport;
27
28   public EnumBoundedTreeModelDecorator(TreeModel decoree) {
29     super(decoree);
30     childrenMap = new HashMap JavaDoc();
31     changeSupport = new TreeModelChangeSupport(this);
32     setDecoree(decoree);
33   }
34
35   public EnumBoundedTreeModelDecorator(TreeModel decoree, Comparator JavaDoc nodeComparator) {
36     super(decoree);
37     childrenMap = new TreeMap JavaDoc(nodeComparator);
38     changeSupport = new TreeModelChangeSupport(this);
39     setDecoree(decoree);
40   }
41
42   public void setDecoree(TreeModel model) {
43     TreeModel decoree = getDecoree();
44     if (decoree != null)
45       decoree.removeTreeModelChangeListener(this);
46     super.setDecoree(model);
47     decoree = getDecoree();
48     if (decoree != null)
49       decoree.addTreeModelChangeListener(this);
50   }
51
52   public boolean isBounded(Object JavaDoc parent) {
53     return childrenMap.containsKey(parent);
54   }
55
56   public void unbound(Object JavaDoc parent) {
57     if (childrenMap.remove(parent) != null) {
58       changeSupport.fireModelChanged(false, parent);
59     }
60   }
61
62   public boolean hasChildren(Object JavaDoc node) {
63     Object JavaDoc[] children = (Object JavaDoc[]) childrenMap.get(node);
64     if (children != null)
65       return true;
66     return super.hasChildren(node);
67   }
68
69   public Object JavaDoc[] getChildren(Object JavaDoc node) {
70     Object JavaDoc[] children = (Object JavaDoc[]) childrenMap.get(node);
71     if (children != null)
72       return children;
73     return super.getChildren(node);
74   }
75
76   /**
77    * sets the visible nodes.
78    * @param nodes the collection of visible nodes. These nodes (and their parents)
79    * will be visible before refresh.
80    */

81   public void setVisible(Collection JavaDoc nodes) {
82     setVisible(getDecoree(), nodes);
83   }
84
85   /**
86    * sets the visible nodes.
87    * @param model the tree model used to comupte the parents of the visible
88    * nodes.
89    *
90    * @param nodes the collection of visible nodes. These nodes (and their parents)
91    * will be visible before refresh.
92    */

93   public void setVisible(TreeModel model, Collection JavaDoc nodes) {
94     childrenMap.clear();
95     for (Iterator JavaDoc it = nodes.iterator(); it.hasNext();) {
96       Object JavaDoc node = it.next();
97       while (node != null) {
98         Object JavaDoc parent = model.getParent(node);
99         addVisible(parent, node);
100         node = parent;
101       }
102     }
103   }
104
105   private void addVisible(Object JavaDoc parent, Object JavaDoc child) {
106     if (parent == null || child == null)
107       return;
108
109     Object JavaDoc[] children = (Object JavaDoc[]) childrenMap.get(parent);
110     if (children == null) {
111       childrenMap.put(parent, new Object JavaDoc[] { child});
112       return;
113     }
114     // already there?
115
for (int i = 0; i < children.length; i++) {
116       if (children[i].equals(child))
117         return;
118     }
119     Object JavaDoc[] newChildren = new Object JavaDoc[children.length + 1];
120     System.arraycopy(children, 0, newChildren, 0, children.length);
121     newChildren[children.length] = child;
122     childrenMap.put(parent, newChildren);
123
124   }
125
126   public void addTreeModelChangeListener(TreeModelChangeListener l) {
127     changeSupport.addTreeModelChangeListener(l);
128   }
129
130   public void fireModelChanged(boolean identityChanged) {
131     changeSupport.fireModelChanged(identityChanged);
132   }
133
134   public void removeTreeModelChangeListener(TreeModelChangeListener l) {
135     changeSupport.removeTreeModelChangeListener(l);
136   }
137
138   public void treeModelChanged(TreeModelChangeEvent event) {
139     changeSupport.fireModelChanged(event);
140   }
141
142 }
143
Popular Tags