001: /*******************************************************************************
002: * Copyright (c) 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.expressions;
011:
012: import org.eclipse.core.expressions.EvaluationResult;
013: import org.eclipse.core.expressions.Expression;
014: import org.eclipse.core.expressions.ExpressionInfo;
015: import org.eclipse.core.expressions.IEvaluationContext;
016: import org.eclipse.ui.ISources;
017: import org.eclipse.ui.internal.services.SourcePriorityNameMapping;
018:
019: /**
020: * <p>
021: * An expression representing the <code>part id</code> of the legacy editor
022: * action bar contribution.
023: * </p>
024: * <p>
025: * This class is not intended for use outside of the
026: * <code>org.eclipse.ui.workbench</code> plug-in.
027: * </p>
028: *
029: * @since 3.2
030: */
031: public class LegacyEditorActionBarExpression extends Expression {
032: /**
033: * The seed for the hash code for all schemes.
034: */
035: private static final int HASH_INITIAL = LegacyEditorActionBarExpression.class
036: .getName().hashCode();
037:
038: /**
039: * The identifier for the editor that must be active for this expression to
040: * evaluate to <code>true</code>. This value is never <code>null</code>.
041: */
042: private final String activeEditorId;
043:
044: /**
045: * Constructs a new instance of <code>LegacyEditorActionBarExpression</code>
046: *
047: * @param editorId
048: * The identifier of the editor to match with the active editor;
049: * must not be <code>null</code>
050: */
051: public LegacyEditorActionBarExpression(final String editorId) {
052:
053: if (editorId == null) {
054: throw new IllegalArgumentException(
055: "The targetId for an editor contribution must not be null"); //$NON-NLS-1$
056: }
057: activeEditorId = editorId;
058: }
059:
060: public final void collectExpressionInfo(final ExpressionInfo info) {
061: info.addVariableNameAccess(ISources.ACTIVE_PART_ID_NAME);
062: info
063: .addVariableNameAccess(SourcePriorityNameMapping.LEGACY_LEGACY_NAME);
064: }
065:
066: protected final int computeHashCode() {
067: int hashCode = HASH_INITIAL * HASH_FACTOR
068: + hashCode(activeEditorId);
069: return hashCode;
070: }
071:
072: public final boolean equals(final Object object) {
073: if (object instanceof LegacyEditorActionBarExpression) {
074: final LegacyEditorActionBarExpression that = (LegacyEditorActionBarExpression) object;
075: return activeEditorId.equals(that.activeEditorId);
076: }
077:
078: return false;
079: }
080:
081: /*
082: * (non-Javadoc)
083: *
084: * @see org.eclipse.core.expressions.Expression#evaluate(org.eclipse.core.expressions.IEvaluationContext)
085: */
086: public final EvaluationResult evaluate(
087: final IEvaluationContext context) {
088: final Object variable = context
089: .getVariable(ISources.ACTIVE_PART_ID_NAME);
090: if (equals(activeEditorId, variable)) {
091: return EvaluationResult.TRUE;
092: }
093: return EvaluationResult.FALSE;
094: }
095:
096: public final String toString() {
097: final StringBuffer buffer = new StringBuffer();
098: buffer.append("LegacyEditorActionBarExpression("); //$NON-NLS-1$
099: buffer.append(activeEditorId);
100: buffer.append(')');
101: return buffer.toString();
102: }
103: }
|