Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fc9faa3e3089bdaf872c5f1cb76c7263eefd1ce5 (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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*****************************************************************************
 * Copyright (c) 2018 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:
 *   Nicolas FAUVERGUE (CEA LIST) nicolas.fauvergue@cea.fr - Initial API and implementation
 *   
 *****************************************************************************/

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.papyrus.infra.widgets.Activator;
import org.eclipse.papyrus.infra.widgets.creation.ReferenceValueFactory;
import org.eclipse.papyrus.infra.widgets.editors.IElementSelectionListener;
import org.eclipse.papyrus.infra.widgets.editors.IElementSelector;
import org.eclipse.papyrus.infra.widgets.messages.Messages;
import org.eclipse.papyrus.infra.widgets.providers.CollectionContentProvider;
import org.eclipse.papyrus.infra.widgets.util.ValueUtils;
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.Control;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.services.IDisposable;

/**
 * The widget for the simple multi value (without selection widget).
 * 
 * @since 3.3
 */
public class MultipleValueWidget implements ISelectionChangedListener, IDoubleClickListener, IElementSelectionListener, SelectionListener, IDisposable {

	/**
	 * The object selector.
	 */
	protected IElementSelector selector;

	/**
	 * The up/down buttons section.
	 */
	protected Composite rightButtonsSection;

	/**
	 * The listViewer for chosen elements.
	 */
	protected TreeViewer selectedElementsViewer;

	/**
	 * The list for chosen elements.
	 */
	protected Tree selectedElements;

	/**
	 * The create action button.
	 */
	protected Button create;

	/**
	 * The delete action button.
	 */
	protected Button delete;

	/**
	 * the up action button.
	 */
	protected Button up;

	/**
	 * the down action button.
	 */
	protected Button down;

	/**
	 * The label provider for the listViewer of chosen elements.
	 */
	protected ILabelProvider labelProvider;

	/**
	 * The currently chosen elements.
	 */
	protected final Collection<Object> allElements;

	/**
	 * Indicates if the values should be unique (according to Object.equals()).
	 */
	protected boolean unique;

	/**
	 * Indicates if the list is ordered.
	 */
	protected boolean ordered;

	/**
	 * The factory for creating new elements.
	 */
	protected ReferenceValueFactory factory;

	/**
	 * The model element being edited (if any), to which elements are to be added or removed.
	 */
	protected Object contextElement;

	/**
	 * The list of newly created objects.
	 */
	protected Set<Object> newObjects = new HashSet<Object>();


	/**
	 * The maximum number of values selected.
	 */
	protected int upperBound;

	/**
	 * the initial selection.
	 */
	protected List<?> initialSelection;

	/**
	 * Constructor.
	 *
	 * @param selector
	 *            The element selector used by this dialog
	 */
	public MultipleValueWidget(IElementSelector selector) {
		this(selector, false, false);
	}

	/**
	 *
	 * Constructor.
	 *
	 * @param selector
	 *            The element selector used by this dialog
	 * @param unique
	 *            True if the values returned by this dialog should be unique
	 */
	public MultipleValueWidget(IElementSelector selector, boolean unique) {
		this(selector, unique, false);
	}

	/**
	 *
	 * Constructor.
	 *
	 * @param selector
	 *            The element selector used by this dialog
	 * @param unique
	 *            True if the values returned by this dialog should be unique
	 */
	public MultipleValueWidget(IElementSelector selector, boolean unique, boolean ordered) {
		this(selector, unique, false, ValueUtils.MANY);
	}

	/**
	 *
	 * Constructor.
	 *
	 * @param selector
	 *            The element selector used by this dialog
	 * @param unique
	 *            True if the values returned by this dialog should be unique
	 * @param upperBound
	 *            The maximum number of values selected.
	 */
	public MultipleValueWidget(IElementSelector selector, boolean unique, boolean ordered, int upperBound) {
		Assert.isNotNull(selector, "The element selector should be defined"); //$NON-NLS-1$
		this.selector = selector;
		allElements = unique ? new LinkedHashSet<Object>() : new LinkedList<Object>();
		this.unique = unique;
		this.ordered = ordered;
		this.upperBound = upperBound;
		selector.addElementSelectionListener(this);
		init();
	}

	/**
	 * initialize the widget if required
	 */
	protected void init() {
		// nothing to do
	}

	/**
	 * Create the contents of the dialog
	 * 
	 * @return
	 * 		the the composite which encapsulate all the sub composite
	 */
	public final Composite create(Composite composite) {
		Composite parent = createContents(composite);
		updateControls();
		return parent;
	}

	/**
	 * Create the contents of the dialog
	 * 
	 * @return
	 * 		the the composite which encapsulate all the sub composite
	 */
	protected Composite createContents(Composite parent) {
		Object parentLayout = parent.getLayout();
		GridLayout layout = null;
		if (parentLayout instanceof GridLayout) {
			layout = (GridLayout) parentLayout;
		} else {
			parent = new Composite(parent, SWT.NONE);
			layout = new GridLayout();
			parent.setLayout(layout);
		}

		createSections(parent);

		allElements.clear();
		allElements.addAll(getInitialElementSelections());
		return parent;
	}

	/**
	 * This allows to create the sections of the dialog.
	 * 
	 * @param parent
	 *            The parent composite.
	 */
	protected void createSections(final Composite parent) {
		final Composite selectedPane = new Composite(parent, SWT.NONE);
		selectedPane.setLayout(new GridLayout(2, false));
		selectedPane.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		createListSection(selectedPane);
		createRightButtonsSection(selectedPane);
	}


	public void setInitialSelections(List<?> selectedElements) {
		this.initialSelection = selectedElements;
		allElements.clear();
		allElements.addAll(getInitialElementSelections());
	}

	/**
	 * @return
	 */
	private Collection<? extends Object> getInitialElementSelections() {
		if (this.initialSelection == null) {
			return Collections.emptyList();
		}
		return this.initialSelection;
	}

	/**
	 * Creates the list displaying the currently selected elements
	 *
	 * @param parent
	 *            The composite in which the section is created
	 */
	protected void createListSection(Composite parent) {

		selectedElements = new Tree(parent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		// selectedElements.addSelectionListener(this);
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		selectedElements.setLayoutData(data);
		selectedElementsViewer = new TreeViewer(selectedElements);

		selectedElementsViewer.addSelectionChangedListener(this);
		selectedElementsViewer.addDoubleClickListener(this);

		selectedElementsViewer.setContentProvider(createListSectionContentProvider());

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

		selectedElementsViewer.setInput(allElements);
		selector.setSelectedElements(allElements.toArray());
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.infra.nattable.wizard.pages.MultipleValueSelectionWizard#createListSectionContentProvider()
	 *
	 * @return
	 * 		the content provider to use in the list section (right part)
	 */
	protected IContentProvider createListSectionContentProvider() {
		return CollectionContentProvider.instance;
	}

	/**
	 * Creates the up/down controls section
	 *
	 * @param parent
	 *            The composite in which the section is created
	 */
	protected void createRightButtonsSection(Composite parent) {
		rightButtonsSection = new Composite(parent, SWT.NONE);
		rightButtonsSection.setLayout(new GridLayout(1, true));

		up = new Button(rightButtonsSection, SWT.PUSH);
		up.setImage(Activator.getDefault().getImage("/icons/Up_12x12.gif")); //$NON-NLS-1$
		up.addSelectionListener(this);
		up.setToolTipText(Messages.MultipleValueEditor_MoveSelectedElementsUp);

		down = new Button(rightButtonsSection, SWT.PUSH);
		down.setImage(Activator.getDefault().getImage("/icons/Down_12x12.gif")); //$NON-NLS-1$
		down.addSelectionListener(this);
		down.setToolTipText(Messages.MultipleValueEditor_MoveSelectedElementsDown);

		create = new Button(rightButtonsSection, SWT.PUSH);
		create.setImage(Activator.getDefault().getImage("/icons/Add_12x12.gif")); //$NON-NLS-1$
		create.addSelectionListener(this);
		create.setToolTipText(Messages.MultipleValueSelectorDialog_CreateNewElement);

		delete = new Button(rightButtonsSection, SWT.PUSH);
		delete.setImage(Activator.getDefault().getImage("/icons/Delete_12x12.gif")); //$NON-NLS-1$
		delete.addSelectionListener(this);
		delete.setToolTipText(Messages.MultipleValueSelectorDialog_DeleteNewElement);
		delete.setEnabled(false);
	}

	/**
	 * Sets the label provider used to display the selected elements
	 *
	 * @param labelProvider
	 */
	public void setLabelProvider(ILabelProvider labelProvider) {
		this.labelProvider = labelProvider;
	}

	/**
	 * {@inheritDoc} Handles the events on one of the control buttons
	 *
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
	 *
	 * @param e
	 *            The event that occurred
	 */
	@Override
	public void widgetSelected(SelectionEvent e) {
		if (e.widget == up) {
			upAction();
		} else if (e.widget == down) {
			downAction();
		} else if (e.widget == create) {
			createAction();
		} else if (e.widget == delete) {
			deleteAction();
		}

		updateControls();
	}

	/**
	 * Sets the {@link ReferenceValueFactory} for this editor. The {@link ReferenceValueFactory} is used to create
	 * new instances and edit existing ones.
	 *
	 * @param factory
	 *            The {@link ReferenceValueFactory} to be used by this editor
	 */
	public void setFactory(ReferenceValueFactory factory) {
		this.factory = factory;
		updateControls();
	}

	/**
	 * Handles the "Up" action
	 */
	protected void upAction() {
		IStructuredSelection selection = (IStructuredSelection) selectedElementsViewer.getSelection();

		// We need a list to move objects. LinkedHashSet can't do that
		java.util.List<Object> list = new LinkedList<Object>(allElements);
		for (Object o : selection.toArray()) {
			int oldIndex = list.indexOf(o);
			if (oldIndex > 0) {
				move(list, oldIndex, oldIndex - 1);
			}
		}

		allElements.clear();
		allElements.addAll(list);

		IStructuredSelection selectionCopy = new StructuredSelection(selection.toArray());
		selectedElementsViewer.setSelection(selectionCopy);
		selectedElementsViewer.refresh();
	}

	/**
	 * Handles the "Down" action
	 */
	protected void downAction() {
		IStructuredSelection selection = (IStructuredSelection) selectedElementsViewer.getSelection();

		// We need a list to move objects. LinkedHashSet can't do that
		java.util.List<Object> list = new LinkedList<Object>(allElements);

		int maxIndex = list.size() - 1;

		Object[] selectionArray = selection.toArray();
		for (int i = selectionArray.length - 1; i >= 0; i--) {
			Object o = selectionArray[i];
			int oldIndex = list.indexOf(o);
			if (oldIndex < maxIndex) {
				move(list, oldIndex, oldIndex + 1);
			}
		}

		allElements.clear();
		allElements.addAll(list);

		IStructuredSelection selectionCopy = new StructuredSelection(selection.toArray());
		selectedElementsViewer.setSelection(selectionCopy);
		selectedElementsViewer.refresh();
	}

	/**
	 * Handles the "Create" action
	 */
	protected void createAction() {
		if (factory == null) {
			return;
		}

		Object newObject;

		try {
			newObject = factory.createObject(this.create, this.contextElement);
		} catch (OperationCanceledException e) {
			// The user cancelled and we rolled back pending model changes
			newObject = null;
		}

		if (newObject == null) {
			return;
		}

		newObjects.add(newObject);
		selector.newObjectCreated(newObject);

		Object[] createdObjects = new Object[] { newObject };
		addElements(createdObjects);

		selector.setSelectedElements(allElements.toArray());
	}


	/**
	 * Moves an element from oldIndex to newIndex
	 *
	 * @param list
	 *            The list in which to move the object
	 * @param oldIndex
	 * @param newIndex
	 */
	private void move(java.util.List<Object> list, int oldIndex, int newIndex) {
		int size = list.size();

		if (oldIndex < 0 || oldIndex >= size) {
			throw new IndexOutOfBoundsException("oldIndex: " + oldIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$
		}
		if (newIndex < 0 || newIndex >= size) {
			throw new IndexOutOfBoundsException("newIndex: " + newIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$
		}
		Object element = list.remove(oldIndex);
		list.add(newIndex, element);
	}

	/**
	 * Handles the "Remove" action
	 */
	protected void removeAction() {
		if (canRemove()) {
			IStructuredSelection selection = (IStructuredSelection) selectedElementsViewer.getSelection();
			if (selection.isEmpty()) {
				return;
			}

			for (Object element : selection.toArray()) {
				allElements.remove(element);
			}

			selector.setSelectedElements(allElements.toArray());
			selectedElementsViewer.setSelection(null);
			selectedElementsViewer.refresh();
		}
	}

	/**
	 * Handles the "Delete" action
	 */
	protected void deleteAction() {
		// nothing to do here
		ISelection selection = selectedElementsViewer.getSelection();
		if (selection instanceof IStructuredSelection) {
			final Iterator<?> selectedObjects = ((IStructuredSelection) selection).iterator();
			List<Object> removedObjects = new ArrayList<Object>();
			while (selectedObjects.hasNext()) {
				Object selectedObject = selectedObjects.next();
				if (newObjects.contains(selectedObject)) {
					newObjects.remove(selectedObject);
				}
				removedObjects.add(selectedObject);
			}

			removeElements(removedObjects.toArray());
			selector.setSelectedElements(allElements.toArray());
		}
	}

	/**
	 * Adds the specified elements to the currently selected elements (For
	 * "Add" and "Add all" actions)
	 *
	 * @param elements
	 *            The elements to be added
	 */
	@Override
	public void addElements(final Object[] elements) {
		if (null != elements && elements.length > 0) {
			allElements.addAll(Arrays.asList(elements));
			selectedElementsViewer.refresh();
		}
	}

	/**
	 * Adds the specified elements to the currently selected elements (For
	 * "Remove" and "Remove all" actions)
	 *
	 * @param elements
	 *            The elements to be added
	 */
	public void removeElements(final Object[] elements) {
		if (null != elements && elements.length > 0) {
			allElements.removeAll(Arrays.asList(elements));
			selectedElementsViewer.refresh();
		}
	}

	public List<Object> getSelection() {
		if (factory != null) {
			java.util.List<Object> objectsToValidate = new LinkedList<Object>();
			for (Object object : newObjects) {
				if (allElements.contains(object)) {
					objectsToValidate.add(object);
				}
			}
			factory.validateObjects(objectsToValidate);
			selector.clearTemporaryElements();
		}
		return new LinkedList<Object>(allElements);
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
		// Nothing (see #doubleClick())
	}

	/**
	 * Indicates if the selected values should be unique (According to Object.equals())
	 *
	 * @param unique
	 */
	public void setUnique(boolean unique) {
		this.unique = unique;
		updateControls();
	}

	/**
	 * Indicates if the selected elements should be ordered
	 *
	 * @param ordered
	 */
	public void setOrdered(boolean ordered) {
		this.ordered = ordered;
		updateControls();
	}

	public void updateControls() {
		updateControl(up, ordered && canMove(true));
		updateControl(down, ordered && canMove(false));
		updateControl(create, this.factory != null && this.factory.canCreateObject());
		updateControl(delete, canDelete());
	}

	/**
	 * @return
	 */
	protected boolean canDelete() {
		return true;
	}

	/**
	 * 
	 * @param up
	 *            if <code>true</code> we try to move the element to the up, if <code>false</code> we try to move the element to the down
	 * @return
	 * 		<code>true</code> if the element can be moved
	 */
	protected boolean canMove(boolean up) {
		return null != selectedElementsViewer && null != selectedElementsViewer.getSelection() && !selectedElementsViewer.getSelection().isEmpty();
	}

	/**
	 * 
	 * @return
	 * 		<code>true</code> if we can add elements
	 */
	protected boolean canAdd() {
		/* Disable the bouton 'add' if the upperBound is reached */
		boolean canAdd = true;
		if (this.upperBound != ValueUtils.MANY) {
			if (allElements.size() >= this.upperBound) {
				canAdd = false;
			}
		}
		return canAdd;
	}

	protected void updateControl(Control control, boolean enabled) {
		if (control != null) {
			control.setEnabled(enabled);
		}
	}

	public void setSelector(IElementSelector selector) {
		this.selector = selector;
	}

	/**
	 * Set the maximum number of values selected.
	 *
	 * @param upperBound
	 */
	public void setUpperBound(int upperBound) {
		this.upperBound = upperBound;
	}

	/**
	 * Sets the optional context of the element that is being edited, in which others will be added and removed.
	 *
	 * @param contextElement
	 *            the model element that is being edited
	 */
	public void setContextElement(Object contextElement) {
		this.contextElement = contextElement;
	}

	/**
	 * Queries the optional context of the element that is being edited, in which others will be added and removed.
	 *
	 * @return the model element that is being edited
	 */
	public Object getContextElement() {
		return contextElement;
	}

	/**
	 * {@inheritDoc}
	 *
	 * Handles double click event on the right-panel tree viewer {@link #selectedElementsViewer}
	 *
	 */
	@Override
	public void doubleClick(DoubleClickEvent event) {
		removeAction();
	}

	/**
	 * 
	 * @return
	 * 		<code>true</code> if the selected elements can be removed (moved from right to left)
	 */
	protected boolean canRemove() {
		return true;
	}

	/**
	 * {@inheritDoc}
	 *
	 * Handles selection change event on the right-panel tree viewer {@link #selectedElementsViewer}
	 */
	@Override
	public void selectionChanged(SelectionChangedEvent event) {
		updateControls();
	}

	/**
	 * @see org.eclipse.ui.services.IDisposable#dispose()
	 *
	 */
	@Override
	public void dispose() {
		selector.removeElementSelectionListener(this);
	}

	/**
	 * 
	 * @return
	 * 		the initial selection
	 */
	protected List<?> getInitialSelection() {
		return this.initialSelection;
	}
}

Back to the top