KickJava   Java API By Example, From Geeks To Geeks.

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


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.ISelection;
20 import org.eclipse.ui.ISources;
21 import org.eclipse.ui.IWorkbenchWindow;
22 import org.eclipse.ui.SelectionEnabler;
23
24 /**
25  * <p>
26  * An expression wrapper for the legacy {@link SelectionEnabler}. This emulates
27  * an {@link Expression} using an instance of <code>SelectionEnabler</code>.
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 LegacySelectionEnablerWrapper extends
37         WorkbenchWindowExpression {
38
39     /**
40      * The seed for the hash code for all schemes.
41      */

42     private static final int HASH_INITIAL = LegacySelectionEnablerWrapper.class
43             .getName().hashCode();
44
45     /**
46      * The enabler for this expression; never <code>null</code>.
47      */

48     private final SelectionEnabler enabler;
49
50     /**
51      * Constructs a new instance of <code>SelectionEnablerExpression</code>.
52      *
53      * @param enabler
54      * The enabler; 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 LegacySelectionEnablerWrapper(final SelectionEnabler enabler,
61             final IWorkbenchWindow window) {
62         super(window);
63
64         if (enabler == null) {
65             throw new NullPointerException JavaDoc("There is no enabler"); //$NON-NLS-1$
66
}
67         this.enabler = enabler;
68     }
69
70     public final void collectExpressionInfo(final ExpressionInfo info) {
71         super.collectExpressionInfo(info);
72         info.markDefaultVariableAccessed();
73     }
74
75     protected final int computeHashCode() {
76         int hashCode = HASH_INITIAL * HASH_FACTOR + hashCode(getWindow());
77         hashCode = hashCode * HASH_FACTOR + hashCode(enabler);
78         return hashCode;
79     }
80
81     public final boolean equals(final Object JavaDoc object) {
82         if (object instanceof LegacySelectionEnablerWrapper) {
83             final LegacySelectionEnablerWrapper that = (LegacySelectionEnablerWrapper) object;
84             return equals(this.enabler, that.enabler)
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         final Object JavaDoc defaultVariable = context
99                 .getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);
100         if (defaultVariable instanceof ISelection) {
101             final ISelection selection = (ISelection) defaultVariable;
102             if (enabler.isEnabledForSelection(selection)) {
103                 return EvaluationResult.TRUE;
104             }
105         }
106
107         return EvaluationResult.FALSE;
108     }
109
110     public final String JavaDoc toString() {
111         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
112         buffer.append("LegacySelectionEnablerWrapper("); //$NON-NLS-1$
113
buffer.append(enabler);
114         buffer.append(',');
115         buffer.append(getWindow());
116         buffer.append(')');
117         return buffer.toString();
118     }
119 }
120
Popular Tags