KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
16  * If the root of the tree consists of a single node and all the user
17  * can do with that node is expand it, then the root is replaced
18  * with its contents.
19  *
20  * @author av
21  */

22
23 public class SimpleOptimizingTreeModelDecorator extends TreeModelDecorator {
24   NodeFilter filter;
25
26   Object JavaDoc[] roots;
27   boolean dirty = true;
28
29   /**
30    * creates a reduced view on a tree model that uses a HashMap for implementation
31    * @param treeModel the tree model to decorate
32    * @param filter accepted nodes are considered "significant"
33    */

34   public SimpleOptimizingTreeModelDecorator(NodeFilter filter, TreeModel decoree) {
35     super(decoree);
36     this.filter = filter;
37     decoree.addTreeModelChangeListener(new TreeModelChangeListener() {
38       public void treeModelChanged(TreeModelChangeEvent event) {
39         dirty = true;
40       }
41     });
42   }
43
44   public Object JavaDoc[] getRoots() {
45     if (dirty)
46       initialize();
47     return roots;
48   }
49
50   public Object JavaDoc getParent(Object JavaDoc node) {
51     if (dirty)
52       initialize();
53
54     // its our "artificial" root, then return null
55
for (int i = 0; i < roots.length; i++)
56       if (roots[i].equals(node))
57         return null;
58     // return real parent
59
return super.getParent(node);
60   }
61
62   void initialize() {
63     dirty = false;
64     roots = super.getRoots();
65     while (roots.length == 1 && !filter.accept(roots[0]))
66       roots = getChildren(roots[0]);
67   }
68
69 }
70
Popular Tags