Index: TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/Application.java
===================================================================
--- TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/Application.java	(revision 3629)
+++ TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/Application.java	(revision 3630)
@@ -44,6 +44,7 @@
 import org.eclipse.equinox.app.IApplicationContext;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.util.Util;
+import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.osgi.util.TextProcessor;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Display;
@@ -57,12 +58,16 @@
 import org.osgi.framework.Version;
 import org.osgi.service.prefs.Preferences;
 import org.txm.Toolbox;
+import org.txm.core.engines.Engine;
+import org.txm.core.engines.EngineType;
+import org.txm.core.engines.EnginesManager;
 import org.txm.core.messages.TXMCoreMessages;
 import org.txm.core.preferences.TBXPreferences;
 import org.txm.objects.Project;
 import org.txm.objects.Workspace;
 import org.txm.rcp.commands.workspace.Load080BinaryCorpus;
 import org.txm.rcp.handlers.scripts.ExecuteImportScript;
+import org.txm.rcp.handlers.scripts.ExecuteScript;
 import org.txm.rcp.messages.TXMUIMessages;
 import org.txm.rcp.preferences.RCPPreferences;
 import org.txm.rcp.swt.dialog.CGUMessageDialog;
@@ -81,16 +86,16 @@
 public class Application implements IApplication {
 	/** The Constant PLUGIN_ID. */
 	public static final String PLUGIN_ID = "org.txm.rcp"; //$NON-NLS-1$
-
+	
 	/** The Constant FIRST_LAUNCH. */
 	public static final String FIRST_LAUNCH = "first_launch"; //$NON-NLS-1$
-
+	
 	private static ApplicationWorkbenchAdvisor awa;
-
+	
 	private static boolean headlessMode = false;
 	
 	private static boolean restMode = false;
-
+	
 	// public static OpenDocumentEventProcessor openDocProcessor = new OpenDocumentEventProcessor();
 	/*
 	 * (non-Javadoc)
@@ -98,11 +103,11 @@
 	 */
 	@Override
 	public Object start(IApplicationContext context) throws Exception {
-
+		
 		Map args = context.getArguments();
 		List<String> argsList = Arrays.asList((String[]) (args.get("application.args"))); //$NON-NLS-1$
 		System.out.println(TXMCoreMessages.bind(TXMUIMessages.argsColon, argsList));
-
+		
 		headlessMode = argsList.contains("--headless");
 		if (headlessMode) {
 			return startHeadLess(argsList);
@@ -112,15 +117,15 @@
 		if (restMode) {
 			return startRest(argsList);
 		}
-
+		
 		return startDesktop(argsList);
 	}
-
+	
 	private int startDesktop(List<String> argsList) {
 		// if not headless continue as usual for the Desktop version to start
 		Display display = PlatformUI.createDisplay();
-
-
+		
+		
 		// if (Platform.inDevelopmentMode() || Platform.inDebugMode()) {
 		// // this can not work in development environment
 		// // as it need to have a default workspace set to none
@@ -132,10 +137,10 @@
 		// } else {
 		// in Release mode only : platform workspace selection on startup
 		// then restart if needed (on workspace error...)
-
+		
 		System.out.println("instance location=" + Platform.getInstanceLocation().getURL()); //$NON-NLS-1$
 		if (Platform.getInstanceLocation().getURL() == null) {
-
+			
 			Integer ret = openCGU();
 			if (ret == IApplication.EXIT_RESTART) {
 				System.out.println("** Working directory set by user."); //$NON-NLS-1$
@@ -149,23 +154,23 @@
 				// OK
 			}
 		}
-
+		
 		// Die if platform WS not set (user cancel)
 		// ---------------------------
-
+		
 		if (Platform.getInstanceLocation().getURL() == null) {
 			System.out.println("** Working directory not set."); //$NON-NLS-1$
 			return IApplication.EXIT_OK;
 		}
-
-
+		
+		
 		// load JVM fonts in SWT env
 		if (System.getProperty("os.name").contains("Linux")) { // $NON-NLS-1$ $NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-2$
 			File javaFontsPath = new File(System.getProperty("java.home") //$NON-NLS-1$
 					+ System.getProperty("file.separator") + "lib" //$NON-NLS-1$ //$NON-NLS-2$
 					+ System.getProperty("file.separator") + "fonts" //$NON-NLS-1$ //$NON-NLS-2$
 					+ System.getProperty("file.separator")); //$NON-NLS-1$
-
+			
 			String[] fontFiles = javaFontsPath.list();
 			if (fontFiles != null) {
 				for (int i = 0; i < fontFiles.length; i++) {
@@ -178,7 +183,7 @@
 				}
 			}
 		}
-
+		
 		if (argsList.contains("-log")) { //$NON-NLS-1$
 			System.out.println(TXMUIMessages.logOptionDetected);
 			Log.setPrintInConsole(true);
@@ -188,7 +193,7 @@
 			Toolbox.setPreference(TBXPreferences.LOG_LEVEL, "ALL"); //$NON-NLS-1$
 			Toolbox.setPreference(TBXPreferences.LOG_STACKTRACE, true);
 		}
-
+		
 		if (argsList.contains("-run") //$NON-NLS-1$
 				|| System.getProperty("os.name").contains("Mac") //$NON-NLS-1$
 				|| Platform.inDevelopmentMode()) { // special case when TXM is run with Eclipse
@@ -231,22 +236,22 @@
 		else {
 			System.err.println("TXM must be launch with the argument '-run' to start."); //$NON-NLS-1$
 			System.err.println(TXMUIMessages.thisFileIsNotMeantToRunTXMPleaseConsultTXMUserManual);
-
+			
 			javax.swing.JOptionPane.showMessageDialog(null, TXMUIMessages.thisFileIsNotMeantToRunTXMPleaseConsultTXMUserManual);
 			return IApplication.EXIT_OK;
 		}
 		// test if Java 1.6 is installed
 		String version = System.getProperty("java.version"); //$NON-NLS-1$
 		Log.warning("java.version : " + version); //$NON-NLS-1$
-
+		
 		boolean exit = false;// parseCommandLine();
 		if (exit) {
 			return IApplication.EXIT_OK;
 		}
-
-
+		
+		
 		// display.addListener(SWT.OpenDocument, openDocProcessor);
-
+		
 		// //Lock the RCP workspace, thus there is only one TXM instance running
 		// Log.warning(TXMUIMessages.lockRCPWorkspace);
 		// if (!Platform.getInstanceLocation().lock()){
@@ -254,7 +259,7 @@
 		// System.out.println("TXM is already running. Exiting...");
 		// return IApplication.EXIT_OK;
 		// }
-
+		
 		try {
 			Log.warning(TXMUIMessages.activateProxyService);
 			activateProxyService();
@@ -262,19 +267,19 @@
 		catch (Exception e) {
 			Log.warning(TXMCoreMessages.bind(TXMUIMessages.couldNotStartProxyConfigurationColon, e));
 		}
-
+		
 		try {			
 			// run the application mainloop
 			awa = new ApplicationWorkbenchAdvisor();
 			System.out.println("Running TXM workbench."); //$NON-NLS-1$
-
+			
 			int returnCode = PlatformUI.createAndRunWorkbench(display, awa);
-
+			
 			if (returnCode == PlatformUI.RETURN_RESTART) {
 				System.out.println("Restarting TXM."); //$NON-NLS-1$
 				return IApplication.EXIT_RESTART;
 			}
-
+			
 			System.out.println("Exiting TXM."); //$NON-NLS-1$
 			return IApplication.EXIT_OK;
 		}
@@ -289,26 +294,26 @@
 		
 		return 0;
 	}
-
+	
 	private int startHeadLess(List<String> argsList) throws IllegalStateException, IOException {
-
+		
 		if (Toolbox.getTxmHomePath() == null || Toolbox.getTxmHomePath().isEmpty()) {
 			File txmhomedir = Platform.getLocation().toFile().getCanonicalFile();
 			TBXPreferences.getInstance().put(TBXPreferences.USER_TXM_HOME, txmhomedir.getAbsolutePath());
 		}
 		int ret = IApplication.EXIT_OK;
-
+		
 		try {
 			if (Toolbox.initialize(TBXPreferences.class, new LogMonitor())) {
-
+				
 				if (argsList.contains("--action=list")) {
 					ret = doList();
 				}
-
+				
 				if (argsList.contains("--action=new")) {
 					String path = argsList.get(argsList.size() -2);
 					File sourcePath = new File(path);
-
+					
 					String name = argsList.get(argsList.size() -1);
 					
 					ret = doNew(sourcePath, name);
@@ -325,14 +330,26 @@
 					String path = argsList.get(argsList.size() -1).trim();
 					ret = doRemove(path);
 				} else if (argsList.contains("--action=command")) {
-
+					
 					String name = argsList.get(argsList.size() -2).trim();
-
+					
 					String path = argsList.get(argsList.size() -1).trim();
-
+					
 					ret = doCommand(name, path);
+				} else if (argsList.contains("--action=groovy")) {
+					
+					
+					String path = argsList.get(argsList.size() -1).trim();
+					
+					ret = doGroovy(path, argsList);
+				} else if (argsList.contains("--action=R")) {
+					
+					
+					String path = argsList.get(argsList.size() -1).trim();
+					
+					ret = doR(path, argsList);
 				}
-
+				
 				Toolbox.workspace.saveParameters(true);
 				Toolbox.shutdown();
 			} else {
@@ -343,16 +360,43 @@
 			e.printStackTrace();
 			ret = -1;
 		}
-
+		
 		return ret;
 	}
-
+	
+	private int doGroovy(String path, List<String> argsList) {
+		
+		File script = new File(path);
+		if (!script.isFile()) return -1;
+		if (!script.exists()) return -1;
+		if (!script.getName().endsWith(".groovy")) return -1;
+		ExecuteScript.executeScript(path, null, new StructuredSelection(), argsList);
+		return 0;
+	}
+	
+	private int doR(String path, List<String> argsList) {
+		
+		File script = new File(path);
+		if (!script.isFile()) return -1;
+		if (!script.exists()) return -1;
+		if (!script.getName().endsWith(".R")) return -1;
+		EnginesManager<? extends Engine> em = Toolbox.getEngineManager(EngineType.STATS);
+		if (em == null) return -1;
+		
+		Engine e = em.getEngine("R");
+		if (e == null || !e.isRunning()) return -1;
+		
+		ExecuteScript.executeScript(path, null, new StructuredSelection(), argsList);
+		
+		return 0;
+	}
+	
 	private int doList() throws UnexpectedAnswerException, IOException, CqiServerError {
 		
 		System.out.println(Arrays.asList(CQPSearchEngine.getCqiClient().listCorpora()));
 		return IApplication.EXIT_OK;
 	}
-
+	
 	private int doExport(String path) throws IOException {
 		
 		int ret = IApplication.EXIT_OK;
@@ -372,7 +416,7 @@
 		}
 		return ret;
 	}
-
+	
 	private int doCommand(String name, String path) {
 		
 		int ret = IApplication.EXIT_OK;
@@ -387,7 +431,7 @@
 		}
 		return ret;
 	}
-
+	
 	private int doRemove(String path) {
 		
 		int ret = IApplication.EXIT_OK;
@@ -407,7 +451,7 @@
 		}
 		return ret;
 	}
-
+	
 	private int doImport(File sourcePath) throws Exception {
 		
 		int ret = IApplication.EXIT_OK;
@@ -422,17 +466,17 @@
 			ret = -1;
 		} else { // DO IT
 			Project p = Load080BinaryCorpus.loadBinaryCorpusArchive(sourcePath, new LogMonitor(), true);
-
+			
 			if (p == null) {
 				ret = -1;
 			} 
-
+			
 			System.out.println(Arrays.asList(CQPSearchEngine.getCqiClient().listCorpora()));
 			ret = 0;
 		}
 		return ret;
 	}
-
+	
 	private int doNew(File sourcePath, String name) throws InterruptedException {
 		
 		int ret = IApplication.EXIT_OK;
@@ -451,15 +495,15 @@
 			p.setDirty();
 			JobHandler job = ExecuteImportScript.executeScript(p);
 			job.join();
-
+			
 			ret = job.getResult() == Status.OK_STATUS ? IApplication.EXIT_OK:-1;
 		}
 		return ret;
 	}
-
+	
 	private Integer openCGU() {
 		try {
-
+			
 			Version v = BundleUtils.getBundleVersion("org.txm.rcp");//$NON-NLS-1$
 			if (v == null) {
 				System.out.println("Error: no 'org.txm.rcp' version found."); //$NON-NLS-1$
@@ -471,11 +515,11 @@
 				version += "-dev"; //$NON-NLS-1$
 			}
 			System.out.println("Version=" + version); //$NON-NLS-1$
-
+			
 			File txmhomedir = new File(System.getProperty("user.home"), "TXM-" + version); //$NON-NLS-1$ //$NON-NLS-2$
-
-
-
+			
+			
+			
 			// System.out.println("CGU="+ConfigurationScope.INSTANCE.getNode(IDEWorkbenchPlugin.IDE_WORKBENCH).getBoolean(RCPPreferences.CGU, false));
 			if (!ConfigurationScope.INSTANCE.getNode(IDEWorkbenchPlugin.IDE_WORKBENCH).getBoolean(RCPPreferences.CGU, false)) {
 				ChooseWorkspaceData launchData = new ChooseWorkspaceData(txmhomedir.getAbsolutePath());
@@ -485,7 +529,7 @@
 				boolean ret = (dialog.open() == MessageDialog.OK) && dialog.hasCheckBeenPressed();
 				if (ret) {
 					// TBXPreferences.getInstance().put(RCPPreferences.CGU, true);
-
+					
 					// check choosen path
 					String path = launchData.getSelection();
 					if (path != null) { // reset the letadata file
@@ -498,19 +542,19 @@
 					if (ret2 != -1) {
 						return ret2;
 					}
-
+					
 					// validate Current as default Workspace, save configuration & history
-
+					
 					launchData.writePersistedData();
 					System.out.println("save workspace location: " + launchData.getSelection()); //$NON-NLS-1$
 					ChooseWorkspaceData.setShowDialogValue(false);
-
+					
 					Preferences node = ConfigurationScope.INSTANCE.getNode(IDEWorkbenchPlugin.IDE_WORKBENCH);
 					node.putBoolean(RCPPreferences.CGU, true);
 					node.flush();
-
+					
 					return -1;
-
+					
 				}
 				else {
 					return IApplication.EXIT_OK;
@@ -519,7 +563,7 @@
 			else {
 				ChooseWorkspaceData launchData = new ChooseWorkspaceData(txmhomedir.getAbsolutePath());
 				String[] recent = launchData.getRecentWorkspaces();
-
+				
 				// If the selection dialog was not used then the workspace to use is either the
 				// most recent selection or the initialDefault (if there is no history).
 				String workspace = null;
@@ -530,7 +574,7 @@
 					workspace = launchData.getInitialDefault();
 				}
 				launchData.workspaceSelected(TextProcessor.deprocess(workspace));
-
+				
 				return setInstanceLocation(launchData);
 			}
 		}
@@ -540,14 +584,14 @@
 			return IApplication.EXIT_OK;
 		}
 	}
-
+	
 	private Integer setInstanceLocation(ChooseWorkspaceData launchData) {
 		String selWS = launchData.getSelection();
 		if (selWS == null) {
 			System.out.println("** No workspace set in configuration scope."); //$NON-NLS-1$
 			return IApplication.EXIT_OK;// user cancelled (no restart)
 		}
-
+		
 		// Switch workspace :
 		URL url = null;
 		try {
@@ -557,12 +601,12 @@
 			showWorkspaceError("Workspace path could not be found."); //$NON-NLS-1$
 			return IApplication.EXIT_OK;
 		}
-
+		
 		try {
 			if (org.eclipse.core.runtime.Platform.inDevelopmentMode()) {
 				return -1; // can't call Platform.getInstanceLocation().set(...) in dev mode
 			}
-
+			
 			if (!Platform.getInstanceLocation().set(url, true)) {
 				showWorkspaceError("TXM is already running. Exiting..."); //$NON-NLS-1$
 				ChooseWorkspaceData.setShowDialogValue(false);
@@ -582,10 +626,10 @@
 			ChooseWorkspaceData.setShowDialogValue(true);
 			return IApplication.EXIT_OK;
 		}
-
-
+		
+		
 	}
-
+	
 	// /**
 	// * Open choose workspace dialog and validate the user's choice. Returns true
 	// * if restart needed.
@@ -638,30 +682,30 @@
 	// ChooseWorkspaceData.setShowDialogValue(false);
 	// return -1;// user cancelled (no restart)
 	// }
-
+	
 	private static void showWorkspaceError(String message) {
 		MessageBox messageBox = new MessageBox(new Shell(), SWT.DIALOG_TRIM | SWT.ERROR);
 		messageBox.setText(TXMUIMessages.TXTStartingError);
 		messageBox.setMessage(message);
 		messageBox.open();
 	}
-
+	
 	public ApplicationWorkbenchAdvisor getApplicationWorkbenchAdvisor() {
 		return awa;
 	}
-
+	
 	@Override
 	public void stop() {
 		// TODO Auto-generated method stub
-
+		
 	}
-
+	
 	/**
 	 * Activates the proxy service and sets the network connection active provider as native.
 	 */
 	private void activateProxyService() {
 		String systemProxyHost = System.getProperty("http.proxyHost"); // must be called before IProxyService is used //$NON-NLS-1$
-
+		
 		Bundle bundle = Platform.getBundle("org.eclipse.ui.ide"); //$NON-NLS-1$
 		IProxyService proxyService = null;
 		if (bundle != null) {
@@ -679,7 +723,7 @@
 				System.out.println("System proxy detected. Force network provider to manual with proxy_host=" + systemProxyHost); //$NON-NLS-1$
 				proxyService.setProxiesEnabled(true);
 			}
-
+			
 			// if (Util.isMac()) { // configure only for Mac OS X
 			// // String proxy_conf = "";
 			// // ProxyConf conf = new ProxyConf(this);
@@ -706,11 +750,11 @@
 			// }
 		}
 	}
-
+	
 	public static boolean isHeadLess() {
 		return headlessMode || restMode;
 	}
-
+	
 	// /**
 	// * Parses the command line.
 	// *
