KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.tree;
14
15 import java.util.Comparator JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.TreeMap JavaDoc;
19
20 import com.tonbeller.wcf.changeorder.ChangeOrderModel;
21 import com.tonbeller.wcf.changeorder.ChangeOrderUtils;
22
23 /**
24  * a caching TreeModel that allows to change the parent/child relationship.
25  * @author av
26  */

27 public class MutableTreeModelDecorator implements DecoratedTreeModel, MutableTreeModel, ChangeOrderModel {
28   TreeModel decoree;
29
30   Object JavaDoc[] roots;
31   Object JavaDoc[] noChildren = new Object JavaDoc[] {};
32
33   /** contains parent/child relationship, key = parent / Object, value = children / Object[] */
34   Map JavaDoc childrenMap;
35
36   /** contains child/parent relationship, key = child / Object, value = parent / Object */
37   Map JavaDoc parentMap;
38
39   /** support for ChangeOrderModel */
40   boolean enableChangeOrder = true;
41
42   TreeModelChangeSupport changeSupport;
43
44   public MutableTreeModelDecorator(TreeModel decoree) {
45     childrenMap = new HashMap JavaDoc();
46     parentMap = new HashMap JavaDoc();
47     changeSupport = new TreeModelChangeSupport(this);
48     setDecoree(decoree);
49   }
50
51   public MutableTreeModelDecorator(TreeModel decoree, Comparator JavaDoc comp) {
52     childrenMap = new TreeMap JavaDoc(comp);
53     parentMap = new TreeMap JavaDoc(comp);
54     changeSupport = new TreeModelChangeSupport(this);
55     setDecoree(decoree);
56   }
57
58   /**
59    * registers a changeListener with the newDecoree
60    */

61   public void setDecoree(TreeModel newDecoree) {
62     if (decoree != null)
63       decoree.removeTreeModelChangeListener(changeListener);
64     decoree = newDecoree;
65     decoree.addTreeModelChangeListener(changeListener);
66   }
67
68   public TreeModel getDecoree() {
69     return decoree;
70   }
71
72   /**
73    * if the underlying model changes, all changes are discarded
74    */

75   private TreeModelChangeListener changeListener = new TreeModelChangeListener() {
76     public void treeModelChanged(TreeModelChangeEvent event) {
77       childrenMap.clear();
78       parentMap.clear();
79       roots = null;
80       changeSupport.fireModelChanged(event);
81     }
82   };
83
84   public void change(Object JavaDoc parent, Object JavaDoc[] children) {
85     if (children == null)
86       children = noChildren;
87     childrenMap.put(parent, children);
88     for (int i = 0; i < children.length; i++)
89       parentMap.put(children[i], parent);
90     changeSupport.fireModelChanged(false, parent);
91   }
92
93   public Object JavaDoc[] getRoots() {
94     if (roots == null)
95       roots = decoree.getRoots();
96     return roots;
97   }
98
99   public boolean hasChildren(Object JavaDoc node) {
100     Object JavaDoc value = childrenMap.get(node);
101     if (value == null) {
102       // not cached
103
boolean hasChildren = decoree.hasChildren(node);
104       // if no children, don't ask again. If there are children, we wait until
105
// they are really fetched. This prevents us from fetching tons of
106
// children, that are not used by the application.
107
if (hasChildren)
108         return true;
109       childrenMap.put(node, noChildren);
110       return false;
111     }
112     return value != noChildren;
113   }
114
115   public Object JavaDoc[] getChildren(Object JavaDoc node) {
116     Object JavaDoc[] children = (Object JavaDoc[]) childrenMap.get(node);
117     if (children == null) {
118       children = decoree.getChildren(node);
119       change(node, children);
120     }
121     return children;
122   }
123
124   public Object JavaDoc getParent(Object JavaDoc node) {
125     Object JavaDoc parent = parentMap.get(node);
126     if (parent != null)
127       return parent;
128     return decoree.getParent(node);
129   }
130
131   int indexOf(Object JavaDoc obj, Object JavaDoc[] arr) {
132     for (int i = 0; i < arr.length; i++)
133       if (arr[i] == obj)
134         return i;
135     return -1;
136   }
137
138   public boolean mayMove(Object JavaDoc scope, Object JavaDoc node) {
139     return enableChangeOrder;
140   }
141
142   public void move(Object JavaDoc scope, Object JavaDoc node, int oldIndex, int newIndex) {
143     Object JavaDoc parent = getParent(node);
144     Object JavaDoc[] children;
145     if (parent == null)
146       children = getRoots();
147     else
148       children = getChildren(parent);
149     // change order
150
ChangeOrderUtils.move(children, oldIndex, newIndex);
151     // save changes
152
change(parent, children);
153   }
154
155   /**
156    * if enabled, changeOrder will be allowed
157    * @return boolean
158    */

159   public boolean isEnableChangeOrder() {
160     return enableChangeOrder;
161   }
162
163   /**
164    * if enabled, changeOrder will be allowed
165    * @param enableChangeOrder The enableChangeOrder to set
166    */

167   public void setEnableChangeOrder(boolean enableChangeOrder) {
168     this.enableChangeOrder = enableChangeOrder;
169   }
170
171   public void fireModelChanged(boolean identityChanged) {
172     changeSupport.fireModelChanged(identityChanged);
173   }
174
175   public void addTreeModelChangeListener(TreeModelChangeListener l) {
176     changeSupport.addTreeModelChangeListener(l);
177   }
178
179   public void removeTreeModelChangeListener(TreeModelChangeListener l) {
180     changeSupport.removeTreeModelChangeListener(l);
181   }
182
183 }
184
Popular Tags