01: /*******************************************************************************
02: * Copyright (c) 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: ******************************************************************************/package org.eclipse.ui.internal.expressions;
11:
12: import org.eclipse.core.expressions.EvaluationResult;
13: import org.eclipse.core.expressions.Expression;
14: import org.eclipse.core.expressions.ExpressionInfo;
15: import org.eclipse.core.expressions.IEvaluationContext;
16: import org.eclipse.ui.ISources;
17: import org.eclipse.ui.IWorkbenchPart;
18:
19: /**
20: * <p>
21: * An expression that is bound to a particular part instance.
22: * </p>
23: * <p>
24: * This class is not intended for use outside of the
25: * <code>org.eclipse.ui.workbench</code> plug-in.
26: * </p>
27: *
28: * @since 3.2
29: */
30: public final class ActivePartExpression extends Expression {
31:
32: /**
33: * The seed for the hash code for all schemes.
34: */
35: private static final int HASH_INITIAL = ActivePartExpression.class
36: .getName().hashCode();
37:
38: /**
39: * The part that must be active for this expression to evaluate to
40: * <code>true</code>. This value is never <code>null</code>.
41: */
42: private final IWorkbenchPart activePart;
43:
44: /**
45: * Constructs a new instance of <code>ActivePartExpression</code>
46: *
47: * @param activePart
48: * The part to match with the active part; may be
49: * <code>null</code>
50: */
51: public ActivePartExpression(final IWorkbenchPart activePart) {
52: if (activePart == null) {
53: throw new NullPointerException(
54: "The active part must not be null"); //$NON-NLS-1$
55: }
56: this .activePart = activePart;
57: }
58:
59: public final void collectExpressionInfo(final ExpressionInfo info) {
60: info.addVariableNameAccess(ISources.ACTIVE_PART_NAME);
61: }
62:
63: protected final int computeHashCode() {
64: return HASH_INITIAL * HASH_FACTOR + hashCode(activePart);
65: }
66:
67: public final boolean equals(final Object object) {
68: if (object instanceof ActivePartExpression) {
69: final ActivePartExpression that = (ActivePartExpression) object;
70: return equals(this .activePart, that.activePart);
71: }
72:
73: return false;
74: }
75:
76: public final EvaluationResult evaluate(
77: final IEvaluationContext context) {
78: final Object variable = context
79: .getVariable(ISources.ACTIVE_PART_NAME);
80: if (equals(activePart, variable)) {
81: return EvaluationResult.TRUE;
82: }
83: return EvaluationResult.FALSE;
84: }
85:
86: public final String toString() {
87: final StringBuffer buffer = new StringBuffer();
88: buffer.append("ActivePartExpression("); //$NON-NLS-1$
89: buffer.append(activePart);
90: buffer.append(')');
91: return buffer.toString();
92: }
93: }
|