source: default/trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/modelingtools/SaveXMIHandler.java @ 5

Last change on this file since 5 was 5, checked in by zeiss, 14 years ago
  • Property svn:mime-type set to text/plain
File size: 1.8 KB
Line 
1package de.ugoe.cs.swe.bnftools.ui.modelingtools;
2
3import org.eclipse.core.commands.AbstractHandler;
4import org.eclipse.core.commands.ExecutionEvent;
5import org.eclipse.core.commands.ExecutionException;
6import org.eclipse.core.resources.IProject;
7import org.eclipse.core.resources.IResource;
8import org.eclipse.core.runtime.IPath;
9import org.eclipse.core.runtime.NullProgressMonitor;
10import org.eclipse.emf.common.util.URI;
11import org.eclipse.emf.ecore.EObject;
12import org.eclipse.emf.ecore.resource.Resource;
13import org.eclipse.emf.ecore.xmi.impl.XMLResourceFactoryImpl;
14import org.eclipse.ui.handlers.HandlerUtil;
15import org.eclipse.xtext.ui.editor.XtextEditor;
16
17import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
18
19public 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}
Note: See TracBrowser for help on using the repository browser.