KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.core.runtime.CoreException;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.ui.ISources;
21 import org.eclipse.ui.IWorkbenchWindow;
22 import org.eclipse.ui.internal.ActionExpression;
23
24 /**
25  * <p>
26  * This wrappers the old {@link ActionExpression} class so that it can
27  * communicate via the {@link Expression} contract.
28  * </p>
29  * <p>
30  * This class is not intended for use outside of the
31  * <code>org.eclipse.ui.workbench</code> plug-in.
32  * </p>
33  *
34  * @since 3.2
35  */

36 public final class LegacyActionExpressionWrapper extends
37         WorkbenchWindowExpression {
38
39     /**
40      * The seed for the hash code for all schemes.
41      */

42     private static final int HASH_INITIAL = LegacyActionExpressionWrapper.class
43             .getName().hashCode();
44
45     /**
46      * The legacy action expression being wrapped; never <code>null</code>.
47      */

48     private final ActionExpression expression;
49
50     /**
51      * Constructs a new instance of {@link LegacyActionExpressionWrapper}.
52      *
53      * @param expression
54      * The expression to wrap; must not be <code>null</code>.
55      * @param window
56      * The workbench window which must be active for this expression
57      * to evaluate to <code>true</code>; may be <code>null</code>
58      * if the window should be disregarded.
59      */

60     public LegacyActionExpressionWrapper(final ActionExpression expression,
61             final IWorkbenchWindow window) {
62         super(window);
63
64         if (expression == null) {
65             throw new NullPointerException JavaDoc(
66                     "The action expression cannot be null"); //$NON-NLS-1$
67
}
68         this.expression = expression;
69     }
70
71     public final void collectExpressionInfo(final ExpressionInfo info) {
72         super.collectExpressionInfo(info);
73         info.markDefaultVariableAccessed();
74     }
75
76     protected final int computeHashCode() {
77         int hashCode = HASH_INITIAL * HASH_FACTOR + hashCode(getWindow());
78         hashCode = hashCode * HASH_FACTOR + hashCode(expression);
79         return hashCode;
80     }
81
82     public final boolean equals(final Object JavaDoc object) {
83         if (object instanceof LegacyActionExpressionWrapper) {
84             final LegacyActionExpressionWrapper that = (LegacyActionExpressionWrapper) object;
85             return equals(this.expression, that.expression)
86                     && equals(this.getWindow(), that.getWindow());
87         }
88
89         return false;
90     }
91
92     public final EvaluationResult evaluate(final IEvaluationContext context)
93             throws CoreException {
94         final EvaluationResult result = super.evaluate(context);
95         if (result == EvaluationResult.FALSE) {
96             return result;
97         }
98
99         final Object JavaDoc defaultVariable = context
100                 .getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);
101         if (defaultVariable instanceof IStructuredSelection) {
102             final IStructuredSelection selection = (IStructuredSelection) defaultVariable;
103             if (expression.isEnabledFor(selection)) {
104                 return EvaluationResult.TRUE;
105             }
106         } else if (expression.isEnabledFor(defaultVariable)) {
107             return EvaluationResult.TRUE;
108         }
109
110         return EvaluationResult.FALSE;
111     }
112
113     public final String JavaDoc toString() {
114         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
115         buffer.append("LegacyActionExpressionWrapper("); //$NON-NLS-1$
116
buffer.append(expression);
117         buffer.append(',');
118         buffer.append(getWindow());
119         buffer.append(')');
120         return buffer.toString();
121     }
122 }
123
Popular Tags