1 19 20 package org.netbeans.modules.tasklist.core.filter; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 import java.util.Vector ; 30 import org.openide.util.NbBundle; 31 32 33 38 public abstract class Filter { 39 43 private boolean allTrue = false; 44 45 46 private List appliedConditions = null; 47 48 49 private String name = null; 50 51 52 private boolean flattened; 53 54 private PropertyChangeSupport pcs = null; 55 protected PropertyChangeSupport getPCS() { 56 if (pcs == null) pcs = new PropertyChangeSupport (this); 57 return pcs; 58 } 59 protected boolean hasListeners() { return pcs != null;} 60 61 private static final String NO_FILTER = NbBundle.getMessage(Filter.class, "no-filter"); 62 63 public static final String PROP_NAME = "PropName"; 64 public static final String PROP_ALLTRUE = "PropAllTrue"; 65 public static final String PROP_CONDITIONS = "PropConditions"; 66 public static final String PROP_FLATTENED = "PropFlattened"; 67 68 69 public void addPropertyChangeListener(PropertyChangeListener listener) { 70 getPCS().addPropertyChangeListener(listener); 71 } 72 73 public void removePropertyChangeListener(PropertyChangeListener listener) { 74 getPCS().removePropertyChangeListener(listener); 75 } 76 77 public void addPropertyChangeListener(String property, PropertyChangeListener listener) { 78 getPCS().addPropertyChangeListener(property, listener); 79 } 80 81 public void removePropertyChangeListener(String property, PropertyChangeListener listener) { 82 getPCS().removePropertyChangeListener(property, listener); 83 } 84 89 public Filter(String name) { 90 this(name, true, new ArrayList (), false); 91 } 92 93 104 public Filter(String name, boolean allTrue, List conditions, boolean flattened) { 105 this.name = name; 106 this.allTrue = allTrue; 107 this.appliedConditions = conditions; 108 this.flattened = flattened; 109 } 110 111 114 protected Filter(final Filter rhs) { 115 this(rhs.name, rhs.allTrue, cloneConditions(rhs.appliedConditions), rhs.flattened); 116 } 117 118 119 120 protected Filter() {this.name = null; this.appliedConditions = null; }; 121 122 123 private static List cloneConditions(List conditions) { 124 LinkedList l = new LinkedList (); 125 Iterator it = conditions.iterator(); 126 while (it.hasNext()) { 127 l.add(((AppliedFilterCondition)it.next()).clone()); 128 } 129 130 return l; 131 } 132 133 public abstract Object clone(); 134 135 141 public abstract AppliedFilterCondition[] createConditions(SuggestionProperty property); 142 143 151 public abstract SuggestionProperty[] getProperties(); 152 153 154 157 public void clear() { 158 Vector oldc = null; 159 if (hasListeners()) oldc = new Vector (this.appliedConditions); 160 getConditions().clear(); 161 if (hasListeners()) fireChange(PROP_CONDITIONS, oldc, Collections.EMPTY_LIST);; 162 } 163 164 171 public boolean isFlattened() { 172 return flattened; 173 } 174 175 181 public void setFlattened(boolean f) { 182 if (f != this.flattened) { 183 this.flattened = f; 184 fireChange(PROP_FLATTENED, Boolean.valueOf(!f), Boolean.valueOf(f)); 185 } 186 } 187 188 193 public boolean accept(Object node) { 194 if (!hasConstraints()) { 195 return true; } 197 Iterator it = appliedConditions.iterator(); 198 boolean b = true; 199 while (it.hasNext()) { 200 AppliedFilterCondition acond = (AppliedFilterCondition)it.next(); 201 b = acond.isTrue(node); 202 if (b && !allTrue) { 203 return true; 204 } else if (!b && allTrue) { 205 return false; 206 } 207 } 208 return b; 209 } 210 211 216 public boolean matchAll() { 217 return allTrue; 218 } 219 220 224 public boolean hasConstraints() { 225 return (appliedConditions != null) && (appliedConditions.size() > 0); 226 } 227 228 233 public void setMatchAll(boolean b) { 234 if (this.allTrue != b) { 235 this.allTrue = b; 236 fireChange(PROP_ALLTRUE, Boolean.valueOf(!b), Boolean.valueOf(b)); 237 } 238 } 239 240 245 public final List getConditions() { 246 return appliedConditions; 247 } 248 249 254 public final void setConditions(List conditions) { 255 Vector oldc = null; 256 if (hasListeners()) oldc = new Vector (conditions); 257 this.appliedConditions = conditions; 258 if (hasListeners()) fireChange(PROP_CONDITIONS, oldc, this.appliedConditions); 259 } 260 261 264 private void fireChange(String property, Object oldv, Object newv) { 265 if (pcs!=null) 266 getPCS().firePropertyChange(property, oldv, newv); 267 } 268 269 273 public String getName() { 274 return name; 275 } 276 277 281 public void setName(String name) { 282 String oldname = this.name; 283 this.name = name; 284 fireChange(PROP_NAME, oldname, name); 285 } 286 287 294 public String toString() { 295 Iterator it = appliedConditions.iterator(); 296 StringBuffer sb = new StringBuffer (); 297 sb.append(getClass().getName() + "[name=" + name + ", "); sb.append(allTrue ? 299 "ALL of the following conditions" : "ANY of the following conditions"); sb.append(", "); while (it.hasNext()) { 303 sb.append(it.next()); 304 sb.append(", "); } 306 sb.append("]"); return sb.toString(); 308 } 309 310 313 public static class ListModelElement { 314 315 public Filter filter; 316 public ListModelElement(Filter f) { 317 filter = f; 318 } 319 320 public String toString() { return (filter != null) ? filter.getName() : NO_FILTER;} 321 322 public int hashCode() { 323 return (filter != null) ? (filter.hashCode()) : 0; 324 } 325 326 public boolean equals(Object rhs) { 327 if (rhs instanceof ListModelElement) rhs = ((ListModelElement)rhs).filter; 328 return this.filter == rhs; 329 } 330 } 331 332 333 343 protected static AppliedFilterCondition [] applyConditions(SuggestionProperty property, FilterCondition [] conds) { 344 if (conds == null) return null; 345 else { 346 AppliedFilterCondition [] applied = new AppliedFilterCondition[conds.length]; 347 for (int i = 0; i < conds.length; i++) { 348 applied[i] = new AppliedFilterCondition(property, conds[i]); 349 } 350 return applied; 351 } 352 } 353 354 } 355 | Popular Tags |