1 | package de.ugoe.cs.swe.bnftools.ui.handler;
|
---|
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.commands.IHandler;
|
---|
7 | import org.eclipse.core.resources.IFile;
|
---|
8 | import org.eclipse.core.resources.IFolder;
|
---|
9 | import org.eclipse.core.resources.IProject;
|
---|
10 | import org.eclipse.core.runtime.CoreException;
|
---|
11 | import org.eclipse.core.runtime.NullProgressMonitor;
|
---|
12 | import org.eclipse.emf.common.util.URI;
|
---|
13 | import org.eclipse.emf.ecore.resource.Resource;
|
---|
14 | import org.eclipse.emf.ecore.resource.ResourceSet;
|
---|
15 | import org.eclipse.jface.viewers.ISelection;
|
---|
16 | import org.eclipse.jface.viewers.IStructuredSelection;
|
---|
17 | import org.eclipse.ui.handlers.HandlerUtil;
|
---|
18 | import org.eclipse.xtext.builder.EclipseResourceFileSystemAccess;
|
---|
19 | import org.eclipse.xtext.generator.IGenerator;
|
---|
20 | import org.eclipse.xtext.resource.IResourceDescriptions;
|
---|
21 | import org.eclipse.xtext.ui.resource.IResourceSetProvider;
|
---|
22 |
|
---|
23 | import com.google.inject.Inject;
|
---|
24 | import com.google.inject.Provider;
|
---|
25 |
|
---|
26 | public class GenerationHandler extends AbstractHandler implements IHandler {
|
---|
27 |
|
---|
28 | @Inject
|
---|
29 | private IGenerator generator;
|
---|
30 |
|
---|
31 | @Inject
|
---|
32 | private Provider<EclipseResourceFileSystemAccess> fileAccessProvider;
|
---|
33 |
|
---|
34 | @Inject
|
---|
35 | IResourceDescriptions resourceDescriptions;
|
---|
36 |
|
---|
37 | @Inject
|
---|
38 | IResourceSetProvider resourceSetProvider;
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public Object execute(ExecutionEvent event) throws ExecutionException {
|
---|
42 |
|
---|
43 | ISelection selection = HandlerUtil.getCurrentSelection(event);
|
---|
44 | if (selection instanceof IStructuredSelection) {
|
---|
45 | IStructuredSelection structuredSelection = (IStructuredSelection) selection;
|
---|
46 | Object firstElement = structuredSelection.getFirstElement();
|
---|
47 | if (firstElement instanceof IFile) {
|
---|
48 | IFile file = (IFile) firstElement;
|
---|
49 | IProject project = file.getProject();
|
---|
50 | IFolder srcGenFolder = project.getFolder("src-gen");
|
---|
51 | if (!srcGenFolder.exists()) {
|
---|
52 | try {
|
---|
53 | srcGenFolder.create(true, true,
|
---|
54 | new NullProgressMonitor());
|
---|
55 | } catch (CoreException e) {
|
---|
56 | return null;
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | final EclipseResourceFileSystemAccess fsa = fileAccessProvider.get();
|
---|
61 | fsa.setOutputPath(srcGenFolder.getFullPath().toString());
|
---|
62 |
|
---|
63 | URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
|
---|
64 | ResourceSet rs = resourceSetProvider.get(project);
|
---|
65 | Resource r = rs.getResource(uri, true);
|
---|
66 | generator.doGenerate(r, fsa);
|
---|
67 |
|
---|
68 |
|
---|
69 | }
|
---|
70 | }
|
---|
71 | return null;
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public boolean isEnabled() {
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 |
|
---|
79 | } |
---|