KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ObjectFilterTest


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.internal;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.ui.IActionFilter;
18 import org.eclipse.ui.internal.util.Util;
19
20 /**
21  * An ObjectFilterTest is used to read an object filter from XML,
22  * and evaluate the results for a given object.
23  */

24 public class ObjectFilterTest {
25     private HashMap JavaDoc filterElements;
26
27     /**
28      * Create a new object filter.
29      */

30     public ObjectFilterTest() {
31         // do nothing
32
}
33
34     /**
35      * Add a filter element to the test. This element must contain
36      * a name value filter pair, as defined by the
37      * <code>org.eclipse.ui.actionFilters</code> extension point.
38      */

39     public boolean addFilterElement(IConfigurationElement element) {
40         String JavaDoc name = element.getAttribute("name");//$NON-NLS-1$
41
if (name == null) {
42             return false;
43         }
44
45         // Get positive property.
46
String JavaDoc value = element.getAttribute("value");//$NON-NLS-1$
47
if (value == null) {
48             return false;
49         }
50         if (filterElements == null) {
51             filterElements = new HashMap JavaDoc();
52         }
53         filterElements.put(name, value);
54         return true;
55     }
56
57     /**
58      * Returns whether the object filter correctly matches a
59      * given object. The results will be <code>true</code> if there is
60      * a filter match.
61      * <p>
62      * If <code>adaptable is true</code>, the result will also be
63      * <code>rue</code> if the object is a wrapper for a resource, and
64      * the resource produces a filter match.
65      * </p>
66      *
67      * @param object the object to examine
68      * @returns <code>true</code> if there is a filter match.
69      */

70     public boolean matches(Object JavaDoc object, boolean adaptable) {
71         // Optimize it.
72
if (filterElements == null) {
73             return true;
74         }
75
76         // Try out the object.
77
if (this.preciselyMatches(object)) {
78             return true;
79         }
80         return false;
81     }
82
83     /**
84      * Returns whether the object filter correctly matches a given object.
85      */

86     private boolean preciselyMatches(Object JavaDoc object) {
87         // Get the action filter.
88
IActionFilter filter = (IActionFilter)Util.getAdapter(object, IActionFilter.class);
89         if (filter == null) {
90             return false;
91         }
92
93         // Run the action filter.
94
Iterator JavaDoc iter = filterElements.keySet().iterator();
95         while (iter.hasNext()) {
96             String JavaDoc name = (String JavaDoc) iter.next();
97             String JavaDoc value = (String JavaDoc) filterElements.get(name);
98             if (!filter.testAttribute(object, name, value)) {
99                 return false;
100             }
101         }
102         return true;
103     }
104 }
105
106
Popular Tags