Organized imports
[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 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
23  * match the given string patterns.
24  * <p>
25  * The following characters have special meaning:
26  *   ? => any character
27  *   * => any string
28  * </p>
29  * 
30  * @since 2.0
31  */
32 public class NamePatternFilter extends ViewerFilter {
33         private String[] fPatterns;
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)
52          * Method declared on ViewerFilter.
53          */
54         public boolean select(Viewer viewer, Object parentElement, Object element) {
55                 String matchName= null;
56                 if (element instanceof IJavaElement) {
57                         matchName= ((IJavaElement) element).getElementName();
58                 } else if (element instanceof IAdaptable) {
59                         IAdaptable adaptable= (IAdaptable) element;
60                         IJavaElement javaElement= (IJavaElement)adaptable.getAdapter(IJavaElement.class);
61                         if (javaElement != null)
62                                 matchName= javaElement.getElementName();
63                         else {
64                                 IResource resource= (IResource)adaptable.getAdapter(IResource.class);
65                                 if (resource != null)
66                                         matchName= resource.getName();
67                         }
68                 }
69                 if (matchName != null) {
70                         StringMatcher[] testMatchers= getMatchers();
71                         for (int i = 0; i < testMatchers.length; i++) {
72                                 if (testMatchers[i].match(matchName))
73                                         return false;
74                         }
75                         return true;
76                 }
77                 return true;
78         }
79         
80         /**
81          * Sets the patterns to filter out for the receiver.
82          * <p>
83          * The following characters have special meaning:
84          *   ? => 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 }