Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f7249f3146c6ff91195c6226d838ed25b9782af5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*****************************************************************************
 * Copyright (c) 2010, 2014 CEA LIST and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Thibault Le Ouay t.leouay@sherpa-eng.com - Add binding implementation
 *  Christian W. Damus (CEA) - bug 402525
 *  Mickaël ADAM (ALL4TEC) mickael.adam@all4tec.net - bug 435415
 *  Christian W. Damus (CEA) - bug 417409
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.widgets.editors;

import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.conversion.IConverter;
import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.IObserving;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.IValueChangeListener;
import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.papyrus.infra.widgets.validator.AbstractValidator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

/**
 * An abstract class to represent Single-value Editors. Single-value editors are
 * based on the Eclipse Databinding Framework They take {@link IObservableValue} s as Input
 *
 * @author Camille Letavernier
 *
 */
public abstract class AbstractValueEditor extends AbstractEditor {

	/**
	 * The IObservableValue associated to the model property
	 */
	protected IObservableValue modelProperty;

	/**
	 * The IObservableValue associated to the widget
	 */
	protected IObservableValue widgetObservable;

	/**
	 * The UpdateStrategy for binding data from widget to model
	 */
	protected UpdateValueStrategy targetToModelStrategy;

	/**
	 * The UpdateStrategy for binding data from model to widget
	 */
	protected UpdateValueStrategy modelToTargetStrategy;

	/**
	 * the Validator for the target
	 */
	protected AbstractValidator targetValidator;

	/**
	 * the Validator for the model
	 */
	protected IValidator modelValidator;

	protected boolean errorBinding = false;

	protected ControlDecoration controlDecoration;

	protected static final Color VALID = new Color(Display.getCurrent(), 144, 238, 144); //CSS LightGreen

	protected static final Color DEFAULT = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);

	protected static final Color EDIT = new Color(Display.getCurrent(), 255, 204, 153); //Orange

	protected static final Color ERROR = new Color(Display.getCurrent(), 255, 153, 153); //Red

	protected AbstractValueEditor(Composite parent) {
		super(parent);
	}

	protected AbstractValueEditor(Composite parent, int style, String label) {
		super(parent, style, label);
	}

	protected AbstractValueEditor(Composite parent, int style) {
		super(parent, style);
	}

	protected AbstractValueEditor(Composite parent, String label) {
		super(parent, label);
	}

	/**
	 * Sets this editor's IObservableValue associated to the widget property
	 *
	 * @param widgetObservable
	 * @param targetToModel
	 *        the IConverter to convert data from Widget to Model
	 * @param modelToTarget
	 *        the IConverter to convert data from Model to Widget
	 */
	protected void setWidgetObservable(IObservableValue widgetObservable, IConverter targetToModel, IConverter modelToTarget) {
		this.widgetObservable = widgetObservable;
		setConverters(targetToModel, modelToTarget);
	}

	/**
	 * Sets this editor's widgetObservable
	 *
	 * @param widgetObservable
	 *        The widget observable value
	 * @param commitOnChange
	 *        If true, CommitListeners will be notified when the widget
	 *        observable changes
	 */
	protected void setWidgetObservable(IObservableValue widgetObservable, boolean commitOnChange) {
		this.widgetObservable = widgetObservable;
		if(commitOnChange) {
			this.widgetObservable.addChangeListener(new IChangeListener() {

				@Override
				public void handleChange(ChangeEvent event) {
					commit();
				}
			});
		}
	}

	/**
	 * Sets this editor's IObservableValue associated to the widget property
	 *
	 * @param widgetObservable
	 */
	protected void setWidgetObservable(IObservableValue widgetObservable) {
		setWidgetObservable(widgetObservable, false);
	}

	/**
	 * Sets this editor's IObservableValue associated to the model property, and
	 * binds it to the Editor's Widget
	 *
	 * @param modelProperty
	 */
	public void setModelObservable(IObservableValue modelProperty) {
		this.modelProperty = modelProperty;
		doBinding();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setConverters(IConverter targetToModel, IConverter modelToTarget) {
		if(targetToModelStrategy == null) {
			targetToModelStrategy = new UpdateValueStrategy();
		}
		if(modelToTargetStrategy == null) {
			modelToTargetStrategy = new UpdateValueStrategy();
		}
		targetToModelStrategy.setConverter(targetToModel);
		modelToTargetStrategy.setConverter(modelToTarget);
	}

	/**
	 * Sets the UpdateStrategies for databinding between the widget and the
	 * model
	 *
	 * @param targetToModelStrategy
	 *        The widget to model Update strategy
	 * @param modelToTargetStrategy
	 *        The model to widget Update strategy
	 */
	public void setUpdateStrategies(UpdateValueStrategy targetToModelStrategy, UpdateValueStrategy modelToTargetStrategy) {
		this.targetToModelStrategy = targetToModelStrategy;
		this.modelToTargetStrategy = modelToTargetStrategy;
	}

	/**
	 * Binds the Widget Observable to the Model observable property, using the
	 * specified converters or Update strategies when available
	 *
	 * When overriding this method, you should also override {@link #refreshValue()}
	 *
	 * @see org.eclipse.papyrus.infra.widgets.editors.AbstractEditor#doBinding()
	 */
	@Override
	protected void doBinding() {
		if(modelProperty == null || widgetObservable == null) {
			return;
		}
		setBinding();
	}

	/**
	 * Returns the value from the widget May be used even when the Model
	 * Observable is not set
	 *
	 * @return The current value for this editor
	 */
	public abstract Object getValue();

	/**
	 * Initialize binding
	 */
	private void setBinding() {
		binding = getBindingContext().bindValue(widgetObservable, modelProperty, targetToModelStrategy, modelToTargetStrategy);
		binding.getValidationStatus().addValueChangeListener(new IValueChangeListener() {

			@Override
			public void handleValueChange(ValueChangeEvent event) {
				// Don't handle validation changes if we don't have a validator, because then it could only be green and it isn't useful.
				// Also, if we're showing in a dialog, then our widget may have been disposed already if we're validating a change applied
				// by hitting the OK button
				if((modelValidator) != null) {
					//Check if the widget is disposed before isReadOnly() to avoid NPE
					if(!AbstractValueEditor.this.isDisposed() && !isReadOnly()) { //Bug 434787 : Shouldn't not execute the timer thread if the widget is disposed
						IStatus status = (IStatus)binding.getValidationStatus().getValue(); //Bug 435415 : Update the status only if the widget isn't disposed
						updateStatus(status);
						changeColorField();
					}
				}
			}

		});
	}

	public void updateStatus(IStatus status) {
	}

	/**
	 * Set the target to model Strategy to after get validation
	 *
	 * @param targetToModelValidator
	 */
	public void setTargetAfterGetValidator(AbstractValidator targetToModelValidator) {
		if(targetToModelValidator != null) {
			targetToModelStrategy.setAfterGetValidator(targetToModelValidator);
		}
	}

	/**
	 * Set the model strategy with After get validation
	 * Set the target strategy with before set validation
	 *
	 * @param modelValidator
	 */
	public void setModelValidator(IValidator targetToModelValidator) {
			this.modelValidator = targetToModelValidator;
			targetToModelStrategy.setBeforeSetValidator(targetToModelValidator);
			modelToTargetStrategy.setAfterGetValidator(targetToModelValidator);
	}

	/**
	 * Initialize both strategies with default values
	 */
	public void setStrategies() {
		if(modelToTargetStrategy == null) {
			modelToTargetStrategy = new UpdateValueStrategy();
		}
		if(targetToModelStrategy == null) {
			targetToModelStrategy = new UpdateValueStrategy();
		}
	}

	@Override
	protected Object getContextElement() {
		// Our observables for features of EMF objects are expected to implement IObserving because
		// the observe the value of the object's feature
		return (modelProperty instanceof IObserving) ? ((IObserving)modelProperty).getObserved() : null;
	}
}

Back to the top