Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 29fb6106cfd99dd7fe46e8e8e27e953bb5e9d925 (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
/*****************************************************************************
 * 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:
 *   Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.customization.nattableconfiguration.pages;

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

import org.eclipse.emf.common.ui.celleditor.ExtendedComboBoxCellEditor;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EOperation;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.DialogCellEditor;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.customization.nattableconfiguration.helper.TableConfigurationHelper;
import org.eclipse.papyrus.customization.nattableconfiguration.messages.Messages;
import org.eclipse.papyrus.customization.nattableconfiguration.utils.NattableConfigurationConstants;
import org.eclipse.papyrus.customization.nattableconfiguration.utils.NattableConfigurationEditingSupport;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.nattable.comparator.IntegerFilterComparator;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.EObjectAxis;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.EOperationAxis;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.EStructuralFeatureAxis;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.FeatureIdAxis;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.IAxis;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.IdAxis;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.NattableaxisFactory;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.ObjectIdAxis;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxisconfiguration.AxisManagerRepresentation;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxisprovider.AbstractAxisProvider;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxisprovider.NattableaxisproviderFactory;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxisprovider.SlaveObjectAxisProvider;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableconfiguration.TableConfiguration;
import org.eclipse.papyrus.infra.nattable.utils.StringComparator;
import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
import org.eclipse.papyrus.infra.services.labelprovider.service.impl.LabelProviderServiceImpl;
import org.eclipse.papyrus.infra.widgets.Activator;
import org.eclipse.papyrus.infra.widgets.editors.ITreeSelectorDialog;
import org.eclipse.papyrus.infra.widgets.editors.TreeSelectorDialog;
import org.eclipse.papyrus.infra.widgets.providers.CompoundFilteredRestrictedContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.FlattenableRestrictedFilteredContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.IRestrictedContentProvider;
import org.eclipse.papyrus.infra.widgets.selectors.ReferenceSelector;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
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.Table;
import org.eclipse.swt.widgets.TableColumn;

/**
 * The slave configuration wizard page for the nattable configuration.
 */
public class SlaveConfigurationWizardPage extends AbstractTableConfigurationWizardPage {

	/**
	 * The table configuration.
	 */
	protected final TableConfiguration configuration;

	/**
	 * The label provider for the UML elements.
	 */
	protected final ILabelProvider labelProvider;

	/**
	 * The button which allow to add an axis.
	 */
	protected Button addAxis;

	/**
	 * The button which allow to remove the axis.
	 */
	protected Button removeAxis;

	/**
	 * The button which allow to move up the axis.
	 */
	protected Button upAxis;

	/**
	 * The button which allow to move down the axis.
	 */
	protected Button downAxis;

	/**
	 * Constructor.
	 *
	 * @param helper
	 *            The table configuration helper.
	 */
	public SlaveConfigurationWizardPage(final TableConfigurationHelper helper) {
		super(Messages.SlaveConfigurationWizardPage_pageName, helper);
		configuration = helper.getTableConfiguration();
		
		LabelProviderService labelProviderService = new LabelProviderServiceImpl();
		try {
			labelProviderService.startService();
		} catch (ServiceException ex) {
			Activator.log.error(ex);
		}

		labelProvider = labelProviderService.getLabelProvider();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createControl(final Composite parent) {

		// Create the parent composite
		final Composite container = new Composite(parent, SWT.BORDER);
		container.setLayout(new GridLayout(2, false));

		final SlaveObjectAxisProvider slave = getOrCreateSlaveObjectAxisProvider(configuration);

		// Create the table viewer (table, column and its layout)
		final TableViewer tableViewer = createTableViewer(container, slave);

		// Create the buttons composite
		final Composite buttonsComposite = new Composite(container, SWT.NONE);
		buttonsComposite.setLayout(new GridLayout(1, false));
		buttonsComposite.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));

		// Create the add button
		addAxis = new Button(buttonsComposite, SWT.PUSH);
		addAxis.setImage(Activator.getDefault().getImage(NattableConfigurationConstants.ADD_ICON_PATH));
		addAxis.setLayoutData(new GridData(SWT.CENTER, SWT.BEGINNING, false, false));
		// Manage the add action which allow to create an axis
		addAxis.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				int lastPosition = slave.getAxis().size();
				if (!tableViewer.getStructuredSelection().isEmpty()) {
					final Iterator<?> selectedObjects = tableViewer.getStructuredSelection().iterator();
					while (selectedObjects.hasNext()) {
						final Object selectedObject = selectedObjects.next();
						final int objectPosition = slave.getAxis().indexOf(selectedObject);
						if (objectPosition > lastPosition) {
							lastPosition = objectPosition;
						}
					}
				}

				final EStructuralFeatureAxis createdAxis = NattableaxisFactory.eINSTANCE.createEStructuralFeatureAxis();
				if (lastPosition + 1 < slave.getAxis().size()) {
					getSlaveObjectAxisProvider(configuration).getAxis().add(lastPosition + 1, createdAxis);
				} else {
					getSlaveObjectAxisProvider(configuration).getAxis().add(createdAxis);
				}
				recalculateButtonsAvailability(tableViewer.getStructuredSelection(), slave);
				tableViewer.refresh();
			}
		});

		// Create the remove button
		removeAxis = new Button(buttonsComposite, SWT.PUSH);
		removeAxis.setImage(Activator.getDefault().getImage(NattableConfigurationConstants.DELETE_ICON_PATH));
		removeAxis.setLayoutData(new GridData(SWT.CENTER, SWT.BEGINNING, false, false));
		// Manage the remove action which allow to remove an axis
		removeAxis.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				final Iterator<?> selectedObjects = tableViewer.getStructuredSelection().iterator();
				while (selectedObjects.hasNext()) {
					slave.getAxis().remove(selectedObjects.next());
				}
				recalculateButtonsAvailability(tableViewer.getStructuredSelection(), slave);
				tableViewer.refresh();
			}
		});

		// Create the move up button
		upAxis = new Button(buttonsComposite, SWT.UP);
		upAxis.setImage(Activator.getDefault().getImage(NattableConfigurationConstants.UP_ICON_PATH));
		upAxis.setLayoutData(new GridData(SWT.CENTER, SWT.BEGINNING, false, false));
		// Manage the remove action which allow to move up an axis
		upAxis.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				// Create a list of index to move.
				// To move all the selected item correctly, get all index to move, sort the integer and move it from the first to the last
				// (to avoid multiple same object moved)
				final List<Integer> indexToMove = new ArrayList<Integer>();

				final Iterator<?> selectedObjects = tableViewer.getStructuredSelection().iterator();
				while (selectedObjects.hasNext()) {
					IAxis selectedAxis = (IAxis) selectedObjects.next();
					indexToMove.add(slave.getAxis().indexOf(selectedAxis));
				}

				// Sort the index to move
				indexToMove.sort(IntegerFilterComparator.getInstance());

				// Move the objects from the first to the last
				for (final Integer index : indexToMove) {
					slave.getAxis().move(index - 1, index);
				}

				recalculateButtonsAvailability(tableViewer.getStructuredSelection(), slave);
				tableViewer.refresh();
			}
		});

		// Create the move down button
		downAxis = new Button(buttonsComposite, SWT.DOWN);
		downAxis.setImage(Activator.getDefault().getImage(NattableConfigurationConstants.DOWN_ICON_PATH));
		downAxis.setLayoutData(new GridData(SWT.CENTER, SWT.BEGINNING, false, false));
		// Manage the remove action which allow to move down an axis
		downAxis.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				// Create a list of index to move.
				// To move all the selected item correctly, get all index to move, sort the integer and move it from the last to the first
				// (to avoid multiple same object moved)
				final List<Integer> indexToMove = new ArrayList<Integer>();

				final Iterator<?> selectedObjects = tableViewer.getStructuredSelection().iterator();
				while (selectedObjects.hasNext()) {
					final IAxis selectedAxis = (IAxis) selectedObjects.next();
					indexToMove.add(slave.getAxis().indexOf(selectedAxis));
				}

				// Sort the index to move
				indexToMove.sort(IntegerFilterComparator.getInstance());

				// Move the objects from the last to the first
				for (int it = indexToMove.size() - 1; it >= 0; it--) {
					int index = indexToMove.get(it);
					slave.getAxis().move(index + 1, index);
				}

				recalculateButtonsAvailability(tableViewer.getStructuredSelection(), slave);
				tableViewer.refresh();
			}
		});

		// Manage the selection change
		tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {

			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				recalculateButtonsAvailability(event.getSelection(), slave);
			}
		});
		tableViewer.setSelection(new StructuredSelection());

		setControl(container);
	}

	/**
	 * This allows to calculate if buttons availability depending to the selection.
	 * 
	 * @param selection
	 *            The current table viewer selection.
	 * @param slave
	 *            The slave axis provider.
	 */
	protected void recalculateButtonsAvailability(final ISelection selection, final SlaveObjectAxisProvider slave) {
		if (null != selection && !selection.isEmpty()) {
			// A selection is available, enabled the remove button
			removeAxis.setEnabled(true);

			// check if the is not the first or last position, because otherwise, the up or down button must be always disabled
			int firstPosition = -1;
			int lastPosition = -1;
			if (selection instanceof StructuredSelection) {
				final Iterator<?> selectedObjects = ((StructuredSelection) selection).iterator();
				while (selectedObjects.hasNext()) {
					final Object selectedObject = selectedObjects.next();
					final int objectPosition = slave.getAxis().indexOf(selectedObject);
					if (-1 == firstPosition || objectPosition < firstPosition) {
						firstPosition = objectPosition;
					}
					if (objectPosition > lastPosition) {
						lastPosition = objectPosition;
					}
				}
			}

			upAxis.setEnabled(firstPosition != 0);
			downAxis.setEnabled(lastPosition < slave.getAxis().size() - 1);
		} else {
			// Only add button must be enable when no selection occured
			removeAxis.setEnabled(false);
			upAxis.setEnabled(false);
			downAxis.setEnabled(false);
		}
	}

	/**
	 * This allows to create the table viewer and its columns.
	 * 
	 * @param parent
	 *            The parent composite.
	 * @param slave
	 *            The slave axis provider.
	 * @return The created table viewer.
	 */
	protected TableViewer createTableViewer(final Composite parent, final SlaveObjectAxisProvider slave) {

		final TableViewer tableViewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);

		// Create the columns
		createColumns(tableViewer);

		// Manage some table display
		final Table table = tableViewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		// The content provider of the initial selection
		tableViewer.setContentProvider(new ArrayContentProvider());

		// Define the table input
		tableViewer.setInput(slave.getAxis());

		// define layout for the viewer
		tableViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		tableViewer.refresh();
		return tableViewer;
	}

	/**
	 * This allows to create the table viewer columns.
	 * 
	 * @param tableViewer
	 *            The table viewer.
	 */
	protected void createColumns(final TableViewer tableViewer) {

		final Table table = tableViewer.getTable();

		// Define the titles and bounds of each columns
		final int[] bounds = { 150, 250, 450, 100 };
		final String[] titles = { Messages.SlaveConfigurationWizardPage_typeColumnName, Messages.SlaveConfigurationWizardPage_elementColumnName, Messages.SlaveConfigurationWizardPage_managerColumnName, Messages.SlaveConfigurationWizardPage_aliasColumnName };

		// Create the first column and manage its display by the label provider of the initial selection
		final List<String> typesPossible = new ArrayList<String>();
		typesPossible.add("EObjectAxis"); //$NON-NLS-1$
		typesPossible.add("EOperationAxis"); //$NON-NLS-1$
		typesPossible.add("EStructuralFeatureAxis"); //$NON-NLS-1$
		typesPossible.add("FeatureIdAxis"); //$NON-NLS-1$
		typesPossible.add("ObjectAxis"); //$NON-NLS-1$

		// Create the first column for the type
		final TableViewerColumn firstColumn = createTableViewerColumn(tableViewer, titles[0], bounds[0]);
		final ColumnLabelProvider typeLabelProvider = new ColumnLabelProvider() {

			@Override
			public String getText(final Object element) {
				return ((IAxis) element).getClass().getSimpleName().substring(0, ((IAxis) element).getClass().getSimpleName().length() - 4);
			}
		};
		firstColumn.setLabelProvider(typeLabelProvider);
		// Create the combo box of the type
		firstColumn.setEditingSupport(new NattableConfigurationEditingSupport(tableViewer, typeLabelProvider) {

			@Override
			protected void setValue(final Object element, final Object value) {
				final IAxis axis = (IAxis) element;

				if (null == element || !((IAxis) element).getClass().getSimpleName().substring(0, ((IAxis) element).getClass().getSimpleName().length() - 4).equals(value) && !value.toString().isEmpty()) {
					// Create the axis
					IAxis recreatedAxis = null;
					if ("EObjectAxis".equals(value)) { //$NON-NLS-1$
						recreatedAxis = NattableaxisFactory.eINSTANCE.createEObjectAxis();
					} else if ("EOperationAxis".equals(value)) { //$NON-NLS-1$
						recreatedAxis = NattableaxisFactory.eINSTANCE.createEOperationAxis();
					} else if ("EStructuralFeatureAxis".equals(value)) { //$NON-NLS-1$
						recreatedAxis = NattableaxisFactory.eINSTANCE.createEStructuralFeatureAxis();
					} else if ("FeatureIdAxis".equals(value)) { //$NON-NLS-1$
						recreatedAxis = NattableaxisFactory.eINSTANCE.createFeatureIdAxis();
					} else if ("ObjectAxis".equals(value)) { //$NON-NLS-1$
						recreatedAxis = NattableaxisFactory.eINSTANCE.createObjectIdAxis();
					}

					recreatedAxis.setAlias(axis.getAlias());
					recreatedAxis.setManager(axis.getManager());

					SlaveObjectAxisProvider slave = getSlaveObjectAxisProvider(configuration);
					int indexAxisInParent = slave.getAxis().indexOf(axis);
					slave.getAxis().add(indexAxisInParent, recreatedAxis);
					slave.getAxis().remove(axis);

					tableViewer.refresh();
				}
			}

			@Override
			protected CellEditor getCellEditor(final Object element) {
				return new ExtendedComboBoxCellEditor(table, typesPossible, new LabelProvider(), SWT.READ_ONLY);
			}
		});

		// Create the second viewer column and manage its display and edition
		final TableViewerColumn secondColumn = createTableViewerColumn(tableViewer, titles[1], bounds[1]);
		final ColumnLabelProvider elementLabelProvider = new ColumnLabelProvider() {

			@Override
			public String getText(final Object element) {
				String result = null;
				if (element instanceof IAxis) {
					final IAxis axis = (IAxis) element;
					if (null != axis.getElement()) {
						result = labelProvider.getText(axis.getElement());
					}
				}
				return null != result ? result : null;
			}

			@Override
			public Image getImage(final Object element) {
				Image result = null;
				if (element instanceof IAxis) {
					final IAxis axis = (IAxis) element;
					if (null != axis.getElement()) {
						result = labelProvider.getImage(axis.getElement());
					}
				}
				return null != result ? result : null;

			}
		};
		secondColumn.setLabelProvider(elementLabelProvider);
		secondColumn.setEditingSupport(new NattableConfigurationEditingSupport(tableViewer, labelProvider) {

			@Override
			protected void setValue(final Object element, final Object value) {
				final IAxis axis = (IAxis) element;

				if (null == axis.getElement() || !axis.getElement().equals(value)) {
					if (axis instanceof EObjectAxis && value instanceof EObject) {
						((EObjectAxis) axis).setElement((EObject) value);
					} else if (axis instanceof EOperationAxis && value instanceof EOperation) {
						((EOperationAxis) axis).setElement((EOperation) value);
					} else if (axis instanceof EStructuralFeatureAxis && value instanceof EStructuralFeature) {
						((EStructuralFeatureAxis) axis).setElement((EStructuralFeature) value);
					} else if (axis instanceof FeatureIdAxis && value instanceof String) {
						((FeatureIdAxis) axis).setElement((String) value);
					} else if (axis instanceof ObjectIdAxis && value instanceof String) {
						((ObjectIdAxis) axis).setElement((String) value);
					}
					tableViewer.refresh();
				}
			}

			@Override
			protected Object getValue(final Object element) {
				String result = null;
				if (element instanceof IAxis) {
					final IAxis axis = (IAxis) element;
					if (null != axis.getElement()) {
						result = labelProvider.getText(axis.getElement());
					}
				}
				return null != result ? result : ""; //$NON-NLS-1$
			}

			@Override
			protected CellEditor getCellEditor(final Object element) {
				if (element instanceof IdAxis) {
					return new TextCellEditor(table);
				} else {
					final IAxis existingAxis = (IAxis) element;
					final Object initialValue = getValue(element);
					return new DialogCellEditor(table) {

						@Override
						protected Object openDialogBox(Control cellEditorWindow) {
							final ITreeSelectorDialog dialog = new TreeSelectorDialog(getShell()) {
								@Override
								protected void initViewerAndProvider() {
									super.initViewerAndProvider();
									// Set a comparator to sort the tree viewer
									getViewer().setComparator(new ViewerComparator(new StringComparator()));// should always be string element
								}
							};
							dialog.setTitle(org.eclipse.papyrus.customization.nattableconfiguration.messages.Messages.FeatureFillingConfigurationDialog_listenFeatureDialogName);

							final CompoundFilteredRestrictedContentProvider contentProvider = new CompoundFilteredRestrictedContentProvider() {

								@Override
								public Object[] getChildren(final Object parentElement) {
									List<Object> childrenList = Arrays.asList(super.getChildren(parentElement));
									if (parentElement instanceof EClass) {
										childrenList = new ArrayList<Object>(childrenList);
										final EClass eClass = (EClass) parentElement;
										if (isIgnoringInheritedElements()) {
											childrenList.addAll(eClass.getEOperations());
										} else {
											childrenList.addAll(eClass.getEAllOperations());
										}
										childrenList.remove(EcorePackage.eINSTANCE.getEModelElement_EAnnotations());
									}
									return childrenList.toArray();
								}
							};
							final ReferenceSelector selector = new ReferenceSelector(false) {

								@Override
								public void createControls(Composite parent) {
									super.createControls(parent);
									this.treeViewer.setComparator(new ViewerComparator(new StringComparator()));// should always be string element
								}
							};

							selector.setLabelProvider(labelProvider);
							selector.setContentProvider(contentProvider);
							final ITreeContentProvider treeContentProvider = new FlattenableRestrictedFilteredContentProvider((IRestrictedContentProvider) contentProvider, selector) {

								@Override
								public boolean isValidValue(final Object element) {
									// EMF dependency, must not be done here, it should be better with a new content provider service
									boolean result = false;

									if (null != existingAxis) {
										result = element != EcorePackage.eINSTANCE.getEModelElement_EAnnotations();

										// Depending to the axis, objects displayed are not the same
										if (existingAxis instanceof EObjectAxis && result) {
											result = element instanceof EClass;
										} else if (existingAxis instanceof EStructuralFeatureAxis && result) {
											result = element instanceof EStructuralFeature;
										} else if (existingAxis instanceof EOperationAxis && result) {
											result = element instanceof EOperation;
										}
									}

									return result;
								}
							};

							dialog.setContentProvider(treeContentProvider);
							dialog.setLabelProvider(labelProvider);
							if (null != initialValue) {
								dialog.setInitialElementSelections(Collections.singletonList(initialValue));
							}
							Object value = null;
							int result = dialog.open();
							if (Window.OK == result) {
								Object[] newValue = dialog.getResult();
								if (null != newValue) {
									if (0 == newValue.length) {
										value = null;
									} else {
										value = newValue[0];
									}
								}
								setValue(value);
							}

							return value;
						}
					};
				}
			}
		});

		// Create the third viewer column and manage its display and edition
		final TableViewerColumn thirdColumn = createTableViewerColumn(tableViewer, titles[2], bounds[2]);
		final ColumnLabelProvider managerLabelProvider = new ColumnLabelProvider() {

			@Override
			public String getText(final Object element) {
				return null != ((IAxis) element).getManager() ? ((IAxis) element).getManager().getAxisManagerId() : ""; //$NON-NLS-1$
			}
		};
		thirdColumn.setLabelProvider(managerLabelProvider);
		// Create the combo box for the manager
		thirdColumn.setEditingSupport(new NattableConfigurationEditingSupport(tableViewer, managerLabelProvider) {

			@Override
			protected void setValue(final Object element, final Object value) {
				final IAxis axis = (IAxis) element;

				if (null != value && !value.equals(axis.getManager())) {
					// Change the manager
					axis.setManager((AxisManagerRepresentation) value);
					tableViewer.refresh();
				}
			}

			@Override
			protected CellEditor getCellEditor(final Object element) {
				return new ExtendedComboBoxCellEditor(table, configuration.getColumnHeaderAxisConfiguration().getAxisManagers(), new LabelProvider() {
					@Override
					public String getText(final Object element) {
						return ((AxisManagerRepresentation) element).getAxisManagerId();
					}
				}, SWT.READ_ONLY);
			}
		});

		// Create the fourth viewer column and manage its display and edition
		final TableViewerColumn fourthColumn = createTableViewerColumn(tableViewer, titles[3], bounds[3]);
		final ColumnLabelProvider aliasLabelProvider = new ColumnLabelProvider() {

			@Override
			public String getText(final Object element) {
				return null != ((IAxis) element).getAlias() ? ((IAxis) element).getAlias() : ""; //$NON-NLS-1$
			}
		};
		fourthColumn.setLabelProvider(aliasLabelProvider);
		// Create the text editor for the manager
		fourthColumn.setEditingSupport(new NattableConfigurationEditingSupport(tableViewer, aliasLabelProvider) {

			@Override
			protected void setValue(final Object element, final Object value) {
				final IAxis axis = (IAxis) element;

				if (!value.toString().isEmpty() && !value.equals(axis.getAlias())) {
					// Change the manager
					axis.setAlias((String) value);
					tableViewer.refresh();
				}
			}

			@Override
			protected CellEditor getCellEditor(Object element) {
				return new TextCellEditor(table);
			}
		});
	}

	/**
	 * This allows to create and return a table viewer column.
	 * 
	 * @param tableViewer
	 *            The parent table viewer of the column to create.
	 * @param title
	 *            The title of the column to create.
	 * @param width
	 *            The width of the column to create.
	 * @return The created viewer column.
	 */
	private TableViewerColumn createTableViewerColumn(final TableViewer tableViewer, final String title, final int width) {
		final TableViewerColumn viewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		final TableColumn column = viewerColumn.getColumn();
		column.setText(title);
		column.setWidth(width);
		column.setResizable(true);
		column.setMoveable(true);
		return viewerColumn;
	}

	/**
	 * This allows to get the slave axis provider of the nattable configuration.
	 * 
	 * @param configuration
	 *            The nattable configuration.
	 * @return The slave axis provider is exists or <code>null</code>.
	 */
	protected SlaveObjectAxisProvider getSlaveObjectAxisProvider(final TableConfiguration configuration) {
		SlaveObjectAxisProvider slave = null;

		// Try to get an existing slave axis provider
		if (0 < configuration.getColumnAxisProviders().size()) {
			final Iterator<AbstractAxisProvider> columnAxisProvidersIterator = configuration.getColumnAxisProviders().iterator();
			while (columnAxisProvidersIterator.hasNext() && null == slave) {
				AbstractAxisProvider axisProvider = columnAxisProvidersIterator.next();
				if (axisProvider instanceof SlaveObjectAxisProvider) {
					slave = (SlaveObjectAxisProvider) axisProvider;
				}
			}
		}

		return slave;
	}

	/**
	 * Get the existing slave axis provider or create it otherwise.
	 * 
	 * @param configuration
	 *            The nattable configuration.
	 * @return The existing or created axis provider.
	 */
	protected SlaveObjectAxisProvider getOrCreateSlaveObjectAxisProvider(final TableConfiguration configuration) {
		SlaveObjectAxisProvider slave = getSlaveObjectAxisProvider(configuration);

		// The slave doesn't exist, create it
		if (null == slave) {
			slave = NattableaxisproviderFactory.eINSTANCE.createSlaveObjectAxisProvider();
			configuration.getColumnAxisProviders().add(slave);
		}

		// Set the slave as default row axis provider if this is not already done
		if (null == configuration.getDefaultColumnAxisProvider()) {
			configuration.setDefaultColumnAxisProvider(slave);
		}

		return slave;
	}
}

Back to the top