Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eb4a42b15f28561fe79b7e755b42d047607e7df6 (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
/*****************************************************************************
 * Copyright (c) 2009, 2017 CEA LIST, LIFL, 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:
 *  Cedric Dumoulin  Cedric.dumoulin@lifl.fr - Initial API and implementation
 *  Vincent Lorenzo (CEA-LIST) vincent.lorenzo@cea.fr
 *  Christian W. Damus (CEA) - bug 430880
 *  Christian W. Damus (CEA) - bug 437217
 *  Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Bug 496905
 *  Thanh Liem PHAN (ALL4TEC) thanhliem.phan@all4tec.net - Bug 459220
 *****************************************************************************/
package org.eclipse.papyrus.infra.nattable.common.editor;


import java.io.IOException;
import java.util.Collection;
import java.util.Collections;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.nebula.widgets.nattable.NatTable;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.infra.emf.nattable.selection.EObjectSelectionExtractor;
import org.eclipse.papyrus.infra.internationalization.common.editor.IInternationalizationEditor;
import org.eclipse.papyrus.infra.internationalization.utils.utils.LabelInternationalization;
import org.eclipse.papyrus.infra.internationalization.utils.utils.LabelInternationalizationUtils;
import org.eclipse.papyrus.infra.nattable.common.Activator;
import org.eclipse.papyrus.infra.nattable.common.helper.TableReconcileHelper;
import org.eclipse.papyrus.infra.nattable.common.reconciler.TableVersioningUtils;
import org.eclipse.papyrus.infra.nattable.common.utils.TableEditorInput;
import org.eclipse.papyrus.infra.nattable.manager.table.IMatrixTableWidgetManager;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.papyrus.infra.nattable.model.nattable.Table;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableconfiguration.NattableconfigurationPackage;
import org.eclipse.papyrus.infra.nattable.utils.NattableModelManagerFactory;
import org.eclipse.papyrus.infra.nattable.utils.TableEditingDomainUtils;
import org.eclipse.papyrus.infra.nattable.utils.TableHelper;
import org.eclipse.papyrus.infra.ui.editor.reload.IReloadContextProvider;
import org.eclipse.papyrus.infra.widgets.util.NavigationTarget;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;


/**
 * Abstract class for TableEditor.
 */
public abstract class AbstractEMFNattableEditor extends EditorPart implements NavigationTarget, IInternationalizationEditor {

	/** the service registry */
	protected ServicesRegistry servicesRegistry;

	/**
	 * the table manager
	 */
	protected INattableModelManager tableManager;

	/**
	 * the part name synchronizer
	 */
	private PartNameSynchronizer synchronizer;

	/**
	 * the workspace preference store for this table editor
	 */
	@SuppressWarnings("unused")
	// not yet used in the table
	private PreferenceStore workspacePreferenceStore;

	/**
	 * @param servicesRegistry
	 * @param rawModel
	 *
	 */
	public AbstractEMFNattableEditor(final ServicesRegistry servicesRegistry, final Table rawModel) {
		this.servicesRegistry = servicesRegistry;
		this.tableManager = NattableModelManagerFactory.INSTANCE.createNatTableModelManager(rawModel, new EObjectSelectionExtractor());
		this.synchronizer = new PartNameSynchronizer(rawModel);
		this.workspacePreferenceStore = getWorkspaceViewerPreferenceStore();

		// Need to manage the part label synchronizer for the table labels
		LabelInternationalizationUtils.managePartLabelSynchronizer(rawModel, this);
	}

	/**
	 * Returns the workspace viewer <code>PreferenceStore</code>
	 *
	 * @return the workspace viewer <code>PreferenceStore</code>
	 */
	public PreferenceStore getWorkspaceViewerPreferenceStore() {
		// not yet used by tables
		return null;

		// if(this.workspacePreferenceStore != null) {
		// return this.workspacePreferenceStore;
		// } else {
		// // Try to load it
		// IPath path = Activator.getDefault().getStateLocation();
		// String id = getIdStr(this.tableManager.getTable());
		//
		// String fileName = path.toString() + "/" + id;//$NON-NLS-1$
		// java.io.File file = new File(fileName);
		// this.workspacePreferenceStore = new PreferenceStore(fileName);
		// if(file.exists()) {
		// // Load it
		// try {
		// this.workspacePreferenceStore.load();
		// } catch (Exception e) {
		// // Create the default
		// addDefaultPreferences();
		// }
		// } else {
		// // Create it
		// addDefaultPreferences();
		// }
		// return this.workspacePreferenceStore;
		// }
	}

	// /**
	// * returns the unique GUID of the view
	// *
	// * @param view
	// * the view
	// * @return String the GUID of a view (constant)
	// */
	// private static String getIdStr(Table table) {
	// Resource resource = table.eResource();
	// if(resource instanceof XMLResource) {
	// String id = ((XMLResource)resource).getID(table);
	// if(id != null) {
	// return id;
	// }
	// }
	//
	// // Remain compatible with previous behavior.
	// return StringStatics.BLANK;
	// }

	// not used : add a workspace preference to the table
	// private void addDefaultPreferences() {
	// final IPreferenceStore globalPreferenceStore = Activator.getDefault().getPreferenceStore();
	// String tableKind = this.tableManager.getTable().getTableConfiguration().getType();
	// }

	/**
	 *
	 * @see org.eclipse.emf.facet.widgets.nattable.workbench.editor.NatTableEditor#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
	 *
	 * @param site
	 * @param input
	 * @throws PartInitException
	 */
	@Override
	public void init(final IEditorSite site, final IEditorInput input) throws PartInitException {
		final TableEditorInput tableEditorInput = new TableEditorInput(this.tableManager.getTable(), getEditingDomain());
		setSite(site);
		setInput(tableEditorInput);
		setPartName(LabelInternationalization.getInstance().getTableLabel(this.tableManager.getTable()));
	}

	@Override
    protected void setInput(IEditorInput input) {
		super.setInput(input);
		if (getTable() != null && !TableVersioningUtils.isOfCurrentPapyrusVersion(getTable())) {
			try {
				new TableReconcileHelper(getEditingDomain()).reconcileTable(getTable());
			} catch (CoreException e) {
				Activator.log.error(e);
			}
		}
    }
    
	/**
	 *
	 * @see org.eclipse.emf.facet.widgets.nattable.workbench.editor.NatTableEditor#getEditingDomain()
	 *
	 * @return
	 */
	public TransactionalEditingDomain getEditingDomain() {
		try {
			return ServiceUtils.getInstance().getTransactionalEditingDomain(this.servicesRegistry);
		} catch (final ServiceException e) {
			Activator.log.error(e);
		}
		return null;
	}

	@Override
	public void doSave(final IProgressMonitor monitor) {
		// nothing to do
	}

	@Override
	public void doSaveAs() {
		// nothing to do

	}

	@Override
	public boolean isDirty() {
		// nothing to do
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// nothing to do
		return false;
	}

	@Override
	public void setFocus() {
		// nothing to do
	}

	@Override
	public void createPartControl(final Composite parent) {
		this.tableManager.createNattable(parent, SWT.NONE, getSite());
	}

	@Override
	public Object getAdapter(@SuppressWarnings("rawtypes") final Class adapter) {
		if (adapter == INattableModelManager.class) {
			return this.tableManager;
		}
		if(adapter == IMatrixTableWidgetManager.class) {
			if(this.tableManager instanceof IMatrixTableWidgetManager) {
				return this.tableManager;
			}
			return null;
		}

		// Give direct access to the Table model element
		// Most actions should be done through the TableManager.
		// Be careful when using directly the Table element.
		if (adapter == Table.class) {
			if (tableManager != null) {
				return this.tableManager.getTable();
			}
		}

		if (adapter == IReloadContextProvider.class) {
			return new NattableReloadContextProvider(this);
		}

		return super.getAdapter(adapter);
	}

	/**
	 * this method is used dispose the existing nattable widget and recreate a new one.
	 * It has been created to be able to reload a table when a bug broke the table after a user action.
	 *
	 * see bug 466447: [TreeTable] Missing method to reload a (hierarchic) table
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=466447
	 */
	public void reloadNattableModelManager() {
		if (null != this.tableManager) {
			if (TableHelper.isTreeTable(tableManager)) {
				reloadTreeNattableModelManager();
			} else {
				Table rawModel = this.tableManager.getTable();
				// we dispose the previous nattable widget
				NatTable nattable = this.tableManager.getAdapter(NatTable.class);
				Composite parent = nattable.getParent();
				this.tableManager.dispose();
				this.tableManager = NattableModelManagerFactory.INSTANCE.createNatTableModelManager(rawModel, new EObjectSelectionExtractor());
				nattable = this.tableManager.createNattable(parent, SWT.NONE, getSite());
				nattable.getParent().layout();
			}
		}
	}

	/**
	 * the reload method for Treetable (due to bug Bug 488234)
	 */
	protected void reloadTreeNattableModelManager() {
		Table rawModel = this.tableManager.getTable();
		// we dispose the previous nattable widget
		NatTable nattable = this.tableManager.getAdapter(NatTable.class);
		Composite sliderComposite = nattable.getParent();
		Composite greatParent = sliderComposite.getParent();
		this.tableManager.dispose();
		sliderComposite.dispose();

		this.tableManager = NattableModelManagerFactory.INSTANCE.createNatTableModelManager(rawModel, new EObjectSelectionExtractor());
		nattable = this.tableManager.createNattable(greatParent, SWT.NONE, getSite());
		greatParent.layout();
	}

	@Override
	public void dispose() {
		saveLocalPreferenceStoreValues();
		this.tableManager.dispose();
		this.synchronizer.dispose();
		this.tableManager = null;
		this.servicesRegistry = null;
		this.synchronizer = null;
		super.dispose();
	}

	protected void saveLocalPreferenceStoreValues() {
		// Write the settings, if necessary
		try {
			if (getWorkspaceViewerPreferenceStore() != null && getWorkspaceViewerPreferenceStore().needsSaving()) {
				getWorkspaceViewerPreferenceStore().save();
			}
		} catch (IOException ioe) {
			Activator.log.warn("Preferences can' be saved"); //$NON-NLS-1$
		}
	}

	/**
	 * A class taking in charge the synchronization of the partName and the table name.
	 * When table name change, the other is automatically updated.
	 *
	 *
	 */
	public class PartNameSynchronizer {

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

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

			/**
			 *
			 * @see org.eclipse.emf.common.notify.Adapter#notifyChanged(org.eclipse.emf.common.notify.Notification)
			 *
			 * @param notification
			 */
			@Override
			public void notifyChanged(final Notification notification) {
				if (notification.getFeature() == NattableconfigurationPackage.eINSTANCE.getTableNamedElement_Name()) {
					setPartName(LabelInternationalization.getInstance().getTableLabel(PartNameSynchronizer.this.papyrusTable));
				}
			}
		};

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

		public void dispose() {
			this.papyrusTable.eAdapters().remove(this.tableNameListener);
			this.papyrusTable = null;
		}

		/**
		 * Change the associated diagram.
		 *
		 * @param papyrusTable
		 */
		public void setTable(final Table 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(LabelInternationalization.getInstance().getTableLabel(papyrusTable));
			// Listen to name change
			papyrusTable.eAdapters().add(this.tableNameListener);
		}
	}

	/**
	 * @see org.eclipse.ui.part.EditorPart#setPartName(java.lang.String)
	 *
	 * @param partName
	 */
	@Override
	public void setPartName(String partName) {
		super.setPartName(partName);
	}

	/**
	 *
	 * used to link the selection between the model explorer and the table
	 *
	 * @param element
	 */
	@Override
	public boolean revealElement(Object element) {
		return revealElement(Collections.singleton(element));
	}

	/**
	 *
	 * used to link the selection between the model explorer and the table
	 *
	 * @param elements
	 */
	@Override
	public boolean revealElement(Collection<?> elements) {
		if (tableManager instanceof NavigationTarget) {
			return ((NavigationTarget) tableManager).revealElement(elements);
		}
		return false;
	}

	/**
	 *
	 * @return
	 * 		the table model displayed by the editor
	 */
	public Table getTable() {
		return tableManager.getTable();
	}

	/**
	 *
	 * @return
	 * 		the table editing domain
	 */
	public TransactionalEditingDomain getTableEditingDomain() {
		return TableEditingDomainUtils.getTableEditingDomain(getTable());
	}

	/**
	 *
	 * @return
	 * 		the table context editing domain
	 */
	public TransactionalEditingDomain getTableContextEditingDomain() {
		return TableEditingDomainUtils.getTableContextEditingDomain(getTable());
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.papyrus.infra.internationalization.common.editor.IInternationalizationEditor#modifyPartName(java.lang.String)
	 */
	@Override
	public void modifyPartName(final String name) {
		setPartName(name);
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.papyrus.infra.internationalization.common.editor.IInternationalizationEditor#refreshEditorPart()
	 */
	@Override
	public void refreshEditorPart() {
		// We don't need to refresh the editor part, the table is refreshed alone
	}
}

Back to the top