/******************************************************************************* * Copyright (c) 2000, 2006 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.rubypeople.rdt.internal.ui.search; import java.util.StringTokenizer; import org.rubypeople.rdt.core.IField; import org.rubypeople.rdt.core.IImportDeclaration; import org.rubypeople.rdt.core.IType; import org.rubypeople.rdt.core.search.IRubySearchConstants; import org.rubypeople.rdt.core.search.SearchMatch; import org.rubypeople.rdt.internal.ui.RubyPlugin; import org.rubypeople.rdt.ui.search.ElementQuerySpecification; import org.rubypeople.rdt.ui.search.PatternQuerySpecification; import org.rubypeople.rdt.ui.search.QuerySpecification; abstract class MatchFilter { private static final String SETTINGS_LAST_USED_FILTERS= "filters_last_used"; //$NON-NLS-1$ public static MatchFilter[] getLastUsedFilters() { String string= RubyPlugin.getDefault().getDialogSettings().get(SETTINGS_LAST_USED_FILTERS); if (string != null && string.length() > 0) { return decodeFiltersString(string); } return getDefaultFilters(); } public static void setLastUsedFilters(MatchFilter[] filters) { String encoded= encodeFilters(filters); RubyPlugin.getDefault().getDialogSettings().put(SETTINGS_LAST_USED_FILTERS, encoded); } public static MatchFilter[] getDefaultFilters() { return new MatchFilter[] { IMPORT_FILTER }; } private static String encodeFilters(MatchFilter[] enabledFilters) { StringBuffer buf= new StringBuffer(); buf.append(enabledFilters.length); for (int i= 0; i < enabledFilters.length; i++) { buf.append(';'); buf.append(enabledFilters[i].getID()); } return buf.toString(); } private static MatchFilter[] decodeFiltersString(String encodedString) { StringTokenizer tokenizer= new StringTokenizer(encodedString, String.valueOf(';')); MatchFilter[] res; try { int count= Integer.valueOf(tokenizer.nextToken()).intValue(); res= new MatchFilter[count]; for (int i= 0; i < count; i++) { res[i]= findMatchFilter(tokenizer.nextToken()); } } catch (NumberFormatException e) { res= getDefaultFilters(); } return res; } public abstract boolean isApplicable(RubySearchQuery query); public abstract boolean filters(RubyElementMatch match); public abstract String getName(); public abstract String getActionLabel(); public abstract String getDescription(); public abstract String getID(); private static final MatchFilter POTENTIAL_FILTER= new PotentialFilter(); private static final MatchFilter IMPORT_FILTER= new ImportFilter(); private static final MatchFilter JAVADOC_FILTER= new RubydocFilter(); private static final MatchFilter READ_FILTER= new ReadFilter(); private static final MatchFilter WRITE_FILTER= new WriteFilter(); private static final MatchFilter[] ALL_FILTERS= new MatchFilter[] { POTENTIAL_FILTER, IMPORT_FILTER, JAVADOC_FILTER, READ_FILTER, WRITE_FILTER }; public static MatchFilter[] allFilters() { return ALL_FILTERS; } private static MatchFilter findMatchFilter(String id) { for (int i= 0; i < ALL_FILTERS.length; i++) { if (ALL_FILTERS[i].getID().equals(id)) return ALL_FILTERS[i]; } return IMPORT_FILTER; // just return something, should not happen } } class PotentialFilter extends MatchFilter { public boolean filters(RubyElementMatch match) { return match.getAccuracy() == SearchMatch.A_INACCURATE; } public String getName() { return SearchMessages.MatchFilter_PotentialFilter_name; } public String getActionLabel() { return SearchMessages.MatchFilter_PotentialFilter_actionLabel; } public String getDescription() { return SearchMessages.MatchFilter_PotentialFilter_description; } public boolean isApplicable(RubySearchQuery query) { return true; } public String getID() { return "filter_potential"; //$NON-NLS-1$ } } class ImportFilter extends MatchFilter { public boolean filters(RubyElementMatch match) { return match.getElement() instanceof IImportDeclaration; } public String getName() { return SearchMessages.MatchFilter_ImportFilter_name; } public String getActionLabel() { return SearchMessages.MatchFilter_ImportFilter_actionLabel; } public String getDescription() { return SearchMessages.MatchFilter_ImportFilter_description; } public boolean isApplicable(RubySearchQuery query) { QuerySpecification spec= query.getSpecification(); if (spec instanceof ElementQuerySpecification) { ElementQuerySpecification elementSpec= (ElementQuerySpecification) spec; return elementSpec.getElement() instanceof IType; } else if (spec instanceof PatternQuerySpecification) { PatternQuerySpecification patternSpec= (PatternQuerySpecification) spec; return patternSpec.getSearchFor() == IRubySearchConstants.TYPE; } return false; } public String getID() { return "filter_imports"; //$NON-NLS-1$ } } abstract class FieldFilter extends MatchFilter { public boolean isApplicable(RubySearchQuery query) { QuerySpecification spec= query.getSpecification(); if (spec instanceof ElementQuerySpecification) { ElementQuerySpecification elementSpec= (ElementQuerySpecification) spec; return elementSpec.getElement() instanceof IField; } else if (spec instanceof PatternQuerySpecification) { PatternQuerySpecification patternSpec= (PatternQuerySpecification) spec; return patternSpec.getSearchFor() == IRubySearchConstants.FIELD; } return false; } } class WriteFilter extends FieldFilter { public boolean filters(RubyElementMatch match) { return match.isWriteAccess() && !match.isReadAccess(); } public String getName() { return SearchMessages.MatchFilter_WriteFilter_name; } public String getActionLabel() { return SearchMessages.MatchFilter_WriteFilter_actionLabel; } public String getDescription() { return SearchMessages.MatchFilter_WriteFilter_description; } public String getID() { return "filter_writes"; //$NON-NLS-1$ } } class ReadFilter extends FieldFilter { public boolean filters(RubyElementMatch match) { return match.isReadAccess() && !match.isWriteAccess(); } public String getName() { return SearchMessages.MatchFilter_ReadFilter_name; } public String getActionLabel() { return SearchMessages.MatchFilter_ReadFilter_actionLabel; } public String getDescription() { return SearchMessages.MatchFilter_ReadFilter_description; } public String getID() { return "filter_reads"; //$NON-NLS-1$ } } class RubydocFilter extends MatchFilter { public boolean filters(RubyElementMatch match) { return match.isRubydoc(); } public String getName() { return SearchMessages.MatchFilter_RubydocFilter_name; } public String getActionLabel() { return SearchMessages.MatchFilter_RubydocFilter_actionLabel; } public String getDescription() { return SearchMessages.MatchFilter_RubydocFilter_description; } public boolean isApplicable(RubySearchQuery query) { return true; } public String getID() { return "filter_javadoc"; //$NON-NLS-1$ } }