KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > views > properties > tabbed > view > TabbedPropertyRegistryClassSectionFilter


1 /*******************************************************************************
2  * Copyright (c) 2001, 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.views.properties.tabbed.view;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.jface.viewers.IFilter;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.ui.views.properties.tabbed.ISectionDescriptor;
25 import org.eclipse.ui.views.properties.tabbed.ITypeMapper;
26
27 /**
28  * Provides a section filtering mechanism where the selection is an
29  * IStructuredSelection and filtering is based on class.
30  *
31  * @author Anthony Hunter
32  */

33 public class TabbedPropertyRegistryClassSectionFilter {
34
35     private ITypeMapper typeMapper = null;
36
37     /**
38      * constructor.
39      */

40     protected TabbedPropertyRegistryClassSectionFilter(ITypeMapper typeMapper) {
41         super();
42         this.typeMapper = typeMapper;
43     }
44
45     /**
46      * Verifies if the property section extension represented by sectionElement
47      * applies to the given input.
48      */

49     protected boolean appliesToSelection(ISectionDescriptor descriptor,
50             ISelection selection) {
51
52         if (selection instanceof IStructuredSelection
53             && selection.isEmpty() == false) {
54
55             if (descriptor.getEnablesFor() != ISectionDescriptor.ENABLES_FOR_ANY
56                 && ((IStructuredSelection) selection).size() != descriptor
57                     .getEnablesFor()) {
58                 /**
59                  * enablesFor does not match the size of the selection, do not
60                  * display section.
61                  */

62                 return false;
63             }
64
65             IFilter filter = descriptor.getFilter();
66             
67             if (filter != null) {
68                 for (Iterator JavaDoc i = ((IStructuredSelection) selection).iterator(); i
69                 .hasNext();) {
70                     Object JavaDoc object = i.next();
71
72                     if (filter != null && filter.select(object) == false) {
73                         /**
74                          * filter fails so section does not apply to the selection,
75                          * do not display section.
76                          */

77                         return false;
78                     }
79                 }
80                 /**
81                  * filter passes for all objects in the selection.
82                  */

83                 return true;
84             }
85
86             Set JavaDoc effectiveTypes = new HashSet JavaDoc();
87
88             for (Iterator JavaDoc i = ((IStructuredSelection) selection).iterator(); i
89                 .hasNext();) {
90
91                 Object JavaDoc object = i.next();
92
93                 Class JavaDoc remapType = object.getClass();
94                 if (typeMapper != null) {
95                     remapType = typeMapper.mapType(object);
96                 }
97
98                 if (effectiveTypes.add(remapType)) {
99
100                     // the effective types of the selection
101
if (appliesToEffectiveType(descriptor, remapType) == false) {
102                         return false;
103                     }
104                 }
105             }
106         }
107
108         return true;
109     }
110
111     private boolean appliesToEffectiveType(ISectionDescriptor descriptor,
112             Class JavaDoc inputClass) {
113
114         ArrayList JavaDoc classTypes = getClassTypes(inputClass);
115
116         List JavaDoc sectionInputTypes = descriptor.getInputTypes();
117         for (Iterator JavaDoc j = sectionInputTypes.iterator(); j.hasNext();) {
118             String JavaDoc type = (String JavaDoc) j.next();
119             if (classTypes.contains(type)) {
120                 // found a match
121
return true;
122             }
123         }
124
125         return false;
126     }
127
128     /**
129      * Returns the classes and interfaces the given target class
130      * extends/implements.
131      */

132     protected ArrayList JavaDoc getClassTypes(Class JavaDoc target) {
133         ArrayList JavaDoc result = new ArrayList JavaDoc();
134         // add classes
135
List JavaDoc classes = computeClassOrder(target);
136         for (Iterator JavaDoc i = classes.iterator(); i.hasNext();) {
137             result.add(((Class JavaDoc) i.next()).getName());
138         }
139         // add interfaces
140
result.addAll(computeInterfaceOrder(classes));
141         return result;
142     }
143
144     private List JavaDoc computeClassOrder(Class JavaDoc target) {
145         List JavaDoc result = new ArrayList JavaDoc(4);
146         Class JavaDoc clazz = target;
147         while (clazz != null) {
148             result.add(clazz);
149             clazz = clazz.getSuperclass();
150         }
151         return result;
152     }
153
154     private List JavaDoc computeInterfaceOrder(List JavaDoc classes) {
155         List JavaDoc result = new ArrayList JavaDoc(4);
156         Map JavaDoc seen = new HashMap JavaDoc(4);
157         for (Iterator JavaDoc iter = classes.iterator(); iter.hasNext();) {
158             Class JavaDoc[] interfaces = ((Class JavaDoc) iter.next()).getInterfaces();
159             internalComputeInterfaceOrder(interfaces, result, seen);
160         }
161         return result;
162     }
163
164     private void internalComputeInterfaceOrder(Class JavaDoc[] interfaces, List JavaDoc result,
165             Map JavaDoc seen) {
166         List JavaDoc newInterfaces = new ArrayList JavaDoc(seen.size());
167         for (int i = 0; i < interfaces.length; i++) {
168             Class JavaDoc interfac = interfaces[i];
169             if (seen.get(interfac) == null) {
170                 result.add(interfac.getName());
171                 seen.put(interfac, interfac);
172                 newInterfaces.add(interfac);
173             }
174         }
175         for (Iterator JavaDoc iter = newInterfaces.iterator(); iter.hasNext();) {
176             internalComputeInterfaceOrder(
177                 ((Class JavaDoc) iter.next()).getInterfaces(), result, seen);
178         }
179     }
180 }
181
Popular Tags