78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2007, 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 *******************************************************************************/
|
|
11
|
|
12 module org.eclipse.jface.databinding.viewers.ObservableValueEditingSupport;
|
|
13
|
|
14 import java.lang.all;
|
|
15
|
|
16 import org.eclipse.core.databinding.Binding;
|
|
17 import org.eclipse.core.databinding.DataBindingContext;
|
|
18 import org.eclipse.core.databinding.UpdateValueStrategy;
|
|
19 import org.eclipse.core.databinding.observable.value.IObservableValue;
|
|
20 import org.eclipse.core.runtime.Assert;
|
|
21 import org.eclipse.jface.viewers.CellEditor;
|
|
22 import org.eclipse.jface.viewers.ColumnViewer;
|
|
23 import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
|
|
24 import org.eclipse.jface.viewers.ColumnViewerEditorActivationListener;
|
|
25 import org.eclipse.jface.viewers.ColumnViewerEditorDeactivationEvent;
|
|
26 import org.eclipse.jface.viewers.EditingSupport;
|
|
27 import org.eclipse.jface.viewers.ViewerCell;
|
|
28
|
|
29 /**
|
|
30 * {@link EditingSupport} using the JFace Data Binding concepts to handle the
|
|
31 * updating of an element from a {@link CellEditor}.
|
|
32 *
|
|
33 * @since 1.2
|
|
34 */
|
|
35 public abstract class ObservableValueEditingSupport : EditingSupport {
|
|
36 /**
|
|
37 * Maintains references to the instances currently imployed while editing.
|
|
38 * Will be <code>null</code> when not editing.
|
|
39 */
|
|
40 private EditingState editingState;
|
|
41
|
85
|
42 private const ColumnViewerEditorActivationListenerHelper activationListener;
|
78
|
43
|
|
44 private ColumnViewer viewer;
|
|
45
|
|
46 private DataBindingContext dbc;
|
|
47
|
|
48 /**
|
|
49 * Constructs a new instance with the provided <code>viewer</code> and
|
|
50 * <code>dbc</code>.
|
|
51 *
|
|
52 * @param viewer
|
|
53 * viewer to edit
|
|
54 * @param dbc
|
|
55 * dbc to create <code>Bindings</code>
|
|
56 */
|
|
57 public this(ColumnViewer viewer,
|
|
58 DataBindingContext dbc) {
|
85
|
59 activationListener = new ColumnViewerEditorActivationListenerHelper();
|
78
|
60 super(viewer);
|
|
61
|
|
62 if (dbc is null) {
|
|
63 throw new IllegalArgumentException("Parameter dbc was null."); //$NON-NLS-1$
|
|
64 }
|
|
65
|
|
66 this.viewer = viewer;
|
|
67 this.dbc = dbc;
|
|
68 }
|
|
69
|
|
70 /**
|
|
71 * Default implementation always returns <code>true</code>.
|
|
72 *
|
|
73 * @see org.eclipse.jface.viewers.EditingSupport#canEdit(java.lang.Object)
|
|
74 */
|
|
75 protected bool canEdit(Object element) {
|
|
76 return true;
|
|
77 }
|
|
78
|
|
79 /**
|
|
80 * Default implementation always returns <code>null</code> as this will be
|
|
81 * handled by the Binding.
|
|
82 *
|
|
83 * @see org.eclipse.jface.viewers.EditingSupport#getValue(java.lang.Object)
|
|
84 */
|
|
85 protected Object getValue(Object element) {
|
|
86 // no op
|
|
87 return null;
|
|
88 }
|
|
89
|
|
90 /**
|
|
91 * Default implementation does nothing as this will be handled by the
|
|
92 * Binding.
|
|
93 *
|
|
94 * @see org.eclipse.jface.viewers.EditingSupport#setValue(java.lang.Object,
|
|
95 * java.lang.Object)
|
|
96 */
|
|
97 protected void setValue(Object element, Object value) {
|
|
98 // no op
|
|
99 }
|
|
100
|
|
101 /**
|
|
102 * Creates a {@link Binding} between the editor and the element to be
|
|
103 * edited. Invokes {@link #doCreateCellEditorObservable(CellEditor)},
|
|
104 * {@link #doCreateElementObservable(Object, ViewerCell)}, and then
|
|
105 * {@link #createBinding(IObservableValue, IObservableValue)}.
|
|
106 */
|
|
107 final protected void initializeCellEditorValue(CellEditor cellEditor,
|
|
108 ViewerCell cell) {
|
|
109 IObservableValue target = doCreateCellEditorObservable(cellEditor);
|
|
110 Assert
|
85
|
111 .isNotNull(cast(Object)target,
|
78
|
112 "doCreateCellEditorObservable(...) did not return an observable"); //$NON-NLS-1$
|
|
113
|
|
114 IObservableValue model = doCreateElementObservable(cell.getElement(),
|
|
115 cell);
|
85
|
116 Assert.isNotNull(cast(Object)model,
|
78
|
117 "doCreateElementObservable(...) did not return an observable"); //$NON-NLS-1$
|
|
118
|
|
119 Binding binding = createBinding(target, model);
|
|
120 Assert
|
|
121 .isNotNull(binding,
|
|
122 "createBinding(...) did not return a binding"); //$NON-NLS-1$
|
|
123
|
|
124 editingState = new EditingState(binding, target, model);
|
|
125
|
|
126 getViewer().getColumnViewerEditor().addEditorActivationListener(
|
|
127 activationListener);
|
|
128 }
|
|
129
|
|
130 /**
|
|
131 * Creates the observable value for the CellEditor.
|
|
132 *
|
|
133 * @param cellEditor
|
|
134 * @return observable value
|
|
135 */
|
|
136 protected abstract IObservableValue doCreateCellEditorObservable(
|
|
137 CellEditor cellEditor);
|
|
138
|
|
139 /**
|
|
140 * Creates the observable value for the element.
|
|
141 *
|
|
142 * @param element
|
|
143 * @param cell
|
|
144 * @return observable value
|
|
145 */
|
|
146 protected abstract IObservableValue doCreateElementObservable(
|
|
147 Object element, ViewerCell cell);
|
|
148
|
|
149 /**
|
|
150 * Creates a new binding for the provided <code>target</code> and
|
|
151 * <code>model</code>. Default
|
|
152 * {@link UpdateValueStrategy value update strategies} are used with the
|
|
153 * target to model updating on {@link UpdateValueStrategy#POLICY_CONVERT}.
|
|
154 *
|
|
155 * @param target
|
|
156 * @param model
|
|
157 * @return binding
|
|
158 */
|
|
159 protected Binding createBinding(IObservableValue target,
|
|
160 IObservableValue model) {
|
|
161 return dbc.bindValue(target, model, new UpdateValueStrategy(
|
|
162 UpdateValueStrategy.POLICY_CONVERT), null);
|
|
163 }
|
|
164
|
|
165 /**
|
|
166 * Updates the model from the target.
|
|
167 */
|
|
168 final protected void saveCellEditorValue(CellEditor cellEditor,
|
|
169 ViewerCell cell) {
|
|
170 editingState.binding.updateTargetToModel();
|
|
171 }
|
|
172
|
|
173 private class ColumnViewerEditorActivationListenerHelper :
|
|
174 ColumnViewerEditorActivationListener {
|
|
175
|
|
176 public void afterEditorActivated(ColumnViewerEditorActivationEvent event) {
|
|
177 // do nothing
|
|
178 }
|
|
179
|
|
180 public void afterEditorDeactivated(
|
|
181 ColumnViewerEditorDeactivationEvent event) {
|
|
182 editingState.dispose();
|
|
183 editingState = null;
|
|
184
|
|
185 viewer.getColumnViewerEditor().removeEditorActivationListener(this);
|
|
186 }
|
|
187
|
|
188 public void beforeEditorActivated(
|
|
189 ColumnViewerEditorActivationEvent event) {
|
|
190 // do nothing
|
|
191 }
|
|
192
|
|
193 public void beforeEditorDeactivated(
|
|
194 ColumnViewerEditorDeactivationEvent event) {
|
|
195 // do nothing
|
|
196 }
|
|
197 }
|
|
198
|
|
199 /**
|
|
200 * Maintains references to objects that only live for the length of the edit
|
|
201 * cycle.
|
|
202 */
|
|
203 private static class EditingState {
|
|
204 IObservableValue target;
|
|
205
|
|
206 IObservableValue model;
|
|
207
|
|
208 Binding binding;
|
|
209
|
|
210 this(Binding binding, IObservableValue target,
|
|
211 IObservableValue model) {
|
|
212 this.binding = binding;
|
|
213 this.target = target;
|
|
214 this.model = model;
|
|
215 }
|
|
216
|
|
217 void dispose() {
|
|
218 target.dispose();
|
|
219 model.dispose();
|
|
220 binding.dispose();
|
|
221 }
|
|
222 }
|
|
223 }
|