1 11 package org.eclipse.jdt.internal.ui.search; 12 13 import java.util.StringTokenizer ; 14 15 import org.eclipse.jdt.core.IField; 16 import org.eclipse.jdt.core.IImportDeclaration; 17 import org.eclipse.jdt.core.IMethod; 18 import org.eclipse.jdt.core.IType; 19 import org.eclipse.jdt.core.ITypeParameter; 20 import org.eclipse.jdt.core.JavaModelException; 21 import org.eclipse.jdt.core.search.IJavaSearchConstants; 22 import org.eclipse.jdt.core.search.SearchMatch; 23 import org.eclipse.jdt.core.search.SearchPattern; 24 25 import org.eclipse.jdt.ui.search.ElementQuerySpecification; 26 import org.eclipse.jdt.ui.search.PatternQuerySpecification; 27 import org.eclipse.jdt.ui.search.QuerySpecification; 28 29 import org.eclipse.jdt.internal.ui.JavaPlugin; 30 31 abstract class MatchFilter { 32 33 private static final String SETTINGS_LAST_USED_FILTERS= "filters_last_used"; 35 public static MatchFilter[] getLastUsedFilters() { 36 String string= JavaPlugin.getDefault().getDialogSettings().get(SETTINGS_LAST_USED_FILTERS); 37 if (string != null && string.length() > 0) { 38 return decodeFiltersString(string); 39 } 40 return getDefaultFilters(); 41 } 42 43 public static void setLastUsedFilters(MatchFilter[] filters) { 44 String encoded= encodeFilters(filters); 45 JavaPlugin.getDefault().getDialogSettings().put(SETTINGS_LAST_USED_FILTERS, encoded); 46 } 47 48 public static MatchFilter[] getDefaultFilters() { 49 return new MatchFilter[] { IMPORT_FILTER }; 50 } 51 52 private static String encodeFilters(MatchFilter[] enabledFilters) { 53 StringBuffer buf= new StringBuffer (); 54 buf.append(enabledFilters.length); 55 for (int i= 0; i < enabledFilters.length; i++) { 56 buf.append(';'); 57 buf.append(enabledFilters[i].getID()); 58 } 59 return buf.toString(); 60 } 61 62 private static MatchFilter[] decodeFiltersString(String encodedString) { 63 StringTokenizer tokenizer= new StringTokenizer (encodedString, String.valueOf(';')); 64 int count= Integer.valueOf(tokenizer.nextToken()).intValue(); 65 MatchFilter[] res= new MatchFilter[count]; 66 for (int i= 0; i < count; i++) { 67 res[i]= findMatchFilter(tokenizer.nextToken()); 68 } 69 return res; 70 } 71 72 73 public abstract boolean isApplicable(JavaSearchQuery query); 74 75 public abstract boolean filters(JavaElementMatch match); 76 77 public abstract String getName(); 78 public abstract String getActionLabel(); 79 80 public abstract String getDescription(); 81 82 public abstract String getID(); 83 84 private static final MatchFilter POTENTIAL_FILTER= new PotentialFilter(); 85 private static final MatchFilter IMPORT_FILTER= new ImportFilter(); 86 private static final MatchFilter JAVADOC_FILTER= new JavadocFilter(); 87 private static final MatchFilter READ_FILTER= new ReadFilter(); 88 private static final MatchFilter WRITE_FILTER= new WriteFilter(); 89 private static final MatchFilter INEXACT_FILTER= new InexactMatchFilter(); 90 private static final MatchFilter ERASURE_FILTER= new ErasureMatchFilter(); 91 92 private static final MatchFilter[] ALL_FILTERS= new MatchFilter[] { 93 POTENTIAL_FILTER, 94 IMPORT_FILTER, 95 JAVADOC_FILTER, 96 READ_FILTER, 97 WRITE_FILTER, 98 INEXACT_FILTER, 99 ERASURE_FILTER 100 }; 101 102 public static MatchFilter[] allFilters() { 103 return ALL_FILTERS; 104 } 105 106 private static MatchFilter findMatchFilter(String id) { 107 for (int i= 0; i < ALL_FILTERS.length; i++) { 108 if (ALL_FILTERS[i].getID().equals(id)) 109 return ALL_FILTERS[i]; 110 } 111 return IMPORT_FILTER; } 113 114 115 } 116 117 class PotentialFilter extends MatchFilter { 118 public boolean filters(JavaElementMatch match) { 119 return match.getAccuracy() == SearchMatch.A_INACCURATE; 120 } 121 122 public String getName() { 123 return SearchMessages.MatchFilter_PotentialFilter_name; 124 } 125 126 public String getActionLabel() { 127 return SearchMessages.MatchFilter_PotentialFilter_actionLabel; 128 } 129 130 public String getDescription() { 131 return SearchMessages.MatchFilter_PotentialFilter_description; 132 } 133 134 public boolean isApplicable(JavaSearchQuery query) { 135 return true; 136 } 137 138 public String getID() { 139 return "filter_potential"; } 141 } 142 143 class ImportFilter extends MatchFilter { 144 public boolean filters(JavaElementMatch match) { 145 return match.getElement() instanceof IImportDeclaration; 146 } 147 148 public String getName() { 149 return SearchMessages.MatchFilter_ImportFilter_name; 150 } 151 152 public String getActionLabel() { 153 return SearchMessages.MatchFilter_ImportFilter_actionLabel; 154 } 155 156 public String getDescription() { 157 return SearchMessages.MatchFilter_ImportFilter_description; 158 } 159 160 public boolean isApplicable(JavaSearchQuery query) { 161 QuerySpecification spec= query.getSpecification(); 162 if (spec instanceof ElementQuerySpecification) { 163 ElementQuerySpecification elementSpec= (ElementQuerySpecification) spec; 164 return elementSpec.getElement() instanceof IType; 165 } else if (spec instanceof PatternQuerySpecification) { 166 PatternQuerySpecification patternSpec= (PatternQuerySpecification) spec; 167 return patternSpec.getSearchFor() == IJavaSearchConstants.TYPE; 168 } 169 return false; 170 } 171 172 public String getID() { 173 return "filter_imports"; } 175 } 176 177 abstract class FieldFilter extends MatchFilter { 178 public boolean isApplicable(JavaSearchQuery query) { 179 QuerySpecification spec= query.getSpecification(); 180 if (spec instanceof ElementQuerySpecification) { 181 ElementQuerySpecification elementSpec= (ElementQuerySpecification) spec; 182 return elementSpec.getElement() instanceof IField; 183 } else if (spec instanceof PatternQuerySpecification) { 184 PatternQuerySpecification patternSpec= (PatternQuerySpecification) spec; 185 return patternSpec.getSearchFor() == IJavaSearchConstants.FIELD; 186 } 187 return false; 188 } 189 190 } 191 192 class WriteFilter extends FieldFilter { 193 public boolean filters(JavaElementMatch match) { 194 return match.isWriteAccess() && !match.isReadAccess(); 195 } 196 public String getName() { 197 return SearchMessages.MatchFilter_WriteFilter_name; 198 } 199 public String getActionLabel() { 200 return SearchMessages.MatchFilter_WriteFilter_actionLabel; 201 } 202 public String getDescription() { 203 return SearchMessages.MatchFilter_WriteFilter_description; 204 } 205 public String getID() { 206 return "filter_writes"; } 208 } 209 210 class ReadFilter extends FieldFilter { 211 public boolean filters(JavaElementMatch match) { 212 return match.isReadAccess() && !match.isWriteAccess(); 213 } 214 public String getName() { 215 return SearchMessages.MatchFilter_ReadFilter_name; 216 } 217 public String getActionLabel() { 218 return SearchMessages.MatchFilter_ReadFilter_actionLabel; 219 } 220 public String getDescription() { 221 return SearchMessages.MatchFilter_ReadFilter_description; 222 } 223 public String getID() { 224 return "filter_reads"; } 226 } 227 228 class JavadocFilter extends MatchFilter { 229 public boolean filters(JavaElementMatch match) { 230 return match.isJavadoc(); 231 } 232 public String getName() { 233 return SearchMessages.MatchFilter_JavadocFilter_name; 234 } 235 public String getActionLabel() { 236 return SearchMessages.MatchFilter_JavadocFilter_actionLabel; 237 } 238 public String getDescription() { 239 return SearchMessages.MatchFilter_JavadocFilter_description; 240 } 241 public boolean isApplicable(JavaSearchQuery query) { 242 return true; 243 } 244 public String getID() { 245 return "filter_javadoc"; } 247 } 248 249 abstract class GenericTypeFilter extends MatchFilter { 250 public boolean isApplicable(JavaSearchQuery query) { 251 QuerySpecification spec= query.getSpecification(); 252 if (spec instanceof ElementQuerySpecification) { 253 ElementQuerySpecification elementSpec= (ElementQuerySpecification) spec; 254 Object element= elementSpec.getElement(); 255 ITypeParameter[] typeParameters= null; 256 try { 257 if (element instanceof IType) { 258 typeParameters= ((IType)element).getTypeParameters(); 259 } else if (element instanceof IMethod) { 260 typeParameters= ((IMethod)element).getTypeParameters(); 261 } 262 } catch (JavaModelException e) { 263 return false; 264 } 265 return typeParameters != null && typeParameters.length > 0; 266 } 267 return false; 268 } 269 } 270 271 class ErasureMatchFilter extends GenericTypeFilter { 272 public boolean filters(JavaElementMatch match) { 273 return (match.getMatchRule() & (SearchPattern.R_FULL_MATCH | SearchPattern.R_EQUIVALENT_MATCH)) == 0; 274 } 275 public String getName() { 276 return SearchMessages.MatchFilter_ErasureFilter_name; 277 } 278 public String getActionLabel() { 279 return SearchMessages.MatchFilter_ErasureFilter_actionLabel; 280 } 281 public String getDescription() { 282 return SearchMessages.MatchFilter_ErasureFilter_description; 283 } 284 public String getID() { 285 return "filter_erasure"; } 287 } 288 289 class InexactMatchFilter extends GenericTypeFilter { 290 public boolean filters(JavaElementMatch match) { 291 return (match.getMatchRule() & (SearchPattern.R_FULL_MATCH)) == 0; 292 } 293 public String getName() { 294 return SearchMessages.MatchFilter_InexactFilter_name; 295 } 296 public String getActionLabel() { 297 return SearchMessages.MatchFilter_InexactFilter_actionLabel; 298 } 299 public String getDescription() { 300 return SearchMessages.MatchFilter_InexactFilter_description; 301 } 302 public String getID() { 303 return "filter_inexact"; } 305 } 306 307 | Popular Tags |