Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 24c5fcf3728bde5fbd7fb1ae7b9a4cc5f0f2d780 (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
/**
 * Copyright (c) 2012 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:
 *   Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *   Duplicated code from EMF-Facet
 */

package org.eclipse.papyrus.infra.table.efacet.common.editor;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.command.BasicCommandStack;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.emf.facet.widgets.table.metamodel.v0_2_0.table.Table;
import org.eclipse.emf.facet.widgets.table.ui.internal.exported.ITableWidget;
import org.eclipse.emf.facet.widgets.table.ui.internal.exported.ITableWidgetFactory;
import org.eclipse.emf.facet.widgets.table.ui.internal.exported.ITableWidgetInternal;
import org.eclipse.emf.facet.widgets.table.ui.internal.exported.ITableWidgetProvider;
import org.eclipse.emf.facet.widgets.table.ui.internal.exported.ITableWidgetView;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.table.efacet.common.Activator;
import org.eclipse.papyrus.infra.table.efacet.common.input.PapyrusTableEditorInput;
import org.eclipse.papyrus.infra.table.efacet.metamodel.papyrustable.PapyrusTable;
import org.eclipse.papyrus.infra.table.efacet.metamodel.papyrustable.PapyrustablePackage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;


/**
 * Adapted code from EMF-Facet
 * 
 * 
 */
public class AbstractTableEditor extends AbstractPapyrusNestedEditor implements ISelectionProvider {

	/** listener on the selection changes */
	private final ISelectionChangedListener localSelectionListener = new SelectionChangeListener();

	/** the list of the registered selection listeners */
	private final List<ISelectionChangedListener> registeredSelectionListener = new ArrayList<ISelectionChangedListener>();

	/** the id for the menu of the editor */
	private final static String ID = "org.eclipse.papyrus.infra.table.efacet.common.editor";

	protected ITableWidget natTableWidget;

	/** the table instance */
	protected PapyrusTable rawModel;



	public AbstractTableEditor(final ServicesRegistry servicesRegistry, final PapyrusTable rawModel) {
		super(servicesRegistry);
		this.rawModel = rawModel;
		new PartNameSynchronizer(rawModel);
	}

	@SuppressWarnings("rawtypes")
	// We cannot change the method signature because of the override
	@Override
	public Object getAdapter(final Class adapter) {
		if(adapter == ITableWidgetProvider.class) {
			return new ITableWidgetProvider() {

				public ITableWidget getTableWidget() {
					return AbstractTableEditor.this.natTableWidget;
				}
			};
		}
		if(adapter == PapyrusTable.class) {
			return this.natTableWidget.getTable().eContainer();
		} else if(adapter == Table.class) {
			return this.natTableWidget.getTable();
		}
		return null;
	}

	@Override
	public void init(final IEditorSite site, final IEditorInput input) throws PartInitException {
		if(input instanceof PapyrusTableEditorInput) {
			setSite(site);
			setInput(input);
			setPartName(this.rawModel.getName());
			configureEditorEditingDomain();
		} else {
			throw new PartInitException("Input should be of type TableEditorInput"); //$NON-NLS-1$
		}
	}



	/**
	 * 
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 * 
	 * @param parent
	 */
	@Override
	public void createPartControl(final Composite parent) {

		final Composite editorComposite = createCompositeCompositeWthTableBorder(parent);

		// the composite owning the table
		final Composite tableComposite = new Composite(editorComposite, SWT.NONE);
		final GridLayout tableCompositeGridLayout = new GridLayout(1, true);
		tableComposite.setLayout(tableCompositeGridLayout);

		final GridData compositeTableGridLayout = new GridData();
		compositeTableGridLayout.grabExcessHorizontalSpace = true;
		compositeTableGridLayout.grabExcessVerticalSpace = true;
		compositeTableGridLayout.horizontalAlignment = SWT.FILL;
		compositeTableGridLayout.verticalAlignment = SWT.FILL;
		tableComposite.setLayoutData(compositeTableGridLayout);
		final FilteredMenuManager menuMgr = new FilteredMenuManager("#PopUp", AbstractTableEditor.ID);
		this.natTableWidget = createNattableWidget(tableComposite, this, this.rawModel.getTable(), menuMgr);
		this.natTableWidget.addSelectionChangedListener(new SelectionChangeListener());
		getSite().setSelectionProvider(this);
		getSite().registerContextMenu(menuMgr, this.natTableWidget);
	}

	/**
	 * 
	 * @param tableComposite
	 *        the composite
	 * @param domainProvider
	 *        the editing domain provider
	 * @param table
	 *        the table
	 * @param menuMgr2
	 *        the menu manager for the widget
	 * @return
	 */
	private ITableWidget createNattableWidget(final Composite tableComposite, final IEditingDomainProvider domainProvider, final Table table, final MenuManager menuMgr) {
		final ITableWidget widget = ITableWidgetFactory.INSTANCE.createTableWidget(tableComposite, domainProvider, table, menuMgr);
		final GridData tableGridData = new GridData();
		tableGridData.grabExcessHorizontalSpace = true;
		tableGridData.grabExcessVerticalSpace = true;
		tableGridData.horizontalAlignment = SWT.FILL;
		tableGridData.verticalAlignment = SWT.FILL;
		widget.getComposite().setLayoutData(tableGridData);
		return widget;
	}

	private Composite createCompositeCompositeWthTableBorder(final Composite parent) {
		final Composite editorComposite = new Composite(parent, SWT.BORDER);
		final GridLayout editorGridLayout = new GridLayout(1, true);
		editorGridLayout.marginHeight = 0;
		editorGridLayout.marginWidth = 0;

		final GridData data = new GridData();
		data.grabExcessHorizontalSpace = true;
		data.horizontalAlignment = SWT.FILL;
		editorComposite.setLayoutData(data);
		editorComposite.setLayout(editorGridLayout);
		return editorComposite;
	}

	@Override
	public void setFocus() {
		this.natTableWidget.getComposite().setFocus();
	}

	@Override
	public boolean isDirty() {
		return ((BasicCommandStack)getEditingDomain().getCommandStack()).isSaveNeeded();
	}

	@Override
	public void doSave(final IProgressMonitor monitor) {
		//nothing to do here, the save is done by the CoreMultiDiagramEditor
	}

	@Override
	public void doSaveAs() {
		//nothing to do here, the save is done by the CoreMultiDiagramEditor
	}

	public void setSelection(final ISelection selection) {
		this.natTableWidget.setSelection(selection);
	}



	@Override
	public void dispose() {
		super.dispose();
		this.natTableWidget.removeSelectionChangedListener(this.localSelectionListener);
		this.registeredSelectionListener.clear();
	}


	/**
	 * Warning, we should avoid to use this method.
	 * It will be better to ask to emffacet to open their API
	 * 
	 * @return
	 *         the grid selection
	 */
	//currently never used
	public List<Object> getGridSelection() {
		final String errorMessage = "We are using java reflect API to get the Grid Selection of the table";
		Activator.log.warn(errorMessage);
		final Class<?> cl = this.natTableWidget.getClass();
		ITableWidgetView view = null;
		ITableWidgetInternal nattableWidget = null;
		try {
			Field field = cl.getDeclaredField("nattableWidget");
			field.setAccessible(true);
			nattableWidget = (ITableWidgetInternal)field.get(this.natTableWidget);
			field = nattableWidget.getClass().getDeclaredField("view");
			field.setAccessible(true);
			view = (ITableWidgetView)field.get(nattableWidget);
		} catch (final SecurityException e) {
			Activator.log.error(errorMessage, e);
		} catch (final NoSuchFieldException e) {
			Activator.log.error(errorMessage, e);
		} catch (final IllegalArgumentException e) {
			Activator.log.error(errorMessage, e);
		} catch (final IllegalAccessException e) {
			Activator.log.error(errorMessage, e);
		}
		return view.getGridSelection();
	}

	public ISelection getSelection() {

		//		StructuredSelection selection = null;
		//		final List<EObject> selectedEObject = ((org.eclipse.emf.facet.widgets.table.ui.ITableWidget)this.natTableWidget).getSelectedRowEObjects();
		//		final ITableWidgetView view = getTableWidgetView();
		//
		//		final List<Object> gridSelection = view.getGridSelection();
		//		final List<Column> selectedColumn = new ArrayList<Column>();
		//		final List<Row> selectedRow = new ArrayList<Row>();
		//		final List<IGridElement> grid = new ArrayList<IGridElement>();
		//		if(gridSelection.isEmpty()) {
		//			return new StructuredSelection(this.tableEditorInput.getPapyrusTable());
		//		}
		//
		//		for(final Object current : gridSelection) {
		//			if(current instanceof Column) {
		//				selectedColumn.add((Column)current);
		//			} else if(current instanceof IGridElement) {
		//				grid.add((IGridElement)current);
		//			} else if(current instanceof Row) {
		//				selectedRow.add((Row)current);
		//			}
		//			//			org.eclipse.emf.facet.widgets.table.ui.nattable.internal.nattable.dataprovider.GridElement
		//			int i = 0;
		//			i++;
		//		}
		//
		//		//we ignore columns!
		//		if(!selectedRow.isEmpty()) {
		//			selection = new StructuredSelection(((org.eclipse.emf.facet.widgets.table.ui.ITableWidget)this.natTableWidget).getSelectedRowEObjects());
		//		} else if(!gridSelection.isEmpty()) {
		//			final List<Object> eobject = new ArrayList<Object>();
		//			final IFacetManager facetManager = IFacetManagerFactory.DEFAULT.getOrCreateFacetManager(this.tableEditorInput.getPapyrusTable().eResource().getResourceSet());
		//			for(final IGridElement gridElement : grid) {
		//
		//				try {
		//					eobject.add(TableWidgetUtils.getValueOf(gridElement, facetManager));
		//				} catch (final FacetManagerException e) {
		//					// TODO Auto-generated catch block
		//					e.printStackTrace();
		//				}
		//				int i = 0;
		//				i++;
		//			}
		//			return new StructuredSelection(eobject);
		//		}
		//
		//
		//
		//		if(!selectedEObject.isEmpty()) {
		//			//return 
		//			selection = new StructuredSelection(selectedEObject);
		//		} else {
		//			//		final ISelection tableSelection = this.natTableWidget.getSelection();
		//			//		if(tableSelection.isEmpty()) {
		//			selection = new StructuredSelection(this.tableEditorInput.getPapyrusTable());
		//			//		}
		//		}
		StructuredSelection selection = null;
		selection = (StructuredSelection)this.natTableWidget.getSelection();
		if(selection.isEmpty()) {
			selection = new StructuredSelection(this.rawModel);
		}
		return selection;
	}

	public void addSelectionChangedListener(final ISelectionChangedListener listener) {
		this.registeredSelectionListener.add(listener);
	}

	public void removeSelectionChangedListener(final ISelectionChangedListener listener) {
		this.registeredSelectionListener.remove(listener);
	}

	/**
	 * 
	 * This listener dispatches the selection change events to the registered listeners
	 * 
	 */
	private class SelectionChangeListener implements ISelectionChangedListener {

		/**
		 * 
		 * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
		 *      s
		 * @param event
		 */
		public void selectionChanged(final SelectionChangedEvent event) {
			ISelection selection = event.getSelection();
			if(selection.isEmpty()) {
				selection = new StructuredSelection(AbstractTableEditor.this.rawModel);
			}
			for(final ISelectionChangedListener current : AbstractTableEditor.this.registeredSelectionListener) {
				current.selectionChanged(new SelectionChangedEvent(AbstractTableEditor.this, selection));
			}
		}
	}

	/**
	 * 
	 * This menu manager allows to filter the contribution to the menu of the table
	 * FIXME : try to do that with capabilities and activities
	 * 
	 */
	private class FilteredMenuManager extends MenuManager {


		/** the qualified name of forbidden contribution */
		private List<String> forbiddenContributions = null;

		/** the pattern of authorized contribution */
		private List<String> authorizedPattern = new ArrayList<String>();

		public FilteredMenuManager(final String text, final String id) {
			super(text, id);
			this.forbiddenContributions = new ArrayList<String>();
			this.forbiddenContributions.add("org.eclipse.emf.facet.widgets.nattable.examples.ecore.internal.commands.openEclorTabularEditor");
			this.forbiddenContributions.add("org.eclipse.emf.facet.widgets.nattable.workbench.action4");
			this.forbiddenContributions.add("org.eclipse.emf.facet.widgets.nattable.workbench.action3");
			this.forbiddenContributions.add("org.eclipse.emf.facet.widgets.table.ui.workbench.command1");
			this.forbiddenContributions.add("org.eclipse.emf.facet.widgets.table.ui.workbench.command2");

			this.authorizedPattern = new ArrayList<String>();
			this.authorizedPattern.add("papyrus");
			this.authorizedPattern.add("table");
			this.authorizedPattern.add("org.eclipse.ui.edit.delete");
			this.authorizedPattern.add("org.eclipse.ui.edit.copy");
			this.authorizedPattern.add("org.eclipse.ui.edit.selectAll");
		}



		/**
		 * We filter the contribution to the table menu
		 * 
		 * @see org.eclipse.jface.action.MenuManager#doItemFill(org.eclipse.jface.action.IContributionItem, int)
		 * 
		 * @param ci
		 * @param index
		 */
		@Override
		protected void doItemFill(final IContributionItem ci, final int index) {
			if(isAllowedInTableMenu(ci)) {
				super.doItemFill(ci, index);
			}
		}

		private boolean isAllowedInTableMenu(final IContributionItem ci) {
			final String contributionId = ci.getId();
			boolean autorized = true;
			for(final String current : this.forbiddenContributions) {
				if(contributionId.contains(current)) {
					autorized = false;
				}
			}
			if(autorized) {
				for(final String current : this.authorizedPattern) {
					if(contributionId.contains(current)) {
						autorized = true;
					}
				}
			}
			return autorized;
		}

	}

	/**
	 * A class taking in charge the synchronization of the partName and the table name.
	 * When table name change, the other is automatically updated.
	 * 
	 * @author vincent lorenzo
	 *         adapted class from UmlGmfDiagramEditor
	 */
	public class PartNameSynchronizer {

		/** the papyrus table */
		private PapyrusTable papyrusTable;

		/**
		 * Listener on diagram name change.
		 */
		private final Adapter tableNameListener = new Adapter() {

			/**
			 * 
			 * @see org.eclipse.emf.common.notify.Adapter#notifyChanged(org.eclipse.emf.common.notify.Notification)
			 * 
			 * @param notification
			 */
			public void notifyChanged(final Notification notification) {
				if(notification.getFeatureID(PapyrusTable.class) == PapyrustablePackage.PAPYRUS_TABLE__NAME && notification.getNotifier() == PartNameSynchronizer.this.papyrusTable) {
					setPartName(PartNameSynchronizer.this.papyrusTable.getName());
				}
			}

			/**
			 * 
			 * @see org.eclipse.emf.common.notify.Adapter#getTarget()
			 * 
			 * @return
			 */
			public Notifier getTarget() {
				return null;
			}

			/**
			 * 
			 * @see org.eclipse.emf.common.notify.Adapter#setTarget(org.eclipse.emf.common.notify.Notifier)
			 * 
			 * @param newTarget
			 */
			public void setTarget(final Notifier newTarget) {
			}

			/**
			 * 
			 * @see org.eclipse.emf.common.notify.Adapter#isAdapterForType(java.lang.Object)
			 * 
			 * @param type
			 * @return
			 */
			public boolean isAdapterForType(final Object type) {
				return false;
			}

		};

		/**
		 * 
		 * Constructor.
		 * 
		 * @param diagram
		 */
		public PartNameSynchronizer(final PapyrusTable papyrusTable) {
			setTable(papyrusTable);
		}

		/**
		 * Change the associated diagram.
		 * 
		 * @param papyrusTable
		 */
		public void setTable(final PapyrusTable papyrusTable) {
			// Remove from old table, if any
			if(this.papyrusTable != null) {
				papyrusTable.eAdapters().remove(this.tableNameListener);
			}
			// Set new table
			this.papyrusTable = papyrusTable;
			// Set editor name
			setPartName(papyrusTable.getName());
			// Listen to name change
			papyrusTable.eAdapters().add(this.tableNameListener);
		}
	}
}

Back to the top