Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 83a65b2c1d1ecb0292969c63da1d09e33dd0c8dc (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
/*****************************************************************************
 * Copyright (c) 2015, 2016 CEA LIST, Christian W. Damus, 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:
 *   CEA LIST - Initial API and implementation
 *   Christian W. Damus - bug 485220
 *   
 *****************************************************************************/

package org.eclipse.papyrus.infra.nattable.common.api;

import java.util.Collection;
import java.util.Iterator;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.infra.architecture.representation.PapyrusRepresentationKind;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.NotFoundException;
import org.eclipse.papyrus.infra.core.sashwindows.di.service.IPageManager;
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.utils.ServiceUtilsForResource;
import org.eclipse.papyrus.infra.nattable.common.Activator;
import org.eclipse.papyrus.infra.nattable.common.helper.TableViewPrototype;
import org.eclipse.papyrus.infra.nattable.common.modelresource.PapyrusNattableModel;
import org.eclipse.papyrus.infra.nattable.internal.common.commands.CreateAndOpenTableEditorCommand;
import org.eclipse.papyrus.infra.nattable.model.nattable.Table;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableconfiguration.TableConfiguration;
import org.eclipse.papyrus.infra.nattable.representation.PapyrusTable;
import org.eclipse.papyrus.infra.viewpoints.policy.PolicyChecker;
import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype;

/**
 * This class can be used to create a new table editor programmatically
 *
 */
public class TableEditorCreationHelper {

	/*---------the field initialized by the constructor*/

	/**
	 * the context of the table to create
	 */
	private EObject tableContext;

	/**
	 * the owner of the created table (will be used to display the table as its child in the ModelExplorer View). If <code>null</code> we use the tableContext.
	 */
	private EObject tableOwner;

	/**
	 * the type of the table to create (also called implementationID in the Viewpoint framework)
	 */
	private String tableType;

	/**
	 * the name of the new table
	 */
	private String tableName;

	/**
	 * the description to use for the new table
	 */
	private String tableDescription;

	/**
	 * a boolean to know if we must open the table editor after its creation
	 */
	private boolean openCreatedEditor;

	/*--------------the calculated field ---------------------*/
	/**
	 * the service registry of the edited model
	 */
	private ServicesRegistry servicesRegistry;

	/**
	 * the model set of the edited model
	 */
	private ModelSet modelSet;

	/**
	 * the papyrus nattable model (used to register the created table)
	 */
	private PapyrusNattableModel papyrusNattableModel;

	/**
	 * the table view prototype
	 */
	private TableViewPrototype tableViewPrototype;

	/**
	 * the table configuration used to create the table
	 */
	private TableConfiguration tableConfiguration;

	/**
	 * the editing domain used to create the command
	 */
	private TransactionalEditingDomain editingDomain;

	/**
	 * the page manager used to open the created table
	 */
	private IPageManager pageMngr;

	/**
	 * the command to use to create the table
	 */
	private Command creationCommand;

	/**
	 * 
	 * Constructor.
	 *
	 * @param tableContext
	 *            the context used to create the table, it should not be <code>null</code>. This object will be used as owner of the table too.
	 * @param tableType
	 *            the type of the table to create. It should not be <code>null</code>
	 * @param name
	 *            the name of the new created table. It should not be <code>null</code>
	 * @param openCreatedEditor
	 *            if <code>true</code>, the created table will be open
	 */
	public TableEditorCreationHelper(final EObject tableContext, final String tableType, final String name, final boolean openCreatedEditor) {
		this(tableContext, tableType, name, openCreatedEditor, null, null);
	}

	/**
	 * 
	 * Constructor.
	 *
	 * @param tableContext
	 *            the context used to create the table, it should not be <code>null</code>. This object will be used as owner of the table too.
	 * @param tableType
	 *            the type of the table to create. It should not be <code>null</code>
	 * @param name
	 *            the name of the new created table. It should not be <code>null</code>
	 * @param openCreatedEditor
	 *            if <code>true</code>, the created table will be open
	 * @param description
	 *            the description of the created table
	 */
	public TableEditorCreationHelper(final EObject tableContext, final String tableType, final String name, final boolean openCreatedEditor, final String description) {
		this(tableContext, tableType, name, openCreatedEditor, null, description);
	}

	/**
	 * 
	 * Constructor.
	 *
	 * @param tableContext
	 *            the context used to create the table, it should not be <code>null</code>
	 * @param tableType
	 *            the type of the table to create. It should not be <code>null</code>
	 * @param name
	 *            the name of the new created table. It should not be <code>null</code>
	 * @param openCreatedEditor
	 *            if <code>true</code>, the created table will be open
	 * @param tableOwner
	 *            the owner of the created table. If <code>null</code>, we will use the context of the table as owner
	 */
	public TableEditorCreationHelper(final EObject tableContext, final String tableType, final String name, final boolean openCreatedEditor, final EObject tableOwner) {
		this(tableContext, tableType, name, openCreatedEditor, tableOwner, null);
	}

	/**
	 * 
	 * Constructor.
	 *
	 * the context used to create the table, it should not be <code>null</code>
	 * 
	 * @param tableType
	 *            the type of the table to create. It should not be <code>null</code>
	 * @param name
	 *            the name of the new created table. It should not be <code>null</code>
	 * @param openCreatedEditor
	 *            if <code>true</code>, the created table will be open
	 * @param tableOwner
	 *            the owner of the created table. If <code>null</code>, we will use the context of the table as owner
	 * @param description
	 *            the description of the created table
	 */
	public TableEditorCreationHelper(final EObject tableContext, final String tableType, final String name, final boolean openCreatedEditor, final EObject tableOwner, final String description) {
		this.tableContext = tableContext;
		this.tableOwner = tableOwner;
		this.tableType = tableType;
		this.tableName = name;
		this.tableDescription = description;
		this.openCreatedEditor = openCreatedEditor;
	}


	/**
	 * This method initis the fields required to create a new table and return a IStatus indicating if the table can be created with the given context
	 * 
	 * @return
	 * 		a IStatus to know if the table can be created according to the table context and the table type:
	 *         <ul>
	 *         <li>{@link #tableContext}</li>
	 *         <li>{@link #tableType}</li>
	 *         <li>{@link #tableName}</li>
	 *         </ul>
	 *         The returned IStatus provides a message for each error and a code. The possible code are described in {@link ITableEditorStatusCode}
	 * 
	 */
	public IStatus canCreateTable() {
		final String pluginID = getPluginID();
		// 0. check field
		if (this.tableContext == null) {
			return new Status(IStatus.ERROR, pluginID, ITableEditorStatusCode.TABLE_CONTEXT_IS_NULL, "The table context is null", null); //$NON-NLS-1$
		}

		if (this.tableType == null) {
			return new Status(IStatus.ERROR, pluginID, ITableEditorStatusCode.TABLE_TYPE_IS_NULL, "The table type is null", null); //$NON-NLS-1$
		}

		if (this.tableName == null || this.tableName.isEmpty()) {
			return new Status(IStatus.ERROR, pluginID, ITableEditorStatusCode.TABLE_NAME_IS_NULL, "The name of the table to create is null or empty", null); //$NON-NLS-1$
		}
		// 1. check the service registry
		try {
			this.servicesRegistry = ServiceUtilsForResource.getInstance().getServiceRegistry(tableContext.eResource());
		} catch (ServiceException e) {
			return new Status(IStatus.ERROR, pluginID, ITableEditorStatusCode.SERVICES_REGISTRY_NOT_FOUND, e.getMessage(), e);
		}


		// 2. check the model set prototype
		try {
			this.modelSet = ServiceUtils.getInstance().getModelSet(this.servicesRegistry);
		} catch (ServiceException e) {
			return new Status(IStatus.ERROR, pluginID, ITableEditorStatusCode.MODEL_SET_NOT_FOUND, e.getMessage(), e);
		}

		// 3. check the editing model
		try {
			this.editingDomain = ServiceUtils.getInstance().getTransactionalEditingDomain(servicesRegistry);
		} catch (ServiceException e) {
			return new Status(IStatus.ERROR, pluginID, ITableEditorStatusCode.EDITING_DOMAIN_NOT_FOUND, e.getMessage(), e);
		}

		if (editingDomain == null) {
			return new Status(IStatus.ERROR, pluginID, ITableEditorStatusCode.EDITING_DOMAIN_NOT_FOUND, "The editing domain has not been found", null); //$NON-NLS-1$
		}

		// 4. check the page manager
		try {
			this.pageMngr = ServiceUtils.getInstance().getService(IPageManager.class, this.servicesRegistry);
		} catch (ServiceException e) {
			return new Status(IStatus.ERROR, pluginID, ITableEditorStatusCode.PAGE_MANAGER_NOT_FOUND, e.getMessage(), e);
		}

		if (this.pageMngr == null) {
			return new Status(IStatus.ERROR, pluginID, ITableEditorStatusCode.PAGE_MANAGER_NOT_FOUND, "The page manager has not be found", null); //$NON-NLS-1$
		}

		// 5. check the Papyrus Nattable Model
		try {
			this.papyrusNattableModel = (PapyrusNattableModel) modelSet.getModelChecked(PapyrusNattableModel.MODEL_ID);
		} catch (NotFoundException e) {
			return new Status(IStatus.ERROR, pluginID, ITableEditorStatusCode.PAPYRUS_NATTABLE_MODEL_NOT_FOUND, e.getMessage(), e);
		}

		if (this.papyrusNattableModel == null) {
			return new Status(IStatus.ERROR, pluginID, ITableEditorStatusCode.PAPYRUS_NATTABLE_MODEL_NOT_FOUND, "Papyrus Nattable Model not found", null); //$NON-NLS-1$
		}

		// 6. check the table view prototype
		this.tableViewPrototype = getTableViewPrototype(tableContext, tableType);
		if (this.tableViewPrototype == null) {
			return new Status(IStatus.ERROR, pluginID, ITableEditorStatusCode.TABLE_VIEW_PROTOTYPE_NOT_FOUND, NLS.bind("The View Prototype to create a {0} table has not been found", this.tableType), null); //$NON-NLS-1$
		}

		// 7. get the nattable configuration
		this.tableConfiguration = getTableConfiguration(tableViewPrototype);

		if (this.tableConfiguration == null) {
			return new Status(IStatus.ERROR, pluginID, ITableEditorStatusCode.TABLE_CONFIGURATION_NOT_FOUND, NLS.bind("Table configuration not found for the table type {0}", this.tableType), null); //$NON-NLS-1$
		}

		// 9. create and check the command
		this.creationCommand = getCreateTableEditorCommand();
		if (this.creationCommand == null) {
			return new Status(IStatus.ERROR, pluginID, ITableEditorStatusCode.COMMAND_NULL, "The creation command cannot be created", null); //$NON-NLS-1$
		}
		if (!this.creationCommand.canExecute()) {
			return new Status(IStatus.ERROR, pluginID, ITableEditorStatusCode.COMMAND_NOT_EXECUTABLE, "The creation command is not executable", null); //$NON-NLS-1$
		}
		return Status.OK_STATUS;
	}

	/**
	 * 
	 * @return
	 * 		the activator id of the plugin
	 */
	protected String getPluginID() {
		return Activator.PLUGIN_ID;
	}

	/**
	 * 
	 * @return
	 * 		the command to create a open (if {@link #openCreatedEditor} is <code>true</code> a new table editor
	 */
	public Command getCreateTableEditorCommand() {
		EObject tableOwner = this.tableOwner;
		if (tableOwner == null) {
			this.tableOwner = this.tableContext;
		}
		if (openCreatedEditor) {
			return new CreateAndOpenTableEditorCommand(this.editingDomain, this.papyrusNattableModel, this.tableViewPrototype, this.tableConfiguration, this.tableContext, this.tableName, tableOwner, this.tableDescription, this.pageMngr);
		} else {
			return new CreateAndOpenTableEditorCommand(this.editingDomain, this.papyrusNattableModel, this.tableViewPrototype, this.tableConfiguration, this.tableContext, this.tableName, tableOwner, this.tableDescription, null);
		}
	}

	/**
	 * 
	 * @return
	 * 		the created table or <code>null</code> if it is not possible to create the command
	 */
	public Table createTableEditor() {
		if (canCreateTable().isOK()) {
			if (this.creationCommand != null && this.creationCommand.canExecute()) {
				this.editingDomain.getCommandStack().execute(creationCommand);
			}
			if (this.creationCommand != null) {
				Collection<?> result = creationCommand.getResult();
				if (result.size() > 0) {
					Object res = result.iterator().next();
					if (res instanceof Table) {
						return (Table) res;
					}
				}
			}
		}
		return null;
	}

	/**
	 * @param viewPrototype
	 *            a view {@link TableViewPrototype}, must not be <code>null</code>
	 * @return
	 * 		a TableConfiguration, or <code>null</code> if not found
	 * @since 3.0
	 */
	protected TableConfiguration getTableConfiguration(TableViewPrototype viewPrototype) {
		Assert.isNotNull(viewPrototype);
		if (viewPrototype.getRepresentationKind() instanceof PapyrusTable) {
			PapyrusTable papyrusTable = (PapyrusTable) viewPrototype.getRepresentationKind();
			return papyrusTable.getConfiguration();
		}
		return null;
	}

	/**
	 * 
	 * @param tableContext
	 *            the context to use to create the table, should not be <code>null</code>
	 * @param tableType
	 *            the type of the table (also called implementationID in the Viewpoint framework), should not be <code>null</code>
	 * @return
	 * 		the {@link TableViewPrototype} if we found it, or <code>null</code>
	 */
	protected TableViewPrototype getTableViewPrototype(final EObject tableContext, final String tableType) {
		if (tableContext == null || tableType == null) {
			return null;
		}
		// 1. get all available view prototype for the table context
		Collection<ViewPrototype> prototypes = PolicyChecker.getFor(tableContext).getPrototypesFor(tableContext);
		Iterator<ViewPrototype> iter = prototypes.iterator();

		// 2. find the view prototype allowing to create the wanted table, identified by it ViewPrototype
		while (iter.hasNext()) {
			ViewPrototype current = iter.next();
			if (current instanceof TableViewPrototype) {
				TableViewPrototype prototype = (TableViewPrototype) current;
				String implementationID = prototype.getImplementation();
				if (tableType.equals(implementationID)) {
					return prototype;
				}
				if (implementationID == null || implementationID.isEmpty()) {
					PapyrusRepresentationKind representationKind = prototype.getRepresentationKind();
					if (representationKind instanceof PapyrusTable) {
						// we need to load the real table configuration to check the type
						PapyrusTable papyrusTable = (PapyrusTable) representationKind;
						TableConfiguration configuration = papyrusTable.getConfiguration();
						if (configuration.getType().equals(tableType)) { // to check ?
							return prototype;
						}
					}
				}
			}
		}
		return null;
	}


	/**
	 * @param tableContext
	 *            the tableContext to set
	 */
	public void setTableContext(EObject tableContext) {
		this.tableContext = tableContext;
	}


	/**
	 * @param tableOwner
	 *            the tableOwner to set
	 */
	public void setTableOwner(EObject tableOwner) {
		this.tableOwner = tableOwner;
	}


	/**
	 * @param tableType
	 *            the tableType to set
	 */
	public void setTableType(String tableType) {
		this.tableType = tableType;
	}


	/**
	 * @param name
	 *            the name to set
	 */
	public void setTableName(String name) {
		this.tableName = name;
	}


	/**
	 * @param description
	 *            the description to set
	 */
	public void setTableDescription(String description) {
		this.tableDescription = description;
	}


}

Back to the top