1 | package de.ugoe.cs.swe.bnftools.ui.handler;
|
---|
2 |
|
---|
3 | import java.util.Iterator;
|
---|
4 | import java.util.Map;
|
---|
5 | import java.util.Map.Entry;
|
---|
6 |
|
---|
7 | import org.eclipse.core.commands.AbstractHandler;
|
---|
8 | import org.eclipse.core.commands.ExecutionEvent;
|
---|
9 | import org.eclipse.core.commands.ExecutionException;
|
---|
10 | import org.eclipse.core.commands.IHandler;
|
---|
11 | import org.eclipse.core.commands.common.NotDefinedException;
|
---|
12 | import org.eclipse.core.resources.IFile;
|
---|
13 | import org.eclipse.core.resources.IFolder;
|
---|
14 | import org.eclipse.core.resources.IProject;
|
---|
15 | import org.eclipse.core.runtime.CoreException;
|
---|
16 | import org.eclipse.core.runtime.NullProgressMonitor;
|
---|
17 | import org.eclipse.emf.common.util.URI;
|
---|
18 | import org.eclipse.emf.ecore.EcorePackage;
|
---|
19 | import org.eclipse.emf.ecore.resource.Resource;
|
---|
20 | import org.eclipse.emf.ecore.resource.ResourceSet;
|
---|
21 | import org.eclipse.emf.ecore.xml.type.SimpleAnyType;
|
---|
22 | import org.eclipse.emf.ecore.xml.type.XMLTypeFactory;
|
---|
23 | import org.eclipse.jface.viewers.ISelection;
|
---|
24 | import org.eclipse.jface.viewers.IStructuredSelection;
|
---|
25 | import org.eclipse.ui.handlers.HandlerUtil;
|
---|
26 | import org.eclipse.xtext.builder.EclipseResourceFileSystemAccess2;
|
---|
27 | import org.eclipse.xtext.generator.IGenerator;
|
---|
28 | import org.eclipse.xtext.generator.OutputConfiguration;
|
---|
29 | import org.eclipse.xtext.resource.IResourceDescriptions;
|
---|
30 | import org.eclipse.xtext.ui.resource.IResourceSetProvider;
|
---|
31 |
|
---|
32 | import com.google.inject.Inject;
|
---|
33 | import com.google.inject.Provider;
|
---|
34 |
|
---|
35 | public 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 | } |
---|