f1dc30a90e412878260640ee3427eb2ac80dc8a2
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / 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 net.sourceforge.phpdt.core.IJavaElement;
14 import net.sourceforge.phpdt.internal.ui.util.StringMatcher;
15
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;
20
21 /**
22  * The NamePatternFilter selects the elements which match the given string
23  * patterns.
24  * <p>
25  * The following characters have special meaning: ? => any character * => any
26  * string
27  * </p>
28  * 
29  * @since 2.0
30  */
31 public class NamePatternFilter extends ViewerFilter {
32         private String[] fPatterns;
33
34         private StringMatcher[] fMatchers;
35
36         /**
37          * Return the currently configured StringMatchers.
38          */
39         private StringMatcher[] getMatchers() {
40                 return fMatchers;
41         }
42
43         /**
44          * Gets the patterns for the receiver.
45          */
46         public String[] getPatterns() {
47                 return fPatterns;
48         }
49
50         /*
51          * (non-Javadoc) Method declared on ViewerFilter.
52          */
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();
63                         else {
64                                 IResource resource = (IResource) adaptable
65                                                 .getAdapter(IResource.class);
66                                 if (resource != null)
67                                         matchName = resource.getName();
68                         }
69                 }
70                 if (matchName != null) {
71                         StringMatcher[] testMatchers = getMatchers();
72                         for (int i = 0; i < testMatchers.length; i++) {
73                                 if (testMatchers[i].match(matchName))
74                                         return false;
75                         }
76                         return true;
77                 }
78                 return true;
79         }
80
81         /**
82          * Sets the patterns to filter out for the receiver.
83          * <p>
84          * The following characters have special meaning: ? => any character * =>
85          * any string
86          * </p>
87          */
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);
94                 }
95         }
96 }