KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > RegistryPageContributor


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.ui.internal.dialogs;
12
13 import java.lang.ref.SoftReference JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.expressions.EvaluationContext;
21 import org.eclipse.core.expressions.EvaluationResult;
22 import org.eclipse.core.expressions.Expression;
23 import org.eclipse.core.expressions.ExpressionConverter;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IAdaptable;
26 import org.eclipse.core.runtime.IConfigurationElement;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.jface.resource.ImageDescriptor;
30 import org.eclipse.ui.IActionFilter;
31 import org.eclipse.ui.IWorkbenchPropertyPage;
32 import org.eclipse.ui.SelectionEnabler;
33 import org.eclipse.ui.internal.LegacyResourceSupport;
34 import org.eclipse.ui.internal.WorkbenchPlugin;
35 import org.eclipse.ui.internal.registry.CategorizedPageRegistryReader;
36 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
37 import org.eclipse.ui.internal.registry.PropertyPagesRegistryReader;
38 import org.eclipse.ui.internal.util.Util;
39 import org.eclipse.ui.model.IWorkbenchAdapter;
40 import org.eclipse.ui.plugin.AbstractUIPlugin;
41
42 /**
43  * This property page contributor is created from page entry in the registry.
44  * Since instances of this class are created by the workbench, there is no
45  * danger of premature loading of plugins.
46  */

47
48 public class RegistryPageContributor implements IPropertyPageContributor,
49         IAdaptable {
50     private static final String JavaDoc CHILD_ENABLED_WHEN = "enabledWhen"; //$NON-NLS-1$
51

52     private String JavaDoc pageId;
53
54     /**
55      * The list of subpages (immediate children) of this node (element type:
56      * <code>RegistryPageContributor</code>).
57      */

58     private Collection JavaDoc subPages = new ArrayList JavaDoc();
59
60     private boolean adaptable = false;
61
62     private IConfigurationElement pageElement;
63
64     private SoftReference JavaDoc filterProperties;
65
66     private Expression enablementExpression;
67
68     /**
69      * PropertyPageContributor constructor.
70      *
71      * @param pageId
72      * the id
73      * @param element
74      * the element
75      */

76     public RegistryPageContributor(String JavaDoc pageId, IConfigurationElement element) {
77         this.pageId = pageId;
78         this.pageElement = element;
79         adaptable = Boolean
80                 .valueOf(
81                         pageElement
82                                 .getAttribute(PropertyPagesRegistryReader.ATT_ADAPTABLE))
83                 .booleanValue();
84         initializeEnablement(element);
85     }
86
87     /*
88      * (non-Javadoc)
89      *
90      * @see org.eclipse.ui.internal.dialogs.IPropertyPageContributor#contributePropertyPages(org.eclipse.ui.internal.dialogs.PropertyPageManager,
91      * java.lang.Object)
92      */

93     public boolean contributePropertyPages(PropertyPageManager mng,
94             Object JavaDoc element) {
95         PropertyPageNode node = new PropertyPageNode(this, element);
96
97         if (getCategory() == null) {
98             mng.addToRoot(node);
99             return true;
100         }
101         if (!mng.addToDeep(getCategory(), node))
102             mng.addToRoot(node);
103
104         return true;
105     }
106
107     /**
108      * Creates the page based on the information in the configuration element.
109      *
110      * @param element
111      * the adaptable element
112      * @return the property page
113      * @throws CoreException
114      * thrown if there is a problem creating the apge
115      */

116     public IWorkbenchPropertyPage createPage(Object JavaDoc element)
117             throws CoreException {
118         IWorkbenchPropertyPage ppage = null;
119         ppage = (IWorkbenchPropertyPage) WorkbenchPlugin.createExtension(
120                 pageElement, IWorkbenchRegistryConstants.ATT_CLASS);
121
122         ppage.setTitle(getPageName());
123
124         Object JavaDoc adapted = element;
125         if (adaptable) {
126             adapted = getAdaptedElement(element);
127             if (adapted == null) {
128                 String JavaDoc message = "Error adapting selection to Property page " + pageId + " is being ignored"; //$NON-NLS-1$ //$NON-NLS-2$
129
throw new CoreException(new Status(IStatus.ERROR,
130                         WorkbenchPlugin.PI_WORKBENCH, IStatus.ERROR, message,
131                         null));
132             }
133         }
134
135         if (adapted instanceof IAdaptable)
136             ppage.setElement((IAdaptable) adapted);
137         else
138             ppage.setElement(new AdaptableForwarder(adapted));
139
140         return ppage;
141     }
142
143     /**
144      * Find an adapted element from the receiver.
145      *
146      * @param element
147      * @return the adapted element or <code>null</code> if it could not be
148      * found.
149      */

150     private Object JavaDoc getAdaptedElement(Object JavaDoc element) {
151         Object JavaDoc adapted = LegacyResourceSupport.getAdapter(element,
152                 getObjectClass());
153         if (adapted != null)
154             return adapted;
155
156         return null;
157     }
158
159     /**
160      * Return the object class name
161      *
162      * @return the object class name
163      */

164     public String JavaDoc getObjectClass() {
165         return pageElement
166                 .getAttribute(PropertyPagesRegistryReader.ATT_OBJECTCLASS);
167     }
168
169     /**
170      * Returns page icon as defined in the registry.
171      *
172      * @return the page icon
173      */

174     public ImageDescriptor getPageIcon() {
175         String JavaDoc iconName = pageElement
176                 .getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
177         if (iconName == null)
178             return null;
179         return AbstractUIPlugin.imageDescriptorFromPlugin(pageElement
180                 .getNamespaceIdentifier(), iconName);
181     }
182
183     /**
184      * Returns page ID as defined in the registry.
185      *
186      * @return the page id
187      */

188
189     public String JavaDoc getPageId() {
190         return pageId;
191     }
192
193     /**
194      * Returns page name as defined in the registry.
195      *
196      * @return the page name
197      */

198     public String JavaDoc getPageName() {
199         return pageElement.getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
200     }
201
202     /**
203      * Return true if name filter is not defined in the registry for this page,
204      * or if name of the selected object matches the name filter.
205      */

206     public boolean isApplicableTo(Object JavaDoc object) {
207
208         if (failsEnablement(object))
209             return false;
210
211         // Test name filter
212
String JavaDoc nameFilter = pageElement
213                 .getAttribute(PropertyPagesRegistryReader.ATT_NAME_FILTER);
214         if (nameFilter != null) {
215             String JavaDoc objectName = object.toString();
216             IWorkbenchAdapter adapter = (IWorkbenchAdapter) Util.getAdapter(object,
217                     IWorkbenchAdapter.class);
218             if (adapter != null) {
219                 String JavaDoc elementName = adapter.getLabel(object);
220                 if (elementName != null) {
221                     objectName = elementName;
222                 }
223             }
224             if (!SelectionEnabler.verifyNameMatch(objectName, nameFilter))
225                 return false;
226         }
227
228         // Test custom filter
229
if (getFilterProperties() == null)
230             return true;
231         IActionFilter filter = null;
232
233         // Do the free IResource adapting
234
Object JavaDoc adaptedObject = LegacyResourceSupport.getAdaptedResource(object);
235         if (adaptedObject != null) {
236             object = adaptedObject;
237         }
238
239         filter = (IActionFilter)Util.getAdapter(object, IActionFilter.class);
240
241         if (filter != null)
242             return testCustom(object, filter);
243
244         return true;
245     }
246
247     /**
248      * Return whether or not object fails the enablement criterea.
249      *
250      * @param object
251      * @return boolean <code>true</code> if it fails the enablement test
252      */

253     private boolean failsEnablement(Object JavaDoc object) {
254         if (enablementExpression == null)
255             return false;
256         try {
257             return enablementExpression.evaluate(
258                     new EvaluationContext(null, object)).equals(
259                     EvaluationResult.FALSE);
260         } catch (CoreException e) {
261             WorkbenchPlugin.log(e);
262             return false;
263         }
264     }
265
266     /**
267      * Initialize the enablement expression for this decorator
268      */

269     protected void initializeEnablement(IConfigurationElement definingElement) {
270         IConfigurationElement[] elements = definingElement
271                 .getChildren(CHILD_ENABLED_WHEN);
272
273         if (elements.length == 0)
274             return;
275
276         try {
277             IConfigurationElement[] enablement = elements[0].getChildren();
278             if (enablement.length == 0)
279                 return;
280             enablementExpression = ExpressionConverter.getDefault().perform(
281                     enablement[0]);
282         } catch (CoreException e) {
283             WorkbenchPlugin.log(e);
284         }
285
286     }
287
288     /**
289      * Returns whether the object passes a custom key value filter implemented
290      * by a matcher.
291      */

292     private boolean testCustom(Object JavaDoc object, IActionFilter filter) {
293         Map JavaDoc filterProperties = getFilterProperties();
294
295         if (filterProperties == null)
296             return false;
297         Iterator JavaDoc iter = filterProperties.keySet().iterator();
298         while (iter.hasNext()) {
299             String JavaDoc key = (String JavaDoc) iter.next();
300             String JavaDoc value = (String JavaDoc) filterProperties.get(key);
301             if (!filter.testAttribute(object, key, value))
302                 return false;
303         }
304         return true;
305     }
306
307     /*
308      * @see IObjectContributor#canAdapt()
309      */

310     public boolean canAdapt() {
311         return adaptable;
312     }
313
314     /**
315      * Get the id of the category.
316      *
317      * @return String
318      * @since 3.1
319      */

320     public String JavaDoc getCategory() {
321         return pageElement
322                 .getAttribute(CategorizedPageRegistryReader.ATT_CATEGORY);
323     }
324
325     /**
326      * Return the children of the receiver.
327      *
328      * @return Collection
329      */

330     public Collection JavaDoc getSubPages() {
331         return subPages;
332     }
333
334     /**
335      * Add child to the list of children.
336      *
337      * @param child
338      */

339     public void addSubPage(RegistryPageContributor child) {
340         subPages.add(child);
341     }
342
343     private Map JavaDoc getFilterProperties() {
344         if (filterProperties == null || filterProperties.get() == null) {
345             Map JavaDoc map = new HashMap JavaDoc();
346             filterProperties = new SoftReference JavaDoc(map);
347             IConfigurationElement[] children = pageElement.getChildren();
348             for (int i = 0; i < children.length; i++) {
349                 processChildElement(map, children[i]);
350             }
351         }
352         return (Map JavaDoc) filterProperties.get();
353     }
354
355     /**
356      * Get the child with the given id.
357      *
358      * @param id
359      * @return RegistryPageContributor
360      */

361     public Object JavaDoc getChild(String JavaDoc id) {
362         Iterator JavaDoc iterator = subPages.iterator();
363         while (iterator.hasNext()) {
364             RegistryPageContributor next = (RegistryPageContributor) iterator
365                     .next();
366             if (next.getPageId().equals(id))
367                 return next;
368         }
369         return null;
370     }
371
372     /**
373      * Parses child element and processes it.
374      *
375      * @since 3.1
376      */

377     private void processChildElement(Map JavaDoc map, IConfigurationElement element) {
378         String JavaDoc tag = element.getName();
379         if (tag.equals(PropertyPagesRegistryReader.TAG_FILTER)) {
380             String JavaDoc key = element
381                     .getAttribute(PropertyPagesRegistryReader.ATT_FILTER_NAME);
382             String JavaDoc value = element
383                     .getAttribute(PropertyPagesRegistryReader.ATT_FILTER_VALUE);
384             if (key == null || value == null)
385                 return;
386             map.put(key, value);
387         }
388     }
389
390     /*
391      * (non-Javadoc)
392      *
393      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
394      * @since 3.1
395      */

396     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
397         if (adapter.equals(IConfigurationElement.class)) {
398             return getConfigurationElement();
399         }
400         return null;
401     }
402
403     /**
404      * @return the configuration element
405      * @since 3.1
406      */

407     IConfigurationElement getConfigurationElement() {
408         return pageElement;
409     }
410 }
411
Popular Tags