78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006, 2008 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 * Brad Reynolds - bug 159768
|
|
11 * Boris Bokowski - bug 218269
|
|
12 * Matthew Hall - bug 218269
|
|
13 *******************************************************************************/
|
|
14
|
|
15 module org.eclipse.core.databinding.Binding;
|
|
16
|
|
17 import java.lang.all;
|
|
18
|
|
19 import java.util.Collections;
|
|
20
|
|
21 import org.eclipse.core.databinding.observable.IObservable;
|
|
22 import org.eclipse.core.databinding.observable.Observables;
|
|
23 import org.eclipse.core.databinding.observable.list.IObservableList;
|
|
24 import org.eclipse.core.databinding.observable.value.IObservableValue;
|
|
25
|
|
26 /**
|
|
27 * This abstract class represents a binding between a model and a target. Newly
|
|
28 * created instances need to be added to a data binding context using
|
|
29 * {@link #initcast(DataBindingContext)}.
|
|
30 *
|
|
31 * @since 1.0
|
|
32 */
|
|
33 public abstract class Binding : ValidationStatusProvider {
|
|
34
|
|
35 protected DataBindingContext context;
|
|
36 private IObservable target;
|
|
37 private IObservable model;
|
|
38
|
|
39 /**
|
|
40 * Creates a new binding.
|
|
41 *
|
|
42 * @param target target observable
|
|
43 * @param model model observable
|
|
44 */
|
|
45 public this(IObservable target, IObservable model) {
|
|
46 this.target = target;
|
|
47 this.model = model;
|
|
48 }
|
|
49
|
|
50 /**
|
|
51 * Initializes this binding with the given context and adds it to the list
|
|
52 * of bindings of the context.
|
|
53 * <p>
|
|
54 * Subclasses may extend, but must call the super implementation.
|
|
55 * </p>
|
|
56 *
|
|
57 * @param context
|
|
58 */
|
|
59 public final void init(DataBindingContext context) {
|
|
60 this.context = context;
|
|
61 preInit();
|
|
62 context.addBinding(this);
|
|
63 postInit();
|
|
64 }
|
|
65
|
|
66 /**
|
|
67 * Called by {@link #initcast(DataBindingContext)} after setting
|
|
68 * {@link #context} but before adding this binding to the context.
|
|
69 * Subclasses may use this method to perform initialization that could not
|
|
70 * be done in the constructor. Care should be taken not to cause any events
|
|
71 * while running this method.
|
|
72 */
|
|
73 protected abstract void preInit();
|
|
74
|
|
75 /**
|
|
76 * Called by {@link #initcast(DataBindingContext)} after adding this binding to
|
|
77 * the context. Subclasses may use this method to perform initialization
|
|
78 * that may cause events to be fired, including BindingEvents that are
|
|
79 * forwarded to the data binding context.
|
|
80 */
|
|
81 protected abstract void postInit();
|
|
82
|
|
83 /**
|
|
84 * @return an observable value containing the current validation status
|
|
85 */
|
|
86 public abstract IObservableValue getValidationStatus();
|
|
87
|
|
88 /**
|
|
89 * Updates the model's state from the target's state at the next reasonable
|
|
90 * opportunity. There is no guarantee that the state will have been updated
|
|
91 * by the time this call returns.
|
|
92 */
|
|
93 public abstract void updateTargetToModel();
|
|
94
|
|
95 /**
|
|
96 * Updates the target's state from the model's state at the next reasonable
|
|
97 * opportunity. There is no guarantee that the state will have been updated
|
|
98 * by the time this call returns.
|
|
99 */
|
|
100 public abstract void updateModelToTarget();
|
|
101
|
|
102 /**
|
|
103 * Validates the target's state at the next reasonable
|
|
104 * opportunity. There is no guarantee that the validation status will have been updated
|
|
105 * by the time this call returns.
|
|
106 */
|
|
107 public abstract void validateTargetToModel();
|
|
108
|
|
109 /**
|
|
110 * Validates the model's state at the next reasonable
|
|
111 * opportunity. There is no guarantee that the validation status will have been updated
|
|
112 * by the time this call returns.
|
|
113 */
|
|
114 public abstract void validateModelToTarget();
|
|
115
|
|
116 /**
|
|
117 * Disposes of this Binding. Subclasses may extend, but must call super.dispose().
|
|
118 */
|
|
119 public void dispose() {
|
|
120 if (context !is null) {
|
|
121 context.removeBinding(this);
|
|
122 }
|
|
123 context = null;
|
|
124 target = null;
|
|
125 model = null;
|
|
126 super.dispose();
|
|
127 }
|
|
128
|
|
129 /**
|
|
130 * @param context
|
|
131 */
|
|
132 /* package */ void setDataBindingContext(DataBindingContext context) {
|
|
133 this.context = context;
|
|
134 }
|
|
135
|
|
136 /**
|
|
137 * @return target observable
|
|
138 */
|
|
139 public IObservable getTarget() {
|
|
140 return target;
|
|
141 }
|
|
142
|
|
143 /**
|
|
144 * @since 1.1
|
|
145 */
|
|
146 public IObservableList getTargets() {
|
|
147 return Observables.staticObservableList(context.getValidationRealm(),
|
|
148 Collections.singletonList(target));
|
|
149 }
|
|
150
|
|
151 /**
|
|
152 * @return model observable
|
|
153 */
|
|
154 public IObservable getModel() {
|
|
155 return model;
|
|
156 }
|
|
157
|
|
158 /**
|
|
159 * @since 1.1
|
|
160 */
|
|
161 public IObservableList getModels() {
|
|
162 return Observables.staticObservableList(context.getValidationRealm(),
|
|
163 Collections.singletonList(model));
|
|
164 }
|
|
165 }
|