From 722e6e825b745e5db25156835242c3ab247c428a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mr=20Adrian=20Nor=C3=A5s?= <adrian.corma@gmail.com>
Date: Wed, 13 Jul 2011 08:25:17 +0000
Subject: [PATCH] Made changes so that when the transform command is started,
 the MOFScript Console is opened and output generated by the transformation is
 displayed.

---
 .../META-INF/MANIFEST.MF                      |   3 +-
 .../handlers/TransformationHandler.java       | 236 ++++++++++--------
 2 files changed, 132 insertions(+), 107 deletions(-)

diff --git a/transformations/org.universaal.tools.transformationcommand/META-INF/MANIFEST.MF b/transformations/org.universaal.tools.transformationcommand/META-INF/MANIFEST.MF
index 3f0b0e690..ef2f8f879 100644
--- a/transformations/org.universaal.tools.transformationcommand/META-INF/MANIFEST.MF
+++ b/transformations/org.universaal.tools.transformationcommand/META-INF/MANIFEST.MF
@@ -11,7 +11,8 @@ Require-Bundle: org.eclipse.ui,
  org.eclipse.mofscript.parser;bundle-version="1.3.0",
  org.eclipse.mofscript.runtime;bundle-version="1.3.0",
  org.eclipse.mofscript.model;bundle-version="1.3.0",
- org.eclipse.core.runtime;bundle-version="3.4.0"
+ org.eclipse.core.runtime;bundle-version="3.4.0",
+ org.eclipse.ui.console;bundle-version="3.5.100"
 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 Import-Package: org.osgi.framework;version="1.4.0"
 Bundle-ActivationPolicy: lazy
diff --git a/transformations/org.universaal.tools.transformationcommand/src/org/istmusic/tools/transformationcommand/handlers/TransformationHandler.java b/transformations/org.universaal.tools.transformationcommand/src/org/istmusic/tools/transformationcommand/handlers/TransformationHandler.java
index 495a82095..95a3eb230 100644
--- a/transformations/org.universaal.tools.transformationcommand/src/org/istmusic/tools/transformationcommand/handlers/TransformationHandler.java
+++ b/transformations/org.universaal.tools.transformationcommand/src/org/istmusic/tools/transformationcommand/handlers/TransformationHandler.java
@@ -24,7 +24,6 @@ import org.eclipse.emf.ecore.resource.ResourceSet;
 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
 import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
 import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.mofscript.MOFScriptModel.MOFScriptSpecification;
@@ -34,9 +33,12 @@ import org.eclipse.mofscript.runtime.ExecutionManager;
 import org.eclipse.mofscript.runtime.ExecutionMessageListener;
 import org.eclipse.mofscript.runtime.MofScriptExecutionException;
 import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.console.ConsolePlugin;
+import org.eclipse.ui.console.IConsole;
+import org.eclipse.ui.console.IConsoleManager;
+import org.eclipse.ui.console.MessageConsole;
+import org.eclipse.ui.console.MessageConsoleStream;
 import org.eclipse.ui.handlers.HandlerUtil;
-import org.istmusic.tools.transformationcommand.activator.Activator;
-import org.istmusic.tools.transformationcommand.preferences.PreferenceConstants;
 
 
 /**
@@ -47,24 +49,27 @@ import org.istmusic.tools.transformationcommand.preferences.PreferenceConstants;
 public abstract class TransformationHandler extends AbstractHandler implements ExecutionMessageListener {
 	String transformationFileName;
 	String thisBundleName;
-	
-	
+	private MessageConsole myConsole;
+	private MessageConsoleStream stream;
+
+
 	/**
 	 * Finally, I am subversive
 	 */
 	public void setFileAndBundleName(String theTransformationFile, String theBundle) {
 		transformationFileName = theTransformationFile;
 		thisBundleName = theBundle;	
-		
+
 	}	
-	
-	
+
+
 
 	/**
 	 * the command has been executed, so extract extract the needed information
 	 * from the application context.
 	 */
 	public Object execute(ExecutionEvent event) throws ExecutionException {
+
 		// First, retrieve the current selection and check whether it is a file
 		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
 		ISelection selection = window.getSelectionService().getSelection();
@@ -79,18 +84,20 @@ public abstract class TransformationHandler extends AbstractHandler implements E
 			MessageDialog.openInformation(
 					window.getShell(),
 					"Transformation Command",
-			"The selection is not a valid source for transformation" );			
+					"The selection is not a valid source for transformation" );			
 		}		
 
 		return null;
 	}
-	
-	
+
+
 	public void doTransform(IFile inputFile, ExecutionEvent event) {
-		
+		myConsole = findConsole("MOFScript2 Console");
+		stream = myConsole.newMessageStream();
+
 		IPath path = new Path(transformationFileName);
 		URL l = FileLocator.find(Platform.getBundle(thisBundleName), path, null);
-		
+
 		try {
 			l = FileLocator.toFileURL(l);
 		} catch (IOException e) {
@@ -101,113 +108,117 @@ public abstract class TransformationHandler extends AbstractHandler implements E
 			System.out.print("Running transformation script: ");
 			System.out.println(l);
 		}
-		
+
 		ParserUtil parserUtil = new ParserUtil();
-        ExecutionManager execMgr = ExecutionManager.getExecutionManager();       
-        //
-        // The parserutil parses and sets the input transformation model
-        // for the execution manager.
-        //
-        
-        File f = null;        
+		ExecutionManager execMgr = ExecutionManager.getExecutionManager();       
+		//
+		// The parserutil parses and sets the input transformation model
+		// for the execution manager.
+		//
+
+		File f = null;        
 		try {
 			f = new File (l.toURI());
 		} catch (URISyntaxException e) {
 			System.out.println("Could not find URI for transformation script");
 			return;
 		}
-		
-        MOFScriptSpecification spec = parserUtil.parse(f, true);
-        // check for errors:
-        int errorCount = ParserUtil.getModelChecker().getErrorCount();
-        Iterator errorIt = ParserUtil.getModelChecker().getErrors(); // Iterator of MofScriptParseError objects
-       
-        System.out.println ("Preparing transformation...");       
-        if (errorCount > 0) {
-
-        	System.out.println ("Error parsing transformation: " + errorCount + " errors");       
-           
-            for (;errorIt.hasNext();) {
-                MofScriptParseError parseError = (MofScriptParseError) errorIt.next();
-                System.out.println("\t \t: Error: " + parseError.toString());
-            }           
-            return;           
-        }
-       
-       
-       
-        // load source model
-        XMIResourceFactoryImpl _xmiFac = new XMIResourceFactoryImpl();         
-        EObject sourceModel = null;
-//        File sourceModelFile = new File(selectedFile.getLocationURI()); // new File ("SM.ecore");       
-        ResourceSet rSet = new ResourceSetImpl ();
-        rSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", _xmiFac);
-        
-        //System.out.println("Converting URI for selected file: " + inputFile.getLocationURI().toString());
-        
-        URI uri = null;
-        try {
-        	uri = URI.createURI(inputFile.getLocationURI().toString());
-        }
-        catch (Exception ex) {
-        	ex.printStackTrace();
-        	return;
-        }
-        System.out.println("Converted URI for selected file");
-
-        Resource resource = rSet.getResource(uri, true);
-      
-        if (resource != null) {
-            if (resource.getContents().size() > 0) {
-                sourceModel = (EObject) resource.getContents().get(0);
-            }
-        }       
-       
-        if (sourceModel == null) {
-            System.out.println("Source model could not be located");
-        	return;
-        }
-        System.out.println("Adding source model");
-        
-        // set the source model for the execution manager   
-        execMgr.addSourceModel(sourceModel);
-        
-        // sets the root output directory, if any is desired (e.g. "c:/temp")
-        IProject project = inputFile.getProject();
-        execMgr.setRootDirectory(findDirectory(project));
 
+		MOFScriptSpecification spec = parserUtil.parse(f, true);
+		// check for errors:
+		int errorCount = ParserUtil.getModelChecker().getErrorCount();
+		Iterator errorIt = ParserUtil.getModelChecker().getErrors(); // Iterator of MofScriptParseError objects
+
+		System.out.println ("Preparing transformation...");       
+		if (errorCount > 0) {
+
+			System.out.println ("Error parsing transformation: " + errorCount + " errors");       
+
+			for (;errorIt.hasNext();) {
+				MofScriptParseError parseError = (MofScriptParseError) errorIt.next();
+				System.out.println("\t \t: Error: " + parseError.toString());
+			}           
+			return;           
+		}
+
+
+
+		// load source model
+		XMIResourceFactoryImpl _xmiFac = new XMIResourceFactoryImpl();         
+		EObject sourceModel = null;
+		//        File sourceModelFile = new File(selectedFile.getLocationURI()); // new File ("SM.ecore");       
+		ResourceSet rSet = new ResourceSetImpl ();
+		rSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", _xmiFac);
+
+		//System.out.println("Converting URI for selected file: " + inputFile.getLocationURI().toString());
+
+		URI uri = null;
+		try {
+			uri = URI.createURI(inputFile.getLocationURI().toString());
+		}
+		catch (Exception ex) {
+			ex.printStackTrace();
+			return;
+		}
+		System.out.println("Converted URI for selected file");
+
+		Resource resource = rSet.getResource(uri, true);
+
+		if (resource != null) {
+			if (resource.getContents().size() > 0) {
+				sourceModel = (EObject) resource.getContents().get(0);
+			}
+		}       
+
+		if (sourceModel == null) {
+			System.out.println("Source model could not be located");
+			return;
+		}
+		System.out.println("Adding source model");
         
-        // if true, files are not generated to the file system, but populated into a filemodel
-        // which can be fetched afterwards. Value false will result in standard file generation
-        execMgr.setUseFileModel(false);
-        // Turns on/off system logging
-        execMgr.setUseLog(false);
-        
-        
-        execMgr.setBlockCommentTag("//");
-        
-        // Adds an output listener for the transformation execution.
-        execMgr.getExecutionStack().addOutputMessageListener(this);   
-        try {
-            System.out.println("Performing transformation");
-
-            execMgr.executeTransformation();           
-            System.out.println("Completed transformation");
-            //New code
-            project.refreshLocal(IProject.DEPTH_INFINITE, new NullProgressMonitor());
-        } catch (MofScriptExecutionException mex) {
-            mex.printStackTrace();
-        } catch (CoreException e){
-        	e.printStackTrace();
-        }
+		// set the source model for the execution manager   
+		execMgr.addSourceModel(sourceModel);
+
+		// sets the root output directory, if any is desired (e.g. "c:/temp")
+		IProject project = inputFile.getProject();
+		execMgr.setRootDirectory(findDirectory(project));
+
+		
+		// if true, files are not generated to the file system, but populated into a filemodel
+		// which can be fetched afterwards. Value false will result in standard file generation
+		execMgr.setUseFileModel(false);
+		// Turns on/off system logging
+		execMgr.setUseLog(false);
+		
+		
+		execMgr.setBlockCommentTag("//");
+		
+		// Adds an output listener for the transformation execution.
+		execMgr.getExecutionStack().addOutputMessageListener(this);   
+		try {
+			System.out.println("Performing transformation");
+
+			execMgr.executeTransformation();           
+			System.out.println("Completed transformation");
+			//New code
+			project.refreshLocal(IProject.DEPTH_INFINITE, new NullProgressMonitor());
+		} catch (MofScriptExecutionException mex) {
+			mex.printStackTrace();
+		} catch (CoreException e){
+			e.printStackTrace();
+		}     
 	}
 
 	@Override
 	public void executionMessage(String arg0, String arg1) {
 		// Ignore messages from MOFscript for now
-        System.out.println(arg1);		
+//		System.out.println(arg1);		
+		if (arg0 == null || arg0.equals("") || arg0.equals("println"))
+			stream.println(arg1);
+		else if (arg0.equalsIgnoreCase("print"))
+			stream.print(arg1);
 	}
-	
+
 	/**
 	 * Reads preferences and finds the correct directory to save files to.
 	 * @param inputFile
@@ -225,7 +236,20 @@ public abstract class TransformationHandler extends AbstractHandler implements E
 		}
 
 	}
-	
+
 	protected abstract String getDirectoryFromPreferences();
 	protected abstract boolean getAbsolutePathBooleanFromPreferences();
+	
+	private MessageConsole findConsole(String name) {
+	      ConsolePlugin plugin = ConsolePlugin.getDefault();
+	      IConsoleManager conMan = plugin.getConsoleManager();
+	      IConsole[] existing = conMan.getConsoles();
+	      for (int i = 0; i < existing.length; i++)
+	         if (name.equals(existing[i].getName()))
+	            return (MessageConsole) existing[i];
+	      //no console found, so create a new one
+	      MessageConsole myConsole = new MessageConsole(name, null);
+	      conMan.addConsoles(new IConsole[]{myConsole});
+	      return myConsole;
+	   }
 }
-- 
GitLab