Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 748c329cd3ad462e30ad83089b8265c39ab94a2a (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
/*****************************************************************************
 * Copyright (c) 2013 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:
 *  Patrick Tessier (CEA LIST) - Initial API and implementation
 /*****************************************************************************/
package org.eclipse.papyrus.uml.diagram.component.custom.ui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.papyrus.infra.widgets.Activator;
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.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
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.Label;
import org.eclipse.swt.widgets.Table;
/**
 * Extension of selection editor with a table to display icon of element
 *normally this class must inherits of SelectionEditor but list and table are type that are not compatible, so entire creation of this class was needed
 */

public class ExtendedSelectionEditor extends Composite implements SelectionListener {


		/** the initial selection */
		protected final java.util.List<Object> initialSelection;

		/** The object selector */
		private IElementSelector selector;

		/** The SWT Composite in which the selector is drawn */
		protected Composite selectorSection;

		/** The message section */
		protected Composite messageSection;

		/** The additional button section */
		protected Composite createAdditionalButtonSection;

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

		/** The listViewer for chosen elements */
		protected TableViewer selectedElementsViewer;

		/**
		 * 
		 * @return the table viewer in order to get the element selection
		 */
		public TableViewer getSelectedElementsViewer() {
			return selectedElementsViewer;
		}

		/** The list for chosen elements */
		protected Table selectedElements;

		/** The add action button */
		private Button add;

		/** The remove action button */
		private Button remove;

		/** The add all action button */
		private Button addAll;

		/** The remove all action button */
		private Button removeAll;

		/** the up action button */
		private Button up;

		/** the down action button */
		private Button down;

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

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

		/** the listener for the additional button */
		private SelectionListener additionalButtonSelectionListener;

		/** the message for the editor */
		private String message;


		/*
		 * This dialog can provide a Button to do some optional action
		 */

		/** Indicates if the dialog provides a button */
		private boolean withAdditionalButton = false;

		/** the label of the button */
		private String additionalButtonLabel = null;

		/** the button */
		private Button additionalButton;

		/** The add/remove/addAll buttons section */
		protected Composite buttonSection;

		/**
		 * 
		 * Constructor.
		 * 
		 * @param parent
		 *        the parent of this composite
		 * @param selector
		 *        the selector
		 * @param labelProvider
		 *        the label provider
		 * @param initialSelection
		 *        the initial selection
		 */
		public ExtendedSelectionEditor(Composite parent, IElementSelector selector, ILabelProvider labelProvider, java.util.List<Object> initialSelection) {
			super(parent, SWT.NONE);
			this.selector = selector;
			this.labelProvider = labelProvider;
			this.initialSelection = new ArrayList<Object>();
			for(Object current : initialSelection) {
				this.initialSelection.add(current);
			}
			currentSelection = initialSelection;
		}

		/**
		 * This methods create this editor
		 */
		public void create() {
			Composite parent = this;
			GridLayout layout = new GridLayout(1, true);
			GridData data = new GridData();
			data.grabExcessHorizontalSpace = true;
			data.grabExcessVerticalSpace = true;
			parent.setLayout(layout);
			createMessageSection(parent);
			createBody(parent);
			createCreateButtonSection(parent);
		}

		/**
		 * This method create the body of the editor :
		 * <ul>
		 * <li>the selector</li>
		 * <li>the list</li>
		 * <li>the buttons Add, AddAll, Remove, RemoveAll, Up, Down</li>
		 * </ul>
		 * 
		 * @param parent
		 *        the parent composite
		 */
		protected void createBody(Composite parent) {

			Composite par = new Composite(parent, SWT.NONE);
			createSelectorSection(par);
			createControlsSection(par);
			createListSection(par);
			createRightButtonsSection(par);
			GridLayout layout = new GridLayout();
			layout.makeColumnsEqualWidth = false;
			layout.numColumns = 4;
			par.setLayout(layout);
			GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
			par.setLayoutData(data);

		}

		/**
		 * Creates the Message section
		 * 
		 * @param parent
		 *        the parent of the {@link Composite}
		 */
		protected void createMessageSection(Composite parent) {
			messageSection = new Composite(parent, SWT.NONE);
			Label label = new Label(messageSection, SWT.NONE);
			label.setText(message);
			messageSection.setLayout(new FillLayout(SWT.HORIZONTAL));
		}

		/**
		 * Creates the button section
		 * 
		 * @param parent
		 *        the parent of the {@link Composite}
		 */
		protected void createCreateButtonSection(Composite parent) {
			if(this.withAdditionalButton) {
				buttonSection = new Composite(parent, SWT.NONE);
				additionalButton = new Button(buttonSection, SWT.PUSH);
				if(additionalButtonLabel != null) {
					additionalButton.setText(additionalButtonLabel);
				}
				buttonSection.setLayout(new FillLayout());
				this.additionalButton.addSelectionListener(additionalButtonSelectionListener);
			}
		}

		/**
		 * Creates the selector section
		 * 
		 * @param parent
		 *        The composite in which the section is created
		 */
		protected void createSelectorSection(Composite parent) {
			selectorSection = new Composite(parent, SWT.NONE);
			selectorSection.setLayout(new FillLayout());
			GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
			data.widthHint = 400;

			//		data.exclude = true;
			selectorSection.setLayoutData(data);
			selector.createControls(selectorSection);

		}

		/**
		 * Creates the main controls section (Add, remove, Add all, remove all)
		 * 
		 * @param parent
		 *        The composite in which the section is created
		 */
		protected void createControlsSection(Composite parent) {
			buttonSection = new Composite(parent, SWT.NONE);
			buttonSection.setLayout(new GridLayout(1, true));

			add = new Button(buttonSection, SWT.PUSH);
			add.setImage(Activator.getDefault().getImage("/icons/arrow_right.gif")); //$NON-NLS-1$
			add.addSelectionListener(this);
			add.setToolTipText(Messages.MultipleValueSelectorDialog_AddSelectedElements);

			remove = new Button(buttonSection, SWT.PUSH);
			remove.setImage(Activator.getDefault().getImage("/icons/arrow_left.gif")); //$NON-NLS-1$
			remove.addSelectionListener(this);
			remove.setToolTipText(Messages.MultipleValueEditor_RemoveSelectedElements);

			addAll = new Button(buttonSection, SWT.PUSH);
			addAll.setImage(Activator.getDefault().getImage("/icons/arrow_double.gif")); //$NON-NLS-1$
			addAll.addSelectionListener(this);
			addAll.setToolTipText(Messages.MultipleValueSelectorDialog_AddAllElements);

			removeAll = new Button(buttonSection, SWT.PUSH);
			removeAll.setImage(Activator.getDefault().getImage("/icons/arrow_left_double.gif")); //$NON-NLS-1$
			removeAll.addSelectionListener(this);
			removeAll.setToolTipText(Messages.MultipleValueSelectorDialog_RemoveAllElements);
		}


		/**
		 * Creates the list displaying the currently selected elements
		 * 
		 * @param parent
		 *        The composite in which the section is created
		 */
		protected void createListSection(Composite parent) {
			Composite listSection = new Composite(parent, SWT.NONE);
			selectedElements = new Table(listSection, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
			GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
			data.widthHint = 400;
			listSection.setLayout(new FillLayout());
			listSection.setLayoutData(data);

			selectedElementsViewer = new TableViewer(selectedElements);

			selectedElementsViewer.setContentProvider(CollectionContentProvider.instance);

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

			selectedElementsViewer.setInput(currentSelection);
			selector.setSelectedElements(currentSelection.toArray());

		}

		/**
		 * 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);
		}



		/**
		 * {@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
		 */
		public void widgetSelected(SelectionEvent e) {
			if(e.widget == add) {
				addAction();
			} else if(e.widget == remove) {
				removeAction();
			} else if(e.widget == addAll) {
				addAllAction();
			} else if(e.widget == removeAll) {
				removeAllAction();
			} else if(e.widget == up) {
				upAction();
			} else if(e.widget == down) {
				downAction();
			}
		}

		/**
		 * Handles the "Add" action
		 */
		protected void addAction() {
			Object[] elements = selector.getSelectedElements();
			addElements(elements);
		}

		/**
		 * 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>(currentSelection);
			for(Object o : selection.toArray()) {
				int oldIndex = list.indexOf(o);
				if(oldIndex > 0)
					move(list, oldIndex, oldIndex - 1);
			}

			currentSelection.clear();
			currentSelection.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>(currentSelection);

			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);
			}

			currentSelection.clear();
			currentSelection.addAll(list);

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

		/**
		 * Moves an element from oldIndex to newIndex
		 * 
		 * @param list
		 *        The list in which to move the object
		 * @param oldIndex
		 * @param newIndex
		 */
		protected 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() {
			IStructuredSelection selection = (IStructuredSelection)selectedElementsViewer.getSelection();
			if(selection.isEmpty())
				return;

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

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

		/**
		 * Handles the "Remove all" action
		 */
		protected void removeAllAction() {
			currentSelection.clear();
			selector.setSelectedElements(new Object[0]);
			selectedElementsViewer.setSelection(null);
			selectedElementsViewer.refresh();
		}

		/**
		 * Handles the "Add All" action
		 */
		protected void addAllAction() {
			Object[] elements = selector.getAllElements();
			addElements(elements);
		}

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


		/**
		 * 
		 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
		 * 
		 * @param e
		 */
		public void widgetDefaultSelected(SelectionEvent e) {
			//Nothing to do
		}

		/**
		 * 
		 * @param withAdditionalButton
		 * @param messageButton
		 * @param listener
		 */
		public void setAdditionalButton(boolean withAdditionalButton, String messageButton, SelectionListener listener) {
			this.withAdditionalButton = withAdditionalButton;
			this.additionalButtonLabel = messageButton;
			this.additionalButtonSelectionListener = listener;
		}

		/**
		 * Set the message displayed by the Editor
		 * 
		 * @param string
		 *        the message displayed by the editor
		 */
		public void setMessage(String string) {
			this.message = string;
		}

		/**
		 * Returns all selected elements
		 * 
		 * @return
		 *         all selected elements
		 */
		public Collection<?> getSelectedElements() {
			return currentSelection;
		}


		/**
		 * Returns the list of the elements to remove
		 * 
		 * @return
		 *         the list of the elements to remove
		 */
		public java.util.List<Object> getElementToRemove() {
			java.util.List<Object> removedObject = new ArrayList<Object>();
			for(Object current : this.initialSelection) {
				if(!getSelectedElements().contains(current)) {
					removedObject.add(current);
				}
			}
			return removedObject;
		}

		/**
		 * Returns the list of the elements to add
		 * 
		 * @return
		 *         the list of the elements to add
		 */
		public java.util.List<Object> getElementToAdd() {
			java.util.List<Object> addedObject = new ArrayList<Object>();
			for(Object current : getSelectedElements()) {
				if(!this.initialSelection.contains(current)) {
					addedObject.add(current);
				}
			}
			return addedObject;
		}
	}

Back to the top