KickJava   Java API By Example, From Geeks To Geeks.

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


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.ExpressionInfo;
16 import org.eclipse.core.expressions.IEvaluationContext;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.ui.ISources;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.internal.registry.IActionSetDescriptor;
21
22 /**
23  * <p>
24  * An expression that evaluates whether a particular action set is active.
25  * </p>
26  * <p>
27  * This class is not intended for use outside of the
28  * <code>org.eclipse.ui.workbench</code> plug-in.
29  * </p>
30  *
31  * @since 3.2
32  */

33 public final class LegacyActionSetExpression extends WorkbenchWindowExpression {
34
35     /**
36      * The seed for the hash code for all schemes.
37      */

38     private static final int HASH_INITIAL = LegacyActionSetExpression.class
39             .getName().hashCode();
40
41     /**
42      * The identifier of the action set that must be active for this expression
43      * to evaluate to <code>true</code>. This value is never
44      * <code>null</code>.
45      */

46     private final String JavaDoc actionSetId;
47
48     /**
49      * Constructs a new instance of {@link LegacyActionSetExpression}.
50      *
51      * @param actionSetId
52      * The identifier of the action set that must be active for this
53      * expression to evaluate to <code>true</code>; must not be
54      * <code>null</code>.
55      * @param window
56      * The workbench window in which this handler should be active.
57      * This avoids conflicts between handlers from different windows.
58      * This should not be <code>null</code>.
59      */

60     public LegacyActionSetExpression(final String JavaDoc actionSetId,
61             final IWorkbenchWindow window) {
62         super(window);
63         if (actionSetId == null) {
64             throw new NullPointerException JavaDoc(
65                     "The action set identifier cannot be null"); //$NON-NLS-1$
66
}
67         this.actionSetId = actionSetId;
68     }
69
70     public final void collectExpressionInfo(final ExpressionInfo info) {
71         super.collectExpressionInfo(info);
72         info.addVariableNameAccess(ISources.ACTIVE_ACTION_SETS_NAME);
73     }
74
75     protected final int computeHhashCode() {
76         int hashCode = HASH_INITIAL * HASH_FACTOR + hashCode(getWindow());
77         hashCode = hashCode * HASH_FACTOR + hashCode(actionSetId);
78         return hashCode;
79     }
80
81     public final boolean equals(final Object JavaDoc object) {
82         if (object instanceof LegacyActionSetExpression) {
83             final LegacyActionSetExpression that = (LegacyActionSetExpression) object;
84             return equals(this.actionSetId, that.actionSetId)
85                     && equals(this.getWindow(), that.getWindow());
86         }
87
88         return false;
89     }
90
91     public final EvaluationResult evaluate(final IEvaluationContext context)
92             throws CoreException {
93         final EvaluationResult result = super.evaluate(context);
94         if (result == EvaluationResult.FALSE) {
95             return result;
96         }
97
98         // Check the action sets.
99
final Object JavaDoc variable = context
100                 .getVariable(ISources.ACTIVE_ACTION_SETS_NAME);
101         if (variable instanceof IActionSetDescriptor[]) {
102             final IActionSetDescriptor[] descriptors = (IActionSetDescriptor[]) variable;
103             for (int i = 0; i < descriptors.length; i++) {
104                 final IActionSetDescriptor descriptor = descriptors[i];
105                 final String JavaDoc currentId = descriptor.getId();
106                 if (actionSetId.equals(currentId)) {
107                     return EvaluationResult.TRUE;
108                 }
109             }
110         }
111
112         return EvaluationResult.FALSE;
113     }
114
115     public final String JavaDoc toString() {
116         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
117         buffer.append("ActionSetExpression("); //$NON-NLS-1$
118
buffer.append(actionSetId);
119         buffer.append(',');
120         buffer.append(getWindow());
121         buffer.append(')');
122         return buffer.toString();
123     }
124 }
125
Popular Tags