KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > extensions > NavigatorContentDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.navigator.extensions;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.Set JavaDoc;
15 import java.util.TreeSet JavaDoc;
16
17 import org.eclipse.core.expressions.ElementHandler;
18 import org.eclipse.core.expressions.EvaluationContext;
19 import org.eclipse.core.expressions.EvaluationResult;
20 import org.eclipse.core.expressions.Expression;
21 import org.eclipse.core.expressions.ExpressionConverter;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IConfigurationElement;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.jface.viewers.ILabelProvider;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.viewers.ITreeContentProvider;
28 import org.eclipse.osgi.util.NLS;
29 import org.eclipse.ui.IPluginContribution;
30 import org.eclipse.ui.WorkbenchException;
31 import org.eclipse.ui.internal.navigator.CommonNavigatorMessages;
32 import org.eclipse.ui.internal.navigator.CustomAndExpression;
33 import org.eclipse.ui.internal.navigator.NavigatorPlugin;
34 import org.eclipse.ui.navigator.ICommonContentProvider;
35 import org.eclipse.ui.navigator.ICommonLabelProvider;
36 import org.eclipse.ui.navigator.INavigatorContentDescriptor;
37 import org.eclipse.ui.navigator.Priority;
38
39 /**
40  * Encapsulates the <code>org.eclipse.ui.navigator.navigatorContent</code>
41  * extension point.
42  * <p>
43  * <strong>EXPERIMENTAL</strong>. This class or interface has been added as
44  * part of a work in progress. There is a guarantee neither that this API will
45  * work nor that it will remain the same. Please do not use this API without
46  * consulting with the Platform/UI team.
47  * </p>
48  *
49  * @since 3.2
50  */

51 public final class NavigatorContentDescriptor implements
52         INavigatorContentDescriptor, INavigatorContentExtPtConstants {
53
54     private static final int HASH_CODE_NOT_COMPUTED = -1;
55     private String JavaDoc id;
56
57     private String JavaDoc name;
58
59     private IConfigurationElement configElement;
60
61     private int priority = Priority.NORMAL_PRIORITY_VALUE;
62
63     private Expression enablement;
64
65     private Expression possibleChildren;
66
67     private String JavaDoc icon;
68
69     private boolean activeByDefault;
70
71     private IPluginContribution contribution;
72
73     private Set JavaDoc overridingExtensions;
74
75     private OverridePolicy overridePolicy;
76
77     private String JavaDoc suppressedExtensionId;
78
79     private INavigatorContentDescriptor overriddenDescriptor;
80
81     private int hashCode = HASH_CODE_NOT_COMPUTED;
82
83     private boolean providesSaveables;
84
85     /**
86      * Creates a new content descriptor from a configuration element.
87      *
88      * @param configElement
89      * configuration element to create a descriptor from
90      *
91      * @throws WorkbenchException
92      * if the configuration element could not be parsed. Reasons
93      * include:
94      * <ul>
95      * <li>A required attribute is missing.</li>
96      * <li>More elements are define than is allowed.</li>
97      * </ul>
98      */

99     /* package */ NavigatorContentDescriptor(IConfigurationElement configElement)
100             throws WorkbenchException {
101         super();
102         this.configElement = configElement;
103         init();
104     }
105
106     /*
107      * (non-Javadoc)
108      *
109      * @see org.eclipse.ui.internal.navigator.extensions.INavigatorContentDescriptor#getId()
110      */

111     public String JavaDoc getId() {
112         return id;
113     }
114
115     /*
116      * (non-Javadoc)
117      *
118      * @see org.eclipse.ui.internal.navigator.extensions.INavigatorContentDescriptor#getName()
119      */

120     public String JavaDoc getName() {
121         return name;
122     }
123
124     /*
125      * (non-Javadoc)
126      *
127      * @see org.eclipse.ui.internal.navigator.extensions.INavigatorContentDescriptor#getPriority()
128      */

129     public int getPriority() {
130         return priority;
131     }
132
133     /**
134      * Parses the configuration element.
135      *
136      * @throws WorkbenchException
137      * if the configuration element could not be parsed. Reasons
138      * include:
139      * <ul>
140      * <li>A required attribute is missing.</li>
141      * <li>More elements are define than is allowed.</li>
142      * </ul>
143      */

144     private void init() throws WorkbenchException {
145         id = configElement.getAttribute(ATT_ID);
146         name = configElement.getAttribute(ATT_NAME);
147         String JavaDoc priorityString = configElement.getAttribute(ATT_PRIORITY);
148         icon = configElement.getAttribute(ATT_ICON);
149
150         String JavaDoc activeByDefaultString = configElement
151                 .getAttribute(ATT_ACTIVE_BY_DEFAULT);
152         activeByDefault = (activeByDefaultString != null && activeByDefaultString
153                 .length() > 0) ? Boolean.valueOf(
154                 configElement.getAttribute(ATT_ACTIVE_BY_DEFAULT))
155                 .booleanValue() : true;
156
157         String JavaDoc providesSaveablesString = configElement
158             .getAttribute(ATT_PROVIDES_SAVEABLES);
159         providesSaveables = (providesSaveablesString != null && providesSaveablesString
160                 .length() > 0) ? Boolean.valueOf(providesSaveablesString)
161                         .booleanValue() : false;
162
163         if (priorityString != null) {
164             try {
165                 Priority p = Priority.get(priorityString);
166                 priority = p != null ? p.getValue()
167                         : Priority.NORMAL_PRIORITY_VALUE;
168             } catch (NumberFormatException JavaDoc exception) {
169                 priority = Priority.NORMAL_PRIORITY_VALUE;
170             }
171         }
172         if (id == null) {
173             throw new WorkbenchException(NLS.bind(
174                     CommonNavigatorMessages.Attribute_Missing_Warning,
175                     new Object JavaDoc[] {
176                             ATT_ID,
177                             id,
178                             configElement.getDeclaringExtension()
179                                     .getNamespaceIdentifier() }));
180         }
181
182         IConfigurationElement[] children = configElement
183                 .getChildren(TAG_ENABLEMENT);
184         if (children.length == 0) {
185
186             children = configElement.getChildren(TAG_TRIGGER_POINTS);
187             if (children.length == 1) {
188                 enablement = new CustomAndExpression(children[0]);
189             } else {
190                 throw new WorkbenchException(NLS.bind(
191                         CommonNavigatorMessages.Attribute_Missing_Warning,
192                         new Object JavaDoc[] {
193                                 TAG_TRIGGER_POINTS,
194                                 id,
195                                 configElement.getDeclaringExtension()
196                                         .getNamespaceIdentifier() }));
197             }
198
199             children = configElement.getChildren(TAG_POSSIBLE_CHILDREN);
200             if (children.length == 1) {
201                 possibleChildren = new CustomAndExpression(children[0]);
202             } else if(children.length > 1){
203                 throw new WorkbenchException(NLS.bind(
204                         CommonNavigatorMessages.Attribute_Missing_Warning,
205                         new Object JavaDoc[] {
206                                 TAG_POSSIBLE_CHILDREN,
207                                 id,
208                                 configElement.getDeclaringExtension()
209                                         .getNamespaceIdentifier() }));
210             }
211         } else if (children.length == 1) {
212             try {
213                 enablement = ElementHandler.getDefault().create(
214                         ExpressionConverter.getDefault(), children[0]);
215             } catch (CoreException e) {
216                 NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e);
217             }
218         } else if (children.length > 1) {
219             throw new WorkbenchException(NLS.bind(
220                     CommonNavigatorMessages.Attribute_Missing_Warning,
221                     new Object JavaDoc[] {
222                             TAG_ENABLEMENT,
223                             id,
224                             configElement.getDeclaringExtension()
225                                     .getNamespaceIdentifier() }));
226         }
227
228         contribution = new IPluginContribution() {
229
230             public String JavaDoc getLocalId() {
231                 return getId();
232             }
233
234             public String JavaDoc getPluginId() {
235                 return configElement.getDeclaringExtension().getNamespaceIdentifier();
236             }
237
238         };
239
240         children = configElement.getChildren(TAG_OVERRIDE);
241         if (children.length == 1) {
242             suppressedExtensionId = children[0]
243                     .getAttribute(ATT_SUPPRESSED_EXT_ID);
244             overridePolicy = OverridePolicy.get(children[0]
245                     .getAttribute(ATT_POLICY));
246         }
247     }
248
249     /**
250      * @return Returns the icon.
251      */

252     public String JavaDoc getIcon() {
253         return icon;
254     }
255
256     /**
257      * @return Returns the suppressedExtensionId or null if none specified.
258      */

259     public String JavaDoc getSuppressedExtensionId() {
260         return suppressedExtensionId;
261     }
262
263     /**
264      * @return Returns the overridePolicy or null if this extension does not
265      * override another extension.
266      */

267     public OverridePolicy getOverridePolicy() {
268         return overridePolicy;
269     }
270
271     /**
272      * @return Returns the contribution.
273      */

274     public IPluginContribution getContribution() {
275         return contribution;
276     }
277
278     /**
279      * The content provider could be an instance of
280      * {@link ICommonContentProvider}, but only {@link ITreeContentProvider} is
281      * required.
282      *
283      *
284      * @return An instance of the Content provider defined for this extension.
285      * @throws CoreException
286      * if an instance of the executable extension could not be
287      * created for any reason
288      *
289      */

290     public ITreeContentProvider createContentProvider() throws CoreException {
291         return (ITreeContentProvider) configElement
292                 .createExecutableExtension(ATT_CONTENT_PROVIDER);
293     }
294
295     /**
296      *
297      * The content provider could be an instance of {@link ICommonLabelProvider},
298      * but only {@link ILabelProvider} is required.
299      *
300      * @return An instance of the Label provider defined for this extension
301      * @throws CoreException
302      * if an instance of the executable extension could not be
303      * created for any reason
304      */

305     public ILabelProvider createLabelProvider() throws CoreException {
306         return (ILabelProvider) configElement
307                 .createExecutableExtension(ATT_LABEL_PROVIDER);
308     }
309
310     /*
311      * (non-Javadoc)
312      *
313      * @see org.eclipse.ui.internal.navigator.extensions.INavigatorContentDescriptor#isEnabledByDefault()
314      */

315     public boolean isActiveByDefault() {
316         return activeByDefault;
317     }
318
319     /**
320      * Determine if this content extension would be able to provide children for
321      * the given element.
322      *
323      * @param anElement
324      * The element that should be used for the evaluation.
325      * @return True if and only if the extension is enabled for the element.
326      */

327     public boolean isTriggerPoint(Object JavaDoc anElement) {
328
329         if (enablement == null || anElement == null) {
330             return false;
331         }
332
333         try {
334             EvaluationContext context = new EvaluationContext(null, anElement);
335             context.setAllowPluginActivation(true);
336             return (enablement.evaluate(context) == EvaluationResult.TRUE);
337         } catch (CoreException e) {
338             NavigatorPlugin.logError(0, e.getMessage(), e);
339         }
340         return false;
341     }
342
343     /**
344      * Determine if this content extension could provide the given element as a
345      * child.
346      *
347      * <p>
348      * This method is used to determine what the parent of an element could be
349      * for Link with Editor support.
350      * </p>
351      *
352      * @param anElement
353      * The element that should be used for the evaluation.
354      * @return True if and only if the extension might provide an object of this
355      * type as a child.
356      */

357     public boolean isPossibleChild(Object JavaDoc anElement) {
358
359         if ((enablement == null && possibleChildren == null)
360                 || anElement == null) {
361             return false;
362         } else if(anElement instanceof IStructuredSelection) {
363             return arePossibleChildren((IStructuredSelection) anElement);
364         }
365
366         try {
367             EvaluationContext context = new EvaluationContext(null, anElement);
368             context.setAllowPluginActivation(true);
369             if (possibleChildren != null) {
370                 return (possibleChildren.evaluate(context) == EvaluationResult.TRUE);
371             } else if (enablement != null) {
372                 return (enablement.evaluate(context) == EvaluationResult.TRUE);
373             }
374         } catch (CoreException e) {
375             NavigatorPlugin.logError(0, e.getMessage(), e);
376         }
377         return false;
378     }
379     
380     /**
381      * A convenience method to check all elements in a selection.
382      *
383      * @param aSelection A non-null selection
384      * @return True if and only if every element in the selection is a possible child.
385      */

386     public boolean arePossibleChildren(IStructuredSelection aSelection) {
387         if(aSelection.isEmpty()) {
388             return false;
389         }
390         for (Iterator JavaDoc iter = aSelection.iterator(); iter.hasNext();) {
391             Object JavaDoc element = iter.next();
392             if(!isPossibleChild(element)) {
393                 return false;
394             }
395         }
396         return true;
397     }
398
399     /**
400      *
401      * Does not force the creation of the set of overriding extensions.
402      *
403      * @return True if this extension has overridding extensions.
404      */

405     public boolean hasOverridingExtensions() {
406         return overridingExtensions != null && overridingExtensions.size() > 0;
407     }
408
409     /**
410      * @return The set of overridding extensions (of type
411      * {@link INavigatorContentDescriptor}
412      */

413     public Set JavaDoc getOverriddingExtensions() {
414         if (overridingExtensions == null) {
415             overridingExtensions = new TreeSet JavaDoc(ExtensionPriorityComparator.DESCENDING);
416         }
417         return overridingExtensions;
418     }
419
420     /*
421      * (non-Javadoc)
422      *
423      * @see java.lang.Object#toString()
424      */

425     public String JavaDoc toString() {
426         return "Content[" + id + ", \"" + name + "\"]"; //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
427
}
428     
429     /* (non-Javadoc)
430      * @see java.lang.Object#hashCode()
431      */

432     public int hashCode() {
433         if (hashCode == HASH_CODE_NOT_COMPUTED) {
434             String JavaDoc hashCodeString = configElement.getNamespaceIdentifier() + getId();
435             hashCode = hashCodeString.hashCode();
436             if (hashCode == HASH_CODE_NOT_COMPUTED)
437                 hashCode++;
438         }
439         return hashCode;
440     }
441
442     /**
443      * @return The descriptor of the <code>suppressedExtensionId</code> if
444      * non-null.
445      */

446     public INavigatorContentDescriptor getOverriddenDescriptor() {
447         return overriddenDescriptor;
448     }
449
450     /**
451      * @param theOverriddenDescriptor
452      * The overriddenDescriptor to set.
453      */

454     /* package */void setOverriddenDescriptor(
455             INavigatorContentDescriptor theOverriddenDescriptor) {
456         overriddenDescriptor = theOverriddenDescriptor;
457     }
458
459     /* (non-Javadoc)
460      * @see org.eclipse.ui.navigator.INavigatorContentDescriptor#hasSaveablesProvider()
461      */

462     public boolean hasSaveablesProvider() {
463         return providesSaveables;
464     }
465
466 }
467
Popular Tags