001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 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.internal.handlers;
011:
012: import java.io.BufferedWriter;
013: import java.io.IOException;
014: import java.io.StringWriter;
015:
016: import org.eclipse.core.commands.IHandler;
017: import org.eclipse.core.expressions.Expression;
018: import org.eclipse.core.expressions.IEvaluationContext;
019: import org.eclipse.ui.ISources;
020: import org.eclipse.ui.handlers.IHandlerActivation;
021: import org.eclipse.ui.handlers.IHandlerService;
022: import org.eclipse.ui.internal.services.EvaluationResultCache;
023:
024: /**
025: * <p>
026: * A token representing the activation of a handler. This token can later be
027: * used to cancel that activation. Without this token, then handler will only
028: * become inactive if the component in which the handler was activated is
029: * destroyed.
030: * </p>
031: * <p>
032: * This caches the command id and the handler, so that they can later be
033: * identified.
034: * </p>
035: * <p>
036: * <b>Note:</b> this class has a natural ordering that is inconsistent with
037: * equals.
038: * </p>
039: *
040: * @since 3.1
041: */
042: final class HandlerActivation extends EvaluationResultCache implements
043: IHandlerActivation {
044:
045: /**
046: * The identifier for the command which the activated handler handles. This
047: * value is never <code>null</code>.
048: */
049: private final String commandId;
050:
051: /**
052: * The depth of services at which this token was created. This is used as a
053: * final tie-breaker if all other things are equivalent.
054: */
055: private final int depth;
056:
057: /**
058: * The handler that has been activated. This value may be <code>null</code>.
059: */
060: private final IHandler handler;
061:
062: /**
063: * The handler service from which this handler activation was request. This
064: * value is never <code>null</code>.
065: */
066: private final IHandlerService handlerService;
067:
068: /**
069: * Constructs a new instance of <code>HandlerActivation</code>.
070: *
071: * @param commandId
072: * The identifier for the command which the activated handler
073: * handles. This value must not be <code>null</code>.
074: * @param handler `
075: * The handler that has been activated. This value may be
076: * <code>null</code>.
077: * @param expression
078: * The expression that must evaluate to <code>true</code>
079: * before this handler is active. This value may be
080: * <code>null</code> if it is always active.</code>.
081: * @param depth
082: * The depth at which this activation was created within the
083: * services hierarchy. This is used as the final tie-breaker if
084: * all other conditions are equal. This should be a positive
085: * integer.
086: * @param handlerService
087: * The handler service from which the handler activation was
088: * requested; must not be <code>null</code>.
089: * @see ISources
090: */
091: HandlerActivation(final String commandId, final IHandler handler,
092: final Expression expression, final int depth,
093: final IHandlerService handlerService) {
094: super (expression);
095:
096: if (commandId == null) {
097: throw new NullPointerException(
098: "The command identifier for a handler activation cannot be null"); //$NON-NLS-1$
099: }
100:
101: if (handlerService == null) {
102: throw new NullPointerException(
103: "The handler service for an activation cannot be null"); //$NON-NLS-1$
104: }
105:
106: this .commandId = commandId;
107: this .depth = depth;
108: this .handler = handler;
109: this .handlerService = handlerService;
110: }
111:
112: public final void clearActive() {
113: clearResult();
114: }
115:
116: /**
117: * Implement {@link Comparable#compareTo(Object)}.
118: * <p>
119: * <b>Note:</b> this class has a natural ordering that is inconsistent with
120: * equals.
121: * </p>
122: */
123: public final int compareTo(final Object object) {
124: final IHandlerActivation activation = (IHandlerActivation) object;
125: int difference;
126:
127: // Check the priorities
128: int this Priority = this .getSourcePriority();
129: int thatPriority = activation.getSourcePriority();
130:
131: // rogue bit problem - ISources.ACTIVE_MENU
132: int this Lsb = 0;
133: int thatLsb = 0;
134:
135: if (((this Priority & ISources.ACTIVE_MENU) | (thatPriority & ISources.ACTIVE_MENU)) != 0) {
136: this Lsb = this Priority & 1;
137: this Priority = (this Priority >> 1) & 0x7fffffff;
138: thatLsb = thatPriority & 1;
139: thatPriority = (thatPriority >> 1) & 0x7fffffff;
140: }
141:
142: difference = this Priority - thatPriority;
143: if (difference != 0) {
144: return difference;
145: }
146:
147: // if all of the higher bits are the same, check the
148: // difference of the LSB
149: difference = this Lsb - thatLsb;
150: if (difference != 0) {
151: return difference;
152: }
153:
154: // Check depth
155: final int this Depth = this .getDepth();
156: final int thatDepth = activation.getDepth();
157: difference = this Depth - thatDepth;
158: return difference;
159: }
160:
161: public final String getCommandId() {
162: return commandId;
163: }
164:
165: public final int getDepth() {
166: return depth;
167: }
168:
169: public final IHandler getHandler() {
170: return handler;
171: }
172:
173: public final IHandlerService getHandlerService() {
174: return handlerService;
175: }
176:
177: public final boolean isActive(final IEvaluationContext context) {
178: return evaluate(context);
179: }
180:
181: public final String toString() {
182: final StringWriter sw = new StringWriter();
183: final BufferedWriter buffer = new BufferedWriter(sw);
184:
185: try {
186: buffer.write("HandlerActivation(commandId="); //$NON-NLS-1$
187: buffer.write(commandId);
188: buffer.write(',');
189: buffer.newLine();
190: buffer.write("\thandler="); //$NON-NLS-1$
191: buffer.write(handler == null ? "" : handler.toString()); //$NON-NLS-1$
192: buffer.write(',');
193: buffer.newLine();
194: buffer.write("\texpression="); //$NON-NLS-1$
195: Expression exp = getExpression();
196: buffer.write(exp == null ? "" : exp.toString()); //$NON-NLS-1$
197: buffer.write(",sourcePriority="); //$NON-NLS-1$
198: buffer.write(Integer.toString(getSourcePriority()));
199: buffer.write(')');
200: buffer.flush();
201: } catch (IOException e) {
202: // we're a string buffer, there should be no IO exception
203: }
204: return sw.toString();
205: }
206: }
|