Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a785c0bb4d65e78b53c2374846944ebd68cf8c32 (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
/*****************************************************************************
 * Copyright (c) 2009 CEA LIST & LIFL 
 *
 *    
 * 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
 *****************************************************************************/
package org.eclipse.papyrus.infra.table.common.editor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.commands.State;
import org.eclipse.core.runtime.Assert;
import org.eclipse.emf.common.command.Command;
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.ecore.EObject;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.facet.infra.query.ModelQuery;
import org.eclipse.emf.facet.infra.query.core.AbstractModelQuery;
import org.eclipse.emf.facet.infra.query.core.ModelQuerySetCatalog;
import org.eclipse.emf.facet.infra.query.core.exception.ModelQueryException;
import org.eclipse.emf.facet.infra.query.runtime.ModelQueryResult;
import org.eclipse.emf.facet.widgets.nattable.INatTableWidgetProvider;
import org.eclipse.emf.facet.widgets.nattable.instance.tableinstance.Column;
import org.eclipse.emf.facet.widgets.nattable.instance.tableinstance.DefaultLabelColumn;
import org.eclipse.emf.facet.widgets.nattable.instance.tableinstance.EContainerColumn;
import org.eclipse.emf.facet.widgets.nattable.instance.tableinstance.MetaClassColumn;
import org.eclipse.emf.facet.widgets.nattable.instance.tableinstance.TableInstance;
import org.eclipse.emf.facet.widgets.nattable.internal.NatTableWidgetInternalUtils;
import org.eclipse.emf.transaction.ResourceSetChangeEvent;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.TriggerListener;
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.table.common.Activator;
import org.eclipse.papyrus.infra.table.common.internal.IPapyrusNatTableWidget;
import org.eclipse.papyrus.infra.table.common.internal.TableEditorInput;
import org.eclipse.papyrus.infra.table.common.listener.ModelTriggerListener;
import org.eclipse.papyrus.infra.table.common.listener.TableTriggerListener;
import org.eclipse.papyrus.infra.table.common.util.FillingQueriesUtil;
import org.eclipse.papyrus.infra.table.instance.papyrustableinstance.PapyrusTableInstance;
import org.eclipse.papyrus.infra.table.instance.papyrustableinstance.PapyrustableinstancePackage;
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.commands.ICommandService;

/**
 * Abstract class for TableEditor
 * 
 * 
 * 
 */
public abstract class AbstractNattableEditor extends org.eclipse.papyrus.infra.table.common.internal.NatTableEditor {

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

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

	/** listener on the model for synchronized table */
	protected TriggerListener modelTriggerListener;

	/** listener on the table for synchronized table : listen the properties "isSynchronized" and "fillingQueries" */
	protected TriggerListener tableTriggerListener;

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

	/**
	 * @param servicesRegistry
	 * @param rawModel
	 * 
	 */
	public AbstractNattableEditor(final ServicesRegistry servicesRegistry, final PapyrusTableInstance rawModel) {
		this.servicesRegistry = servicesRegistry;
		this.rawModel = rawModel;
		this.synchronizer = new PartNameSynchronizer(rawModel);

	}

	/**
	 * 
	 * @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 {
		TableEditorInput tableEditorInput = new TableEditorInput(this.rawModel, getEditingDomain());
		initHiddenColumn(this.rawModel);
		setSite(site);
		setInput(tableEditorInput);
		setPartName(this.rawModel.getName());
		addListeners();
		super.init(site, tableEditorInput);
	}

	/**
	 * add listeners on the context of the table and on the table itself
	 */
	protected void addListeners() {
		EditingDomain editingDomain = getEditingDomain();
		Assert.isTrue(editingDomain instanceof TransactionalEditingDomain);
		this.modelTriggerListener = new ModelTriggerListener(this.rawModel, (INatTableWidgetProvider)getAdapter(INatTableWidgetProvider.class));
		((TransactionalEditingDomain)editingDomain).addResourceSetListener(this.modelTriggerListener);
		this.tableTriggerListener = new TableTriggerListener(this.rawModel, (INatTableWidgetProvider)getAdapter(INatTableWidgetProvider.class));
		((TransactionalEditingDomain)editingDomain).addResourceSetListener(this.tableTriggerListener);
	}

	/**
	 * {@inheritDoc} + update the content of the table if the table is synchronized
	 */
	@Override
	public void createPartControl(Composite parent) {
		super.createPartControl(parent);
		// we update the table
		if(rawModel.isIsSynchronized()) {
			Notification impl = new ENotificationImpl((InternalEObject)rawModel, FillingQueriesUtil.OPEN_TABLE, null, null, null);
			List<Notification> notifications = Collections.singletonList(impl);
			ResourceSetChangeEvent event = new ResourceSetChangeEvent((TransactionalEditingDomain)getEditingDomain(), null, notifications);
			Command cmd = null;
			try {
				cmd = this.modelTriggerListener.transactionAboutToCommit(event);
				if(cmd != null && cmd.canExecute()) {
					cmd.execute();
				}
			} catch (Exception e) {
				Activator.log.error("I can't update the opened table", e); //$NON-NLS-1$
			}
		}
	}

	@Override
	public Object getAdapter(Class adapter) {
		return super.getAdapter(adapter);
	};

	/**
	 * 
	 * @see org.eclipse.papyrus.infra.table.common.internal.NatTableEditor#dispose()
	 * 
	 *      {@inheritDoc}
	 */
	@Override
	public void dispose() {
		if(this.modelTriggerListener != null) {
			((TransactionalEditingDomain)getEditingDomain()).removeResourceSetListener(this.modelTriggerListener);
		}
		if(this.tableTriggerListener != null) {
			((TransactionalEditingDomain)getEditingDomain()).removeResourceSetListener(this.tableTriggerListener);
		}
		synchronizer.dispose();
		super.dispose();
	}

	/**
	 * 
	 * @param rawModel2
	 */
	private void initHiddenColumn(final PapyrusTableInstance rawModel2) {
		for(Column current : rawModel2.getTable().getColumns()) {
			if(current instanceof DefaultLabelColumn || current instanceof MetaClassColumn || current instanceof EContainerColumn) {
				String name = NatTableWidgetInternalUtils.getColumnName(current);
				if(getInitialHiddenColumns().contains(name)) {
					current.setIsHidden(true);
				}
			}
		}
	}

	/**
	 * 
	 * @return
	 *         a list of the names of the columns to hide by default
	 */
	protected List<String> getInitialHiddenColumns() {
		return Collections.emptyList();
	}

	/**
	 * 
	 * @see org.eclipse.emf.facet.widgets.nattable.workbench.editor.NatTableEditor#getEditingDomain()
	 * 
	 * @return
	 */
	@Override
	public EditingDomain getEditingDomain() {
		try {
			return ServiceUtils.getInstance().getTransactionalEditingDomain(this.servicesRegistry);
		} catch (ServiceException e) {
			e.printStackTrace();
			return null;
		}
	}


	/**
	 * This method execute the filling queries
	 */
	@Deprecated
	protected void executeQueries() {
		if(this.rawModel.isIsSynchronized()) {
			TableInstance table = this.rawModel.getTable();
			EObject context = table.getContext();
			List<EObject> elementsToAdd = new ArrayList<EObject>();
			for(ModelQuery query : this.rawModel.getFillingQueries()) {
				ModelQuerySetCatalog catalog = ModelQuerySetCatalog.getSingleton();
				AbstractModelQuery impl = null;
				try {
					impl = catalog.getModelQueryImpl(query);
				} catch (ModelQueryException e) {
					e.printStackTrace();
				}
				if(impl != null) {
					ModelQueryResult result = impl.evaluate(context);
					Object value = result.getValue();
					if(value instanceof Collection<?>) {
						for(Object currentObject : (Collection<?>)value) {
							if(currentObject instanceof EObject && !table.getElements().contains(currentObject)) {
								elementsToAdd.add((EObject)currentObject);
							}
						}

					} else {
						//nothing to do for the moment
					}
				}
			}
			if(!elementsToAdd.isEmpty()) {
				//				this.natTableWidget.addRows(elementsToAdd);
				if(this.natTableWidget instanceof IPapyrusNatTableWidget) {
					((IPapyrusNatTableWidget)this.natTableWidget).addRowsOutOfCommandStack(elementsToAdd);
				}
			}
		}

	}

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

		/** the papyrus table */
		private PapyrusTableInstance 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(PapyrusTableInstance.class) == PapyrustableinstancePackage.PAPYRUS_TABLE_INSTANCE__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 PapyrusTableInstance papyrusTable) {
			setTable(papyrusTable);
		}

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

		/**
		 * Change the associated diagram.
		 * 
		 * @param papyrusTable
		 */
		public void setTable(final PapyrusTableInstance papyrusTable) {
			// Remove from old diagram, 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);
		}
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.infra.table.common.internal.NatTableEditor#setFocus()
	 * 
	 */
	@Override
	public void setFocus() {
		super.setFocus();

		//we update the status of the toggle action on the Table (these actions are declared into oep.infra.table.menu)
		final ICommandService commandService = (ICommandService)this.getEditorSite().getService(ICommandService.class);
		if(commandService != null) {
			org.eclipse.core.commands.Command command = commandService.getCommand("org.eclipse.emf.facet.widget.nattable.common.columns.command"); //$NON-NLS-1$
			if(command != null) {
				final State state = command.getState("org.eclipse.ui.commands.toggleState"); //$NON-NLS-1$
				if(state != null) {
					state.setValue(this.rawModel.getTable().isOnlyShowCommonColumns());
				}
			}
			command = commandService.getCommand("org.eclipse.emf.facet.widget.nattable.empty.columns.command"); //$NON-NLS-1$
			if(command != null) {
				final State state = command.getState("org.eclipse.ui.commands.toggleState"); //$NON-NLS-1$
				if(state != null) {
					state.setValue(this.rawModel.getTable().isHideEmptyColumns());
				}
			}
		}
	}
}

Back to the top