diff options
Diffstat (limited to 'org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/GenericContentTypeRelatedExtension.java')
-rw-r--r-- | org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/GenericContentTypeRelatedExtension.java | 56 |
1 files changed, 28 insertions, 28 deletions
diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/GenericContentTypeRelatedExtension.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/GenericContentTypeRelatedExtension.java index b707112fbe7..7159e378c2c 100644 --- a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/GenericContentTypeRelatedExtension.java +++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/GenericContentTypeRelatedExtension.java @@ -28,11 +28,10 @@ import org.eclipse.jface.text.source.ISourceViewer; import org.eclipse.ui.texteditor.ITextEditor; /** - * This class wraps and proxies an instance of T provided through extensions - * and loads it lazily when it can contribute to the editor, then delegates all operations to - * actual instance. + * This class wraps and proxies an instance of T provided through extensions and loads it lazily when it can contribute to the editor, then delegates all operations to actual instance. * - * @param <T> the actual type to proxy, typically the one defined on the extension point. + * @param <T> + * the actual type to proxy, typically the one defined on the extension point. */ public class GenericContentTypeRelatedExtension<T> { private static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$ @@ -46,13 +45,11 @@ public class GenericContentTypeRelatedExtension<T> { public GenericContentTypeRelatedExtension(IConfigurationElement element) throws Exception { this.extension = element; - this.targetContentType = Platform.getContentTypeManager() - .getContentType(element.getAttribute(CONTENT_TYPE_ATTRIBUTE)); + this.targetContentType = Platform.getContentTypeManager().getContentType(element.getAttribute(CONTENT_TYPE_ATTRIBUTE)); this.enabledWhen = buildEnabledWhen(element); } - @SuppressWarnings("unchecked") - public T createDelegate() { + @SuppressWarnings("unchecked") public T createDelegate() { try { return (T) extension.createExecutableExtension(CLASS_ATTRIBUTE); } catch (CoreException e) { @@ -62,22 +59,21 @@ public class GenericContentTypeRelatedExtension<T> { } /** - * Returns the expression {@link Expression} declared in the - * <code>enabledWhen</code> element. + * Returns the expression {@link Expression} declared in the <code>enabledWhen</code> element. * - * @param configElement the configuration element - * @return the expression {@link Expression} declared in the enabledWhen - * element. - * @throws CoreException when enabledWhen expression is not valid. + * @param configElement + * the configuration element + * @return the expression {@link Expression} declared in the enabledWhen element. + * @throws CoreException + * when enabledWhen expression is not valid. */ private static Expression buildEnabledWhen(IConfigurationElement configElement) throws CoreException { final IConfigurationElement[] children = configElement.getChildren(ENABLED_WHEN_ATTRIBUTE); if (children.length > 0) { IConfigurationElement[] subChildren = children[0].getChildren(); if (subChildren.length != 1) { - throw new CoreException(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, - "One <enabledWhen> element is accepted. Disabling " //$NON-NLS-1$ - + configElement.getAttribute(ID_ATTRIBUTE))); + throw new CoreException(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, "One <enabledWhen> element is accepted. Disabling " //$NON-NLS-1$ + + configElement.getAttribute(ID_ATTRIBUTE))); } final ElementHandler elementHandler = ElementHandler.getDefault(); final ExpressionConverter converter = ExpressionConverter.getDefault(); @@ -87,28 +83,32 @@ public class GenericContentTypeRelatedExtension<T> { } /** - * Returns true if the given viewer, editor matches the enabledWhen expression - * and false otherwise. + * Returns true if the given viewer, editor matches the enabledWhen expression and false otherwise. * - * @param viewer the viewer - * @param editor the editor - * @return true if the given viewer, editor matches the enabledWhen expression - * and false otherwise. + * @param viewer + * the viewer + * @param editor + * the editor + * @return true if the given viewer, editor matches the enabledWhen expression and false otherwise. */ public boolean matches(ISourceViewer viewer, ITextEditor editor) { if (enabledWhen == null) { return true; } - EvaluationContext context = new EvaluationContext(null, editor); + EvaluationContext context = new EvaluationContext(null, editor != null ? editor : viewer); context.setAllowPluginActivation(true); context.addVariable("viewer", viewer); //$NON-NLS-1$ - context.addVariable("editor", editor); //$NON-NLS-1$ - context.addVariable("editorInput", editor.getEditorInput()); //$NON-NLS-1$ + if (viewer.getDocument() != null) { + context.addVariable("document", viewer.getDocument()); //$NON-NLS-1$ + } + if (editor != null) { + context.addVariable("editor", editor); //$NON-NLS-1$ + context.addVariable("editorInput", editor.getEditorInput()); //$NON-NLS-1$ + } try { return enabledWhen.evaluate(context) == EvaluationResult.TRUE; } catch (CoreException e) { - GenericEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, - "Error while 'enabledWhen' evaluation", e)); //$NON-NLS-1$ + GenericEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, "Error while 'enabledWhen' evaluation", e)); //$NON-NLS-1$ return false; } } |