KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > expressions > ActivePartExpression


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.ui.internal.expressions;
13
14 import org.eclipse.core.expressions.EvaluationResult;
15 import org.eclipse.core.expressions.Expression;
16 import org.eclipse.core.expressions.ExpressionInfo;
17 import org.eclipse.core.expressions.IEvaluationContext;
18 import org.eclipse.ui.ISources;
19 import org.eclipse.ui.IWorkbenchPart;
20
21 /**
22  * <p>
23  * An expression that is bound to a particular part instance.
24  * </p>
25  * <p>
26  * This class is not intended for use outside of the
27  * <code>org.eclipse.ui.workbench</code> plug-in.
28  * </p>
29  *
30  * @since 3.2
31  */

32 public final class ActivePartExpression extends Expression {
33
34     /**
35      * The seed for the hash code for all schemes.
36      */

37     private static final int HASH_INITIAL = ActivePartExpression.class
38             .getName().hashCode();
39
40     /**
41      * The part that must be active for this expression to evaluate to
42      * <code>true</code>. This value is never <code>null</code>.
43      */

44     private final IWorkbenchPart activePart;
45
46     /**
47      * Constructs a new instance of <code>ActivePartExpression</code>
48      *
49      * @param activePart
50      * The part to match with the active part; may be
51      * <code>null</code>
52      */

53     public ActivePartExpression(final IWorkbenchPart activePart) {
54         if (activePart == null) {
55             throw new NullPointerException JavaDoc("The active part must not be null"); //$NON-NLS-1$
56
}
57         this.activePart = activePart;
58     }
59
60     public final void collectExpressionInfo(final ExpressionInfo info) {
61         info.addVariableNameAccess(ISources.ACTIVE_PART_NAME);
62     }
63
64     protected final int computeHashCode() {
65         return HASH_INITIAL * HASH_FACTOR + hashCode(activePart);
66     }
67
68     public final boolean equals(final Object JavaDoc object) {
69         if (object instanceof ActivePartExpression) {
70             final ActivePartExpression that = (ActivePartExpression) object;
71             return equals(this.activePart, that.activePart);
72         }
73
74         return false;
75     }
76
77     public final EvaluationResult evaluate(final IEvaluationContext context) {
78         final Object JavaDoc variable = context.getVariable(ISources.ACTIVE_PART_NAME);
79         if (equals(activePart, variable)) {
80             return EvaluationResult.TRUE;
81         }
82         return EvaluationResult.FALSE;
83     }
84
85     public final String JavaDoc toString() {
86         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
87         buffer.append("ActivePartExpression("); //$NON-NLS-1$
88
buffer.append(activePart);
89         buffer.append(')');
90         return buffer.toString();
91     }
92 }
93
Popular Tags