001: /*
002: * FindBugs - Find Bugs in Java programs
003: * Copyright (C) 2006, University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
018: */
019:
020: package edu.umd.cs.findbugs.gui2;
021:
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.List;
025:
026: import javax.swing.event.TreeModelEvent;
027:
028: import edu.umd.cs.findbugs.BugInstance;
029: import edu.umd.cs.findbugs.gui2.BugTreeModel.BranchOperationException;
030:
031: /**
032: * Filter out bugs which fail (match) all filters.
033: * This is what happens when you filter out a branch.
034: */
035: @Deprecated
036: public class StackedFilterMatcher extends FilterMatcher {
037: private static final long serialVersionUID = 3958267780332359162L;
038: private FilterMatcher[] filters;
039:
040: @Override
041: Sortables getFilterBy() {
042: throw new UnsupportedOperationException(
043: "Stacked filter matchers do not filter out a single Sortables, use getFilters()");
044: }
045:
046: @Override
047: String getValue() {
048: throw new UnsupportedOperationException(
049: "Stacked filter matchers do not filter out a single Sortables's value, use getFilters and getValue individually on returned filters.");
050: }
051:
052: public StackedFilterMatcher(FilterMatcher... filters) {
053: super (null, null);
054: this .filters = filters;
055: }
056:
057: //If only FilterMatcher's setActive were as simple as this one... not.
058: //See BugTreeModel's long ranting comment about filtering to see the reason for all this
059: //All this does is not force the tree to rebuild when you turn filters for branches on and off
060: @Override
061: public void setActive(boolean active) {
062: TreeModelEvent event = null;
063: BugTreeModel.TreeModification whatToDo;
064:
065: if (active != this .active) {
066: if (active == false)
067: this .active = active;
068:
069: StackedFilterMatcher theSame = this ;
070: FilterMatcher[] filtersInStack = theSame.getFilters();
071: ArrayList<Sortables> order = MainFrame.getInstance()
072: .getSorter().getOrder();
073: int sizeToCheck = filtersInStack.length;
074: if (order.contains(Sortables.DIVIDER))
075: if (order.indexOf(Sortables.DIVIDER) < filtersInStack.length) {
076: sizeToCheck++;
077: }
078: List<Sortables> sortablesToCheck = order.subList(0, Math
079: .min(sizeToCheck, order.size()));
080: Debug.println("Size to check" + sizeToCheck
081: + " checking list" + sortablesToCheck);
082: Debug.println("checking filters");
083: ArrayList<String> almostPath = new ArrayList<String>();
084: ArrayList<Sortables> almostPathSortables = new ArrayList<Sortables>();
085: for (int x = 0; x < sortablesToCheck.size(); x++) {
086: Sortables s = sortablesToCheck.get(x);
087: for (FilterMatcher fm : filtersInStack) {
088: if (s.equals(fm.getFilterBy())) {
089: almostPath.add(fm.getValue());
090: almostPathSortables.add(fm.getFilterBy());
091: }
092: }
093: }
094: ArrayList<String> finalPath = new ArrayList<String>();
095: for (int x = 0; x < almostPath.size(); x++) {
096: Sortables s = almostPathSortables.get(x);
097: if (MainFrame.getInstance().getSorter()
098: .getOrderBeforeDivider().contains(s))
099: finalPath.add(almostPath.get(x));
100: }
101: try {
102: if (finalPath.size() == filtersInStack.length) {
103: if (active == true) {
104: event = (MainFrame.getInstance()
105: .getBugTreeModel())
106: .removeBranch(finalPath);
107: whatToDo = BugTreeModel.TreeModification.REMOVE;
108: } else {
109: event = (MainFrame.getInstance()
110: .getBugTreeModel())
111: .insertBranch(finalPath);
112: whatToDo = BugTreeModel.TreeModification.INSERT;
113: }
114: } else {
115: event = (MainFrame.getInstance().getBugTreeModel())
116: .restructureBranch(finalPath, active);//if active is true, this removes, if active if false, it inserts
117: if (active)
118: whatToDo = BugTreeModel.TreeModification.REMOVERESTRUCTURE;
119: else
120: whatToDo = BugTreeModel.TreeModification.INSERTRESTRUCTURE;
121: }
122:
123: if (active == true)
124: this .active = active;
125: (MainFrame.getInstance().getBugTreeModel()).sendEvent(
126: event, whatToDo);
127: } catch (BranchOperationException e) {
128: //Another filter already filters out the branch this filter would filter out, set active, but dont send any tree model events.
129: this .active = active;
130: }
131: }
132:
133: }
134:
135: public FilterMatcher[] getFilters() {
136: return filters;
137: }
138:
139: @Override
140: public boolean match(BugInstance bugInstance) {
141: if (!isActive())
142: return true;
143:
144: for (FilterMatcher i : filters)
145: if (i.match(bugInstance))
146: return true;
147:
148: return false;
149: }
150:
151: @Override
152: public String toString() {
153: //return "StackedFilterMatcher: " + Arrays.toString(filters);
154: StringBuilder result = new StringBuilder();
155: for (int i = 0; i < filters.length - 1; i++)
156: result.append(filters[i].toString()
157: + (i == filters.length - 2 ? " " : ", "));
158: if (filters.length > 1)
159: result.append("and ");
160: if (filters.length > 0)
161: result.append(filters[filters.length - 1]);
162: return result.toString();
163: }
164:
165: @Override
166: public boolean equals(Object o) {
167: if (o == null || !(o instanceof StackedFilterMatcher))
168: return false;
169:
170: FilterMatcher[] mine = new FilterMatcher[filters.length];
171: System.arraycopy(this .filters, 0, mine, 0, mine.length);
172: Arrays.sort(mine);
173:
174: FilterMatcher[] others = new FilterMatcher[((StackedFilterMatcher) o).filters.length];
175: System.arraycopy(((StackedFilterMatcher) o).filters, 0, others,
176: 0, others.length);
177: Arrays.sort(others);
178:
179: return (Arrays.equals(mine, others));
180: }
181:
182: @Override
183: public int hashCode() {
184: return filters.hashCode();
185: }
186:
187: public static void main(String[] args) {
188: System.out.println(new StackedFilterMatcher(
189: new FilterMatcher[0]).equals(new StackedFilterMatcher(
190: new FilterMatcher[0])));
191: }
192: }
|