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

Last change on this file was 5, checked in by zeiss, 14 years ago
  • Property svn:mime-type set to text/plain
File size: 2.0 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.parsetree.NodeAdapter;
16import org.eclipse.xtext.parsetree.NodeUtil;
17import org.eclipse.xtext.ui.editor.XtextEditor;
18
19import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
20
21public 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}
Note: See TracBrowser for help on using the repository browser.