Loading plugins/org.etsi.mts.tdl.rt.ui/src/org/etsi/mts/tdl/tools/rt/ui/handlers/TranslationHandler.java 0 → 100644 +144 −0 Original line number Diff line number Diff line package org.etsi.mts.tdl.tools.rt.ui.handlers; import java.util.Arrays; import java.util.LinkedHashMap; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.Platform; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.window.Window; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.dialogs.ElementListSelectionDialog; import org.eclipse.ui.handlers.HandlerUtil; import org.eclipse.ui.part.FileEditorInput; import org.eclipse.xtext.resource.XtextResourceSet; import org.eclipse.xtext.ui.resource.IResourceSetProvider; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.Provider; /** * Our sample handler extends AbstractHandler, an IHandler base class. * @see org.eclipse.core.commands.IHandler * @see org.eclipse.core.commands.AbstractHandler */ public class TranslationHandler extends AbstractHandler { @Inject Injector injector; @Inject Provider<ResourceSet> rsp; @Inject Provider<XtextResourceSet> xrsp; @Inject IResourceSetProvider resourceSetProvider; LinkedHashMap<String, String> targetFormats = new LinkedHashMap<>(); public static String translationTarget = "TDL XF (Part 3, XMI)"; private IWorkbenchWindow window; /** * The constructor. */ public TranslationHandler() { init(); } private void init() { //TODO: do not reload after first init if (!targetFormats.isEmpty()) { // return; } targetFormats.clear(); targetFormats.put("TDL XF (Part 3, XMI)", "tdl"); loadTargetFormat("org.etsi.mts.tdl.TDLtxStandaloneSetup", "TDL TX (Part 8, Braces)", "tdltx"); loadTargetFormat("org.etsi.mts.tdl.TDLtxiStandaloneSetup", "TDL TX (Part 8, Indentation)", "tdltxi"); loadTargetFormat("org.etsi.mts.tdl.TDLan2StandaloneSetup", "TDLan2 (Part 1, Annex B)", "tdlan2"); loadTargetFormat("org.etsi.mts.tdl.TPLan2StandaloneSetup", "TPLan2 (Part 4, Annex B)", "tplan2"); } private void loadTargetFormat(String bundleName, String label, String extension) { if (Platform.getBundle(bundleName) != null) { targetFormats.put(label, extension); } } /** * the command has been executed, so extract extract the needed information * from the application context. */ public Object execute(ExecutionEvent event) throws ExecutionException { init(); ISelection selection = HandlerUtil.getCurrentSelection(event); IEditorInput input = HandlerUtil.getActiveEditorInput(event); IFile file = null; if (input != null && input instanceof FileEditorInput) { file = ((FileEditorInput) input).getFile(); } else if (selection !=null && selection instanceof IStructuredSelection) { IStructuredSelection structuredSelection = (IStructuredSelection) selection; Object firstElement = structuredSelection.getFirstElement(); if (firstElement instanceof IFile) { file = (IFile) firstElement; } } if (file !=null) { URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true); ResourceSet rs = new ResourceSetImpl(); Resource r = rs.getResource(uri, true); ElementListSelectionDialog dialog = new ElementListSelectionDialog(Display.getDefault().getActiveShell(), new LabelProvider()); dialog.setTitle("Translation Configuration"); dialog.setMessage("Translating "+file.getName() +"\n\nSelect the target format"); dialog.setElements(targetFormats.keySet().toArray()); dialog.setInitialElementSelections(Arrays.asList(new String[] {TranslationHandler.translationTarget})); // user pressed cancel if (dialog.open() != Window.OK) { return false; } else { injector = Guice.createInjector(); Object[] result = dialog.getResult(); String selected = (String)result[0]; TranslationHandler.translationTarget = selected; String extension = targetFormats.get(selected); URI targetURI = URI.createURI(uri.toString()+"."+extension); XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class); Resource tr = resourceSet.createResource(targetURI); tr.getContents().addAll(EcoreUtil.copyAll(r.getContents())); try { tr.save(null); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } return null; } public void init(IWorkbenchWindow window) { this.window = window; } @Override public boolean isEnabled() { return true; } } plugins/org.etsi.mts.tdl.rt.ui/src/org/etsi/mts/tdl/tools/rt/ui/handlers/TranslationHandlerLegacy.java +2 −2 Original line number Diff line number Diff line Loading @@ -36,14 +36,14 @@ import com.google.inject.Provider; * @see org.eclipse.core.commands.IHandler * @see org.eclipse.core.commands.AbstractHandler */ public class TranslationHandler extends AbstractHandler { public class TranslationHandlerLegacy extends AbstractHandler { @Inject Provider<ResourceSet> rsp; @Inject Injector injector; /** * The constructor. */ public TranslationHandler() { public TranslationHandlerLegacy() { } /** Loading Loading
plugins/org.etsi.mts.tdl.rt.ui/src/org/etsi/mts/tdl/tools/rt/ui/handlers/TranslationHandler.java 0 → 100644 +144 −0 Original line number Diff line number Diff line package org.etsi.mts.tdl.tools.rt.ui.handlers; import java.util.Arrays; import java.util.LinkedHashMap; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.Platform; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.window.Window; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.dialogs.ElementListSelectionDialog; import org.eclipse.ui.handlers.HandlerUtil; import org.eclipse.ui.part.FileEditorInput; import org.eclipse.xtext.resource.XtextResourceSet; import org.eclipse.xtext.ui.resource.IResourceSetProvider; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.Provider; /** * Our sample handler extends AbstractHandler, an IHandler base class. * @see org.eclipse.core.commands.IHandler * @see org.eclipse.core.commands.AbstractHandler */ public class TranslationHandler extends AbstractHandler { @Inject Injector injector; @Inject Provider<ResourceSet> rsp; @Inject Provider<XtextResourceSet> xrsp; @Inject IResourceSetProvider resourceSetProvider; LinkedHashMap<String, String> targetFormats = new LinkedHashMap<>(); public static String translationTarget = "TDL XF (Part 3, XMI)"; private IWorkbenchWindow window; /** * The constructor. */ public TranslationHandler() { init(); } private void init() { //TODO: do not reload after first init if (!targetFormats.isEmpty()) { // return; } targetFormats.clear(); targetFormats.put("TDL XF (Part 3, XMI)", "tdl"); loadTargetFormat("org.etsi.mts.tdl.TDLtxStandaloneSetup", "TDL TX (Part 8, Braces)", "tdltx"); loadTargetFormat("org.etsi.mts.tdl.TDLtxiStandaloneSetup", "TDL TX (Part 8, Indentation)", "tdltxi"); loadTargetFormat("org.etsi.mts.tdl.TDLan2StandaloneSetup", "TDLan2 (Part 1, Annex B)", "tdlan2"); loadTargetFormat("org.etsi.mts.tdl.TPLan2StandaloneSetup", "TPLan2 (Part 4, Annex B)", "tplan2"); } private void loadTargetFormat(String bundleName, String label, String extension) { if (Platform.getBundle(bundleName) != null) { targetFormats.put(label, extension); } } /** * the command has been executed, so extract extract the needed information * from the application context. */ public Object execute(ExecutionEvent event) throws ExecutionException { init(); ISelection selection = HandlerUtil.getCurrentSelection(event); IEditorInput input = HandlerUtil.getActiveEditorInput(event); IFile file = null; if (input != null && input instanceof FileEditorInput) { file = ((FileEditorInput) input).getFile(); } else if (selection !=null && selection instanceof IStructuredSelection) { IStructuredSelection structuredSelection = (IStructuredSelection) selection; Object firstElement = structuredSelection.getFirstElement(); if (firstElement instanceof IFile) { file = (IFile) firstElement; } } if (file !=null) { URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true); ResourceSet rs = new ResourceSetImpl(); Resource r = rs.getResource(uri, true); ElementListSelectionDialog dialog = new ElementListSelectionDialog(Display.getDefault().getActiveShell(), new LabelProvider()); dialog.setTitle("Translation Configuration"); dialog.setMessage("Translating "+file.getName() +"\n\nSelect the target format"); dialog.setElements(targetFormats.keySet().toArray()); dialog.setInitialElementSelections(Arrays.asList(new String[] {TranslationHandler.translationTarget})); // user pressed cancel if (dialog.open() != Window.OK) { return false; } else { injector = Guice.createInjector(); Object[] result = dialog.getResult(); String selected = (String)result[0]; TranslationHandler.translationTarget = selected; String extension = targetFormats.get(selected); URI targetURI = URI.createURI(uri.toString()+"."+extension); XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class); Resource tr = resourceSet.createResource(targetURI); tr.getContents().addAll(EcoreUtil.copyAll(r.getContents())); try { tr.save(null); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } return null; } public void init(IWorkbenchWindow window) { this.window = window; } @Override public boolean isEnabled() { return true; } }
plugins/org.etsi.mts.tdl.rt.ui/src/org/etsi/mts/tdl/tools/rt/ui/handlers/TranslationHandlerLegacy.java +2 −2 Original line number Diff line number Diff line Loading @@ -36,14 +36,14 @@ import com.google.inject.Provider; * @see org.eclipse.core.commands.IHandler * @see org.eclipse.core.commands.AbstractHandler */ public class TranslationHandler extends AbstractHandler { public class TranslationHandlerLegacy extends AbstractHandler { @Inject Provider<ResourceSet> rsp; @Inject Injector injector; /** * The constructor. */ public TranslationHandler() { public TranslationHandlerLegacy() { } /** Loading