001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.navigator.extensions;
011:
012: import org.eclipse.core.expressions.EvaluationContext;
013: import org.eclipse.core.expressions.EvaluationResult;
014: import org.eclipse.core.expressions.Expression;
015: import org.eclipse.core.expressions.IEvaluationContext;
016: import org.eclipse.core.runtime.Assert;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IConfigurationElement;
019: import org.eclipse.core.runtime.IStatus;
020: import org.eclipse.ui.IEditorInput;
021: import org.eclipse.ui.internal.navigator.CustomAndExpression;
022: import org.eclipse.ui.internal.navigator.NavigatorPlugin;
023: import org.eclipse.ui.navigator.ILinkHelper;
024:
025: /**
026: * Provides a wrapper around
027: * <b>org.eclipse.ui.navigator.navigatorContent/linkHelper</b> extensions.
028: *
029: * @since 3.2
030: *
031: */
032: public class LinkHelperDescriptor implements ILinkHelperExtPtConstants {
033:
034: private final IConfigurationElement configElement;
035:
036: private String id;
037:
038: private Expression editorInputEnablement;
039:
040: /* The following field may be null */
041: private Expression selectionEnablement;
042:
043: private boolean hasLinkHelperFailedCreation;
044:
045: /* package */LinkHelperDescriptor(
046: IConfigurationElement aConfigElement) {
047: Assert
048: .isNotNull(aConfigElement,
049: "LinkHelperRegistry.Descriptor objects cannot be null."); //$NON-NLS-1$
050: Assert
051: .isLegal(LINK_HELPER.equals(aConfigElement.getName()),
052: "LinkHelperRegistry.Descriptor objects must have the name \"linkHelper\"."); //$NON-NLS-1$
053: configElement = aConfigElement;
054: init();
055: }
056:
057: void init() {
058: id = configElement.getAttribute(ATT_ID);
059: IConfigurationElement[] expressions = this .configElement
060: .getChildren(EDITOR_INPUT_ENABLEMENT);
061: Assert
062: .isLegal(
063: expressions.length == 1,
064: "The linkHelper extension point requires exactly one editorInputEnablement child."); //$NON-NLS-1$
065:
066: editorInputEnablement = new CustomAndExpression(expressions[0]);
067:
068: expressions = configElement.getChildren(SELECTION_ENABLEMENT);
069: if (expressions.length > 0) {
070: /* The following attribute is optional */
071: // navigatorContentExtensionId = expressions[0]
072: // .getAttribute(ATT_NAVIGATOR_CONTENT_EXTENSION_ID);
073: if (expressions[0].getChildren() != null
074: && expressions[0].getChildren().length > 0) {
075:
076: selectionEnablement = new CustomAndExpression(
077: expressions[0]);
078:
079: }
080: }
081: }
082:
083: /**
084: * @return Returns the id.
085: */
086: public String getId() {
087: return id;
088: }
089:
090: /**
091: * Create a link helper instance from this descriptors class attribute.
092: *
093: * @return An instance of the helper that is defined by the extension, or a
094: * Skeleton Link Helper.
095: */
096: public ILinkHelper createLinkHelper() {
097: if (hasLinkHelperFailedCreation)
098: return SkeletonLinkHelper.INSTANCE;
099: try {
100: return (ILinkHelper) configElement
101: .createExecutableExtension(ATT_CLASS);
102: } catch (Throwable t) {
103: hasLinkHelperFailedCreation = true;
104: NavigatorPlugin.logError(0, t.getMessage(), t);
105: }
106: return SkeletonLinkHelper.INSTANCE;
107: }
108:
109: /**
110: *
111: * @param anInput
112: * The editor input from the editor that was activated.
113: * @return True if this linkHelper descriptor can produce a selection from
114: * the editor input.
115: */
116: public boolean isEnabledFor(IEditorInput anInput) {
117:
118: if (editorInputEnablement == null || anInput == null) {
119: return false;
120: }
121:
122: try {
123: EvaluationContext context = new EvaluationContext(null,
124: anInput);
125: context.setAllowPluginActivation(true);
126: return (editorInputEnablement.evaluate(context) == EvaluationResult.TRUE);
127: } catch (CoreException e) {
128: NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e);
129: }
130: return false;
131: }
132:
133: /**
134: * @param anObject
135: * The selection from the CommonViewer
136: * @return True if this dscriptor can determine a valid editor to activate
137: * from the selection.
138: */
139: public boolean isEnabledFor(Object anObject) {
140: if (selectionEnablement == null) {
141: return false;
142: }
143:
144: IEvaluationContext context = new EvaluationContext(null,
145: anObject);
146: context.setAllowPluginActivation(true);
147: try {
148: if (selectionEnablement.evaluate(context) != EvaluationResult.TRUE) {
149: return false;
150: }
151: } catch (CoreException e) {
152: NavigatorPlugin.log(IStatus.ERROR, 0, e.getMessage(), e);
153: return false;
154: }
155: return true;
156: }
157: }
|