Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b92e6808210aa70ca3a3ef0c0921cc07b5a6dda0 (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
/*****************************************************************************
 * Copyright (c) 2009 Atos Origin.
 *
 *    
 * 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:
 *  Emilien Perico (Atos Origin) emilien.perico@atosorigin.com - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.services.controlmode.commands;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.command.CompoundCommand;
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.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.command.RemoveCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.ui.EMFEditUIPlugin;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.emf.commands.core.command.EditingDomainUndoContext;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.papyrus.commands.Activator;
import org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.core.modelsetquery.ModelSetQuery;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModel;
import org.eclipse.papyrus.infra.core.resource.sasheditor.SashModelUtils;
import org.eclipse.papyrus.infra.core.sashwindows.di.PageRef;
import org.eclipse.papyrus.infra.core.sashwindows.di.SashWindowsMngr;
import org.eclipse.papyrus.infra.core.sashwindows.di.exception.SashEditorException;
import org.eclipse.papyrus.infra.core.sashwindows.di.util.DiUtils;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResource;
import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationModel;
import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationUtils;
import org.eclipse.papyrus.infra.services.controlmode.commands.IControlCommand.STATE_CONTROL;
import org.eclipse.papyrus.infra.services.controlmode.history.HistoryModel;
import org.eclipse.papyrus.infra.services.controlmode.history.utils.HistoryUtils;
import org.eclipse.papyrus.infra.services.controlmode.mm.history.ControledResource;
import org.eclipse.papyrus.infra.services.controlmode.mm.history.historyFactory;
import org.eclipse.papyrus.infra.services.controlmode.mm.history.historyPackage;
import org.eclipse.papyrus.infra.widgets.toolbox.notification.NotificationRunnable;
import org.eclipse.papyrus.infra.widgets.toolbox.notification.Type;
import org.eclipse.papyrus.infra.widgets.toolbox.notification.builders.IContext;
import org.eclipse.papyrus.infra.widgets.toolbox.notification.builders.NotificationBuilder;
import org.eclipse.papyrus.uml.tools.model.UmlUtils;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;

/**
 * The Class ControlCommand in charge of controlling all papyrus resources
 */
public class ControlCommand extends AbstractTransactionalCommand {

	/** extension point ID for custom control command */
	private static final String CONTROL_EXTENSION_POINT_ID = "org.eclipse.papyrus.infra.services.controlmode.customControlCommand";

	/** attribute ID for the custom command class. */
	private static final String CONTROL_CMD_ATTRIBUTE_EXTENSION_POINT = "controlCommand";

	/** element ID for the custom command class. */
	private static final String CONTROL_CMD_ELEMENT_EXTENSION_POINT = "customControlCommand";

	protected EObject eObject;

	protected ModelSet modelSet;

	protected Resource controlledModel;

	protected Resource controlledNotation;

	protected Resource controlledDI;

	protected List<IControlCommand> commands;

	/**
	 * Instantiates a new control command.
	 * 
	 * @param domain
	 * @param label
	 * @param affectedFiles
	 */
	public ControlCommand(TransactionalEditingDomain domain, Resource model, EObject selectedObject, String label, List<?> affectedFiles) {
		super(domain, label, affectedFiles);
		this.eObject = selectedObject;
		this.controlledModel = model;
		// Add an undo context to allow the editor to react to that change
		addContext(new EditingDomainUndoContext(domain));

		ResourceSet set = domain.getResourceSet();
		if(set instanceof ModelSet) {
			modelSet = (ModelSet)set;
		} else {
			try {
				modelSet = ServiceUtilsForResource.getInstance().getModelSet(model);
			} catch (ServiceException ex) {
				Activator.log.error(ex);
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		commands = getCommandExtensions();
		IStatus status = doRedo(monitor, info);
		CommandResult result;
		if(status.equals(Status.OK_STATUS)) {
			result = CommandResult.newOKCommandResult();
		} else if(status.equals(Status.CANCEL_STATUS)) {
			result = CommandResult.newErrorCommandResult("Unable to execute control command");
		} else {
			result = CommandResult.newCancelledCommandResult();
		}
		return result;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected IStatus doUndo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		// execute uncontrol command
		UncontrolCommand transactionalCommand = new UncontrolCommand(getEditingDomain(), eObject, "Uncontrol", null, true);
		getEditingDomain().getCommandStack().execute(new GMFtoEMFCommandWrapper(transactionalCommand));
		return Status.OK_STATUS;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected IStatus doRedo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		// Create the URI from models that will be created
		final URI newNotationURI = URI.createURI(controlledModel.getURI().trimFileExtension().appendFileExtension(NotationModel.NOTATION_FILE_EXTENSION).toString());
		this.controlledNotation = getResource(newNotationURI);

		final URI newDiURI = URI.createURI(controlledModel.getURI().trimFileExtension().appendFileExtension(DiModel.DI_FILE_EXTENSION).toString());
		this.controlledDI = getResource(newDiURI);
		final List<Diagram> diagrams = getDiagrams(eObject);

		// compound command that executes whole control action
		CompoundCommand compoundCommand = new CompoundCommand();

		controlModel(compoundCommand);
		controlNotation(compoundCommand, diagrams);
		try {
			controlDi(compoundCommand, diagrams);
		} catch (SashEditorException exception) {
			EMFEditUIPlugin.INSTANCE.log(exception);
			return Status.CANCEL_STATUS;
		}

		// Ensure that all proxies are resolved so that references into the controlled object will be saved to reference the new resource
		EcoreUtil.resolveAll(getEditingDomain().getResourceSet());

		if(compoundCommand.canExecute()) {
			compoundCommand.execute();
			return Status.OK_STATUS;
		} else {
			return Status.CANCEL_STATUS;
		}
	}

	/**
	 * Search for diagrams in corresponding notation resource of the eObject model resource
	 * 
	 * @param eObject
	 * @return
	 */
	protected List<Diagram> getDiagrams(EObject eObject) {
		List<Diagram> diagrams = new LinkedList<Diagram>();
		// search for diagrams only in the notation resource corresponding to the model resource where eObject is.
		// Some "sub diagrams" in the model can be in controlled resource, we must not move them.
		Resource resource = eObject.eResource();
		Resource notationResource = modelSet.getResource(resource.getURI().trimFileExtension().appendFileExtension(NotationModel.NOTATION_FILE_EXTENSION), false);
		diagrams.addAll(NotationUtils.getDiagrams(notationResource, eObject));
		return diagrams;
	}

	//	/**
	//	 * Search for diagrams in all the resources
	//	 * 
	//	 * @param eObject
	//	 * @return
	//	 */
	//	private List<Diagram> getDiagrams(EObject eObject) {
	//		List<Diagram> diagrams = new LinkedList<Diagram>();
	//		for(Resource r : diResourceSet.getResources()) {
	//			if(NotationModel.NOTATION_FILE_EXTENSION.equals(r.getURI().fileExtension())) {
	//				diagrams.addAll(NotationUtils.getDiagrams(r, eObject));
	//			}
	//		}
	//		return diagrams;
	//	}

	/**
	 * Control the model resource
	 * 
	 * @param compoundCommand
	 */
	protected void controlModel(CompoundCommand compoundCommand) {
		// PRE control operation
		control(getEditingDomain(), eObject, UmlUtils.getUmlModel(modelSet).getResource(), controlledModel, compoundCommand, STATE_CONTROL.PRE_MODEL);

		// Control the Model
		compoundCommand.append(new AddCommand(getEditingDomain(), controlledModel.getContents(), eObject));

		// update history
		assignControlledResourceOfCurrentElement(getEditingDomain(), compoundCommand, getHistoryResource(eObject), eObject.eResource().getURI().toString(), controlledModel.getURI().toString());

		// POST control operation
		control(getEditingDomain(), eObject, UmlUtils.getUmlModel(modelSet).getResource(), controlledModel, compoundCommand, STATE_CONTROL.POST_MODEL);
	}

	/**
	 * Control the notation resource
	 * 
	 * @param compoundCommand
	 * @param diagrams
	 *        list
	 */
	protected void controlNotation(CompoundCommand compoundCommand, List<Diagram> diagrams) {
		// PRE control operation
		for(Diagram diag : diagrams) {
			control(getEditingDomain(), diag, getNotationResourceForCurrent(eObject), controlledNotation, compoundCommand, STATE_CONTROL.PRE_NOTATION);
		}
		if(!diagrams.isEmpty()) {
			// Control the Notation model
			compoundCommand.append(new AddCommand(getEditingDomain(), controlledNotation.getContents(), diagrams));
		}

		// update history
		Set<Resource> resources = new HashSet<Resource>(diagrams.size());
		for(Diagram d : diagrams) {
			resources.add(d.eResource());
		}
		for(Resource r : resources) {
			assignControlledResourceOfCurrentElement(getEditingDomain(), compoundCommand, getHistoryResource(eObject), r.getURI().toString(), controlledNotation.getURI().toString());
		}

		// POST control operation
		for(Diagram diag : diagrams) {
			control(getEditingDomain(), diag, getNotationResourceForCurrent(eObject), controlledNotation, compoundCommand, STATE_CONTROL.POST_NOTATION);
		}

		//control for PapyrusTable
		control(getEditingDomain(), eObject, getNotationResourceForCurrent(eObject), controlledNotation, compoundCommand, STATE_CONTROL.POST_NOTATION);
	}

	/**
	 * Control the di resource
	 * 
	 * @param compoundCommand
	 * @param diagrams
	 * @throws SashEditorException
	 */
	protected void controlDi(CompoundCommand compoundCommand, final List<Diagram> diagrams) throws SashEditorException {
		// Create a new SashWindowManager
		final SashWindowsMngr windowsMngr = DiUtils.createDefaultSashWindowsMngr();
		Resource diResource = SashModelUtils.getSashModel(modelSet).getResource();
		// add pages to the page list
		for(Diagram diagram : diagrams) {
			PageRef pageRef = DiUtils.getPageRef(diResource, diagram);
			if(pageRef != null) {
				windowsMngr.getPageList().addPage(pageRef.getPageIdentifier());
				DiUtils.addPageToTabFolder(windowsMngr, pageRef);
			}
		}

		// PRE control operation
		control(getEditingDomain(), eObject, diResource, controlledDI, compoundCommand, STATE_CONTROL.PRE_DI);

		// Control the DI model
		compoundCommand.append(new AddCommand(getEditingDomain(), controlledDI.getContents(), windowsMngr, 0));

		//FIXME : remove this workaround after the refactoring
		final Adapter adapter = new Adapter() {

			public void notifyChanged(Notification notification) {
				//nothing to do 
			}

			public Notifier getTarget() {
				return windowsMngr;
			}

			public void setTarget(Notifier newTarget) {
				//nothing to do
			}

			public boolean isAdapterForType(Object type) {
				return type == SashWindowsMngr.class;
			}

		};
		eObject.eAdapters().add(adapter);
		// POST control operation
		control(getEditingDomain(), eObject, diResource, controlledDI, compoundCommand, STATE_CONTROL.POST_DI);
		eObject.eAdapters().remove(adapter);
	}

	/**
	 * Display asynchronous popup to inform user about the control action
	 */
	protected void notifySave() {
		new NotificationBuilder().setMessage("Your element has been controlled.\nYou need to save your model to see modifications in your workspace.\nDo you want to save ?").addAction(new NotificationRunnable() {

			public void run(IContext context) {
				try {
					Display.getDefault().syncExec(new Runnable() {

						public void run() {
							PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().doSave(new NullProgressMonitor());
						}
					});
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			public String getLabel() {
				return "Save";
			}
		}).setTemporary(true).setAsynchronous(true).setType(Type.INFO).setDelay(2000).run();
	}

	/**
	 * Analyze the history model to update the controlled children
	 * 
	 * @param domain
	 * @param compoundCommand
	 * @param model
	 * @param currentURL
	 * @param newURL
	 */
	protected void assignControlledResourceOfCurrentElement(EditingDomain domain, CompoundCommand compoundCommand, Resource model, String currentURL, String newURL) {
		if(model == null) {
			return;
		}
		// create relative path
		URI uriPath = HistoryUtils.getURIFullPath(currentURL);
		String currentURLResolved = HistoryUtils.resolve(uriPath, currentURL);
		String newURLResolved = HistoryUtils.resolve(uriPath, newURL);

		ControledResource child = historyFactory.eINSTANCE.createControledResource();
		child.setResourceURL(newURLResolved);

		ControledResource resource = getControledResource(model);
		Resource parentResource = null;

		// create the controlled resource according to the control action
		ControledResource parent = null;
		if(resource == null) {
			parent = historyFactory.eINSTANCE.createControledResource();
			parent.setResourceURL(currentURLResolved);
			parent.getChildren().add(child);
			parentResource = model;
			compoundCommand.append(new AddCommand(domain, parentResource.getContents(), Collections.singleton(parent)));
		} else {
			if(isCurrentURL(currentURLResolved, resource)) {
				parent = resource;
			}
			if(parent == null) {
				EObject modelRoot = getControledResource(model);
				Collection<EObject> controled = ModelSetQuery.getObjectsOfType(modelRoot, historyPackage.Literals.CONTROLED_RESOURCE);
				for(EObject next : controled) {
					if(next instanceof ControledResource) {
						ControledResource tmp = (ControledResource)next;
						if(isCurrentURL(currentURLResolved, tmp)) {
							parent = tmp;
							break;
						}
					}
				}
			}
			if(parent == null) {
				parent = historyFactory.eINSTANCE.createControledResource();
				parent.setResourceURL(currentURLResolved);
				parentResource = resource.eResource();
				// add controlled resource command for notation is done in assignToChildExistingControledResources
				if(!currentURLResolved.endsWith(NotationModel.NOTATION_FILE_EXTENSION)) {
					compoundCommand.append(new AddCommand(domain, parentResource.getContents(), Collections.singleton(parent)));
				}
			}
			if(parent != null) {
				compoundCommand.append(AddCommand.create(domain, parent, historyPackage.Literals.CONTROLED_RESOURCE__CHILDREN, Collections.singleton(child)));
			}
		}
		List<ControledResource> controledFromParent = new LinkedList<ControledResource>();
		if(parentResource == null) {
			parentResource = model;
		}
		if(parentResource != null) {
			for(EObject e : parentResource.getContents()) {
				if(e instanceof ControledResource) {
					ControledResource aControled = (ControledResource)e;
					controledFromParent.add(aControled);
					for(Iterator<EObject> i = aControled.eAllContents(); i.hasNext();) {
						EObject tmp = i.next();
						if(tmp instanceof ControledResource) {
							controledFromParent.add((ControledResource)tmp);
						}
					}
				}
			}
		}
		// manage move of existing controlled resources
		if(!newURL.endsWith(NotationModel.NOTATION_FILE_EXTENSION)) {
			assignToChildExistingControledResources(domain, compoundCommand, child, newURL, controledFromParent, currentURL, URI.createURI(newURL), uriPath);
		}
	}

	/**
	 * Manage move of existing controlled resources
	 * 
	 * @param domain
	 * @param compoundCommand
	 * @param child
	 * @param controledResourceURL
	 * @param controledFromParent
	 * @param parentURL
	 * @param controledURIFullPath
	 * @param parentURIFullPath
	 */
	protected void assignToChildExistingControledResources(EditingDomain domain, CompoundCommand compoundCommand, ControledResource child, String controledResourceURL, List<ControledResource> controledFromParent, String parentURL, URI controledURIFullPath, URI parentURIFullPath) {
		for(ControledResource r : controledFromParent) {
			if(r.getResourceURL() != null) {
				URI fullPathParent = URI.createURI(r.getResourceURL()).resolve(parentURIFullPath);
				Resource resourceLoaded = modelSet.getResource(fullPathParent, false);
				if(resourceLoaded != null && !resourceLoaded.getContents().isEmpty()) {
					EObject top = resourceLoaded.getContents().get(0);
					if(isInRootHierarchy(top, eObject)) {
						// manage model
						URI newResolvedURIFromChild = fullPathParent.deresolve(controledURIFullPath, false, true, true);
						ControledResource aNewOne = historyFactory.eINSTANCE.createControledResource();
						aNewOne.setResourceURL(newResolvedURIFromChild.toString());
						// add new control resource to the new history
						compoundCommand.append(new AddCommand(domain, getControledResource(controlledDI, URI.createURI(controledResourceURL).lastSegment(), compoundCommand, getEditingDomain()), historyPackage.Literals.CONTROLED_RESOURCE__CHILDREN, aNewOne));
						// remove old controlled resource from the parent resource
						compoundCommand.append(RemoveCommand.create(domain, r.eContainer(), historyPackage.Literals.CONTROLED_RESOURCE__CHILDREN, r));

						// manage notation
						URI newNotation = newResolvedURIFromChild.trimFileExtension().appendFileExtension(NotationModel.NOTATION_FILE_EXTENSION);
						ControledResource aNewOneNotation = historyFactory.eINSTANCE.createControledResource();
						aNewOneNotation.setResourceURL(newNotation.toString());
						// add new control resource to the new history
						compoundCommand.append(new AddCommand(domain, getControledResource(controlledDI, URI.createURI(URI.createURI(controledResourceURL).trimFileExtension().appendFileExtension(NotationModel.NOTATION_FILE_EXTENSION).toString()).lastSegment(), compoundCommand, getEditingDomain()), historyPackage.Literals.CONTROLED_RESOURCE__CHILDREN, aNewOneNotation));
						// remove old controlled resource from the parent resource
						URI notationParentURL = URI.createURI(r.getParent().getResourceURL()).trimFileExtension().appendFileExtension(NotationModel.NOTATION_FILE_EXTENSION);
						ControledResource notationParent = getControledResource(r.eResource(), notationParentURL.toString(), compoundCommand, domain);
						for(ControledResource notationChild : notationParent.getChildren()) {
							//URI notationURI = newResolvedURIFromChild.trimFileExtension().appendFileExtension(NotationModel.NOTATION_FILE_EXTENSION);
							URI notationURI = URI.createURI(r.getResourceURL()).trimFileExtension().appendFileExtension(NotationModel.NOTATION_FILE_EXTENSION);
							if(notationChild.getResourceURL().equals(notationURI.toString())) {
								compoundCommand.append(RemoveCommand.create(domain, notationParent, historyPackage.Literals.CONTROLED_RESOURCE__CHILDREN, notationChild));
							}
						}
					}
				}
			}
		}
	}

	/**
	 * Get the controlled resource in a specified resource
	 */
	protected ControledResource getControledResource(Resource resource) {
		for(EObject e : resource.getContents()) {
			if(e instanceof ControledResource) {
				return (ControledResource)e;

			}
		}
		return null;
	}

	/**
	 * Get the controlled resource for the specified URL, create it if it doesn't exist
	 * 
	 * @param resource
	 * @param controledResourceURL
	 * @param command
	 * @param domain
	 * @return
	 */
	protected ControledResource getControledResource(Resource resource, String controledResourceURL, CompoundCommand command, EditingDomain domain) {
		ControledResource result = null;
		for(EObject e : resource.getContents()) {
			if(e instanceof ControledResource) {
				ControledResource controled = (ControledResource)e;
				if(controledResourceURL != null && controledResourceURL.equals(controled.getResourceURL())) {
					result = controled;
				}
			}
		}
		if(result == null) {
			result = historyFactory.eINSTANCE.createControledResource();
			result.setResourceURL(controledResourceURL);
			command.append(new AddCommand(domain, resource.getContents(), result, 0));
		}
		return result;
	}

	/**
	 * Check if an EObject start is in root hierarchy of another eObject search
	 * 
	 * @param start
	 * @param search
	 * @return
	 */
	protected boolean isInRootHierarchy(EObject start, EObject search) {
		while(start != null && start != search) {
			start = start.eContainer();
		}
		return start != null;
	}

	/**
	 * Check if an URL is the current one of the specified resource
	 * 
	 * @param currentURL
	 * @param resource
	 * @return
	 */
	protected boolean isCurrentURL(String currentURL, ControledResource resource) {
		return resource.getResourceURL() != null && resource.getResourceURL().equals(currentURL);
	}

	/**
	 * Get the notation resource for the specified eObject
	 * 
	 * @param eObject
	 * @return
	 */
	protected Resource getNotationResourceForCurrent(EObject eObject) {
		return modelSet.getResource(eObject.eResource().getURI().trimFileExtension().appendFileExtension(NotationModel.NOTATION_FILE_EXTENSION), true);
	}

	/**
	 * Get the history resource of the specified eObject
	 * 
	 * @param eObject
	 * @return
	 */
	protected Resource getHistoryResource(EObject eObject) {
		if(eObject.eResource() != null) {
			return modelSet.getResource(eObject.eResource().getURI().trimFileExtension().appendFileExtension(HistoryModel.MODEL_FILE_EXTENSION), true);
		}
		return null;
	}

	/**
	 * Control action applied on the specified selection
	 * 
	 * @param domain
	 * @param selection
	 * @param source
	 * @param target
	 * @param command
	 * @param state
	 */
	public void control(EditingDomain domain, EObject selection, Resource source, Resource target, CompoundCommand command, STATE_CONTROL state) {
		for(IControlCommand cmd : commands) {
			if(cmd.provides(selection, state, source, target)) {
				cmd.control(domain, selection, state, source, target, command);
			}
		}
	}

	/**
	 * Gets the custom command extensions that will be executed with the default
	 * control action.
	 * 
	 * @return the command extensions
	 */
	private List<IControlCommand> getCommandExtensions() {
		List<IControlCommand> commands = new LinkedList<IControlCommand>();
		IConfigurationElement[] extensions = Platform.getExtensionRegistry().getConfigurationElementsFor(CONTROL_EXTENSION_POINT_ID);
		for(IConfigurationElement e : extensions) {
			if(CONTROL_CMD_ELEMENT_EXTENSION_POINT.equals(e.getName())) {
				try {
					IControlCommand controlCmd = (IControlCommand)e.createExecutableExtension(CONTROL_CMD_ATTRIBUTE_EXTENSION_POINT);
					commands.add(controlCmd);
				} catch (CoreException exception) {
					exception.printStackTrace();
				}
			}
		}
		return commands;
	}

	/**
	 * Gets the resource from the URI, create it if not exists
	 * 
	 * @param uri
	 * 
	 * @return the resource
	 */
	protected Resource getResource(URI uri) {
		Resource res = getEditingDomain().getResourceSet().getResource(uri, false);
		if(res == null) {
			res = getEditingDomain().getResourceSet().createResource(uri);
		}
		return res;
	}

}

Back to the top