Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9232fe2c0f7a0acaf718c7947d8a47135794ae96 (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
/*******************************************************************************
 * Copyright (c) 2012 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:
 *   Ansgar Radermacher - Initial API and implementation
 *******************************************************************************/

package org.eclipse.papyrus.cpp.codegen.ui.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.cpp.codegen.preferences.CppCodeGenUtils;
import org.eclipse.papyrus.cpp.codegen.transformation.CppModelElementsCreator;
import org.eclipse.papyrus.infra.emf.utils.BusinessModelResolver;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Classifier;

/**
 * <b><u>SyncURI Handler</u></b>
 * <p>
 * Install a filter that only shows events corresponding to a selected URI
 */
public class GenerateCodeHandler extends AbstractHandler {

	// ------------------------------------------------------------------------
	// Execution
	// ------------------------------------------------------------------------

	private EObject selectedEObj;

	@Override
	public boolean isEnabled() {
		// intercept isEnabled operation in order to get selected eObject.

		// Get current selection
		Object selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();

		// Get first element if the selection is an IStructuredSelection
		if(selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection)selection;
			selection = structuredSelection.getFirstElement();
		}

		// Treat non-null selected object (try to adapt and return EObject)
		if(selection != null) {
			Object businessObject = BusinessModelResolver.getInstance().getBusinessModel(selection);
			if(businessObject instanceof EObject) {

				selectedEObj = (EObject)businessObject;
				return true;
			}
		}

		selectedEObj = null;
		return super.isEnabled();
	}

	public Object execute(ExecutionEvent event) throws ExecutionException {

		if(selectedEObj instanceof Classifier) {
			Classifier classifier = (Classifier)selectedEObj;

			
			URI uri = classifier.eResource().getURI();

			// URIConverter uriConverter = resource.getResourceSet().getURIConverter();
			IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
			if(uri.segmentCount() < 2) {
				return null;
			}
			IProject modelProject = root.getProject(uri.segment(1));
			if(modelProject.exists()) {
				String name = classifier.getName();

				// get the container for the current element
				String headerSuffix = CppCodeGenUtils.getHeaderSuffix();
				String bodySuffix = CppCodeGenUtils.getBodySuffix();
				CppModelElementsCreator mec = new CppModelElementsCreator(modelProject);
				IContainer srcPkg = mec.getContainer(classifier);
				try {
					mec.createPackageableElement(srcPkg, null, classifier);

					IFile cppFile = srcPkg.getFile(new Path(name + "." + bodySuffix));
					IFile hFile = srcPkg.getFile(new Path(name + "." + headerSuffix));
					if(!cppFile.exists()) {
						return null;
					}
					if(cppFile != null) {
						cppFile.refreshLocal(0, null);
					}
					if(hFile != null) {
						hFile.refreshLocal(0, null);
					}
				} catch (CoreException coreException) {
					return null;
				}
			}
		}
		return null;
	}
}

Back to the top