1 19 20 package edu.umd.cs.findbugs.gui2; 21 22 import java.io.Serializable ; 23 import java.util.HashSet ; 24 import java.lang.RuntimeException ; 25 import javax.swing.tree.TreePath ; 26 27 import edu.umd.cs.findbugs.BugInstance; 28 import edu.umd.cs.findbugs.filter.Matcher; 29 import edu.umd.cs.findbugs.gui2.BugAspects.StringPair; 30 31 36 public class FilterMatcher implements Matcher, Serializable , Comparable <FilterMatcher> 37 { 38 private static final long serialVersionUID = -4859486064351510016L; 39 public static final int FILTER_EXACTLY = 0; 40 public static final int FILTER_AT_OR_AFTER = 1; 41 public static final int FILTER_AT_OR_BEFORE = 2; 42 private Sortables filterBy; 43 private String value; 44 private int mode; 45 protected boolean active; 46 private static HashSet <FilterListener> listeners = new HashSet <FilterListener>(); 47 48 public FilterMatcher(StringPair sp) 49 { 50 this(sp.key, sp.value); 51 } 52 53 Sortables getFilterBy() 54 { 55 return filterBy; 56 } 57 58 String getValue() 59 { 60 return value; 61 } 62 63 public FilterMatcher(Sortables filterBy, String value, int mode) { 65 this.filterBy = filterBy; 66 this.value = value; 67 this.mode = mode; 68 this.active = true; 69 } 70 71 public FilterMatcher(Sortables filterBy, String value) 72 { 73 this.filterBy = filterBy; 74 this.value = value; 75 this.mode = FILTER_EXACTLY; 76 this.active = true; 77 } 78 public void setActive(boolean active) 79 { 80 if (active != this.active) 81 { 82 this.active = active; 83 if (active==true) 84 notifyListeners(FilterListener.FILTERING, null); 85 else 86 notifyListeners(FilterListener.UNFILTERING, null); 87 } 88 } 89 90 public boolean isActive() 91 { 92 return active; 93 } 94 95 public boolean match(BugInstance bugInstance) 96 { 97 if (!active) 98 return true; 99 100 SortableStringComparator ssc = new SortableStringComparator(filterBy); 101 switch(mode) 102 { 103 case FILTER_EXACTLY: return (ssc.compare(filterBy.getFrom(bugInstance), value) != 0); 104 case FILTER_AT_OR_AFTER: return (ssc.compare(filterBy.getFrom(bugInstance), value) == -1); 105 case FILTER_AT_OR_BEFORE: return (ssc.compare(filterBy.getFrom(bugInstance), value) == 1); 106 default: return true; 107 } 108 } 109 110 public String toString() 111 { 112 switch(mode) 113 { 114 case FILTER_EXACTLY: return filterBy.toString() + " " + edu.umd.cs.findbugs.L10N.getLocalString("dlg.is", "is") + " " + edu.umd.cs.findbugs.L10N.getLocalString("mode.equal_to", "equal to") + " " + filterBy.formatValue(value); 115 case FILTER_AT_OR_AFTER: return filterBy.toString() + " " + edu.umd.cs.findbugs.L10N.getLocalString("dlg.is", "is") + " " + edu.umd.cs.findbugs.L10N.getLocalString("mode.at_or_after", "at or after") + " " + filterBy.formatValue(value); 116 case FILTER_AT_OR_BEFORE: return filterBy.toString() + " " + edu.umd.cs.findbugs.L10N.getLocalString("dlg.is", "is") + " " + edu.umd.cs.findbugs.L10N.getLocalString("mode.at_or_before", "at or before") + " " + filterBy.formatValue(value); 117 default: throw new RuntimeException (); 118 } 119 } 120 121 public static boolean addFilterListener(FilterListener newListener) 122 { 123 return listeners.add(newListener); 124 } 125 126 public static void removeFilterListener(FilterListener toRemove) 127 { 128 listeners.remove(toRemove); 129 } 130 131 public static void notifyListeners(int whatsGoingOnCode, TreePath optionalPath) 132 { 133 HashSet <FilterListener> listeners = (HashSet <FilterListener>)FilterMatcher.listeners.clone(); 134 if (whatsGoingOnCode==FilterListener.FILTERING || whatsGoingOnCode== FilterListener.UNFILTERING) 135 for (FilterListener i : listeners) 136 i.clearCache(); 137 else if (whatsGoingOnCode==FilterListener.SUPPRESSING && optionalPath!=null) 138 for (FilterListener i : listeners) 139 i.suppressBug(optionalPath); 140 else if (whatsGoingOnCode==FilterListener.UNSUPPRESSING && optionalPath!=null) 141 for (FilterListener i : listeners) 142 i.unsuppressBug(optionalPath); 143 } 144 145 public boolean equals(Object o) 146 { 147 if (!(o instanceof FilterMatcher)) 148 return false; 149 if (filterBy.equals(((FilterMatcher)o).filterBy) && value.equals(((FilterMatcher)o).value)) 150 return true; 151 return false; 152 } 153 154 public int hashCode() 155 { 156 return value.hashCode() + filterBy.hashCode(); 157 } 158 159 public int compareTo(FilterMatcher that) 160 { 161 if (this.filterBy != that.filterBy) 162 return (this.filterBy.ordinal() < that.filterBy.ordinal() ? -1 : 1); 163 164 return this.value.compareTo(that.value); 165 } 166 } 167 | Popular Tags |