Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a3325e479aff2e55d114151432cd1716d1d14763 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*****************************************************************************
 * Copyright (c) 2016 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:
 *   Fanch BONNABESSE (ALL4TEC) fanch.bonnabesse@all4tec.net - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.infra.widgets.editors;

import java.util.Collection;
import java.util.Collections;

import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jface.layout.TreeColumnLayout;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.infra.widgets.Activator;
import org.eclipse.papyrus.infra.widgets.creation.ReferenceValueFactory;
import org.eclipse.papyrus.infra.widgets.messages.Messages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Widget;

/**
 * This class allow to define a Tree Reference Value Editor.
 */
public abstract class TreeReferenceValueEditor extends AbstractValueEditor implements SelectionListener {

	/**
	 * The Button used to create a new instance
	 */
	protected Button createInstanceButton;

	/**
	 * The Button used to unset the current value
	 */
	protected Button unsetButton;

	/**
	 * The label provider used to display the values in both the label and the
	 * selection dialog
	 */
	protected ILabelProvider labelProvider;

	/**
	 * The current value for this editor
	 */
	protected Object value;

	/**
	 * The factory used to create or edit objects directly from this editor
	 */
	protected ReferenceValueFactory valueFactory;

	/**
	 * TreeViewer to show
	 */
	protected TreeViewer treeViewer;

	/**
	 * The tree of the TreeViewer.
	 */
	protected Tree tree;

	/**
	 * The parent composite of the tree viewer.
	 */
	protected Composite compositeTree;

	/**
	 * Listener to modify IObservableValue when modelProperty changed.
	 */
	private IChangeListener changeListener = new IChangeListener() {

		@Override
		public void handleChange(ChangeEvent event) {
			setWidgetObservable(createWidgetObservable(modelProperty));
			checkCreateInstanceButton();
			setValueRootContentProvider();
			if (null != treeViewer) {
				treeViewer.refresh();
				if (null != tree && null != tree.getTopItem()) {
					treeViewer.expandToLevel(tree.getTopItem().getData(), 10);
				}
			}
		}
	};

	/**
	 *
	 * Constructs a new ReferenceDialog in the given parent Composite. The style
	 * will be applied to the CLabel displaying the current value.
	 *
	 * @param parent
	 *            The parent composite.
	 * @param style
	 *            The style for the created widget.
	 */
	public TreeReferenceValueEditor(final Composite parent, final int style) {
		super(parent, style);

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

		// Create a parent composite to set the margin
		Composite gridComposite = factory.createComposite(this);
		gridComposite.setLayout(new GridLayout());
		gridComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		compositeTree = factory.createComposite(gridComposite);
		final TreeColumnLayout treeColumnLayout = new TreeColumnLayout();
		compositeTree.setLayout(treeColumnLayout);
		final GridData compositeTreeGridData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		compositeTreeGridData.minimumHeight = 150;
		compositeTree.setLayoutData(compositeTreeGridData);

		treeViewer = new TreeViewer(compositeTree, SWT.MULTI | SWT.NO_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
		tree = treeViewer.getTree();
		tree.setLinesVisible(true);
		tree.setHeaderVisible(true);

		final TreeViewerColumn columnProperty = new TreeViewerColumn(treeViewer, SWT.LEFT);
		columnProperty.getColumn().setAlignment(SWT.LEFT);
		columnProperty.getColumn().setText(Messages.TreeReferenceValueEditor_NameColumnProperty); 
		treeColumnLayout.setColumnData(columnProperty.getColumn(), new ColumnWeightData(100));
		final TreeViewerColumn columnValue = new TreeViewerColumn(treeViewer, SWT.RIGHT);
		columnValue.getColumn().setAlignment(SWT.LEFT);
		columnValue.getColumn().setText(Messages.TreeReferenceValueEditor_NameColumnValue); 
		treeColumnLayout.setColumnData(columnValue.getColumn(), new ColumnWeightData(200));

		columnValue.setEditingSupport(createEditingSupport());

		createButtons();
	}

	/**
	 * Create the buttons.
	 */
	protected void createButtons() {
		((GridLayout) getLayout()).numColumns += 1;

		// Create a parent composite to set add the buttons on the top
		final Composite gridComposite = factory.createComposite(this);
		gridComposite.setLayout(new GridLayout(2, true));
		gridComposite.setLayoutData(new GridData(SWT.NONE, SWT.BEGINNING, false, false));

		createInstanceButton = factory.createButton(gridComposite, null, SWT.PUSH);
		createInstanceButton.setImage(Activator.getDefault().getImage("/icons/Add_12x12.gif")); //$NON-NLS-1$
		createInstanceButton.setToolTipText(Messages.ReferenceDialog_CreateANewObject);
		createInstanceButton.addSelectionListener(this);

		unsetButton = factory.createButton(gridComposite, null, SWT.PUSH);
		unsetButton.setImage(Activator.getDefault().getImage("/icons/Delete_12x12.gif")); //$NON-NLS-1$
		unsetButton.setToolTipText(Messages.ReferenceDialog_UnsetValue);
		unsetButton.addSelectionListener(this);

		if (null != treeViewer && treeViewer.getSelection() != null && !treeViewer.getSelection().isEmpty()) {
			unsetButton.setEnabled(true);
		} else {
			unsetButton.setEnabled(false);
		}
	}

	/**
	 * The action executed when the "create" button is selected Create a new
	 * instance and assign it to this reference.
	 */
	protected void createAction() {
		if (valueFactory != null && valueFactory.canCreateObject()) {
			final Object context = getContextElement();
			getOperationExecutor(context).execute(new Runnable() {

				@Override
				public void run() {
					Object value = valueFactory.createObject(createInstanceButton, context);
					if (null == value) {
						// Cancel the operation
						throw new OperationCanceledException();
					}

					Collection<Object> validatedObjects = valueFactory.validateObjects(Collections.singleton(value));
					if (!validatedObjects.isEmpty()) {
						setValue(validatedObjects.iterator().next());
						if (null != tree) {
							int itemsSize = tree.getItems().length;
							TreeItem item = tree.getItem(itemsSize - 1);
							if (null != item) {
								treeViewer.expandToLevel(item.getData(), TreeViewer.ALL_LEVELS);
							}
						}
						checkCreateInstanceButton();
					}
				}
			}, NLS.bind(Messages.ReferenceDialog_setOperation, labelText));
		}
	}

	/**
	 * Sets the Label provider for this editor If the label provider is null, a
	 * default one will be used. The same label provider is used for both the
	 * editor's label and the selection dialog.
	 *
	 * @param provider
	 *            The label provider
	 */
	public void setLabelProvider(final ILabelProvider provider) {
		if (null == provider) {
			setLabelProvider(new LabelProvider());
			return;
		}
		this.labelProvider = provider;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.papyrus.infra.widgets.editors.AbstractValueEditor#getValue()
	 */
	@Override
	public Object getValue() {
		if (null != modelProperty) {
			return modelProperty.getValue();
		}
		return value;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.papyrus.infra.widgets.editors.AbstractEditor#getEditableType()
	 */
	@Override
	public Object getEditableType() {
		return Object.class;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.papyrus.infra.widgets.editors.AbstractValueEditor#setModelObservable(org.eclipse.core.databinding.observable.value.IObservableValue)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public void setModelObservable(final IObservableValue modelProperty) {
		if (null != modelProperty) {
			this.value = modelProperty.getValue();
		}
		super.setModelObservable(modelProperty);
		setWidgetObservable(createWidgetObservable(modelProperty));
		addListeners();
		checkCreateInstanceButton();
	}

	/**
	 * This allows to set the Reference value factory.
	 * 
	 * @param factory
	 *            The reference value factory.
	 */
	public void setValueFactory(final ReferenceValueFactory factory) {
		this.valueFactory = factory;
	}

	/**
	 * Get the Reference value factory.
	 * 
	 * @return The Reference value factory.
	 */
	public ReferenceValueFactory getValueFactory() {
		return valueFactory;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	@Override
	public void widgetSelected(final SelectionEvent e) {
		Widget widget = e.widget;
		if (widget == createInstanceButton) {
			createAction();
		} else if (widget == unsetButton) {
			unsetAction();
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	@Override
	public void widgetDefaultSelected(final SelectionEvent e) {
		// Do nothing
	}

	/**
	 * Set value of the modelProperty and create the widget observable.
	 *
	 * @param value
	 *            The new value.
	 */
	@SuppressWarnings("unchecked")
	public void setValue(final Object value) {
		removeListeners();
		this.value = value;
		if (null != modelProperty) {
			modelProperty.setValue(value);
			setWidgetObservable(createWidgetObservable(modelProperty));
			addListeners();
		}

		if (null != treeViewer) {
			IContentProvider treeViewerContentProvider = treeViewer.getContentProvider();
			if (null != treeViewerContentProvider) {
				setValueRootContentProvider();
				treeViewer.refresh();
			}
		}

		commit();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.papyrus.infra.widgets.editors.AbstractEditor#isReadOnly()
	 */
	@Override
	public boolean isReadOnly() {
		return false;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.papyrus.infra.widgets.editors.AbstractEditor#setReadOnly(boolean)
	 */
	@Override
	public void setReadOnly(final boolean readOnly) {
		// Do Nothing
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.swt.widgets.Widget#dispose()
	 */
	@Override
	public void dispose() {
		removeListeners();
		super.dispose();
	}

	/**
	 * Add all the listeners.
	 */
	protected void addListeners() {
		if (modelProperty != null) {
			modelProperty.addChangeListener(changeListener);
		}
	};

	/**
	 * Remove all the listeners.
	 */
	protected void removeListeners() {
		if (modelProperty != null) {
			modelProperty.removeChangeListener(changeListener);
		}
	};

	/**
	 * Set the value root to the content provider of the tree viewer.
	 */
	public abstract void setValueRootContentProvider();

	/**
	 * Set the providers of the TreeViewer.
	 */
	public abstract void setProvidersTreeViewer();

	/**
	 * Create the Editing Support.
	 *
	 * @return The EditingSupport created.
	 */
	public abstract EditingSupport createEditingSupport();

	/**
	 * This allow to create the widget observable value.
	 *
	 * @param modelProperty
	 *            The current observable value.
	 * @return The created IObservableValue.
	 */
	@SuppressWarnings("rawtypes")
	public abstract IObservableValue createWidgetObservable(final IObservableValue modelProperty);

	/**
	 * Verify if the CreateInstance button should be enabled or not.
	 */
	public abstract void checkCreateInstanceButton();

	/**
	 * Action associated to the Unset button.
	 */
	public abstract void unsetAction();
}

Back to the top