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.expressions;
011:
012: import org.eclipse.core.expressions.EvaluationResult;
013: import org.eclipse.core.expressions.ExpressionInfo;
014: import org.eclipse.core.expressions.IEvaluationContext;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.ui.ISources;
017: import org.eclipse.ui.IWorkbenchWindow;
018:
019: /**
020: * <p>
021: * An expression representing the <code>targetId</code> of the legacy view
022: * contributions.
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 final class LegacyViewContributionExpression extends
032: WorkbenchWindowExpression {
033:
034: /**
035: * The seed for the hash code for all schemes.
036: */
037: private static final int HASH_INITIAL = LegacyViewContributionExpression.class
038: .getName().hashCode();
039:
040: /**
041: * The identifier for the part that must be active for this expression to
042: * evaluate to <code>true</code>. This value is never <code>null</code>.
043: */
044: private final String activePartId;
045:
046: /**
047: * Constructs a new instance of
048: * <code>LegacyViewContributionExpression</code>
049: *
050: * @param activePartId
051: * The identifier of the part to match with the active part; may
052: * be <code>null</code>
053: * @param window
054: * The workbench window in which this handler should be active.
055: * This value is never <code>null</code>.
056: */
057: public LegacyViewContributionExpression(final String activePartId,
058: final IWorkbenchWindow window) {
059: super (window);
060:
061: if (activePartId == null) {
062: throw new NullPointerException(
063: "The targetId for a view contribution must not be null"); //$NON-NLS-1$
064: }
065: this .activePartId = activePartId;
066: }
067:
068: public final void collectExpressionInfo(final ExpressionInfo info) {
069: super .collectExpressionInfo(info);
070: info.addVariableNameAccess(ISources.ACTIVE_PART_ID_NAME);
071: }
072:
073: protected final int computeHashCode() {
074: int hashCode = HASH_INITIAL * HASH_FACTOR
075: + hashCode(getWindow());
076: hashCode = hashCode * HASH_FACTOR + hashCode(activePartId);
077: return hashCode;
078: }
079:
080: public final boolean equals(final Object object) {
081: if (object instanceof LegacyViewContributionExpression) {
082: final LegacyViewContributionExpression that = (LegacyViewContributionExpression) object;
083: return equals(this .activePartId, that.activePartId)
084: && equals(this .getWindow(), that.getWindow());
085: }
086:
087: return false;
088: }
089:
090: public final EvaluationResult evaluate(
091: final IEvaluationContext context) throws CoreException {
092: final EvaluationResult result = super .evaluate(context);
093: if (result == EvaluationResult.FALSE) {
094: return result;
095: }
096:
097: final Object variable = context
098: .getVariable(ISources.ACTIVE_PART_ID_NAME);
099: if (equals(activePartId, variable)) {
100: return EvaluationResult.TRUE;
101: }
102: return EvaluationResult.FALSE;
103: }
104:
105: public final String toString() {
106: final StringBuffer buffer = new StringBuffer();
107: buffer.append("LegacyViewContributionExpression("); //$NON-NLS-1$
108: buffer.append(activePartId);
109: buffer.append(',');
110: buffer.append(getWindow());
111: buffer.append(')');
112: return buffer.toString();
113: }
114:
115: }
|