|
@@ -0,0 +1,83 @@
|
|
|
+package org.yakindu.sct.generator.ui.action;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Collections;
|
|
|
+
|
|
|
+import org.eclipse.core.resources.IFile;
|
|
|
+import org.eclipse.emf.common.util.EList;
|
|
|
+import org.eclipse.emf.common.util.URI;
|
|
|
+import org.eclipse.emf.ecore.resource.Resource;
|
|
|
+import org.eclipse.emf.ecore.resource.Resource.Factory;
|
|
|
+import org.eclipse.emf.ecore.resource.ResourceSet;
|
|
|
+import org.eclipse.emf.ecore.resource.impl.ResourceFactoryRegistryImpl;
|
|
|
+import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
|
|
|
+import org.eclipse.jface.action.IAction;
|
|
|
+import org.eclipse.jface.viewers.ISelection;
|
|
|
+import org.eclipse.jface.viewers.StructuredSelection;
|
|
|
+import org.eclipse.ui.IObjectActionDelegate;
|
|
|
+import org.eclipse.ui.IWorkbenchPart;
|
|
|
+import org.yakindu.sct.generator.core.ISCTGenerator;
|
|
|
+import org.yakindu.sct.generator.core.extensions.GeneratorExtensions;
|
|
|
+import org.yakindu.sct.generator.core.extensions.GeneratorExtensions.GeneratorDescriptor;
|
|
|
+import org.yakindu.sct.model.sgen.GeneratorEntry;
|
|
|
+import org.yakindu.sct.model.sgen.GeneratorModel;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author andreas muelder - Initial contribution and API
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class GenerateModelAction implements IObjectActionDelegate {
|
|
|
+
|
|
|
+ private ISelection selection;
|
|
|
+
|
|
|
+ public void run(IAction action) {
|
|
|
+ IFile file = unwrap();
|
|
|
+ URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(),
|
|
|
+ true);
|
|
|
+ Resource resource = loadResource(uri);
|
|
|
+ GeneratorModel model = (GeneratorModel) resource.getContents().get(0);
|
|
|
+ String generatorId = model.getGeneratorId();
|
|
|
+ GeneratorDescriptor description = GeneratorExtensions
|
|
|
+ .getGeneratorDescriptorForId(generatorId);
|
|
|
+ ISCTGenerator generator = description.createGenerator();
|
|
|
+ EList<GeneratorEntry> entries = model.getEntries();
|
|
|
+ for (GeneratorEntry generatorEntry : entries) {
|
|
|
+ generator.generate(generatorEntry);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ protected static Resource loadResource(URI uri) {
|
|
|
+ Factory factory = ResourceFactoryRegistryImpl.INSTANCE.getFactory(uri);
|
|
|
+ ResourceSet resourceSet = new ResourceSetImpl();
|
|
|
+ Resource resource = factory.createResource(uri);
|
|
|
+ resourceSet.getResources().add(resource);
|
|
|
+ try {
|
|
|
+ resource.load(Collections.emptyMap());
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return resource;
|
|
|
+ }
|
|
|
+
|
|
|
+ private IFile unwrap() {
|
|
|
+ if (selection instanceof StructuredSelection) {
|
|
|
+ Object firstElement = ((StructuredSelection) selection)
|
|
|
+ .getFirstElement();
|
|
|
+ if (firstElement instanceof IFile) {
|
|
|
+ return (IFile) firstElement;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void selectionChanged(IAction action, ISelection selection) {
|
|
|
+ this.selection = selection;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setActivePart(IAction action, IWorkbenchPart targetPart) {
|
|
|
+ }
|
|
|
+
|
|
|
+}
|