1 11 12 package org.eclipse.ui.internal.services; 13 14 import org.eclipse.core.expressions.Expression; 15 import org.eclipse.core.expressions.IEvaluationContext; 16 import org.eclipse.jface.util.IPropertyChangeListener; 17 import org.eclipse.jface.util.PropertyChangeEvent; 18 import org.eclipse.ui.ISourceProvider; 19 20 24 public final class EvaluationService implements IEvaluationService { 25 26 private EvaluationAuthority restrictionAuthority; 27 private EvaluationAuthority evaluationAuthority; 28 29 private IPropertyChangeListener restrictionListener = new IPropertyChangeListener() { 30 31 public void propertyChange(PropertyChangeEvent event) { 32 IEvaluationReference source = (IEvaluationReference) event 33 .getSource(); 34 if (source == null) 35 return; 36 37 IEvaluationReference mappedReference = source.getTargetReference(); 38 if (mappedReference == null) 39 return; 40 41 boolean evaluationEnabled = false; 42 if (event.getNewValue() != null) { 43 evaluationEnabled = ((Boolean ) event.getNewValue()).booleanValue(); 44 } else { 45 evaluationEnabled = false; 46 } 47 mappedReference.setPostingChanges(evaluationEnabled); 48 } 49 }; 50 51 52 public EvaluationService() { 53 evaluationAuthority = new EvaluationAuthority(); 54 restrictionAuthority = new EvaluationAuthority(); 55 } 56 57 63 public IEvaluationReference addEvaluationListener(Expression expression, 64 IPropertyChangeListener listener, String property, Expression restrictEvaluation) { 65 IEvaluationReference expressionReference = new EvaluationReference(expression, listener, property, null); 66 67 evaluationAuthority.addEvaluationListener(expressionReference); 68 if (restrictEvaluation != null) { 69 IPropertyChangeListener restrictionListener = getRestrictionListener(); 70 IEvaluationReference restrictRef = new EvaluationReference( 72 restrictEvaluation, restrictionListener, 73 "evaluate", expressionReference); 75 expressionReference.setTargetReference(restrictRef); 77 restrictionAuthority.addEvaluationListener(restrictRef); 78 } 79 return expressionReference; 80 } 81 82 85 private IPropertyChangeListener getRestrictionListener() { 86 return restrictionListener ; 87 } 88 89 94 public void removeEvaluationListener(IEvaluationReference ref) { 95 evaluationAuthority.removeEvaluationListener(ref); 96 IEvaluationReference target = ref.getTargetReference(); 97 if (target != null) { 98 restrictionAuthority 99 .removeEvaluationListener(target); 100 } 101 } 102 103 108 public void addSourceProvider(ISourceProvider provider) { 109 restrictionAuthority.addSourceProvider(provider); 110 evaluationAuthority.addSourceProvider(provider); 111 } 112 113 118 public void removeSourceProvider(ISourceProvider provider) { 119 restrictionAuthority.removeSourceProvider(provider); 120 evaluationAuthority.removeSourceProvider(provider); 121 } 122 123 128 public void dispose() { 129 restrictionAuthority.dispose(); 130 evaluationAuthority.dispose(); 131 } 132 133 136 public IEvaluationContext getCurrentState() { 137 return evaluationAuthority.getCurrentState(); 138 } 139 140 143 public void addServiceListener(IPropertyChangeListener listener) { 144 restrictionAuthority.addServiceListener(listener); 145 evaluationAuthority.addServiceListener(listener); 146 } 147 148 151 public void removeServiceListener(IPropertyChangeListener listener) { 152 restrictionAuthority.removeServiceListener(listener); 153 evaluationAuthority.removeServiceListener(listener); 154 } 155 } | Popular Tags |