View Javadoc

1   // $Id: FilteredTreeModel.java 140 2009-02-15 14:11:31Z agony $
2   /*
3    * xsAnalyzer - XML schema analyzing tool. Copyright (C) 2008 Michael Engelhardt
4    * 
5    * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
6    * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
7    * version.
8    * 
9    * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
10   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11   * 
12   * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
13   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14   */
15  package de.mindcrimeilab.xsanalyzer.model;
16  
17  import java.util.Observable;
18  import java.util.Observer;
19  
20  import javax.swing.tree.TreeModel;
21  import javax.swing.tree.TreePath;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.springframework.core.closure.Constraint;
26  import org.springframework.util.Assert;
27  
28  import de.mindcrimeilab.swing.util.AbstractTreeModel;
29  
30  /**
31   * @author Michael Engelhardt<me@mindcrime-ilab.de>
32   * @author $Author: agony $
33   * @version $Revision: 140 $
34   * 
35   */
36  public class FilteredTreeModel extends AbstractTreeModel implements TreeModel, Observer {
37  
38      private static final Log logger = LogFactory.getLog("xsAnalyzerApplicationLogger");
39  
40      private Constraint constraint;
41  
42      private final TreeModel defaultModel;
43  
44      public FilteredTreeModel(TreeModel model, Constraint constraint) {
45          defaultModel = model;
46          setConstraint(constraint);
47      }
48  
49      @Override
50      public Object getChild(Object parent, int index) {
51          Object object = null;
52          int i = 0;
53          int tmpIndex = 0;
54          FilteredTreeModel.logger.debug("getChild(Object parent, int index) parent=[" + parent + "], index =[" + index + "]");
55          if (null != constraint) {
56              for (i = 0; i < defaultModel.getChildCount(parent); ++i) {
57                  object = defaultModel.getChild(parent, i);
58                  if (constraint.test(object)) {
59                      if (index == tmpIndex) {
60                          break;
61                      }
62                      ++tmpIndex;
63                  }
64                  object = null;
65              }
66          }
67          else {
68              object = defaultModel.getChild(parent, index);
69          }
70          FilteredTreeModel.logger.debug("returning child [" + object + "] @index[" + index + "] real index [" + i + "]");
71  
72          return object;
73      }
74  
75      @Override
76      public int getChildCount(Object parent) {
77          int count = 0;
78          final int unfilteredChildCount = defaultModel.getChildCount(parent);
79          FilteredTreeModel.logger.debug("getChildCount(Object parent) parent=[" + parent + "] - unfiltered child count [" + unfilteredChildCount + "]");
80          if (null != constraint) {
81              for (int i = 0; i < unfilteredChildCount; ++i) {
82                  final Object child = defaultModel.getChild(parent, i);
83                  if (constraint.test(child)) {
84                      ++count;
85                  }
86              }
87          }
88          else {
89              count = unfilteredChildCount;
90          }
91          FilteredTreeModel.logger.debug("returning filtered count=[" + count + "]");
92          return count;
93      }
94  
95      @Override
96      public int getIndexOfChild(Object parent, Object child) {
97          int index = 0;
98          FilteredTreeModel.logger.debug("getIndexOfChild(Object parent, Object child) parent=[" + parent + "], child =[" + child + "]");
99          if (null != constraint) {
100             int tmpIndex = 0;
101             for (int i = 0; i < defaultModel.getChildCount(parent); ++i) {
102                 Object tmp = defaultModel.getChild(parent, i);
103                 if (constraint.test(tmp)) {
104                     ++tmpIndex;
105                     if (tmp.equals(child)) {
106                         index = tmpIndex;
107                         break;
108                     }
109                 }
110             }
111         }
112         else {
113             index = defaultModel.getIndexOfChild(parent, child);
114         }
115         FilteredTreeModel.logger.debug("Returning index =[" + index + "]");
116         return index;
117     }
118 
119     @Override
120     public Object getRoot() {
121         return defaultModel.getRoot();
122     }
123 
124     @Override
125     public boolean isLeaf(Object node) {
126         return getChildCount(node) == 0;
127     }
128 
129     /**
130      * @return the constraint
131      */
132     public Constraint getConstraint() {
133         return constraint;
134     }
135 
136     /**
137      * @param constraint
138      *            the constraint to set
139      */
140     public final void setConstraint(Constraint constraint) {
141         Assert.notNull(constraint);
142         if (!constraint.equals(this.constraint)) {
143             if (this.constraint instanceof Observable) {
144                 ((Observable) constraint).deleteObserver(this);
145             }
146             this.constraint = constraint;
147             if (constraint instanceof Observable) {
148                 ((Observable) constraint).addObserver(this);
149             }
150             fireTreeStructureChanged(this, new TreePath[] { new TreePath(getRoot())}, null, null);
151         }
152     }
153 
154     @Override
155     public void update(Observable o, Object arg) {
156         fireTreeStructureChanged(this, new TreePath[] { new TreePath(getRoot())}, null, null);
157     }
158 }