Commit afbe12e0 authored by Philip Makedonski's avatar Philip Makedonski
Browse files

+ added new TTCN-3 renderer in plain java (to be moved to separate plugins), &27

parent 100233ef
Loading
Loading
Loading
Loading
Loading
+1.07 KiB
Loading image diff...
+26 −0
Original line number Original line Diff line number Diff line
@@ -18,6 +18,11 @@
            categoryId="org.etsi.mts.tdl.tools.rt.ui.commands.category"
            categoryId="org.etsi.mts.tdl.tools.rt.ui.commands.category"
            id="org.etsi.mts.tdl.tools.rt.ui.commands.tp2tdCommand">
            id="org.etsi.mts.tdl.tools.rt.ui.commands.tp2tdCommand">
      </command>
      </command>
      <command
            name="Generate TTCN-3 code from TDL model"
            categoryId="org.etsi.mts.tdl.tools.rt.ui.commands.category"
            id="org.etsi.mts.tdl.tools.rt.ui.commands.translateTTCN3Command">
      </command>
      <command
      <command
            name="Reload OCL constraints"
            name="Reload OCL constraints"
            categoryId="org.etsi.mts.tdl.tools.rt.ui.commands.category"
            categoryId="org.etsi.mts.tdl.tools.rt.ui.commands.category"
@@ -34,6 +39,10 @@
            commandId="org.etsi.mts.tdl.tools.rt.ui.commands.tp2tdCommand"
            commandId="org.etsi.mts.tdl.tools.rt.ui.commands.tp2tdCommand"
            class="org.etsi.mts.tdl.tools.rt.ui.handlers.TP2TDHandler">
            class="org.etsi.mts.tdl.tools.rt.ui.handlers.TP2TDHandler">
      </handler>
      </handler>
      <handler
            commandId="org.etsi.mts.tdl.tools.rt.ui.commands.translateTTCN3Command"
            class="org.etsi.mts.tdl.tools.rt.ui.handlers.RendererHandler">
      </handler>
      <handler
      <handler
            commandId="org.etsi.mts.tdl.tools.rt.ui.commands.reloadValidatorCommand"
            commandId="org.etsi.mts.tdl.tools.rt.ui.commands.reloadValidatorCommand"
            class="org.etsi.mts.tdl.tools.rt.ui.handlers.ReloadValidationHandler">
            class="org.etsi.mts.tdl.tools.rt.ui.handlers.ReloadValidationHandler">
@@ -74,6 +83,17 @@
                  id="org.etsi.mts.tdl.tools.rt.ui.menus.tp2tdCommand">
                  id="org.etsi.mts.tdl.tools.rt.ui.menus.tp2tdCommand">
            </command>
            </command>
         </menu>
         </menu>
         <menu
               label="TDL"
               mnemonic="T"
               id="org.etsi.mts.tdl.tools.menus.TDLMenu">
            <command
                  commandId="org.etsi.mts.tdl.tools.rt.ui.commands.translateTTCN3Command"
                  icon="icons/TransformIcon.png"
                  mnemonic="3"
                  id="org.etsi.mts.tdl.tools.rt.ui.menus.translateTTCN3Command">
            </command>
         </menu>
         <menu
         <menu
               label="TDL"
               label="TDL"
               mnemonic="T"
               mnemonic="T"
@@ -102,6 +122,12 @@
                  tooltip="Transform TPs to TDs"
                  tooltip="Transform TPs to TDs"
                  id="org.etsi.mts.tdl.tools.rt.ui.toolbars.tp2tdCommand">
                  id="org.etsi.mts.tdl.tools.rt.ui.toolbars.tp2tdCommand">
            </command>
            </command>
            <command
                  commandId="org.etsi.mts.tdl.tools.rt.ui.commands.translateTTCN3Command"
                  icon="icons/T3Icon.png"
                  tooltip="Generate TTCN-3 code from TDL model"
                  id="org.etsi.mts.tdl.tools.rt.ui.toolbars.translateTTCN3Command">
            </command>
            <command
            <command
                  commandId="org.etsi.mts.tdl.tools.rt.ui.commands.reloadValidatorCommand"
                  commandId="org.etsi.mts.tdl.tools.rt.ui.commands.reloadValidatorCommand"
                  icon="icons/ValidateIcon.png"
                  icon="icons/ValidateIcon.png"
+748 −0

File added.

Preview size limit exceeded, changes collapsed.

+155 −0
Original line number Original line 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.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
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.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.etsi.mts.tdl.Package;
import org.etsi.mts.tdl.tools.rt.renderers.TTCN3Renderer;

import com.google.inject.Guice;
import com.google.inject.Injector;

/**
 * Our sample handler extends AbstractHandler, an IHandler base class.
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class RendererHandler extends AbstractHandler {
	LinkedHashMap<String, String> targetFormats = new LinkedHashMap<>();

	public static String translationTarget = "TTCN-3 (Part 6)";
	
	private IWorkbenchWindow window;

	/**
	 * The constructor.
	 */
	public RendererHandler() {
		init();
	}

	private void init() {
		//TODO: do not reload after first init
		if (!targetFormats.isEmpty()) {
//			return;
		}
		targetFormats.clear();
		targetFormats.put("TTCN-3 (Part 6)", "ttcn3");
//		loadTargetFormat("org.etsi.mts.tdl.tx", "TDL TX (Part 8, Braces)", "tdltx");
//		loadTargetFormat("org.etsi.mts.tdl.txi", "TDL TX (Part 8, Indentation)", "tdltxi");
//		loadTargetFormat("org.etsi.mts.tdl.TDLan2", "TDLan2 (Part 1, Annex B)", "tdlan2");
//		loadTargetFormat("org.etsi.mts.tdl.TPLan2", "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);
			Injector injector = Guice.createInjector();
			XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
//			ResourceSet rs = new ResourceSetImpl();
			XtextResourceSet rs = resourceSet;
			Resource r = rs.getResource(uri, true);
			EcoreUtil.resolveAll(r);

			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[] {RendererHandler.translationTarget}));

			// user pressed cancel
			if (dialog.open() != Window.OK) {
				return false;
			} else {
				Object[] result = dialog.getResult();
				String selected = (String)result[0];
				RendererHandler.translationTarget = selected;
				String extension = targetFormats.get(selected);
				URI targetURI = URI.createURI(uri.toString()+"."+extension);
				
				TTCN3Renderer ttcn3 = new TTCN3Renderer();

				Package p = (Package) r.getContents().get(0);
				String output = ""; 
				output += ttcn3.render(p, "  ");
				//TODO: store
//				System.out.println(output);
				
				IWorkspace workspace = ResourcesPlugin.getWorkspace();
		        IWorkspaceRoot root = workspace.getRoot();
		        IPath target = file.getFullPath().addFileExtension("ttcn");
				IFile targetFile = root.getFile(target);
				try {
					if (!targetFile.exists()) {
					targetFile.create(output.getBytes(), 0, null);
					} else {
						targetFile.setContents(output.getBytes(), true, true, null);
					}
				} catch (CoreException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
		
	public void init(IWorkbenchWindow window) {
		this.window = window;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

}