Added filters to the PHp OutlinePage
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / filters / NamePatternFilter.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.filters;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.runtime.IAdaptable;
15
16 import org.eclipse.jface.viewers.Viewer;
17 import org.eclipse.jface.viewers.ViewerFilter;
18
19 import net.sourceforge.phpdt.core.IJavaElement;
20
21 import net.sourceforge.phpdt.internal.ui.util.StringMatcher;
22
23 /**
24  * The NamePatternFilter selects the elements which
25  * match the given string patterns.
26  * <p>
27  * The following characters have special meaning:
28  *   ? => any character
29  *   * => any string
30  * </p>
31  * 
32  * @since 2.0
33  */
34 public class NamePatternFilter extends ViewerFilter {
35         private String[] fPatterns;
36         private StringMatcher[] fMatchers;
37         
38         /**
39          * Return the currently configured StringMatchers.
40          */
41         private StringMatcher[] getMatchers() {
42                 return fMatchers;
43         }
44         
45         /**
46          * Gets the patterns for the receiver.
47          */
48         public String[] getPatterns() {
49                 return fPatterns;
50         }
51         
52         
53         /* (non-Javadoc)
54          * Method declared on ViewerFilter.
55          */
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();
65                         else {
66                                 IResource resource= (IResource)adaptable.getAdapter(IResource.class);
67                                 if (resource != null)
68                                         matchName= resource.getName();
69                         }
70                 }
71                 if (matchName != null) {
72                         StringMatcher[] testMatchers= getMatchers();
73                         for (int i = 0; i < testMatchers.length; i++) {
74                                 if (testMatchers[i].match(matchName))
75                                         return false;
76                         }
77                         return true;
78                 }
79                 return true;
80         }
81         
82         /**
83          * Sets the patterns to filter out for the receiver.
84          * <p>
85          * The following characters have special meaning:
86          *   ? => any character
87          *   * => any string
88          * </p>
89          */
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);
96                 }
97         }
98 }