KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > actions > CommonActionProviderDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.navigator.actions;
12
13 import java.util.Collections JavaDoc;
14 import java.util.Comparator JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.LinkedHashSet JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.util.TreeSet JavaDoc;
19
20 import org.eclipse.core.expressions.ElementHandler;
21 import org.eclipse.core.expressions.EvaluationContext;
22 import org.eclipse.core.expressions.EvaluationResult;
23 import org.eclipse.core.expressions.Expression;
24 import org.eclipse.core.expressions.ExpressionConverter;
25 import org.eclipse.core.expressions.IEvaluationContext;
26 import org.eclipse.core.runtime.Assert;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IConfigurationElement;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.Status;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.ui.internal.navigator.CustomAndExpression;
33 import org.eclipse.ui.internal.navigator.NavigatorPlugin;
34 import org.eclipse.ui.internal.navigator.extensions.INavigatorContentExtPtConstants;
35 import org.eclipse.ui.internal.navigator.extensions.SkeletonActionProvider;
36 import org.eclipse.ui.navigator.CommonActionProvider;
37 import org.eclipse.ui.navigator.Priority;
38
39 /**
40  *
41  *
42  * @since 3.2
43  */

44 public class CommonActionProviderDescriptor implements
45         INavigatorContentExtPtConstants {
46
47     private static final String JavaDoc DEFAULT_ID = "org.eclipse.ui.navigator.actionProvider"; //$NON-NLS-1$
48

49     private static int count = 0;
50
51     private final IConfigurationElement configurationElement;
52
53     private final boolean isNested;
54
55     private Set JavaDoc dependentDescriptors;
56
57     private Set JavaDoc overridingDescriptors;
58
59     private IConfigurationElement enablementElement;
60
61     private Expression enablement;
62
63     private boolean hasLoadingFailed;
64
65     private String JavaDoc definedId;
66
67     private String JavaDoc visibilityId;
68
69     private String JavaDoc dependsOnId;
70
71     private String JavaDoc overridesId;
72
73     private String JavaDoc toString;
74
75     private Priority priority;
76
77     /**
78      * @param aConfigElement
79      * A configuration element with the name "actionProvider" and a
80      * "class" attribute which subclasses
81      * {@link CommonActionProvider}.
82      */

83     public CommonActionProviderDescriptor(IConfigurationElement aConfigElement) {
84         super();
85         Assert.isTrue(TAG_ACTION_PROVIDER.equals(aConfigElement.getName()));
86         configurationElement = aConfigElement;
87         isNested = false;
88         init();
89     }
90
91     /**
92      * @param aConfigElement
93      * A configuration element with the name "actionProvider" and a
94      * "class" attribute which subclasses
95      * {@link CommonActionProvider}.
96      * @param anEnablementExpression
97      * A configuration element with the name 'enablement' or
98      * 'triggerPoints' and containing an Eclipse Core Expression
99      * @param defaultPriority
100      * @param anOverrideId
101      * A unique identifier for this descriptor. Ids can be used as a
102      * filtering device for activities or viewer***Bindings.
103      * @param nestedUnderNavigatorContent
104      * A value of <b>true</b> indicates that this
105      * CommonActionProvider was declared as a nested
106      * &lt;actionProvider /&gt; element under a &lt;navigatorContent
107      * /&gt; element.
108      */

109     public CommonActionProviderDescriptor(IConfigurationElement aConfigElement,
110             IConfigurationElement anEnablementExpression, Priority defaultPriority, String JavaDoc anOverrideId,
111             boolean nestedUnderNavigatorContent) {
112         super();
113         Assert.isTrue(TAG_ACTION_PROVIDER.equals(aConfigElement.getName()));
114         Assert.isTrue(TAG_POSSIBLE_CHILDREN.equals(anEnablementExpression
115                 .getName())
116                 || TAG_ENABLEMENT.equals(anEnablementExpression.getName()));
117         configurationElement = aConfigElement;
118         enablementElement = anEnablementExpression;
119         visibilityId = anOverrideId;
120         isNested = nestedUnderNavigatorContent;
121         priority = defaultPriority;
122         init();
123     }
124
125     private void init() {
126
127         try {
128
129             definedId = configurationElement.getAttribute(ATT_ID);
130
131             // if there was no id attribute, use the default id.
132
if (definedId == null) {
133                 definedId = DEFAULT_ID + "." + count++; //$NON-NLS-1$
134
}
135
136             // we try the id attribute if no override id was supplied.
137
if (visibilityId == null) {
138                 visibilityId = definedId;
139             }
140
141             dependsOnId = configurationElement.getAttribute(ATT_DEPENDS_ON);
142
143             overridesId = configurationElement.getAttribute(ATT_OVERRIDES);
144             
145             if(priority == null) {
146                 String JavaDoc prio = configurationElement.getAttribute(ATT_PRIORITY);
147                 if(prio != null)
148                     priority = Priority.get(prio);
149                 else
150                     priority = Priority.NORMAL;
151             }
152
153             IConfigurationElement[] children = configurationElement
154                     .getChildren(TAG_ENABLEMENT);
155             // if no child enablement is specified, and we have an override, use
156
// it
157
if (children.length == 0 && enablementElement != null) {
158                 enablement = new CustomAndExpression(enablementElement);
159                 // otherwise the child enablement takes priority
160
} else if (children.length == 1) {
161                 enablement = ElementHandler.getDefault().create(
162                         ExpressionConverter.getDefault(), children[0]);
163
164             } else {
165                 System.err.println("Incorrect number of expressions: " + //$NON-NLS-1$
166
TAG_ENABLEMENT
167                         + " in navigator extension: " + //$NON-NLS-1$
168
configurationElement.getDeclaringExtension()
169                                 .getUniqueIdentifier() + " in plugin " + //$NON-NLS-1$
170
configurationElement.getDeclaringExtension().getNamespaceIdentifier());
171             }
172         } catch (CoreException e) {
173             NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e);
174         }
175     }
176
177     /**
178      *
179      * @return The instantiated CommonActionProvider for this descriptor as
180      * declared in the ATT_CLASS attribute or
181      * {@link SkeletonActionProvider} if a problem occurs while loading
182      * the instance.
183      */

184     public CommonActionProvider createActionProvider() {
185         if (hasLoadingFailed) {
186             return SkeletonActionProvider.INSTANCE;
187         }
188         CommonActionProvider provider = null;
189         try {
190             provider = (CommonActionProvider) configurationElement
191                     .createExecutableExtension(ATT_CLASS);
192         } catch (CoreException exception) {
193             NavigatorPlugin.log(exception.getStatus());
194             hasLoadingFailed = true;
195             provider = SkeletonActionProvider.INSTANCE;
196         } catch (Exception JavaDoc e) {
197             NavigatorPlugin.log(new Status(IStatus.ERROR,
198                     NavigatorPlugin.PLUGIN_ID, 0, e.getMessage(), e));
199             hasLoadingFailed = true;
200             provider = SkeletonActionProvider.INSTANCE;
201         }
202
203         return provider;
204     }
205
206     /**
207      * Determine if this content extension is enabled for the given selection.
208      * The content extension is enabled for the selection if and only if it is
209      * enabled for each element in the selection.
210      *
211      * @param aStructuredSelection
212      * The selection from the viewer
213      * @return True if and only if the extension is enabled for each element in
214      * the selection.
215      */

216     public boolean isEnabledFor(IStructuredSelection aStructuredSelection) {
217         if (enablement == null) {
218             return false;
219         }
220         
221         if(aStructuredSelection.isEmpty()) {
222             IEvaluationContext context = null;
223             context = new EvaluationContext(null, Collections.EMPTY_LIST);
224             context.setAllowPluginActivation(true);
225             try {
226                 if (enablement.evaluate(context) != EvaluationResult.TRUE) {
227                     return false;
228                 }
229             } catch (CoreException e) {
230                 NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e);
231                 return false;
232             }
233         } else {
234
235             IEvaluationContext context = null;
236             Iterator JavaDoc elements = aStructuredSelection.iterator();
237             while (elements.hasNext()) {
238                 context = new EvaluationContext(null, elements.next());
239                 context.setAllowPluginActivation(true);
240                 try {
241                     if (enablement.evaluate(context) != EvaluationResult.TRUE) {
242                         return false;
243                     }
244                 } catch (CoreException e) {
245                     NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e);
246                     return false;
247                 }
248             }
249         }
250         return true;
251     }
252
253     /**
254      * Determine if this content extension is enabled for the given element.
255      *
256      * @param anElement
257      * The element that should be used for the evaluation.
258      * @return True if and only if the extension is enabled for the element.
259      */

260     public boolean isEnabledFor(Object JavaDoc anElement) {
261         if (enablement == null || anElement == null) {
262             return false;
263         }
264
265         try {
266             EvaluationContext context = new EvaluationContext(null, anElement);
267             context.setAllowPluginActivation(true);
268             return (enablement.evaluate(context) == EvaluationResult.TRUE);
269         } catch (CoreException e) {
270             NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e);
271         }
272         return false;
273     }
274
275     /**
276      *
277      * @return An identifier for this ICommonActionProvider. Defaults to
278      * "org.eclipse.ui.navigator.actionProvider". May not be unique.
279      * Used to filter the actionProvider using the visibility state
280      * information.
281      */

282     public String JavaDoc getId() {
283         return visibilityId;
284     }
285
286     /**
287      *
288      * @return An identifier for this ICommonActionProvider. Defaults to
289      * 'org.eclipse.ui.navigator.actionProvider'. May not be unique.
290      * Used to determine override or depends on cases.
291      */

292     public String JavaDoc getDefinedId() {
293         return definedId;
294     }
295
296     /**
297      *
298      * @return True if this is a nested &lt;actionProvider /&gt; element.
299      */

300     public boolean isNested() {
301         return isNested;
302     }
303
304     /**
305      *
306      * @return The value specified by the <i>dependsOn</i> attribute of the
307      * &lt;actionProvider /&gt; element.
308      */

309     public String JavaDoc getDependsOnId() {
310         return dependsOnId;
311     }
312
313     /**
314      *
315      * @return The value specified by the <i>overridesId</i> attribute of the
316      * &lt;actionProvider /&gt; element.
317      */

318     public String JavaDoc getOverridesId() {
319         return overridesId;
320     }
321     
322     /**
323      * Only nested Action Providers have priority (as of 3.2.1).
324      *
325      * @return The priority associated with this Action Provider Descriptor.
326      */

327     public Priority getPriority() {
328         return priority;
329     }
330
331
332     public int hashCode() {
333         final int PRIME = 31;
334         int result = 1;
335         result = PRIME * result + ((definedId == null) ? 0 : definedId.hashCode());
336         result = PRIME * result + ((visibilityId == null) ? 0 : visibilityId.hashCode());
337         return result;
338     }
339
340     public boolean equals(Object JavaDoc obj) {
341         if (this == obj)
342             return true;
343         if (!super.equals(obj))
344             return false;
345         if (getClass() != obj.getClass())
346             return false;
347         final CommonActionProviderDescriptor other = (CommonActionProviderDescriptor) obj;
348         if (definedId == null) {
349             if (other.definedId != null)
350                 return false;
351         } else if (!definedId.equals(other.definedId))
352             return false;
353         if (visibilityId == null) {
354             if (other.visibilityId != null)
355                 return false;
356         } else if (!visibilityId.equals(other.visibilityId))
357             return false;
358         return true;
359     }
360     
361      
362
363     protected void addDependentDescriptor(
364             CommonActionProviderDescriptor dependentDescriptor) {
365         Assert.isTrue(this != dependentDescriptor);
366         if (dependentDescriptors == null) {
367             dependentDescriptors = new LinkedHashSet JavaDoc();
368         }
369         dependentDescriptors.add(dependentDescriptor);
370     }
371
372     protected void addOverridingDescriptor(
373             CommonActionProviderDescriptor overridingDescriptor) {
374         Assert.isTrue(this != overridingDescriptor);
375         if (overridingDescriptors == null) {
376             overridingDescriptors = new TreeSet JavaDoc(CommonActionProviderDescriptorCompator.INSTANCE);
377         }
378         overridingDescriptors.add(overridingDescriptor);
379     }
380
381     protected boolean hasDependentDescriptors() {
382         return dependentDescriptors != null && !dependentDescriptors.isEmpty();
383     }
384
385     protected boolean hasOverridingDescriptors() {
386         return overridingDescriptors != null
387                 && !overridingDescriptors.isEmpty();
388     }
389
390     protected Iterator JavaDoc dependentDescriptors() {
391         return dependentDescriptors.iterator();
392     }
393
394     protected Iterator JavaDoc overridingDescriptors() {
395         return overridingDescriptors.iterator();
396     }
397
398     public String JavaDoc toString() {
399         if (toString == null) {
400             toString = "CommonActionProviderDescriptor[definedId=" + getDefinedId() + ", visibilityId=" + getId() + ", dependsOn=" + getDependsOnId() + ", overrides=" + getOverridesId() + "]"; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
401
}
402         return toString;
403     }
404
405     
406     /**
407      * Sorts CommonActionProviderDescriptors by priority, and then by defined id.
408      * @since 3.2
409      *
410      */

411     public static class CommonActionProviderDescriptorCompator implements Comparator JavaDoc {
412
413         /**
414          * The singleton instance.
415          */

416         public static final CommonActionProviderDescriptorCompator INSTANCE = new CommonActionProviderDescriptorCompator();
417         
418         private static final int LESS_THAN = -1;
419         private static final int EQUALS = 0;
420         
421         /* (non-Javadoc)
422          * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
423          */

424         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
425             CommonActionProviderDescriptor lvalue= null, rvalue= null;
426             
427             if(o1 instanceof CommonActionProviderDescriptor)
428                 lvalue = (CommonActionProviderDescriptor) o1;
429             
430             if(o2 instanceof CommonActionProviderDescriptor)
431                 rvalue = (CommonActionProviderDescriptor) o2;
432             
433             if(lvalue == null || rvalue == null)
434                 return LESS_THAN;
435             if(lvalue.equals(rvalue))
436                 return EQUALS;
437             int comparison = lvalue.getPriority().getValue() - rvalue.getPriority().getValue();
438             if(comparison == 0)
439                 return lvalue.getDefinedId().compareTo(rvalue.getDefinedId());
440             return comparison;
441             
442         }
443         
444          
445     }
446
447 }
448  
449
Popular Tags