KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.log4j.Logger;
21
22 /**
23  * a caching tree model
24  * @author av
25  */

26 public class CachingTreeModelDecorator extends TreeModelDecorator {
27   Object JavaDoc[] roots = null;
28   Map JavaDoc getParentMap;
29   Map JavaDoc getChildrenMap;
30   Map JavaDoc hasChildrenMap;
31   
32   private static Logger logger = Logger.getLogger(CachingTreeModelDecorator.class);
33   /**
34    * creates a CachingTreeModel that uses HashMap for implementation
35    */

36   public CachingTreeModelDecorator(TreeModel decoree) {
37     super(decoree);
38     getParentMap = new HashMap JavaDoc();
39     getChildrenMap = new HashMap JavaDoc();
40     hasChildrenMap = new HashMap JavaDoc();
41     decoree.addTreeModelChangeListener(listener);
42   }
43
44   /**
45    * creates a CachingTreeModel that uses TreeMap for implementation
46    */

47   public CachingTreeModelDecorator(TreeModel decoree, Comparator JavaDoc nodeComparator) {
48     super(decoree);
49     getParentMap = new TreeMap JavaDoc(nodeComparator);
50     getChildrenMap = new TreeMap JavaDoc(nodeComparator);
51     hasChildrenMap = new TreeMap JavaDoc(nodeComparator);
52     decoree.addTreeModelChangeListener(listener);
53   }
54
55   /**
56    * invalidates the cache
57    */

58   TreeModelChangeListener listener = new TreeModelChangeListener() {
59     public void treeModelChanged(TreeModelChangeEvent e) {
60       logger.info("invalidating TreeModelCache");
61       roots = null;
62       getParentMap.clear();
63       getChildrenMap.clear();
64       hasChildrenMap.clear();
65     }
66   };
67
68   public Object JavaDoc[] getRoots() {
69     if (roots == null)
70       roots = super.getRoots();
71     return roots;
72   }
73
74   public boolean hasChildren(Object JavaDoc node) {
75     Boolean JavaDoc b = (Boolean JavaDoc) hasChildrenMap.get(node);
76     if (b == null) {
77       b = new Boolean JavaDoc(super.hasChildren(node));
78       hasChildrenMap.put(node, b);
79     }
80     return b.booleanValue();
81   }
82
83   public Object JavaDoc[] getChildren(Object JavaDoc node) {
84     Object JavaDoc[] children = (Object JavaDoc[]) getChildrenMap.get(node);
85     if (children == null) {
86       children = super.getChildren(node);
87       if (children == null)
88         children = new Object JavaDoc[0];
89       getChildrenMap.put(node, children);
90       for (int i = 0; i < children.length; i++)
91         getParentMap.put(children[i], node);
92     }
93     return children;
94   }
95
96   public Object JavaDoc getParent(Object JavaDoc node) {
97     Object JavaDoc parent = getParentMap.get(node);
98     if (parent == null && !getParentMap.containsKey(node)) {
99       parent = super.getParent(node);
100       getParentMap.put(node, parent);
101     }
102     return parent;
103   }
104
105 }
106
Popular Tags