|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 SAP SE. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * SAP SE - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.ui.internal.texteditor.stickyscroll; |
| 15 | + |
| 16 | +import org.eclipse.core.expressions.ElementHandler; |
| 17 | +import org.eclipse.core.expressions.EvaluationContext; |
| 18 | +import org.eclipse.core.expressions.EvaluationResult; |
| 19 | +import org.eclipse.core.expressions.Expression; |
| 20 | +import org.eclipse.core.expressions.ExpressionConverter; |
| 21 | + |
| 22 | +import org.eclipse.core.runtime.Assert; |
| 23 | +import org.eclipse.core.runtime.CoreException; |
| 24 | +import org.eclipse.core.runtime.IConfigurationElement; |
| 25 | +import org.eclipse.core.runtime.IStatus; |
| 26 | +import org.eclipse.core.runtime.Status; |
| 27 | + |
| 28 | +import org.eclipse.jface.text.source.ISourceViewer; |
| 29 | + |
| 30 | +import org.eclipse.ui.internal.editors.text.EditorsPlugin; |
| 31 | + |
| 32 | +import org.eclipse.ui.texteditor.ITextEditor; |
| 33 | +import org.eclipse.ui.texteditor.stickyscroll.IStickyLinesProvider; |
| 34 | + |
| 35 | +import org.eclipse.ui.editors.text.EditorsUI; |
| 36 | + |
| 37 | +/** |
| 38 | + * Describes an extension to the <code>stickyLinesProviders</code> extension point. |
| 39 | + * |
| 40 | + * @noextend This class is not intended to be extended by clients. |
| 41 | + */ |
| 42 | +class StickyLinesProviderDescriptor { |
| 43 | + /** Name of the <code>class</code> attribute. */ |
| 44 | + private static final String CLASS_ATTRIBUTE= "class"; //$NON-NLS-1$ |
| 45 | + |
| 46 | + /** Name of the <code>id</code> attribute. */ |
| 47 | + private static final String ID_ATTRIBUTE= "id"; //$NON-NLS-1$ |
| 48 | + |
| 49 | + /** Name of the <code>enabledWhen</code> attribute. **/ |
| 50 | + private static final String ENABLED_WHEN_ATTR= "enabledWhen"; //$NON-NLS-1$ |
| 51 | + |
| 52 | + /** The configuration element describing this extension. */ |
| 53 | + private IConfigurationElement configuration; |
| 54 | + |
| 55 | + /** The value of the <code>id</code> attribute, if read. */ |
| 56 | + private String id; |
| 57 | + |
| 58 | + /** The expression value of the <code>enabledWhen</code> attribute. */ |
| 59 | + private final Expression enabledWhen; |
| 60 | + |
| 61 | + /** |
| 62 | + * Creates a new descriptor for <code>element</code>. |
| 63 | + * <p> |
| 64 | + * This method is for internal use only. |
| 65 | + * </p> |
| 66 | + * |
| 67 | + * @param element the extension point element to be described. |
| 68 | + * @throws CoreException when <code>enabledWhen</code> expression is not valid. |
| 69 | + */ |
| 70 | + public StickyLinesProviderDescriptor(IConfigurationElement element) throws CoreException { |
| 71 | + Assert.isLegal(element != null); |
| 72 | + configuration= element; |
| 73 | + enabledWhen= createEnabledWhen(configuration, getId()); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Returns the expression {@link Expression} declared in the <code>enabledWhen</code> element. |
| 78 | + * |
| 79 | + * @param configElement the configuration element |
| 80 | + * @param id the id of the sticky lines provider. |
| 81 | + * @return the expression {@link Expression} declared in the enabledWhen element. |
| 82 | + * @throws CoreException when enabledWhen expression is not valid. |
| 83 | + */ |
| 84 | + private static Expression createEnabledWhen(IConfigurationElement configElement, String id) throws CoreException { |
| 85 | + final IConfigurationElement[] children= configElement.getChildren(ENABLED_WHEN_ATTR); |
| 86 | + if (children.length > 0) { |
| 87 | + IConfigurationElement[] subChildren= children[0].getChildren(); |
| 88 | + if (subChildren.length != 1) { |
| 89 | + throw new CoreException(new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, |
| 90 | + "One <enabledWhen> element is accepted. Disabling " + id)); //$NON-NLS-1$ |
| 91 | + } |
| 92 | + final ElementHandler elementHandler= ElementHandler.getDefault(); |
| 93 | + final ExpressionConverter converter= ExpressionConverter.getDefault(); |
| 94 | + return elementHandler.create(converter, subChildren[0]); |
| 95 | + } |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Reads (if needed) and returns the id of this extension. |
| 101 | + * |
| 102 | + * @return the id for this extension. |
| 103 | + */ |
| 104 | + public String getId() { |
| 105 | + if (id == null) { |
| 106 | + id= configuration.getAttribute(ID_ATTRIBUTE); |
| 107 | + Assert.isNotNull(id); |
| 108 | + } |
| 109 | + return id; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Creates a sticky lines provider as described in the extension's XML and null otherwise. |
| 114 | + * |
| 115 | + * @return the created sticky lines provider and null otherwise. |
| 116 | + */ |
| 117 | + protected IStickyLinesProvider createStickyLinesProvider() { |
| 118 | + try { |
| 119 | + Object extension= configuration.createExecutableExtension(CLASS_ATTRIBUTE); |
| 120 | + if (extension instanceof IStickyLinesProvider stickyLinesProvider) { |
| 121 | + return stickyLinesProvider; |
| 122 | + } else { |
| 123 | + String message= "Invalid extension to stickyLinesProvider. Must extends IStickyLinesProvider: " //$NON-NLS-1$ |
| 124 | + + getId(); |
| 125 | + EditorsPlugin.getDefault().getLog() |
| 126 | + .log(new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, message)); |
| 127 | + return null; |
| 128 | + } |
| 129 | + } catch (CoreException e) { |
| 130 | + EditorsPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, |
| 131 | + "Error while creating stickyLinesProvider: " + getId(), e)); //$NON-NLS-1$ |
| 132 | + return null; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns true if the given viewer, editor matches the enabledWhen expression and false |
| 138 | + * otherwise. |
| 139 | + * |
| 140 | + * @param viewer the viewer |
| 141 | + * @param editor the editor |
| 142 | + * @return true if the given viewer, editor matches the enabledWhen expression and false |
| 143 | + * otherwise. |
| 144 | + */ |
| 145 | + public boolean matches(ISourceViewer viewer, ITextEditor editor) { |
| 146 | + if (enabledWhen == null) { |
| 147 | + return true; |
| 148 | + } |
| 149 | + EvaluationContext context= new EvaluationContext(null, editor); |
| 150 | + context.setAllowPluginActivation(true); |
| 151 | + context.addVariable("viewer", viewer); //$NON-NLS-1$ |
| 152 | + context.addVariable("editor", editor); //$NON-NLS-1$ |
| 153 | + context.addVariable("editorInput", editor.getEditorInput()); //$NON-NLS-1$ |
| 154 | + try { |
| 155 | + return enabledWhen.evaluate(context) == EvaluationResult.TRUE; |
| 156 | + } catch (CoreException e) { |
| 157 | + EditorsPlugin.getDefault().getLog().log( |
| 158 | + new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, "Error while 'enabledWhen' evaluation", e)); //$NON-NLS-1$ |
| 159 | + return false; |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments