Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a2ab67afb210235f039028d089cd9ae7bea1ccbf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*******************************************************************************
 * Copyright (c) 2006, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 ******************************************************************************/

package org.eclipse.ui.internal.expressions;

import org.eclipse.core.expressions.EvaluationResult;
import org.eclipse.core.expressions.Expression;
import org.eclipse.core.expressions.ExpressionInfo;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.ui.ISources;
import org.eclipse.ui.internal.services.SourcePriorityNameMapping;

/**
 * <p>
 * An expression representing the <code>part id</code> of the legacy editor
 * action bar contribution.
 * </p>
 * <p>
 * This class is not intended for use outside of the
 * <code>org.eclipse.ui.workbench</code> plug-in.
 * </p>
 *
 * @since 3.2
 */
public class LegacyEditorActionBarExpression extends Expression {
	/**
	 * The seed for the hash code for all schemes.
	 */
	private static final int HASH_INITIAL = LegacyEditorActionBarExpression.class.getName().hashCode();

	/**
	 * The identifier for the editor that must be active for this expression to
	 * evaluate to <code>true</code>. This value is never <code>null</code>.
	 */
	private final String activeEditorId;

	/**
	 * Constructs a new instance of <code>LegacyEditorActionBarExpression</code>
	 *
	 * @param editorId The identifier of the editor to match with the active editor;
	 *                 must not be <code>null</code>
	 */
	public LegacyEditorActionBarExpression(final String editorId) {

		if (editorId == null) {
			throw new IllegalArgumentException("The targetId for an editor contribution must not be null"); //$NON-NLS-1$
		}
		activeEditorId = editorId;
	}

	@Override
	public final void collectExpressionInfo(final ExpressionInfo info) {
		info.addVariableNameAccess(ISources.ACTIVE_PART_ID_NAME);
		info.addVariableNameAccess(SourcePriorityNameMapping.LEGACY_LEGACY_NAME);
	}

	@Override
	protected final int computeHashCode() {
		int hashCode = HASH_INITIAL * HASH_FACTOR + hashCode(activeEditorId);
		return hashCode;
	}

	@Override
	public final boolean equals(final Object object) {
		if (object instanceof LegacyEditorActionBarExpression) {
			final LegacyEditorActionBarExpression that = (LegacyEditorActionBarExpression) object;
			return activeEditorId.equals(that.activeEditorId);
		}

		return false;
	}

	@Override
	public final EvaluationResult evaluate(final IEvaluationContext context) {
		final Object variable = context.getVariable(ISources.ACTIVE_PART_ID_NAME);
		if (equals(activeEditorId, variable)) {
			return EvaluationResult.TRUE;
		}
		return EvaluationResult.FALSE;
	}

	@Override
	public final String toString() {
		final StringBuilder buffer = new StringBuilder();
		buffer.append("LegacyEditorActionBarExpression("); //$NON-NLS-1$
		buffer.append(activeEditorId);
		buffer.append(')');
		return buffer.toString();
	}
}

Back to the top