KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > filters > NamePatternFilter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.filters;
12
13 import org.eclipse.core.runtime.IAdaptable;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.IStorage;
17
18 import org.eclipse.jface.viewers.Viewer;
19 import org.eclipse.jface.viewers.ViewerFilter;
20
21 import org.eclipse.ui.IWorkingSet;
22 import org.eclipse.ui.model.IWorkbenchAdapter;
23
24 import org.eclipse.jdt.core.IJavaElement;
25
26 import org.eclipse.jdt.internal.ui.util.StringMatcher;
27
28 /**
29  * The NamePatternFilter selects the elements which
30  * match the given string patterns.
31  * <p>
32  * The following characters have special meaning:
33  * ? => any character
34  * * => any string
35  * </p>
36  *
37  * @since 2.0
38  */

39 public class NamePatternFilter extends ViewerFilter {
40     private String JavaDoc[] fPatterns;
41     private StringMatcher[] fMatchers;
42     
43     /**
44      * Return the currently configured StringMatchers.
45      * @return returns the matchers
46      */

47     private StringMatcher[] getMatchers() {
48         return fMatchers;
49     }
50     
51     /**
52      * Gets the patterns for the receiver.
53      * @return returns the patterns to be filtered for
54      */

55     public String JavaDoc[] getPatterns() {
56         return fPatterns;
57     }
58     
59     
60     /* (non-Javadoc)
61      * Method declared on ViewerFilter.
62      */

63     public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
64         if (fMatchers.length == 0) {
65             return true;
66         }
67         String JavaDoc matchName= null;
68         if (element instanceof IJavaElement) {
69             matchName= ((IJavaElement) element).getElementName();
70         } else if (element instanceof IResource) {
71             matchName= ((IResource) element).getName();
72         } else if (element instanceof IStorage) {
73             matchName= ((IStorage) element).getName();
74         } else if (element instanceof IWorkingSet) {
75             matchName= ((IWorkingSet) element).getLabel();
76         } else if (element instanceof IAdaptable) {
77             IWorkbenchAdapter wbadapter= (IWorkbenchAdapter) ((IAdaptable)element).getAdapter(IWorkbenchAdapter.class);
78             if (wbadapter != null) {
79                 matchName= wbadapter.getLabel(element);
80             }
81         }
82         if (matchName != null && matchName.length() > 0) {
83             StringMatcher[] testMatchers= getMatchers();
84             for (int i = 0; i < testMatchers.length; i++) {
85                 if (testMatchers[i].match(matchName))
86                     return false;
87             }
88             return true;
89         }
90         return true;
91     }
92     
93     /**
94      * Sets the patterns to filter out for the receiver.
95      * <p>
96      * The following characters have special meaning:
97      * ? => any character
98      * * => any string
99      * </p>
100      * @param newPatterns the new patterns
101      */

102     public void setPatterns(String JavaDoc[] newPatterns) {
103         fPatterns = newPatterns;
104         fMatchers = new StringMatcher[newPatterns.length];
105         for (int i = 0; i < newPatterns.length; i++) {
106             //Reset the matchers to prevent constructor overhead
107
fMatchers[i]= new StringMatcher(newPatterns[i], true, false);
108         }
109     }
110 }
111
Popular Tags