Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 567f8269df9519dad4fa137d4f0ac8a37e0d1e08 (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
/*****************************************************************************
 * Copyright (c) 2019 CEA LIST and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.emf.ui.editor.factories;

import java.util.Arrays;
import java.util.Collection;

import org.eclipse.core.runtime.Assert;
import org.eclipse.emf.common.ui.celleditor.ExtendedDialogCellEditor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.emf.ui.converters.IDisplayConverter;
import org.eclipse.papyrus.emf.ui.converters.IdentityDisplayConverter;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
import org.eclipse.ui.dialogs.ISelectionStatusValidator;

/**
 * Custom DialogCellEditor for PropertyView
 */
public class CustomExtendedDialogCellEditor extends ExtendedDialogCellEditor {

	/**
	 * the content provider used by the dialog
	 */
	private ITreeContentProvider contentProvider;

	/**
	 * the input for the dialog
	 */
	private Collection<?> dialogInput;

	/**
	 * the validator used by the dialog
	 */
	private ISelectionStatusValidator validator;

	/**
	 * the edited property descriptor
	 */
	private IItemPropertyDescriptor itemPropertyDescriptor;

	/**
	 * the display converter used by the dialog
	 */
	private IDisplayConverter displayConverter;

	/**
	 * the title of the dialog
	 */
	private String dialogTitle;

	/**
	 * the message of the dialogF
	 */
	private String dialogMessage;

	/**
	 * the edited object
	 */
	private EObject editedObject;

	/**
	 * the edited feature
	 */
	private EStructuralFeature editedFeature;

	/**
	 * the image to use for the dialog;
	 */
	private Image image;

	/**
	 *
	 * Constructor.
	 *
	 * @param composite
	 *            the parent composite
	 * @param labelProvider
	 *            the label provider to use for the CellEditor
	 * @param itemPropertyDescriptor
	 *            the property descriptor
	 * @param editedObject
	 *            the edited EObject
	 * @param editedFeature
	 *            the edited feature of the EObject
	 */
	public CustomExtendedDialogCellEditor(final Composite composite, final ILabelProvider labelProvider, final IItemPropertyDescriptor itemPropertyDescriptor, final EObject editedObject, final EStructuralFeature editedFeature) {
		super(composite, labelProvider);
		Assert.isNotNull(itemPropertyDescriptor);
		this.itemPropertyDescriptor = itemPropertyDescriptor;
		Assert.isNotNull(editedObject);
		this.editedObject = editedObject;
		Assert.isNotNull(editedFeature);
		this.editedFeature = editedFeature;
	}

	/**
	 *
	 * @param dialogTitle
	 *            the title of the dialog
	 */
	public void setDialogTitle(final String dialogTitle) {
		this.dialogTitle = dialogTitle;
	}

	/**
	 *
	 * @param dialogMessage
	 *            the message to display in the dialog
	 */
	public void setDialogMessage(final String dialogMessage) {
		this.dialogMessage = dialogMessage;
	}

	/**
	 *
	 * @param contentProvider
	 *            the content provider to use. It can't be <code>null</code>
	 */
	public void setContentProvider(final ITreeContentProvider contentProvider) {
		this.contentProvider = contentProvider;
	}

	/**
	 *
	 * @param displayConverter
	 *            the display converter to use. It can't be <code>null</code>
	 */
	public void setDisplayConverter(final IDisplayConverter displayConverter) {
		this.displayConverter = displayConverter;
	}

	/**
	 *
	 * @param dialogInput
	 *            the input used by the dialog. It must not be <code>null</code> or empty
	 */
	public void setDialogInput(final Collection<?> dialogInput) {
		this.dialogInput = dialogInput;
	}

	/**
	 *
	 * @param validator
	 *            the validator to use
	 */
	public void setSelectionStatusValidator(final ISelectionStatusValidator validator) {
		this.validator = validator;
	}

	/**
	 *
	 * @param image
	 *            the image to use for the dialog
	 */
	public void setImage(final Image image) {
		this.image = image;
	}

	/**
	 * Before calling this method, several fields must be initialized using these methods:
	 * <ul>
	 * <li>{@link #setContentProvider(ITreeContentProvider)} (mandatory)</li>
	 * <li>{@link #setDialogInput(Collection)} (mandatory)</li>
	 * <li>{@link #setSelectionStatusValidator(ISelectionStatusValidator)} (mandatory)</li>
	 * <li>{@link #setDialogMessage(String)}</li>
	 * <li>{@link #setDialogTitle(String)}</li>
	 * <li>{@link #setDisplayConverter(IDisplayConverter)} (by default we used the {@link IdentityDisplayConverter}</li>
	 * <li></li>
	 * </ul>
	 *
	 * @see org.eclipse.jface.viewers.DialogCellEditor#openDialogBox(org.eclipse.swt.widgets.Control)
	 *
	 * @param cellEditorWindow
	 * @return
	 *         the selected element
	 */
	@Override
	protected Object openDialogBox(final Control cellEditorWindow) {
		checkInput();
		final ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(Display.getDefault().getActiveShell(), this.labelProvider, this.contentProvider);
		Object initialSelection = this.displayConverter.semanticToDisplayValue(editedObject.eGet(this.editedFeature), this.editedObject);
		Object[] selectedValue = { initialSelection };

		dialog.setSize(100, 50);
		dialog.setInput(this.dialogInput);
		if (null != this.validator) {
			dialog.setValidator(this.validator);
		}
		dialog.setComparator(new ViewerComparator());
		dialog.setAllowMultiple(isManyFeature());
		dialog.setInitialSelections(selectedValue);
		dialog.setTitle(this.dialogTitle);
		dialog.setMessage(this.dialogMessage);
		if (null != this.image) {
			dialog.setImage(image);
		}
		int userResponse = dialog.open();
		Object toReturn = null;
		if (userResponse == Window.OK) {
			final Object[] result = dialog.getResult();
			if (result == null) {
				toReturn = this.itemPropertyDescriptor.getPropertyValue(null);
			} else {
				if (this.editedFeature.isMany()) {
					toReturn = Arrays.asList(result);
				} else {
					toReturn = result[0];
				}
			}
		} else {
			toReturn = this.itemPropertyDescriptor.getPropertyValue(editedObject);
		}

		toReturn = this.displayConverter.displayToSemanticValue(toReturn, this.editedObject);
		return toReturn;
	}

	/**
	 * This method checks the required field of the class
	 */
	private void checkInput() {
		if (null == this.displayConverter) {
			this.displayConverter = new IdentityDisplayConverter();
		}
		Assert.isNotNull(this.contentProvider, "The ITreeContentProvider has not been set"); //$NON-NLS-1$
		Assert.isNotNull(this.dialogInput, "The dialog input is null"); //$NON-NLS-1$
		Assert.isNotNull(this.validator, "The validator has not been set"); //$NON-NLS-1$
	}

	/**
	 * @see org.eclipse.jface.viewers.CellEditor#dispose()
	 *
	 */
	@Override
	public void dispose() {
		super.dispose();
		this.contentProvider = null;
		this.dialogInput.clear();
		this.dialogInput = null;
		this.displayConverter = null;
		this.editedFeature = null;
		this.editedObject = null;
		this.image = null;
		this.itemPropertyDescriptor = null;
		this.validator = null;
	}

	/**
	 *
	 * @return
	 *         <code>true</code> if the edited feature is multi-valued
	 */
	private boolean isManyFeature() {
		return this.editedFeature.isMany();
	}


}

Back to the top