Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9447822c6e4bd7cfa8a2e3c3817f7ac5ebbb5013 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*****************************************************************************
 * Copyright (c) 2010 CEA LIST.
 *
 * 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
 *****************************************************************************/
package org.eclipse.papyrus.infra.widgets.editors;

import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.papyrus.infra.tools.databinding.AggregatedObservable;
import org.eclipse.papyrus.infra.widgets.databinding.ComboObservableValue;
import org.eclipse.papyrus.infra.widgets.providers.EncapsulatedContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.IStaticContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.UnchangedObject;
import org.eclipse.papyrus.infra.widgets.providers.UnsetObject;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;


/**
 * An editor representing a single reference as a Combo Box
 * This Editor needs a ContentProvider and a LabelProvider,
 * describing the objects that can be referred by this property
 *
 * @author Camille Letavernier
 *
 */
public class ReferenceCombo extends AbstractValueEditor { //implements SelectionListener {

	/**
	 * The viewer displaying the available values from the model
	 */
	protected ComboViewer viewer;

	/**
	 * The combo used to select the reference
	 */
	protected CCombo combo;

	protected boolean unsettable;

	//	protected Button unset;

	protected EncapsulatedContentProvider contentProvider;


	/**
	 *
	 * Constructor.
	 *
	 * @param parent
	 *        The Composite in which this editor is diplayed
	 * @param style
	 *        The style for this editor's combo
	 */
	public ReferenceCombo(Composite parent, int style) {
		this(parent, style, null);
	}

	/**
	 *
	 * Constructor.
	 *
	 * @param parent
	 *        The Composite in which this editor is diplayed
	 * @param style
	 *        The style for this editor's combo
	 * @param label
	 *        The label for this editor
	 */
	public ReferenceCombo(Composite parent, int style, String label) {
		super(parent, label);

		combo = factory.createCCombo(this, style | SWT.BORDER);
		combo.setBackground(new Color(combo.getDisplay(), 255, 255, 255));
		combo.setLayoutData(getDefaultLayoutData());
		combo.setEditable(false);

		viewer = new ComboViewer(combo);

		//		unset = new Button(this, SWT.PUSH);
		//		unset.setImage(Activator.getDefault().getImage("/icons/Delete_12x12.gif")); //$NON-NLS-1$
		//		unset.setToolTipText("Unset the current value");
		//		unset.addSelectionListener(this);

		((GridLayout)getLayout()).numColumns++;

		setCommitOnFocusLost(combo);
		controlDecoration = new ControlDecoration(combo, SWT.TOP | SWT.LEFT);
		GridData gridData = getDefaultLayoutData();
		combo.setLayoutData(gridData);
		gridData.horizontalIndent = FieldDecorationRegistry.getDefault().getMaximumDecorationWidth();
	}

	/**
	 * Sets the Content and Label providers for this editor
	 *
	 * @param contentProvider
	 *
	 * @param labelProvider
	 */
	public void setProviders(IStaticContentProvider contentProvider, ILabelProvider labelProvider) {
		Assert.isNotNull(contentProvider, "The content provider should not be null"); //$NON-NLS-1$
		setContentProvider(contentProvider);

		if(labelProvider != null) {
			setLabelProvider(labelProvider);
		}
	}

	/**
	 * Sets the content provider for this combo. The Content provider should
	 * specify the objects that can be referred by this property
	 *
	 * @param provider
	 */
	public void setContentProvider(IStaticContentProvider provider) {
		this.contentProvider = new EncapsulatedContentProvider(provider);
		viewer.setContentProvider(contentProvider);
		viewer.setInput(""); //$NON-NLS-1$
		updateControls();
		doBinding();
	}

	@Override
	protected void doBinding() {
		if(contentProvider == null || modelProperty == null) {
			return;
		}

		setWidgetObservable(getObservableValue(), true);
		if(modelProperty instanceof AggregatedObservable) {
			if(((AggregatedObservable)modelProperty).hasDifferentValues()) {
				contentProvider.addTemporaryElement(UnchangedObject.instance);
				viewer.refresh();
			}
		}
		super.doBinding();
	}

	protected IObservableValue getObservableValue() {
		return new ComboObservableValue(viewer, modelProperty);
	}

	/**
	 * The Label provider associated to the available objects that
	 * can be referred by this property
	 *
	 * @param provider
	 */
	public void setLabelProvider(ILabelProvider provider) {
		viewer.setLabelProvider(provider);
	}

	/**
	 * Retrieves the ComboViewer associated to this Editor
	 *
	 * @return
	 *         The ComboViewer associated to this editor
	 */
	public ComboViewer getViewer() {
		return viewer;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object getEditableType() {
		return Object.class;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object getValue() {
		IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
		if(selection.isEmpty()) {
			return null;
		}
		return selection.getFirstElement();
	}

	/**
	 * Sets the value for this widget
	 *
	 * @param value
	 */
	public void setValue(Object value) {
		if(value == null) {
			viewer.setSelection(new StructuredSelection());
		} else {
			viewer.setSelection(new StructuredSelection(value), true);
		}
	}

	@Override
	public void setReadOnly(boolean readOnly) {
		combo.setEnabled(!readOnly);
		updateControls();
	}

	@Override
	public boolean isReadOnly() {
		return !combo.isEnabled();
	}

	@Override
	public void setToolTipText(String text) {
		combo.setToolTipText(text);
		super.setLabelToolTipText(text);
	}

	public void setUnsettable(boolean unsettable) {
		this.unsettable = unsettable;
		updateControls();
	}

	/**
	 * Updates the controls display
	 */
	protected void updateControls() {
		//		setExclusion(unset, !unsettable);

		//		if(isReadOnly() && unsettable) {
		//			unset.setEnabled(false);
		//		}

		if(contentProvider != null) {
			if(unsettable) {
				contentProvider.addTemporaryElement(UnsetObject.instance);
			} else {
				contentProvider.removeTemporaryElement(UnsetObject.instance);
			}
			viewer.refresh();
		}
	}

	/**
	 * Changes the viewer for this editor.
	 * The viewer should use a CCombo
	 *
	 * @param comboViewer
	 */
	public void setViewer(ComboViewer comboViewer) {
		this.viewer = comboViewer;
		this.combo = viewer.getCCombo();
	}

	//FIXME error avec multiplicité nulllpointerexception l285
	@Override
	public void updateStatus(IStatus status) {
		switch(status.getSeverity()) {
		case IStatus.OK:
			controlDecoration.hide();
			break;
		case IStatus.WARNING:
			FieldDecoration warning = FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_WARNING);
			controlDecoration.setImage(warning.getImage());
			controlDecoration.showHoverText(status.getMessage());
			controlDecoration.setDescriptionText(status.getMessage());
			controlDecoration.show();
			break;
		case IStatus.ERROR:
			FieldDecoration error = FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_ERROR);
			controlDecoration.setImage(error.getImage());
			controlDecoration.showHoverText(status.getMessage());
			controlDecoration.setDescriptionText(status.getMessage());
			controlDecoration.show();
			break;
		default:
			controlDecoration.hide();
			break;
		}


	}

	//	protected void unsetAction() {
	//		viewer.setSelection(StructuredSelection.EMPTY);
	//		if(modelProperty != null) {
	//			modelProperty.setValue(null);
	//		}
	//	}

	//	public void widgetSelected(SelectionEvent e) {
	//		if(e.widget == unset) {
	//			unsetAction();
	//		}
	//	}

	//	public void widgetDefaultSelected(SelectionEvent e) {
	//		//Nothing
	//	}
}

Back to the top