KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > gui2 > StackedFilterMatcher


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
18  */

19
20 package edu.umd.cs.findbugs.gui2;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.swing.event.TreeModelEvent JavaDoc;
27
28 import edu.umd.cs.findbugs.BugInstance;
29 import edu.umd.cs.findbugs.filter.Matcher;
30 import edu.umd.cs.findbugs.gui2.BugTreeModel.BranchOperationException;
31
32 /**
33  * Filter out bugs which fail (match) all filters.
34  * This is what happens when you filter out a branch.
35  */

36 public class StackedFilterMatcher extends FilterMatcher
37 {
38     private static final long serialVersionUID = 3958267780332359162L;
39     private FilterMatcher[] filters;
40     
41     Sortables getFilterBy()
42     {
43         throw new UnsupportedOperationException JavaDoc("Stacked filter matchers do not filter out a single Sortables, use getFilters()");
44     }
45     
46     String JavaDoc getValue()
47     {
48         throw new UnsupportedOperationException JavaDoc("Stacked filter matchers do not filter out a single Sortables's value, use getFilters and getValue individually on returned filters.");
49     }
50     
51     public StackedFilterMatcher(FilterMatcher... filters)
52     {
53         super(null, null);
54         this.filters = filters;
55     }
56     
57     //If only FilterMatcher's setActive were as simple as this one... not.
58
//See BugTreeModel's long ranting comment about filtering to see the reason for all this
59
//All this does is not force the tree to rebuild when you turn filters for branches on and off
60
public void setActive(boolean active)
61     {
62         TreeModelEvent JavaDoc event=null;
63         int whatToDo=-1;
64         final int REMOVE=0;
65         final int INSERT=1;
66         final int RESTRUCTURE=2;
67
68         if (active != this.active)
69         {
70             if (active==false)
71                 this.active=active;
72
73             StackedFilterMatcher theSame= this;
74             FilterMatcher[] filtersInStack=theSame.getFilters();
75             ArrayList JavaDoc<Sortables> order=MainFrame.getInstance().getSorter().getOrder();
76             int sizeToCheck=filtersInStack.length;
77             if (order.contains(Sortables.DIVIDER))
78                 if (order.indexOf(Sortables.DIVIDER) < filtersInStack.length)
79                 {
80                     sizeToCheck++;
81                 }
82             List JavaDoc<Sortables> sortablesToCheck=order.subList(0, Math.min(sizeToCheck, order.size()));
83             Debug.println("Size to check" + sizeToCheck + " checking list" + sortablesToCheck);
84             Debug.println("checking filters");
85             ArrayList JavaDoc<String JavaDoc> almostPath=new ArrayList JavaDoc<String JavaDoc>();
86             ArrayList JavaDoc<Sortables> almostPathSortables=new ArrayList JavaDoc<Sortables>();
87             for (int x=0; x< sortablesToCheck.size();x++)
88             {
89                 Sortables s=sortablesToCheck.get(x);
90                 for (FilterMatcher fm:filtersInStack)
91                 {
92                     if (s.equals(fm.getFilterBy()))
93                     {
94                         almostPath.add(fm.getValue());
95                         almostPathSortables.add(fm.getFilterBy());
96                     }
97                 }
98             }
99             ArrayList JavaDoc<String JavaDoc> finalPath=new ArrayList JavaDoc<String JavaDoc>();
100             for (int x=0;x<almostPath.size();x++)
101             {
102                 Sortables s=almostPathSortables.get(x);
103                 if (MainFrame.getInstance().getSorter().getOrderBeforeDivider().contains(s))
104                     finalPath.add(almostPath.get(x));
105             }
106             try {
107                 if (finalPath.size()==filtersInStack.length)
108                 {
109                     if (active==true)
110                     {
111                         event=((BugTreeModel)(MainFrame.getInstance().getTree().getModel())).removeBranch(finalPath);
112                         whatToDo=REMOVE;
113                     }
114                     else
115                     {
116                         event=((BugTreeModel)(MainFrame.getInstance().getTree().getModel())).insertBranch(finalPath);
117                         whatToDo=INSERT;
118                     }
119                 }
120                 else
121                 {
122                     event=((BugTreeModel)(MainFrame.getInstance().getTree().getModel())).restructureBranch(finalPath,active);//if active is true, this removes, if active if false, it inserts
123
whatToDo=RESTRUCTURE;
124                 }
125             
126             if (active==true)
127                 this.active=active;
128             ((BugTreeModel)(MainFrame.getInstance().getTree().getModel())).sendEvent(event,whatToDo);
129             }
130             catch (BranchOperationException e)
131             {
132                 //Another filter already filters out the branch this filter would filter out, set active, but dont send any tree model events.
133
this.active=active;
134             }
135         }
136         
137     }
138
139     public FilterMatcher[] getFilters()
140     {
141         return filters;
142     }
143     
144     public boolean match(BugInstance bugInstance)
145     {
146         if (!isActive())
147             return true;
148         
149         for (FilterMatcher i : filters)
150             if (i.match(bugInstance))
151                 return true;
152         
153         return false;
154     }
155
156     @Override JavaDoc
157     public String JavaDoc toString()
158     {
159         //return "StackedFilterMatcher: " + Arrays.toString(filters);
160
StringBuilder JavaDoc result = new StringBuilder JavaDoc();
161         for (int i = 0; i < filters.length - 1; i++)
162             result.append(filters[i].toString() + (i == filters.length - 2 ? " " : ", "));
163         if (filters.length > 1)
164             result.append("and ");
165         if (filters.length > 0)
166             result.append(filters[filters.length - 1]);
167         return result.toString();
168     }
169     
170     @Override JavaDoc
171     public boolean equals(Object JavaDoc o)
172     {
173         if (o == null || !(o instanceof StackedFilterMatcher))
174             return false;
175         
176         FilterMatcher[] mine = new FilterMatcher[filters.length];
177         System.arraycopy(this.filters, 0, mine, 0, mine.length);
178         Arrays.sort(mine);
179         
180         FilterMatcher[] others = new FilterMatcher[((StackedFilterMatcher)o).filters.length];
181         System.arraycopy(((StackedFilterMatcher)o).filters, 0, others, 0, others.length);
182         Arrays.sort(others);
183
184         return (Arrays.equals(mine, others));
185     }
186     
187     @Override JavaDoc
188     public int hashCode()
189     {
190         return filters.hashCode();
191     }
192     
193     public static void main(String JavaDoc[] args)
194     {
195         System.out.println(new StackedFilterMatcher(new FilterMatcher[0]).equals(new StackedFilterMatcher(new FilterMatcher[0])));
196     }
197 }
198
Popular Tags