comparison org.eclipse.jface.databinding/src/org/eclipse/jface/databinding/viewers/ObservableValueEditingSupport.d @ 78:0a55d2d5a946

Added file for databinding
author Frank Benoit <benoit@tionex.de>
date Tue, 14 Apr 2009 11:35:29 +0200
parents
children 6be48cf9f95c
comparison
equal deleted inserted replaced
76:f05e6e8b2f2d 78:0a55d2d5a946
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
42 private final ColumnViewerEditorActivationListenerHelper activationListener = new ColumnViewerEditorActivationListenerHelper();
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) {
59 super(viewer);
60
61 if (dbc is null) {
62 throw new IllegalArgumentException("Parameter dbc was null."); //$NON-NLS-1$
63 }
64
65 this.viewer = viewer;
66 this.dbc = dbc;
67 }
68
69 /**
70 * Default implementation always returns <code>true</code>.
71 *
72 * @see org.eclipse.jface.viewers.EditingSupport#canEdit(java.lang.Object)
73 */
74 protected bool canEdit(Object element) {
75 return true;
76 }
77
78 /**
79 * Default implementation always returns <code>null</code> as this will be
80 * handled by the Binding.
81 *
82 * @see org.eclipse.jface.viewers.EditingSupport#getValue(java.lang.Object)
83 */
84 protected Object getValue(Object element) {
85 // no op
86 return null;
87 }
88
89 /**
90 * Default implementation does nothing as this will be handled by the
91 * Binding.
92 *
93 * @see org.eclipse.jface.viewers.EditingSupport#setValue(java.lang.Object,
94 * java.lang.Object)
95 */
96 protected void setValue(Object element, Object value) {
97 // no op
98 }
99
100 /**
101 * Creates a {@link Binding} between the editor and the element to be
102 * edited. Invokes {@link #doCreateCellEditorObservable(CellEditor)},
103 * {@link #doCreateElementObservable(Object, ViewerCell)}, and then
104 * {@link #createBinding(IObservableValue, IObservableValue)}.
105 */
106 final protected void initializeCellEditorValue(CellEditor cellEditor,
107 ViewerCell cell) {
108 IObservableValue target = doCreateCellEditorObservable(cellEditor);
109 Assert
110 .isNotNull(target,
111 "doCreateCellEditorObservable(...) did not return an observable"); //$NON-NLS-1$
112
113 IObservableValue model = doCreateElementObservable(cell.getElement(),
114 cell);
115 Assert.isNotNull(model,
116 "doCreateElementObservable(...) did not return an observable"); //$NON-NLS-1$
117
118 Binding binding = createBinding(target, model);
119 Assert
120 .isNotNull(binding,
121 "createBinding(...) did not return a binding"); //$NON-NLS-1$
122
123 editingState = new EditingState(binding, target, model);
124
125 getViewer().getColumnViewerEditor().addEditorActivationListener(
126 activationListener);
127 }
128
129 /**
130 * Creates the observable value for the CellEditor.
131 *
132 * @param cellEditor
133 * @return observable value
134 */
135 protected abstract IObservableValue doCreateCellEditorObservable(
136 CellEditor cellEditor);
137
138 /**
139 * Creates the observable value for the element.
140 *
141 * @param element
142 * @param cell
143 * @return observable value
144 */
145 protected abstract IObservableValue doCreateElementObservable(
146 Object element, ViewerCell cell);
147
148 /**
149 * Creates a new binding for the provided <code>target</code> and
150 * <code>model</code>. Default
151 * {@link UpdateValueStrategy value update strategies} are used with the
152 * target to model updating on {@link UpdateValueStrategy#POLICY_CONVERT}.
153 *
154 * @param target
155 * @param model
156 * @return binding
157 */
158 protected Binding createBinding(IObservableValue target,
159 IObservableValue model) {
160 return dbc.bindValue(target, model, new UpdateValueStrategy(
161 UpdateValueStrategy.POLICY_CONVERT), null);
162 }
163
164 /**
165 * Updates the model from the target.
166 */
167 final protected void saveCellEditorValue(CellEditor cellEditor,
168 ViewerCell cell) {
169 editingState.binding.updateTargetToModel();
170 }
171
172 private class ColumnViewerEditorActivationListenerHelper :
173 ColumnViewerEditorActivationListener {
174
175 public void afterEditorActivated(ColumnViewerEditorActivationEvent event) {
176 // do nothing
177 }
178
179 public void afterEditorDeactivated(
180 ColumnViewerEditorDeactivationEvent event) {
181 editingState.dispose();
182 editingState = null;
183
184 viewer.getColumnViewerEditor().removeEditorActivationListener(this);
185 }
186
187 public void beforeEditorActivated(
188 ColumnViewerEditorActivationEvent event) {
189 // do nothing
190 }
191
192 public void beforeEditorDeactivated(
193 ColumnViewerEditorDeactivationEvent event) {
194 // do nothing
195 }
196 }
197
198 /**
199 * Maintains references to objects that only live for the length of the edit
200 * cycle.
201 */
202 private static class EditingState {
203 IObservableValue target;
204
205 IObservableValue model;
206
207 Binding binding;
208
209 this(Binding binding, IObservableValue target,
210 IObservableValue model) {
211 this.binding = binding;
212 this.target = target;
213 this.model = model;
214 }
215
216 void dispose() {
217 target.dispose();
218 model.dispose();
219 binding.dispose();
220 }
221 }
222 }