KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > databinding > ValueBinding


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.core.databinding;
13
14 import org.eclipse.core.databinding.observable.value.IObservableValue;
15 import org.eclipse.core.databinding.observable.value.IValueChangeListener;
16 import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
17 import org.eclipse.core.databinding.observable.value.WritableValue;
18 import org.eclipse.core.databinding.util.Policy;
19 import org.eclipse.core.internal.databinding.BindingStatus;
20 import org.eclipse.core.internal.databinding.Util;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.MultiStatus;
23 import org.eclipse.core.runtime.Status;
24
25 /**
26  * @since 1.0
27  *
28  */

29 class ValueBinding extends Binding {
30     private final UpdateValueStrategy targetToModel;
31     private final UpdateValueStrategy modelToTarget;
32     private WritableValue validationStatusObservable;
33     private IObservableValue target;
34     private IObservableValue model;
35
36     private boolean updatingTarget;
37     private boolean updatingModel;
38     private IValueChangeListener targetChangeListener = new IValueChangeListener() {
39         public void handleValueChange(ValueChangeEvent event) {
40             if (!updatingTarget && !Util.equals(event.diff.getOldValue(), event.diff.getNewValue())) {
41                 doUpdate(target, model, targetToModel, false, false);
42             }
43         }
44     };
45     private IValueChangeListener modelChangeListener = new IValueChangeListener() {
46         public void handleValueChange(ValueChangeEvent event) {
47             if (!updatingModel && !Util.equals(event.diff.getOldValue(), event.diff.getNewValue())) {
48                 doUpdate(model, target, modelToTarget, false, false);
49             }
50         }
51     };
52
53     /**
54      * @param targetObservableValue
55      * @param modelObservableValue
56      * @param targetToModel
57      * @param modelToTarget
58      */

59     public ValueBinding(IObservableValue targetObservableValue,
60             IObservableValue modelObservableValue,
61             UpdateValueStrategy targetToModel, UpdateValueStrategy modelToTarget) {
62         super(targetObservableValue, modelObservableValue);
63         this.target = targetObservableValue;
64         this.model = modelObservableValue;
65         this.targetToModel = targetToModel;
66         this.modelToTarget = modelToTarget;
67         if ((targetToModel.getUpdatePolicy() & (UpdateValueStrategy.POLICY_CONVERT | UpdateValueStrategy.POLICY_UPDATE)) != 0) {
68             target.addValueChangeListener(targetChangeListener);
69         } else {
70             targetChangeListener = null;
71         }
72         if ((modelToTarget.getUpdatePolicy() & (UpdateValueStrategy.POLICY_CONVERT | UpdateValueStrategy.POLICY_UPDATE)) != 0) {
73             model.addValueChangeListener(modelChangeListener);
74         } else {
75             modelChangeListener = null;
76         }
77     }
78
79     protected void preInit() {
80         validationStatusObservable = new WritableValue(context
81                 .getValidationRealm(), Status.OK_STATUS, IStatus.class);
82     }
83
84     protected void postInit() {
85         if (modelToTarget.getUpdatePolicy() == UpdateValueStrategy.POLICY_UPDATE) {
86             updateModelToTarget();
87         }
88         if (targetToModel.getUpdatePolicy() != UpdateValueStrategy.POLICY_NEVER) {
89             validateTargetToModel();
90         }
91     }
92
93     public IObservableValue getValidationStatus() {
94         return validationStatusObservable;
95     }
96
97     public void updateTargetToModel() {
98         doUpdate(target, model, targetToModel, true, false);
99     }
100
101     public void updateModelToTarget() {
102         doUpdate(model, target, modelToTarget, true, false);
103     }
104
105     /**
106      * Incorporates the provided <code>newStats</code> into the
107      * <code>multieStatus</code>.
108      *
109      * @param multiStatus
110      * @param newStatus
111      * @return <code>true</code> if the update should proceed
112      */

113     /* package */boolean mergeStatus(MultiStatus multiStatus, IStatus newStatus) {
114         if (!newStatus.isOK()) {
115             multiStatus.add(newStatus);
116             return multiStatus.getSeverity() < IStatus.ERROR;
117         }
118         return true;
119     }
120
121     /*
122      * This method may be moved to UpdateValueStrategy in the future if clients
123      * need more control over how the source value is copied to the destination
124      * observable.
125      */

126     private void doUpdate(final IObservableValue source,
127             final IObservableValue destination,
128             final UpdateValueStrategy updateValueStrategy,
129             final boolean explicit, final boolean validateOnly) {
130         final int policy = updateValueStrategy.getUpdatePolicy();
131         final MultiStatus[] statusHolder = { BindingStatus.ok() };
132         if (policy != UpdateValueStrategy.POLICY_NEVER) {
133             if (policy != UpdateValueStrategy.POLICY_ON_REQUEST || explicit) {
134                 source.getRealm().exec(new Runnable JavaDoc() {
135                     public void run() {
136                         boolean destinationRealmReached = false;
137                         try {
138                             Object JavaDoc value = source.getValue();
139                             IStatus status = updateValueStrategy
140                                     .validateAfterGet(value);
141                             if (mergeStatus(statusHolder[0], status)) {
142                                 final Object JavaDoc convertedValue = updateValueStrategy
143                                         .convert(value);
144                                 status = updateValueStrategy
145                                         .validateAfterConvert(convertedValue);
146                                 if (mergeStatus(statusHolder[0], status)) {
147                                     if (policy == UpdateValueStrategy.POLICY_CONVERT
148                                             && !explicit) {
149                                     } else {
150                                         status = updateValueStrategy
151                                                 .validateBeforeSet(convertedValue);
152                                         if (mergeStatus(statusHolder[0], status)) {
153                                             if (!validateOnly) {
154                                                 destinationRealmReached = true;
155                                                 destination.getRealm().exec(
156                                                         new Runnable JavaDoc() {
157                                                             public void run() {
158                                                                 if (destination == target) {
159                                                                     updatingTarget = true;
160                                                                 } else {
161                                                                     updatingModel = true;
162                                                                 }
163                                                                 try {
164                                                                     IStatus setterStatus = updateValueStrategy
165                                                                             .doSet(
166                                                                                     destination,
167                                                                                     convertedValue);
168
169                                                                     mergeStatus(
170                                                                             statusHolder[0],
171                                                                             setterStatus);
172                                                                 } finally {
173                                                                     if (destination == target) {
174                                                                         updatingTarget = false;
175                                                                     } else {
176                                                                         updatingModel = false;
177                                                                     }
178                                                                     setValidationStatus(statusHolder[0]);
179                                                                 }
180                                                             }
181                                                         });
182                                             }
183                                         }
184                                     }
185                                 }
186                             }
187                         } catch (Exception JavaDoc ex) {
188                             // This check is necessary as in 3.2.2 Status
189
// doesn't accept a null message (bug 177264).
190
String JavaDoc message = (ex.getMessage() != null) ? ex
191                                     .getMessage() : ""; //$NON-NLS-1$
192

193                             mergeStatus(statusHolder[0], new Status(
194                                     IStatus.ERROR, Policy.JFACE_DATABINDING,
195                                     IStatus.ERROR, message, ex));
196                         } finally {
197                             if (!destinationRealmReached) {
198                                 setValidationStatus(statusHolder[0]);
199                             }
200
201                         }
202                     }
203                 });
204             }
205         }
206     }
207
208     public void validateModelToTarget() {
209         doUpdate(model, target, modelToTarget, true, true);
210     }
211
212     public void validateTargetToModel() {
213         doUpdate(target, model, targetToModel, true, true);
214     }
215
216     private void setValidationStatus(final IStatus status) {
217         validationStatusObservable.getRealm().exec(new Runnable JavaDoc() {
218             public void run() {
219                 validationStatusObservable.setValue(status);
220             }
221         });
222     }
223     
224     public void dispose() {
225         if (targetChangeListener != null) {
226             target.removeValueChangeListener(targetChangeListener);
227             targetChangeListener = null;
228         }
229         if (modelChangeListener != null) {
230             model.removeValueChangeListener(modelChangeListener);
231             modelChangeListener = null;
232         }
233         target = null;
234         model = null;
235         super.dispose();
236     }
237
238 }
239
Popular Tags