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.internal.contexts;
011:
012: import org.eclipse.core.expressions.Expression;
013: import org.eclipse.core.expressions.IEvaluationContext;
014: import org.eclipse.ui.ISources;
015: import org.eclipse.ui.contexts.IContextActivation;
016: import org.eclipse.ui.contexts.IContextService;
017: import org.eclipse.ui.internal.services.EvaluationResultCache;
018:
019: /**
020: * <p>
021: * A token representing the activation of a context. This token can later be
022: * used to cancel that activation. Without this token, then the context will
023: * only become inactive if the component in which the context was activated is
024: * destroyed.
025: * </p>
026: * <p>
027: * This caches the context id, so that they can later be identified.
028: * </p>
029: * <p>
030: * Note: this class has a natural ordering that is inconsistent with equals.
031: * </p>
032: *
033: * @since 3.1
034: */
035: final class ContextActivation extends EvaluationResultCache implements
036: IContextActivation {
037:
038: /**
039: * The identifier for the context which should be active. This value is
040: * never <code>null</code>.
041: */
042: private final String contextId;
043:
044: /**
045: * The context service from which this context activation was requested.
046: * This value is never <code>null</code>.
047: */
048: private final IContextService contextService;
049:
050: /**
051: * Constructs a new instance of <code>ContextActivation</code>.
052: *
053: * @param contextId
054: * The identifier for the context which should be activated. This
055: * value must not be <code>null</code>.
056: * @param expression
057: * The expression that must evaluate to <code>true</code>
058: * before this handler is active. This value may be
059: * <code>null</code> if it is always active.
060: * @param contextService
061: * The context service from which the handler activation was
062: * requested; must not be <code>null</code>.
063: * @see ISources
064: */
065: public ContextActivation(final String contextId,
066: final Expression expression,
067: final IContextService contextService) {
068: super (expression);
069:
070: if (contextId == null) {
071: throw new NullPointerException(
072: "The context identifier for a context activation cannot be null"); //$NON-NLS-1$
073: }
074:
075: if (contextService == null) {
076: throw new NullPointerException(
077: "The context service for an activation cannot be null"); //$NON-NLS-1$
078: }
079:
080: this .contextId = contextId;
081: this .contextService = contextService;
082: }
083:
084: public final void clearActive() {
085: clearResult();
086: }
087:
088: public final String getContextId() {
089: return contextId;
090: }
091:
092: public final IContextService getContextService() {
093: return contextService;
094: }
095:
096: public final boolean isActive(final IEvaluationContext context) {
097: return evaluate(context);
098: }
099:
100: public final String toString() {
101: final StringBuffer buffer = new StringBuffer();
102:
103: buffer.append("ContextActivation(contextId="); //$NON-NLS-1$
104: buffer.append(contextId);
105: buffer.append(",sourcePriority="); //$NON-NLS-1$
106: buffer.append(getSourcePriority());
107: buffer.append(')');
108:
109: return buffer.toString();
110: }
111:
112: }
|