1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.filters;
13 import net.sourceforge.phpdt.core.IJavaElement;
14 import net.sourceforge.phpdt.internal.ui.util.StringMatcher;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.jface.viewers.Viewer;
19 import org.eclipse.jface.viewers.ViewerFilter;
22 * The NamePatternFilter selects the elements which match the given string
25 * The following characters have special meaning: ? => any character * => any
31 public class NamePatternFilter extends ViewerFilter {
32 private String[] fPatterns;
34 private StringMatcher[] fMatchers;
37 * Return the currently configured StringMatchers.
39 private StringMatcher[] getMatchers() {
44 * Gets the patterns for the receiver.
46 public String[] getPatterns() {
51 * (non-Javadoc) Method declared on ViewerFilter.
53 public boolean select(Viewer viewer, Object parentElement, Object element) {
54 String matchName = null;
55 if (element instanceof IJavaElement) {
56 matchName = ((IJavaElement) element).getElementName();
57 } else if (element instanceof IAdaptable) {
58 IAdaptable adaptable = (IAdaptable) element;
59 IJavaElement javaElement = (IJavaElement) adaptable
60 .getAdapter(IJavaElement.class);
61 if (javaElement != null)
62 matchName = javaElement.getElementName();
64 IResource resource = (IResource) adaptable
65 .getAdapter(IResource.class);
67 matchName = resource.getName();
70 if (matchName != null) {
71 StringMatcher[] testMatchers = getMatchers();
72 for (int i = 0; i < testMatchers.length; i++) {
73 if (testMatchers[i].match(matchName))
82 * Sets the patterns to filter out for the receiver.
84 * The following characters have special meaning: ? => any character * =>
88 public void setPatterns(String[] newPatterns) {
89 fPatterns = newPatterns;
90 fMatchers = new StringMatcher[newPatterns.length];
91 for (int i = 0; i < newPatterns.length; i++) {
92 // Reset the matchers to prevent constructor overhead
93 fMatchers[i] = new StringMatcher(newPatterns[i], true, false);