001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui;
011:
012: import org.eclipse.core.expressions.EvaluationResult;
013: import org.eclipse.core.expressions.Expression;
014: import org.eclipse.core.expressions.ExpressionInfo;
015: import org.eclipse.core.expressions.IEvaluationContext;
016: import org.eclipse.swt.widgets.Shell;
017:
018: /**
019: * <p>
020: * An expression encapsulating all of the information from legacy handler
021: * submissions.
022: * </p>
023: *
024: * @since 3.1
025: */
026: public final class LegacyHandlerSubmissionExpression extends Expression {
027:
028: /**
029: * The seed for the hash code for all schemes.
030: */
031: private static final int HASH_INITIAL = LegacyHandlerSubmissionExpression.class
032: .getName().hashCode();
033:
034: /**
035: * The identifier for the part that must be active for this expression to
036: * evaluate to <code>true</code>. If this value is <code>null</code>,
037: * then any part may be active.
038: */
039: private final String activePartId;
040:
041: /**
042: * The shell that must be active for this expression to evaluate to
043: * <code>true</code>. If this value is <code>null</code>, then any
044: * shell may be active.
045: */
046: private final Shell activeShell;
047:
048: /**
049: * The site that must be active for this expression to evaluate to
050: * <code>true</code>. If this value is <code>null</code>, then any
051: * site may be active.
052: */
053: private final IWorkbenchPartSite activeSite;
054:
055: /**
056: * Constructs a new instance of
057: * <code>LegacyHandlerSubmissionExpression</code>
058: *
059: * @param activePartId
060: * The part identifier to match with the active part;
061: * <code>null</code> if it will match any active part.
062: * @param activeShell
063: * The shell to match with the active shell; <code>null</code>
064: * if it will match any active shell.
065: * @param activeSite
066: * The site to match with the active site; <code>null</code> if
067: * it will match any active site.
068: */
069: public LegacyHandlerSubmissionExpression(final String activePartId,
070: final Shell activeShell, final IWorkbenchPartSite activeSite) {
071:
072: this .activePartId = activePartId;
073: this .activeShell = activeShell;
074: this .activeSite = activeSite;
075: }
076:
077: /**
078: * Collect expression info for a legacy handler submission. Namely
079: * the active part id and name, active shell name, active workbench
080: * window shell name and the active site name.
081: *
082: * @since 3.2
083: */
084: public final void collectExpressionInfo(final ExpressionInfo info) {
085: if (activePartId != null) {
086: info.addVariableNameAccess(ISources.ACTIVE_PART_ID_NAME);
087: }
088: if (activeShell != null) {
089: info.addVariableNameAccess(ISources.ACTIVE_SHELL_NAME);
090: info
091: .addVariableNameAccess(ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME);
092: }
093: if (activeSite != null) {
094: info.addVariableNameAccess(ISources.ACTIVE_SITE_NAME);
095: }
096: }
097:
098: protected final int computeHashCode() {
099: int hashCode = HASH_INITIAL * HASH_FACTOR
100: + hashCode(activePartId);
101: hashCode = hashCode * HASH_FACTOR + hashCode(activeShell);
102: hashCode = hashCode * HASH_FACTOR + hashCode(activeSite);
103: return hashCode;
104: }
105:
106: public final boolean equals(final Object object) {
107: if (object instanceof LegacyHandlerSubmissionExpression) {
108: final LegacyHandlerSubmissionExpression that = (LegacyHandlerSubmissionExpression) object;
109: return equals(this .activePartId, that.activePartId)
110: && equals(this .activeShell, that.activeShell)
111: && equals(this .activeSite, that.activeSite);
112: }
113:
114: return false;
115: }
116:
117: /**
118: * Evaluates this expression. This tests the three conditions against the
119: * current state of the application (as defined by <code>context</code>).
120: * If a condition is <code>null</code>, then it matches any possible
121: * value (i.e., it is not tested at all).
122: *
123: * @param context
124: * The context providing the current workbench state; must not be
125: * <code>null</code>.
126: * @return <code>EvaluationResult.TRUE</code> if the conditions all
127: * matches; <code>EvaluationResult.FALSE</code> otherwise.
128: */
129: public final EvaluationResult evaluate(
130: final IEvaluationContext context) {
131: if (activePartId != null) {
132: final Object value = context
133: .getVariable(ISources.ACTIVE_PART_ID_NAME);
134: if (!activePartId.equals(value)) {
135: return EvaluationResult.FALSE;
136: }
137: }
138:
139: if (activeShell != null) {
140: Object value = context
141: .getVariable(ISources.ACTIVE_SHELL_NAME);
142: if (!activeShell.equals(value)) {
143: value = context
144: .getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME);
145: if (!activeShell.equals(value)) {
146: return EvaluationResult.FALSE;
147: }
148: }
149: }
150:
151: if (activeSite != null) {
152: final Object value = context
153: .getVariable(ISources.ACTIVE_SITE_NAME);
154: if (!activeSite.equals(value)) {
155: return EvaluationResult.FALSE;
156: }
157: }
158:
159: return EvaluationResult.TRUE;
160: }
161:
162: public final String toString() {
163: final StringBuffer buffer = new StringBuffer();
164: buffer.append("LegacyHandlerSubmission("); //$NON-NLS-1$
165: buffer.append(activeShell);
166: buffer.append(',');
167: buffer.append(activePartId);
168: buffer.append(',');
169: buffer.append(activeSite);
170: buffer.append(')');
171: return buffer.toString();
172: }
173: }
|