1 | package de.ugoe.cs.swe.bnftools.ui.modelingtools;
|
---|
2 |
|
---|
3 | import org.eclipse.core.commands.AbstractHandler;
|
---|
4 | import org.eclipse.core.commands.ExecutionEvent;
|
---|
5 | import org.eclipse.core.commands.ExecutionException;
|
---|
6 | import org.eclipse.core.resources.IProject;
|
---|
7 | import org.eclipse.core.resources.IResource;
|
---|
8 | import org.eclipse.core.runtime.IPath;
|
---|
9 | import org.eclipse.core.runtime.NullProgressMonitor;
|
---|
10 | import org.eclipse.emf.common.util.URI;
|
---|
11 | import org.eclipse.emf.ecore.EObject;
|
---|
12 | import org.eclipse.emf.ecore.resource.Resource;
|
---|
13 | import org.eclipse.emf.ecore.xmi.impl.XMLResourceFactoryImpl;
|
---|
14 | import org.eclipse.ui.handlers.HandlerUtil;
|
---|
15 | import org.eclipse.xtext.parsetree.NodeAdapter;
|
---|
16 | import org.eclipse.xtext.parsetree.NodeUtil;
|
---|
17 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
18 |
|
---|
19 | import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
|
---|
20 |
|
---|
21 | public class SaveNodeModelXMIHandler extends AbstractHandler {
|
---|
22 |
|
---|
23 | public Object execute(ExecutionEvent event) throws ExecutionException {
|
---|
24 | try {
|
---|
25 | XtextEditor editor = (XtextEditor) HandlerUtil
|
---|
26 | .getActiveEditor(event);
|
---|
27 |
|
---|
28 | EObject root = editor.getDocument().readOnly(new RootEObjectFinder());
|
---|
29 | IProject project = editor.getResource().getProject();
|
---|
30 | IPath projectPath = project.getLocation();
|
---|
31 | String name = editor.getResource().getName();
|
---|
32 | NodeAdapter rootNode = NodeUtil.getNodeAdapter(root);
|
---|
33 | saveModel(projectPath + "//" + name + ".xmi", rootNode.getParserNode());
|
---|
34 |
|
---|
35 | project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
|
---|
36 | } catch (Exception e) {
|
---|
37 | e.printStackTrace();
|
---|
38 | }
|
---|
39 | return null;
|
---|
40 | }
|
---|
41 |
|
---|
42 | // --------------------------------------------------------------------------------
|
---|
43 |
|
---|
44 | public void saveModel(String filename, EObject project) {
|
---|
45 | try {
|
---|
46 | URI fileURI = URI.createFileURI(filename);
|
---|
47 | Resource poResource = new XMLResourceFactoryImpl()
|
---|
48 | .createResource(fileURI);
|
---|
49 | poResource.getContents().add(project);
|
---|
50 | poResource.save(null);
|
---|
51 | } catch (Exception ex) {
|
---|
52 | ex.printStackTrace();
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | }
|
---|