source: default/v2/trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/handler/GenerationHandler.java @ 72

Last change on this file since 72 was 72, checked in by hkaulbersch, 10 years ago

Automatic PDF Generation from .fo files now works

  • Property svn:mime-type set to text/plain
File size: 4.6 KB
Line 
1package de.ugoe.cs.swe.bnftools.ui.handler;
2
3import java.util.Iterator;
4import java.util.Map;
5import java.util.Map.Entry;
6
7import org.eclipse.core.commands.AbstractHandler;
8import org.eclipse.core.commands.ExecutionEvent;
9import org.eclipse.core.commands.ExecutionException;
10import org.eclipse.core.commands.IHandler;
11import org.eclipse.core.commands.common.NotDefinedException;
12import org.eclipse.core.resources.IFile;
13import org.eclipse.core.resources.IFolder;
14import org.eclipse.core.resources.IProject;
15import org.eclipse.core.runtime.CoreException;
16import org.eclipse.core.runtime.NullProgressMonitor;
17import org.eclipse.emf.common.util.URI;
18import org.eclipse.emf.ecore.EcorePackage;
19import org.eclipse.emf.ecore.resource.Resource;
20import org.eclipse.emf.ecore.resource.ResourceSet;
21import org.eclipse.emf.ecore.xml.type.SimpleAnyType;
22import org.eclipse.emf.ecore.xml.type.XMLTypeFactory;
23import org.eclipse.jface.viewers.ISelection;
24import org.eclipse.jface.viewers.IStructuredSelection;
25import org.eclipse.ui.handlers.HandlerUtil;
26import org.eclipse.xtext.builder.EclipseResourceFileSystemAccess2;
27import org.eclipse.xtext.generator.IGenerator;
28import org.eclipse.xtext.generator.OutputConfiguration;
29import org.eclipse.xtext.resource.IResourceDescriptions;
30import org.eclipse.xtext.ui.resource.IResourceSetProvider;
31
32import com.google.inject.Inject;
33import com.google.inject.Provider;
34
35public class GenerationHandler extends AbstractHandler implements IHandler {
36   
37    @Inject
38    private IGenerator generator;
39 
40    @Inject
41    private Provider<EclipseResourceFileSystemAccess2> fileAccessProvider;
42     
43    @Inject
44    IResourceDescriptions resourceDescriptions;
45     
46    @Inject
47    IResourceSetProvider resourceSetProvider;
48     
49    @Override
50    public Object execute(ExecutionEvent event) throws ExecutionException {
51        String mode = "";
52 
53        try {
54                        mode = event.getCommand().getName();
55                } catch (NotDefinedException e1) {
56                        // catch block
57                        e1.printStackTrace();
58                }
59                 
60
61         
62        ISelection selection = HandlerUtil.getCurrentSelection(event);
63        if (selection instanceof IStructuredSelection) {
64            IStructuredSelection structuredSelection = (IStructuredSelection) selection;
65            Object firstElement = structuredSelection.getFirstElement();
66            if (firstElement instanceof IFile) {
67                IFile file = (IFile) firstElement;
68                IProject project = file.getProject();
69                IFolder srcGenFolder = project.getFolder("src-gen");
70                if (!srcGenFolder.exists()) {
71                    try {
72                        srcGenFolder.create(true, true,
73                                new NullProgressMonitor());
74                    } catch (CoreException e) {
75                        return null;
76                    }
77                }
78 
79                final EclipseResourceFileSystemAccess2 fsa = fileAccessProvider.get();
80                fsa.setOutputPath(srcGenFolder.getFullPath().toString());
81               
82                //complete the filesystemaccess
83                fsa.setMonitor(new NullProgressMonitor());
84                Map<String, OutputConfiguration> teste = fsa.getOutputConfigurations();
85                       
86                Iterator<Entry<String, OutputConfiguration>> it = teste.entrySet().iterator();
87               
88                while(it.hasNext()){
89                         
90                    Entry<String, OutputConfiguration> next = it.next();
91                    OutputConfiguration out = next.getValue();
92                    out.isOverrideExistingResources();
93                    out.setCreateOutputDirectory(true); // <--- do not touch this
94                 
95                  }
96                  // ----->
97               
98                URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
99                ResourceSet rs = resourceSetProvider.get(project);
100                Resource r = rs.getResource(uri, true);
101               
102                // to pass a String inside a resource i have to wrap it in a EOBject
103                SimpleAnyType wrapper = XMLTypeFactory.eINSTANCE.createSimpleAnyType();
104                wrapper.setInstanceType(EcorePackage.eINSTANCE.getEString());
105                wrapper.setValue(mode);
106                //
107               
108                // add string to resource
109                r.getContents().add(wrapper);
110           
111                generator.doGenerate(r, fsa);
112                 
113               
114            }
115        }
116        return null;
117    }
118 
119    @Override
120    public boolean isEnabled() {
121        return true;
122    }
123 
124}
Note: See TracBrowser for help on using the repository browser.