KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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
12 package org.eclipse.ui.internal.navigator.extensions;
13
14 import org.eclipse.core.expressions.EvaluationContext;
15 import org.eclipse.core.expressions.EvaluationResult;
16 import org.eclipse.core.expressions.Expression;
17 import org.eclipse.core.expressions.IEvaluationContext;
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.internal.navigator.CustomAndExpression;
24 import org.eclipse.ui.internal.navigator.NavigatorPlugin;
25 import org.eclipse.ui.navigator.ILinkHelper;
26
27 /**
28  * Provides a wrapper around
29  * <b>org.eclipse.ui.navigator.navigatorContent/linkHelper</b> extensions.
30  *
31  * @since 3.2
32  *
33  */

34 public class LinkHelperDescriptor implements ILinkHelperExtPtConstants {
35
36     private final IConfigurationElement configElement;
37
38     private String JavaDoc id;
39
40     private Expression editorInputEnablement;
41
42     /* The following field may be null */
43     private Expression selectionEnablement;
44
45     private boolean hasLinkHelperFailedCreation;
46
47     /* package */LinkHelperDescriptor(IConfigurationElement aConfigElement) {
48         Assert.isNotNull(aConfigElement,
49                 "LinkHelperRegistry.Descriptor objects cannot be null."); //$NON-NLS-1$
50
Assert
51                 .isLegal(LINK_HELPER.equals(aConfigElement.getName()),
52                         "LinkHelperRegistry.Descriptor objects must have the name \"linkHelper\"."); //$NON-NLS-1$
53
configElement = aConfigElement;
54         init();
55     }
56
57     void init() {
58         id = configElement.getAttribute(ATT_ID);
59         IConfigurationElement[] expressions = this.configElement
60                 .getChildren(EDITOR_INPUT_ENABLEMENT);
61         Assert
62                 .isLegal(
63                         expressions.length == 1,
64                         "The linkHelper extension point requires exactly one editorInputEnablement child."); //$NON-NLS-1$
65

66         editorInputEnablement = new CustomAndExpression(expressions[0]);
67
68         expressions = configElement.getChildren(SELECTION_ENABLEMENT);
69         if (expressions.length > 0) {
70             /* The following attribute is optional */
71             // navigatorContentExtensionId = expressions[0]
72
// .getAttribute(ATT_NAVIGATOR_CONTENT_EXTENSION_ID);
73
if (expressions[0].getChildren() != null
74                     && expressions[0].getChildren().length > 0) {
75
76                 selectionEnablement = new CustomAndExpression(expressions[0]);
77
78             }
79         }
80     }
81
82     /**
83      * @return Returns the id.
84      */

85     public String JavaDoc getId() {
86         return id;
87     }
88
89     /**
90      * Create a link helper instance from this descriptors class attribute.
91      *
92      * @return An instance of the helper that is defined by the extension, or a
93      * Skeleton Link Helper.
94      */

95     public ILinkHelper createLinkHelper() {
96         if (hasLinkHelperFailedCreation)
97             return SkeletonLinkHelper.INSTANCE;
98         try {
99             return (ILinkHelper) configElement
100                     .createExecutableExtension(ATT_CLASS);
101         } catch (Throwable JavaDoc t) {
102             hasLinkHelperFailedCreation = true;
103             NavigatorPlugin.logError(0, t.getMessage(), t);
104         }
105         return SkeletonLinkHelper.INSTANCE;
106     }
107
108     /**
109      *
110      * @param anInput
111      * The editor input from the editor that was activated.
112      * @return True if this linkHelper descriptor can produce a selection from
113      * the editor input.
114      */

115     public boolean isEnabledFor(IEditorInput anInput) {
116
117         if (editorInputEnablement == null || anInput == null) {
118             return false;
119         }
120
121         try {
122             EvaluationContext context = new EvaluationContext(null, anInput);
123             context.setAllowPluginActivation(true);
124             return (editorInputEnablement.evaluate(context) == EvaluationResult.TRUE);
125         } catch (CoreException e) {
126             NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e);
127         }
128         return false;
129     }
130
131     /**
132      * @param anObject
133      * The selection from the CommonViewer
134      * @return True if this dscriptor can determine a valid editor to activate
135      * from the selection.
136      */

137     public boolean isEnabledFor(Object JavaDoc anObject) {
138         if (selectionEnablement == null) {
139             return false;
140         }
141
142         IEvaluationContext context = new EvaluationContext(null, anObject);
143         context.setAllowPluginActivation(true);
144         try {
145             if (selectionEnablement.evaluate(context) != EvaluationResult.TRUE) {
146                 return false;
147             }
148         } catch (CoreException e) {
149             NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e);
150             return false;
151         }
152         return true;
153     }
154 }
155
Popular Tags