001: /*******************************************************************************
002: * Copyright (c) 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.Expression;
013: import org.eclipse.core.expressions.IEvaluationContext;
014: import org.eclipse.jface.util.IPropertyChangeListener;
015: import org.eclipse.jface.util.PropertyChangeEvent;
016: import org.eclipse.ui.ISourceProvider;
017:
018: /**
019: * @since 3.3
020: *
021: */
022: public final class EvaluationService implements IEvaluationService {
023:
024: private EvaluationAuthority restrictionAuthority;
025: private EvaluationAuthority evaluationAuthority;
026:
027: private IPropertyChangeListener restrictionListener = new IPropertyChangeListener() {
028:
029: public void propertyChange(PropertyChangeEvent event) {
030: IEvaluationReference source = (IEvaluationReference) event
031: .getSource();
032: if (source == null)
033: return;
034:
035: IEvaluationReference mappedReference = source
036: .getTargetReference();
037: if (mappedReference == null)
038: return;
039:
040: boolean evaluationEnabled = false;
041: if (event.getNewValue() != null) {
042: evaluationEnabled = ((Boolean) event.getNewValue())
043: .booleanValue();
044: } else {
045: evaluationEnabled = false;
046: }
047: mappedReference.setPostingChanges(evaluationEnabled);
048: }
049: };
050:
051: public EvaluationService() {
052: evaluationAuthority = new EvaluationAuthority();
053: restrictionAuthority = new EvaluationAuthority();
054: }
055:
056: /*
057: * (non-Javadoc)
058: *
059: * @see org.eclipse.ui.internal.services.IEvaluationService#addEvaluationListener(org.eclipse.core.expressions.Expression,
060: * org.eclipse.jface.util.IPropertyChangeListener, java.lang.String)
061: */
062: public IEvaluationReference addEvaluationListener(
063: Expression expression, IPropertyChangeListener listener,
064: String property, Expression restrictEvaluation) {
065: IEvaluationReference expressionReference = new EvaluationReference(
066: expression, listener, property, null);
067:
068: evaluationAuthority.addEvaluationListener(expressionReference);
069: if (restrictEvaluation != null) {
070: IPropertyChangeListener restrictionListener = getRestrictionListener();
071: // create a binding from the restriction to the expression
072: IEvaluationReference restrictRef = new EvaluationReference(
073: restrictEvaluation, restrictionListener,
074: "evaluate", expressionReference); //$NON-NLS-1$
075:
076: // now set the pair in the opposite configuration for later cleanup
077: expressionReference.setTargetReference(restrictRef);
078: restrictionAuthority.addEvaluationListener(restrictRef);
079: }
080: return expressionReference;
081: }
082:
083: /**
084: * @return
085: */
086: private IPropertyChangeListener getRestrictionListener() {
087: return restrictionListener;
088: }
089:
090: /*
091: * (non-Javadoc)
092: *
093: * @see org.eclipse.ui.internal.services.IEvaluationService#removeEvaluationListener(org.eclipse.ui.internal.services.IEvaluationReference)
094: */
095: public void removeEvaluationListener(IEvaluationReference ref) {
096: evaluationAuthority.removeEvaluationListener(ref);
097: IEvaluationReference target = ref.getTargetReference();
098: if (target != null) {
099: restrictionAuthority.removeEvaluationListener(target);
100: }
101: }
102:
103: /*
104: * (non-Javadoc)
105: *
106: * @see org.eclipse.ui.services.IServiceWithSources#addSourceProvider(org.eclipse.ui.ISourceProvider)
107: */
108: public void addSourceProvider(ISourceProvider provider) {
109: restrictionAuthority.addSourceProvider(provider);
110: evaluationAuthority.addSourceProvider(provider);
111: }
112:
113: /*
114: * (non-Javadoc)
115: *
116: * @see org.eclipse.ui.services.IServiceWithSources#removeSourceProvider(org.eclipse.ui.ISourceProvider)
117: */
118: public void removeSourceProvider(ISourceProvider provider) {
119: restrictionAuthority.removeSourceProvider(provider);
120: evaluationAuthority.removeSourceProvider(provider);
121: }
122:
123: /*
124: * (non-Javadoc)
125: *
126: * @see org.eclipse.ui.services.IDisposable#dispose()
127: */
128: public void dispose() {
129: restrictionAuthority.dispose();
130: evaluationAuthority.dispose();
131: }
132:
133: /* (non-Javadoc)
134: * @see org.eclipse.ui.internal.services.IEvaluationService#getCurrentState()
135: */
136: public IEvaluationContext getCurrentState() {
137: return evaluationAuthority.getCurrentState();
138: }
139:
140: /* (non-Javadoc)
141: * @see org.eclipse.ui.internal.services.IEvaluationService#addServiceListener(org.eclipse.jface.util.IPropertyChangeListener)
142: */
143: public void addServiceListener(IPropertyChangeListener listener) {
144: restrictionAuthority.addServiceListener(listener);
145: evaluationAuthority.addServiceListener(listener);
146: }
147:
148: /* (non-Javadoc)
149: * @see org.eclipse.ui.internal.services.IEvaluationService#removeServiceListener(org.eclipse.jface.util.IPropertyChangeListener)
150: */
151: public void removeServiceListener(IPropertyChangeListener listener) {
152: restrictionAuthority.removeServiceListener(listener);
153: evaluationAuthority.removeServiceListener(listener);
154: }
155: }
|