1 19 20 package edu.umd.cs.findbugs.gui2; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.List ; 25 26 import javax.swing.event.TreeModelEvent ; 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 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 ("Stacked filter matchers do not filter out a single Sortables, use getFilters()"); 44 } 45 46 String getValue() 47 { 48 throw new UnsupportedOperationException ("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 public void setActive(boolean active) 61 { 62 TreeModelEvent 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 <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 <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 <String > almostPath=new ArrayList <String >(); 86 ArrayList <Sortables> almostPathSortables=new ArrayList <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 <String > finalPath=new ArrayList <String >(); 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); 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 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 157 public String toString() 158 { 159 StringBuilder result = new StringBuilder (); 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 171 public boolean equals(Object 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 188 public int hashCode() 189 { 190 return filters.hashCode(); 191 } 192 193 public static void main(String [] args) 194 { 195 System.out.println(new StackedFilterMatcher(new FilterMatcher[0]).equals(new StackedFilterMatcher(new FilterMatcher[0]))); 196 } 197 } 198 | Popular Tags |