KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > services > EvaluationService


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

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 /**
21  * @since 3.3
22  *
23  */

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 JavaDoc) 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     /*
58      * (non-Javadoc)
59      *
60      * @see org.eclipse.ui.internal.services.IEvaluationService#addEvaluationListener(org.eclipse.core.expressions.Expression,
61      * org.eclipse.jface.util.IPropertyChangeListener, java.lang.String)
62      */

63     public IEvaluationReference addEvaluationListener(Expression expression,
64             IPropertyChangeListener listener, String JavaDoc 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             // create a binding from the restriction to the expression
71
IEvaluationReference restrictRef = new EvaluationReference(
72                     restrictEvaluation, restrictionListener,
73                     "evaluate", expressionReference); //$NON-NLS-1$
74

75             // now set the pair in the opposite configuration for later cleanup
76
expressionReference.setTargetReference(restrictRef);
77             restrictionAuthority.addEvaluationListener(restrictRef);
78         }
79         return expressionReference;
80     }
81
82     /**
83      * @return
84      */

85     private IPropertyChangeListener getRestrictionListener() {
86         return restrictionListener ;
87     }
88
89     /*
90      * (non-Javadoc)
91      *
92      * @see org.eclipse.ui.internal.services.IEvaluationService#removeEvaluationListener(org.eclipse.ui.internal.services.IEvaluationReference)
93      */

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     /*
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 }
Popular Tags