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 org.eclipse.core.resources.IResource;
14 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.jface.viewers.Viewer;
17 import org.eclipse.jface.viewers.ViewerFilter;
19 import net.sourceforge.phpdt.core.IJavaElement;
21 import net.sourceforge.phpdt.internal.ui.util.StringMatcher;
24 * The NamePatternFilter selects the elements which
25 * match the given string patterns.
27 * The following characters have special meaning:
34 public class NamePatternFilter extends ViewerFilter {
35 private String[] fPatterns;
36 private StringMatcher[] fMatchers;
39 * Return the currently configured StringMatchers.
41 private StringMatcher[] getMatchers() {
46 * Gets the patterns for the receiver.
48 public String[] getPatterns() {
54 * Method declared on ViewerFilter.
56 public boolean select(Viewer viewer, Object parentElement, Object element) {
57 String matchName= null;
58 if (element instanceof IJavaElement) {
59 matchName= ((IJavaElement) element).getElementName();
60 } else if (element instanceof IAdaptable) {
61 IAdaptable adaptable= (IAdaptable) element;
62 IJavaElement javaElement= (IJavaElement)adaptable.getAdapter(IJavaElement.class);
63 if (javaElement != null)
64 matchName= javaElement.getElementName();
66 IResource resource= (IResource)adaptable.getAdapter(IResource.class);
68 matchName= resource.getName();
71 if (matchName != null) {
72 StringMatcher[] testMatchers= getMatchers();
73 for (int i = 0; i < testMatchers.length; i++) {
74 if (testMatchers[i].match(matchName))
83 * Sets the patterns to filter out for the receiver.
85 * The following characters have special meaning:
90 public void setPatterns(String[] newPatterns) {
91 fPatterns = newPatterns;
92 fMatchers = new StringMatcher[newPatterns.length];
93 for (int i = 0; i < newPatterns.length; i++) {
94 //Reset the matchers to prevent constructor overhead
95 fMatchers[i]= new StringMatcher(newPatterns[i], true, false);