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.services;
011:
012: import org.eclipse.core.expressions.EvaluationResult;
013: import org.eclipse.core.expressions.Expression;
014: import org.eclipse.core.expressions.IEvaluationContext;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.ui.ISources;
017:
018: /**
019: * <p>
020: * A token representing the activation or contribution of some expression-based
021: * element. This caches the evaluation result so that it is only re-computed as
022: * necessary.
023: * </p>
024: *
025: * @since 3.2
026: */
027: public abstract class EvaluationResultCache implements
028: IEvaluationResultCache {
029:
030: /**
031: * The previous computed evaluation result. If no evaluation result is
032: * available, then this value is <code>null</code>.
033: */
034: private EvaluationResult evaluationResult = null;
035:
036: /**
037: * The expression to evaluate. This value may be <code>null</code>, in
038: * which case the evaluation result is always <code>true</code>.
039: */
040: private final Expression expression;
041:
042: /**
043: * The priority that has been given to this expression.
044: */
045: private final int sourcePriority;
046:
047: /**
048: * Constructs a new instance of <code>EvaluationResultCache</code>.
049: *
050: * @param expression
051: * The expression that must evaluate to <code>true</code>
052: * before this handler is active. This value may be
053: * <code>null</code> if it is always active.
054: * @see ISources
055: */
056: protected EvaluationResultCache(final Expression expression) {
057: this .expression = expression;
058: this .sourcePriority = SourcePriorityNameMapping
059: .computeSourcePriority(expression);
060: }
061:
062: public final void clearResult() {
063: evaluationResult = null;
064: }
065:
066: public final boolean evaluate(final IEvaluationContext context) {
067: if (expression == null) {
068: return true;
069: }
070:
071: if (evaluationResult == null) {
072: try {
073: evaluationResult = expression.evaluate(context);
074: } catch (final CoreException e) {
075: /*
076: * Swallow the exception. It simply means the variable is not
077: * valid it some (most frequently, that the value is null). This
078: * kind of information is not really useful to us, so we can
079: * just treat it as null.
080: */
081: evaluationResult = EvaluationResult.FALSE;
082: return false;
083: }
084: }
085:
086: return evaluationResult == EvaluationResult.TRUE;
087: }
088:
089: public final Expression getExpression() {
090: return expression;
091: }
092:
093: public final int getSourcePriority() {
094: return sourcePriority;
095: }
096:
097: public final void setResult(final boolean result) {
098: if (result) {
099: evaluationResult = EvaluationResult.TRUE;
100: } else {
101: evaluationResult = EvaluationResult.FALSE;
102: }
103: }
104: }
|