Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9785336aa0553d31c93d6c3a17185218e76decbd (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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*****************************************************************************
 * 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.views.properties.runtime.propertyeditor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.IUndoableOperation;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.papyrus.commands.CheckedOperationHistory;
import org.eclipse.papyrus.views.properties.runtime.Activator;
import org.eclipse.papyrus.views.properties.runtime.controller.ILabelProviderController;
import org.eclipse.papyrus.views.properties.runtime.propertyeditor.descriptor.IPropertyEditorDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
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.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.PlatformUI;


/**
 * Abstract class for all table editors
 */
public abstract class AbstractTablePropertyEditor extends AbstractPropertyEditor {

	/** main composite created by this property editor */
	protected Composite composite;

	/** button to add a reference or modify existing one */
	protected Button addButton;

	/** button to remove reference */
	protected Button removeButton;

	/** table area that displays the values */
	protected Table table;

	/** viewer on the previous table */
	protected TableViewer viewer;

	/** current Value in the editor */
	protected List<Object> currentValue;

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Composite createContent(Composite parent) {
		composite = getWidgetFactory().createComposite(parent, SWT.NONE);
		int columnNu = getColumnNumber();
		GridLayout layout = new GridLayout(columnNu, false);
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		composite.setLayout(layout);
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
		composite.setLayoutData(data);

		// creates label. No TOP/DOWN/etc. position, always on top
		createLabel(composite);

		if(!getIsReadOnly()) {
			// create Button area
			addButton = getWidgetFactory().createButton(composite, "", SWT.NONE);
			addButton.setImage(Activator.getImageFromDescriptor(Activator.imageDescriptorFromPlugin(Activator.ID, "icons/Add_12x12.gif")));
			data = new GridData(SWT.FILL, SWT.CENTER, false, false);
			addButton.setLayoutData(data);
			addButton.addMouseListener(new MouseListener() {

				/**
				 * {@inheritDoc}
				 */
				public void mouseUp(MouseEvent e) {
					performAddButtonPressed();
				}

				/**
				 * {@inheritDoc}
				 */
				public void mouseDown(MouseEvent e) {
				}

				/**
				 * {@inheritDoc}
				 */
				public void mouseDoubleClick(MouseEvent e) {
				}
			});

			removeButton = getWidgetFactory().createButton(composite, "", SWT.NONE);
			removeButton.setImage(Activator.getImageFromDescriptor(Activator.imageDescriptorFromPlugin(Activator.ID, "icons/Delete_12x12.gif")));
			data = new GridData(SWT.FILL, SWT.CENTER, false, false);
			removeButton.setLayoutData(data);
			removeButton.addMouseListener(new MouseListener() {

				/**
				 * {@inheritDoc}
				 */
				public void mouseUp(MouseEvent e) {
					performRemoveButtonPressed();
				}

				/**
				 * {@inheritDoc}
				 */
				public void mouseDown(MouseEvent e) {

				}

				/**
				 * {@inheritDoc}
				 */
				public void mouseDoubleClick(MouseEvent e) {

				}
			});

			if(getController().canMoveValues(new ArrayList<Integer>(), 0)) { // only checks if virtual elements can be moved, not real move, so a 0-delta is fine
				Button upButton = getWidgetFactory().createButton(composite, "", SWT.NONE);
				upButton.setImage(Activator.getImageFromDescriptor(Activator.imageDescriptorFromPlugin(Activator.ID, "icons/Up_12x12.gif")));
				data = new GridData(SWT.FILL, SWT.CENTER, false, false);
				upButton.setLayoutData(data);
				upButton.addMouseListener(new MouseListener() {

					/**
					 * {@inheritDoc}
					 */
					public void mouseUp(MouseEvent e) {
						performUpButtonPressed();
					}

					/**
					 * {@inheritDoc}
					 */
					public void mouseDown(MouseEvent e) {
					}

					/**
					 * {@inheritDoc}
					 */
					public void mouseDoubleClick(MouseEvent e) {
					}
				});

				Button downButton = getWidgetFactory().createButton(composite, "", SWT.NONE);
				downButton.setImage(Activator.getImageFromDescriptor(Activator.imageDescriptorFromPlugin(Activator.ID, "icons/Down_12x12.gif")));
				data = new GridData(SWT.FILL, SWT.CENTER, false, false);
				downButton.setLayoutData(data);
				downButton.addMouseListener(new MouseListener() {

					/**
					 * {@inheritDoc}
					 */
					public void mouseUp(MouseEvent e) {
						performDownButtonPressed();
					}

					/**
					 * {@inheritDoc}
					 */
					public void mouseDown(MouseEvent e) {
					}

					/**
					 * {@inheritDoc}
					 */
					public void mouseDoubleClick(MouseEvent e) {
					}
				});
			}
		}

		Composite tableComposite = getWidgetFactory().createComposite(composite, SWT.NONE);
		data = new GridData(SWT.FILL, SWT.FILL, true, true, columnNu, 1);
		data.heightHint = 80;
		data.widthHint = 200;
		tableComposite.setLayoutData(data);
		// creates table for the display of references
		table = new Table(tableComposite, SWT.BORDER | SWT.MULTI);
		viewer = new TableViewer(table);
		TableColumnLayout tableLayout = new TableColumnLayout();
		tableComposite.setLayout(tableLayout);
		final TableViewerColumn tableViewerColumn = new TableViewerColumn(viewer, SWT.NONE);
		TableColumn tableColumn = tableViewerColumn.getColumn();
		tableLayout.setColumnData(tableColumn, new ColumnWeightData(100, 0, true));
		tableColumn.setText("Value");
		tableViewerColumn.setEditingSupport(createEditingSupport());
		viewer.setUseHashlookup(true);
		viewer.setContentProvider(new IStructuredContentProvider() {

			/**
			 * {@inheritDoc}
			 */
			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

			}

			/**
			 * {@inheritDoc}
			 */
			public void dispose() {

			}

			/**
			 * {@inheritDoc}
			 */
			public Object[] getElements(Object inputElement) {
				if(inputElement instanceof List<?>) {
					return ((List<?>)inputElement).toArray();
				}
				return new Object[0];
			}
		});

		if(getController() instanceof ILabelProviderController) {
			viewer.setLabelProvider(((ILabelProviderController)getController()).getEditorLabelProvider());
		} else {
			viewer.setLabelProvider(new LabelProvider());
		}
		viewer.getControl().setToolTipText(getTooltipText());
		return composite;
	}

	/**
	 * Creates the editing support for the table viewer
	 * 
	 * @return the editing support for the table viewer
	 */
	protected abstract EditingSupport createEditingSupport();

	/**
	 * performs the action handled by the "Add" button
	 */
	protected void performAddButtonPressed() {
		// pops up a window to ask for a new reference
		Display display = Display.getCurrent();
		if(display == null && PlatformUI.isWorkbenchRunning()) {
			display = PlatformUI.getWorkbench().getDisplay();
		}
		display = (display != null) ? display : Display.getDefault();

		List<IUndoableOperation> operations = getController().getCreateValueOperations();

		// either one or more options
		// in case there are several, open a sub-menu
		// otherwise, execute the operation
		if(operations == null || operations.isEmpty()) {
			return;
		}
		if(operations.size() == 1) {
			try {
				CheckedOperationHistory.getInstance().execute(operations.get(0), new NullProgressMonitor(), null);
			} catch (ExecutionException e1) {
				Activator.log.error(e1);
			}
		} else {
			Menu menu = new Menu(addButton);
			for(final IUndoableOperation operation : operations) {
				MenuItem item = new MenuItem(menu, SWT.NONE);
				item.setText(operation.getLabel());
				item.addSelectionListener(new SelectionListener() {

					public void widgetSelected(SelectionEvent e) {
						try {
							CheckedOperationHistory.getInstance().execute(operation, new NullProgressMonitor(), null);
						} catch (ExecutionException e1) {
							Activator.log.error(e1);
						}
					}

					public void widgetDefaultSelected(SelectionEvent e) {

					}
				});
			}
			menu.setVisible(true);
		}

		// now select the new value added. Assumes this is the last one
		int lastIndex = table.getItemCount() - 1;
		table.select(lastIndex);

	}

	/**
	 * performs the action handled by the "Remove" button
	 */
	protected void performRemoveButtonPressed() {
		// use selection to remove element
		// retrieve selected element(s)
		IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
		if(selection == null || selection.isEmpty()) {
			// nothing selected. 
			return;
		}

		int[] selectedIndexes = table.getSelectionIndices();
		Arrays.sort(selectedIndexes);
		List<Integer> indexes = new ArrayList<Integer>();
		for(int i = selectedIndexes.length - 1; i >= 0; i--) {
			indexes.add(selectedIndexes[i]);
		}
		IUndoableOperation operation = getController().getDeleteValueOperation(indexes);
		if(operation != null && operation.canExecute()) {
			try {
				CheckedOperationHistory.getInstance().execute(operation, new NullProgressMonitor(), null);
			} catch (ExecutionException e1) {
				Activator.log.error(e1);
			}
		}
	}

	/**
	 * performs the action handled by the "Up" button
	 */
	protected void performUpButtonPressed() {
		int[] selectionIndices = table.getSelectionIndices();
		final List<Integer> selectionList = new ArrayList<Integer>();
		final List<Integer> newSelectionList = new ArrayList<Integer>();
		for(int i : selectionIndices) {
			selectionList.add(i);
			newSelectionList.add(i - 1);
		}
		IUndoableOperation moveOperation = getController().getMoveCurrentValuesOperation(selectionList, -1);
		if(moveOperation != null && moveOperation.canExecute()) {
			try {
				CheckedOperationHistory.getInstance().execute(moveOperation, new NullProgressMonitor(), null);
				// try to restore selection in the view
				int[] newSelection = new int[newSelectionList.size()];
				for(int i = 0; i < newSelectionList.size(); i++) {
					newSelection[i] = newSelectionList.get(i);
				}
				table.select(newSelection);
			} catch (ExecutionException e1) {
				Activator.log.error(e1);
			}
		}
	}

	/**
	 * performs the action handled by the "Down" button
	 */
	protected void performDownButtonPressed() {
		// use selection to remove element
		// retrieve selected element(s)
		int[] selectionIndices = table.getSelectionIndices();
		final List<Integer> selectionList = new ArrayList<Integer>();
		final List<Integer> newSelectionList = new ArrayList<Integer>();
		for(int i : selectionIndices) {
			selectionList.add(i);
			newSelectionList.add(i + 1);
		}
		IUndoableOperation moveOperation = getController().getMoveCurrentValuesOperation(selectionList, +1);
		if(moveOperation != null && moveOperation.canExecute()) {
			try {
				CheckedOperationHistory.getInstance().execute(moveOperation, new NullProgressMonitor(), null);
				// try to restore selection in the view
				int[] newSelection = new int[newSelectionList.size()];
				for(int i = 0; i < newSelectionList.size(); i++) {
					newSelection[i] = newSelectionList.get(i);
				}
				table.select(newSelection);
			} catch (ExecutionException e1) {
				Activator.log.error(e1);
			}
		}
	}



	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Control createLabel(Composite parent) {
		GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		return createLabel(parent, data);
	}

	/**
	 * Returns the number of column for the composite
	 * 
	 * @return the number of column for the composite
	 */
	protected int getColumnNumber() {
		if(getIsReadOnly()) {
			return 1;
		}
		if(getController().canMoveValues(new ArrayList<Integer>(), 0)) {
			return 5;
		}
		return 3;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleContentChanged() {
		// this should tells the controller that the input has to be applied to the model
		getController().updateModel();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public IStatus init(IPropertyEditorDescriptor descriptor) {
		setDescriptor(descriptor);
		setTooltipText(descriptor.getTooltipText());
		return Status.OK_STATUS;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<Object> getValue() {
		return currentValue;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void setValue(Object valueToEdit) {
		if(!isValid(table)) {
			return;
		}
		if(valueToEdit instanceof List<?>) {
			currentValue = (List<Object>)valueToEdit;
		} else if(valueToEdit == null) {
			currentValue = null;
		} else {
			Activator.log.error(valueToEdit + " is not a list of Object for current table: " + getDescriptor().getLabel(), null);
		}
		viewer.setInput((currentValue != null) ? currentValue : Collections.emptyList());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void dispose() {
		if(isValid(composite)) {
			composite.dispose();
			composite = null;
			setController(null);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean isDisposed() {
		if(composite == null) {
			return true;
		}
		return composite.isDisposed();
	}
}

Back to the top