Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 91434299a22b07985fca45e105c3ddf1c35c1f05 (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
/*****************************************************************************
 * Copyright (c) 2015 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:
 * 
 * 		Patrick Tessier (patrick.tessier@cea.fr) CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.requirements.sysml.traceability.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.ui.util.EditorUtils;
import org.eclipse.papyrus.infra.ui.util.ServiceUtilsForHandlers;
import org.eclipse.papyrus.requirements.sysml.traceability.assistant.analysis.TracabilityAnalyzer;
import org.eclipse.papyrus.uml.extensionpoints.profile.RegisteredProfile;
import org.eclipse.papyrus.uml.extensionpoints.utils.Util;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Profile;

/**
 * this class launch the application of the SysML profile
 */

public class ApplyTracabilityHandler extends AbstractHandler { 

	protected TransactionalEditingDomain transactionalEditingDomain=null;
	protected org.eclipse.uml2.uml.Package selectedElement=null;

	public Object execute(ExecutionEvent event) throws ExecutionException {
		ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
		ISelection selection = selectionService.getSelection();
		try {
			transactionalEditingDomain =ServiceUtilsForHandlers.getInstance().getService(TransactionalEditingDomain.class, event);
		} catch (Exception e) {
			System.err.println("impossible to get the Transactional Editing Domain "+e);
		}



		if(selection instanceof IStructuredSelection) {
			Object selectedobject = ((IStructuredSelection)selection).getFirstElement();
			EObject selectedEObject = (EObject)EMFHelper.getEObject(selectedobject);
			if (selectedEObject instanceof Model){
				selectedElement=(Package)selectedEObject;
			}


			if(selectedElement!=null){

				@SuppressWarnings("deprecation")
				TransactionalEditingDomain editingDomain = EditorUtils.getTransactionalEditingDomain();
				if (editingDomain != null && selectedElement!=null) {
					Command command = new RecordingCommand(editingDomain) {

						@Override
						protected void doExecute() {
							changeStructure(selectedElement);

						}
					};
					editingDomain.getCommandStack().execute(command);
					TracabilityAnalyzer analyzer= new TracabilityAnalyzer();
					analyzer.runAnalysis(getToPackage(selectedElement), editingDomain);
				}

			}
		}
		return null;
	}

	private Package getToPackage(Element elem){
		Package tmp= elem.getNearestPackage();
		while(tmp.getOwner()!=null && (tmp.getOwner()instanceof Package)){
			tmp= (Package)tmp.getOwner();
		}
		return tmp;
	}
	/**
	 * 
	 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
	 * 
	 * @return true if the handler is possible
	 */
	@Override
	public boolean isEnabled() {
		return true;
	}

	protected void changeStructure(Package currentPackage) {
		Package topPackage= getToPackage(currentPackage);
		//apply SysML profile
		RegisteredProfile SysMLregisteredProfile=(RegisteredProfile)RegisteredProfile.getRegisteredProfile("SysML");
		URI SysMLmodelUri = SysMLregisteredProfile.uri;
		@SuppressWarnings("deprecation")
		final Resource SysMLResource = Util.getResourceSet(topPackage).getResource(SysMLmodelUri, true);
		Profile sysMLprofile=(Profile) SysMLResource.getContents().get(0);
		topPackage.applyProfile(sysMLprofile);

		URI standardL2URI = URI.createURI("pathmap://UML_PROFILES/StandardL2.profile.uml");
		@SuppressWarnings("deprecation")
		final Resource standardL2Resource = Util.getResourceSet(topPackage).getResource(standardL2URI, true);
		Profile  standardL2Profile=(Profile) standardL2Resource.getContents().get(0);
		topPackage.applyProfile(standardL2Profile);
	}

}

Back to the top