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.ui.editor.XtextEditor;
|
---|
16 |
|
---|
17 | import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
|
---|
18 |
|
---|
19 | public class SaveXMIHandler extends AbstractHandler {
|
---|
20 |
|
---|
21 | public Object execute(ExecutionEvent event) throws ExecutionException {
|
---|
22 | try {
|
---|
23 | XtextEditor editor = (XtextEditor) HandlerUtil
|
---|
24 | .getActiveEditor(event);
|
---|
25 |
|
---|
26 | EObject root = editor.getDocument().readOnly(new RootEObjectFinder());
|
---|
27 | IProject project = editor.getResource().getProject();
|
---|
28 | IPath projectPath = project.getLocation();
|
---|
29 | String name = editor.getResource().getName();
|
---|
30 |
|
---|
31 | saveModel(projectPath + "//" + name + ".xmi", root);
|
---|
32 | project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
|
---|
33 | } catch (Exception e) {
|
---|
34 | e.printStackTrace();
|
---|
35 | }
|
---|
36 | return null;
|
---|
37 | }
|
---|
38 |
|
---|
39 | // --------------------------------------------------------------------------------
|
---|
40 |
|
---|
41 | public void saveModel(String filename, EObject project) {
|
---|
42 | try {
|
---|
43 | URI fileURI = URI.createFileURI(filename);
|
---|
44 | Resource poResource = new XMLResourceFactoryImpl()
|
---|
45 | .createResource(fileURI);
|
---|
46 | poResource.getContents().add(project);
|
---|
47 | poResource.save(null);
|
---|
48 | } catch (Exception ex) {
|
---|
49 | ex.printStackTrace();
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | }
|
---|