Révision 669

tmp/org.txm.rcp/src/main/java/org/txm/rcp/ApplicationWorkbenchAdvisor.java (revision 669)
91 91
import org.txm.importer.Convert5To6;
92 92
import org.txm.objects.Base;
93 93
import org.txm.objects.Workspace;
94
import org.txm.rcp.commands.ExecuteGroovyScript;
95 94
import org.txm.rcp.commands.OpenBrowser;
96 95
import org.txm.rcp.commands.OpenWelcomePage;
97 96
import org.txm.rcp.commands.RestartTXM;
98
import org.txm.rcp.commands.base.DeleteObject;
99 97
import org.txm.rcp.commands.workspace.LoadBinaryCorpus;
98
import org.txm.rcp.handlers.results.DeleteObject;
99
import org.txm.rcp.handlers.scripts.ExecuteGroovyScript;
100 100
import org.txm.rcp.p2.plugins.TXMUpdateHandler;
101 101
import org.txm.rcp.perspective.TXMPerspective;
102 102
import org.txm.rcp.preferences.RCPPreferences;
tmp/org.txm.rcp/src/main/java/org/txm/rcp/views/fileexplorer/Explorer.java (revision 669)
57 57
import org.txm.Toolbox;
58 58
import org.txm.core.preferences.TBXPreferences;
59 59
import org.txm.rcp.RCPMessages;
60
import org.txm.rcp.commands.CopyFile;
61
import org.txm.rcp.commands.CutFile;
62
import org.txm.rcp.commands.DeleteFile;
63
import org.txm.rcp.commands.EditFile;
64 60
import org.txm.rcp.commands.OpenBrowser;
65 61
import org.txm.rcp.commands.OpenGraph;
66
import org.txm.rcp.commands.PasteFile;
67
import org.txm.rcp.commands.RenameFile;
62
import org.txm.rcp.handlers.files.CopyFile;
63
import org.txm.rcp.handlers.files.CutFile;
64
import org.txm.rcp.handlers.files.DeleteFile;
65
import org.txm.rcp.handlers.files.EditFile;
66
import org.txm.rcp.handlers.files.PasteFile;
67
import org.txm.rcp.handlers.files.RenameFile;
68 68
// TODO: Auto-generated Javadoc
69 69
/**
70 70
 * Simple file explorer @ author mdecorde.
tmp/org.txm.rcp/src/main/java/org/txm/rcp/views/fileexplorer/MacroExplorer.java (revision 669)
61 61
import org.txm.core.preferences.TBXPreferences;
62 62
import org.txm.rcp.RCPMessages;
63 63
import org.txm.rcp.TXMWindows;
64
import org.txm.rcp.commands.CopyFile;
65
import org.txm.rcp.commands.CreateMacro;
66
import org.txm.rcp.commands.CutFile;
67
import org.txm.rcp.commands.DeleteFile;
68
import org.txm.rcp.commands.ExecuteGroovyMacro;
69
import org.txm.rcp.commands.PasteFile;
70
import org.txm.rcp.commands.RenameFile;
64
import org.txm.rcp.handlers.files.CopyFile;
65
import org.txm.rcp.handlers.files.CutFile;
66
import org.txm.rcp.handlers.files.DeleteFile;
67
import org.txm.rcp.handlers.files.PasteFile;
68
import org.txm.rcp.handlers.files.RenameFile;
69
import org.txm.rcp.handlers.scripts.CreateMacro;
70
import org.txm.rcp.handlers.scripts.ExecuteGroovyMacro;
71 71
// TODO: Auto-generated Javadoc
72 72
/**
73 73
 * Simple file explorer @ author mdecorde.
tmp/org.txm.rcp/src/main/java/org/txm/rcp/views/corpora/CorporaView.java (revision 669)
73 73
import org.txm.rcp.RCPMessages;
74 74
import org.txm.rcp.StatusLine;
75 75
import org.txm.rcp.TXMWindows;
76
import org.txm.rcp.commands.base.DeleteObject;
77 76
import org.txm.rcp.editors.TXMEditor;
78 77
import org.txm.rcp.editors.TXMResultEditorInput;
78
import org.txm.rcp.handlers.results.DeleteObject;
79 79
import org.txm.rcp.utils.JobHandler;
80 80
import org.txm.searchengine.cqp.corpus.Corpus;
81 81
import org.txm.utils.logger.Log;
tmp/org.txm.rcp/src/main/java/org/txm/rcp/views/BasesView.java (revision 669)
62 62
import org.txm.objects.Workspace;
63 63
import org.txm.rcp.Application;
64 64
import org.txm.rcp.IImageKeys;
65
import org.txm.rcp.commands.EditFile;
66 65
import org.txm.rcp.commands.OpenBrowser;
67
import org.txm.rcp.commands.base.DeleteObject;
68 66
import org.txm.rcp.editors.TXMBrowser;
67
import org.txm.rcp.handlers.files.EditFile;
68
import org.txm.rcp.handlers.results.DeleteObject;
69 69
import org.txm.searchengine.cqp.corpus.Corpus;
70 70
import org.txm.searchengine.cqp.corpus.MainCorpus;
71 71

  
tmp/org.txm.rcp/src/main/java/org/txm/rcp/views/QueriesView.java (revision 669)
69 69
import org.txm.objects.TxmObject;
70 70
import org.txm.rcp.IImageKeys;
71 71
import org.txm.rcp.RCPMessages;
72
import org.txm.rcp.commands.EditFile;
73
import org.txm.rcp.commands.base.DeleteObject;
72
import org.txm.rcp.handlers.files.EditFile;
73
import org.txm.rcp.handlers.results.DeleteObject;
74 74
import org.txm.rcp.swt.dialog.LastOpened;
75 75
import org.txm.searchengine.cqp.clientExceptions.CqiClientException;
76 76
import org.txm.searchengine.cqp.corpus.CorpusManager;
tmp/org.txm.rcp/src/main/java/org/txm/rcp/actions/ImportDialog.java (revision 669)
54 54
import org.txm.core.preferences.TXMPreferences;
55 55
import org.txm.rcp.RCPMessages;
56 56
import org.txm.rcp.TxmPreferences;
57
import org.txm.rcp.commands.EditFile;
57
import org.txm.rcp.handlers.files.EditFile;
58 58
import org.txm.rcp.swt.dialog.LastOpened;
59 59
import org.txm.rcp.swt.dialog.TXMMessageBox;
60 60
// TODO: Auto-generated Javadoc
tmp/org.txm.rcp/src/main/java/org/txm/rcp/handlers/ComputeTXMResult.java (revision 669)
1
package org.txm.rcp.handlers;
2

  
3
import org.eclipse.core.commands.AbstractHandler;
4
import org.eclipse.core.commands.ExecutionEvent;
5
import org.eclipse.core.commands.ExecutionException;
6
import org.eclipse.ui.IWorkbenchPart;
7
import org.eclipse.ui.handlers.HandlerUtil;
8
import org.txm.rcp.editors.TXMEditor;
9

  
10
public class ComputeTXMResult extends AbstractHandler {
11

  
12
	@Override
13
	public Object execute(ExecutionEvent event) throws ExecutionException {
14
		IWorkbenchPart part = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getActivePart();
15
		if (!(part instanceof TXMEditor)) {
16
			System.out.println("Active editor is not a TXMEditor. Aborting.");
17
			return null;
18
		}
19

  
20
		TXMEditor editor = (TXMEditor) part;
21
		editor.compute(false);
22

  
23
		return null;
24
	}
25
}
tmp/org.txm.rcp/src/main/java/org/txm/rcp/handlers/CloneTXMResult.java (revision 669)
1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
// 
22
// 
23
// 
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$ 
27
//
28
package org.txm.rcp.handlers;
29

  
30
import org.eclipse.core.commands.AbstractHandler;
31
import org.eclipse.core.commands.ExecutionEvent;
32
import org.eclipse.core.commands.ExecutionException;
33
import org.eclipse.jface.viewers.IStructuredSelection;
34
import org.eclipse.ui.handlers.HandlerUtil;
35
import org.txm.core.results.TXMResult;
36
import org.txm.rcp.views.corpora.CorporaView;
37

  
38
/**
39
 * Clone the selected TXM result node.
40
 * 
41
 * @author sjacquot
42
 * 
43
 */
44
public class CloneTXMResult extends AbstractHandler {
45

  
46

  
47
	
48
	/* (non-Javadoc)
49
	 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
50
	 */
51
	@Override
52
	public Object execute(ExecutionEvent event) throws ExecutionException {
53

  
54
		System.out.println("CloneTXMResult.execute(): clone tests.");
55
		IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
56
		System.out.println("CloneTXMResult.execute() selection class: " + selection);
57
		if(selection.getFirstElement() instanceof TXMResult)	{
58
			TXMResult srcNode =  ((TXMResult) selection.getFirstElement());
59
			TXMResult clone = (TXMResult) srcNode.clone();
60
			
61
			// FIXME: hierarchy tests
62
			//srcNode.addChild(clone);
63
		}
64
		
65
		CorporaView.refresh();
66

  
67
		return null;
68
	}
69
	
70
}
tmp/org.txm.rcp/src/main/java/org/txm/rcp/handlers/scripts/ExecuteScript.java (revision 669)
1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
// 
22
// 
23
// 
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$ 
27
//
28
package org.txm.rcp.handlers.scripts;
29

  
30
import java.io.File;
31
import java.util.Iterator;
32

  
33
import org.eclipse.core.commands.AbstractHandler;
34
import org.eclipse.core.commands.ExecutionEvent;
35
import org.eclipse.core.commands.ExecutionException;
36
import org.eclipse.jface.dialogs.MessageDialog;
37
import org.eclipse.jface.viewers.ISelection;
38
import org.eclipse.jface.viewers.IStructuredSelection;
39
import org.eclipse.swt.SWT;
40
import org.eclipse.swt.events.SelectionEvent;
41
import org.eclipse.swt.events.SelectionListener;
42
import org.eclipse.swt.widgets.Button;
43
import org.eclipse.swt.widgets.Composite;
44
import org.eclipse.swt.widgets.Control;
45
import org.eclipse.swt.widgets.FileDialog;
46
import org.eclipse.swt.widgets.Shell;
47
import org.eclipse.ui.IWorkbenchPart;
48
import org.eclipse.ui.handlers.HandlerUtil;
49
import org.eclipse.ui.ide.FileStoreEditorInput;
50
import org.txm.core.preferences.TBXPreferences;
51
import org.txm.core.preferences.TXMPreferences;
52
import org.txm.rcp.RCPMessages;
53
import org.txm.rcp.editors.TxtEditor;
54
import org.txm.rcp.preferences.RCPPreferences;
55
import org.txm.rcp.swt.dialog.LastOpened;
56
// TODO: Auto-generated Javadoc
57
/**
58
 * Execute a script. It uses the file extension to execute it as Groovy or R
59
 * 
60
 * Later : use shabang
61
 */
62
public class ExecuteScript extends AbstractHandler {
63

  
64
	private static final String ID = "org.txm.rcp.commands.ExecuteScript"; //$NON-NLS-1$
65
	/* (non-Javadoc)
66
	 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
67
	 */
68
	@Override
69
	public Object execute(ExecutionEvent event) throws ExecutionException {
70

  
71
		// the file to execute
72
		String result = ""; //$NON-NLS-1$
73
		boolean executionFromEditor = false;
74
		TxtEditor txteditor = null;
75
		
76
		IWorkbenchPart page = HandlerUtil.getActiveWorkbenchWindow(event)
77
				.getActivePage().getActivePart();
78
		ISelection selection = HandlerUtil.getCurrentSelection(event);
79
		if (page != null && page instanceof TxtEditor) {
80
			txteditor = (TxtEditor) page;
81
			selection = HandlerUtil.getActiveWorkbenchWindow(event)
82
					.getActivePage().getSelection();
83
			FileStoreEditorInput input = (FileStoreEditorInput) txteditor
84
					.getEditorInput();
85

  
86
			File f = new File(input.getURI().getPath());
87
			result = f.getAbsolutePath();
88
			executionFromEditor = true;
89
		} else if (selection != null
90
				& selection instanceof IStructuredSelection) {
91
			File file = null;
92
			IStructuredSelection strucSelection = (IStructuredSelection) selection;
93
			for (Iterator<Object> iterator = strucSelection.iterator(); iterator
94
					.hasNext();) {
95
				file = (File) iterator.next();
96
				result = file.getAbsolutePath();
97
				break;
98
			}
99
		}
100
		Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event)
101
				.getShell();
102
		// no file found, ask user
103
		if (result == "") //$NON-NLS-1$
104
		{
105
			FileDialog dialog = new FileDialog(shell, SWT.OPEN);
106
			if (LastOpened.getFile(ID) != null) {
107
				dialog.setFilterPath(LastOpened.getFolder(ID));
108
				dialog.setFileName(LastOpened.getFile(ID));
109
			}
110
			dialog.setFilterExtensions(new String[] { "*.groovy" }); //$NON-NLS-1$
111

  
112
			String scriptCurrentDir = TXMPreferences.getString(TBXPreferences.USER_TXM_HOME, TBXPreferences.PREFERENCES_NODE)+ "/scripts";
113

  
114
			dialog.setFilterPath(scriptCurrentDir);
115
			result = dialog.open();
116
			if (result == null) return null;
117
			LastOpened.set(ID, new File(result));
118
		}
119

  
120
		if (executionFromEditor && txteditor != null) { // save file if needed 
121
			if (txteditor.isSaveOnCloseNeeded()) { // check if file need to be saved
122
				boolean doit = TXMPreferences.getBoolean(RCPPreferences.SAVE_BEFORE_EXECUTION, RCPPreferences.PREFERENCES_NODE);
123
				//System.out.println("Save file? "+doit);
124
				if (!doit) { // ask user
125
					MessageDialog dialog = new MessageDialog(shell, RCPMessages.ExecuteScript_0+result, null,
126
							RCPMessages.ExecuteScript_1, MessageDialog.QUESTION, new String[] {RCPMessages.ExecuteScript_2, RCPMessages.ExecuteScript_4,
127
							RCPMessages.ExecuteScript_5}, 0) {
128
						Button checkDefault;
129
						 @Override
130
						  protected Control createDialogArea(Composite parent) {
131
						    Composite container = (Composite) super.createDialogArea(parent);
132
						    checkDefault = new Button(container, SWT.CHECK);
133
						    checkDefault.setText(RCPMessages.ExecuteScript_6);
134
						    boolean b = TXMPreferences.getBoolean(RCPPreferences.SAVE_BEFORE_EXECUTION, RCPPreferences.PREFERENCES_NODE);
135
						    checkDefault.setSelection(b);
136
						    
137
						    checkDefault.addSelectionListener(new SelectionListener() {
138
								
139
								@Override
140
								public void widgetSelected(SelectionEvent e) {
141
									boolean b = checkDefault.getSelection();
142
									TXMPreferences.put(RCPPreferences.PREFERENCES_NODE, RCPPreferences.SAVE_BEFORE_EXECUTION, b);
143
								}
144
								
145
								@Override
146
								public void widgetDefaultSelected(SelectionEvent e) { }
147
							});
148
						    return container;
149
						  }
150
					};
151
					int c = dialog.open();
152
					if (c == 1) {
153
						System.out.println(RCPMessages.ExecuteScript_7);
154
						return null;
155
					} else if (c == 2) {
156
						//System.out.println("Don't save and run.");
157
						doit = false;
158
					} else if (c == 0)  {
159
						//System.out.println("Save and run.");
160
						doit = true;
161
					}
162
				}
163

  
164
				if (doit) {
165
					//System.out.println("Saving file...");
166
					txteditor.doSave(null);
167
				}
168
			}
169
		}
170

  
171
		executeScript(result, page, selection);
172
		return null;
173
	}
174

  
175
	/**
176
	 * Execute script.
177
	 *
178
	 * @param scriptpath the scriptfile
179
	 */
180
	public void executeScript(String scriptpath, final  IWorkbenchPart page, final ISelection selection) {
181
		if (scriptpath == null) return;
182
		File script = new File(scriptpath);
183
		if (!script.exists()) return;
184

  
185
		if (script.getName().endsWith(".groovy")) { //$NON-NLS-1$
186
			ExecuteGroovyScript.executeScript(scriptpath, page, selection);
187
		} else if (script.getName().endsWith(".R")) { //$NON-NLS-1$
188
			System.out.println("ExecuteScript with R not implemented");
189
			//ExecuteRScript.executeScript(scriptpath);
190
		} else {
191
			System.out.println(RCPMessages.ExecuteScript_3+script.getName());
192
		}
193
	}
194
}
0 195

  
tmp/org.txm.rcp/src/main/java/org/txm/rcp/handlers/scripts/ExecuteText.java (revision 669)
1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
// 
22
// 
23
// 
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$ 
27
//
28
package org.txm.rcp.handlers.scripts;
29

  
30
import java.io.File;
31

  
32
import org.eclipse.core.commands.AbstractHandler;
33
import org.eclipse.core.commands.ExecutionEvent;
34
import org.eclipse.core.commands.ExecutionException;
35
import org.eclipse.jface.text.TextSelection;
36
import org.eclipse.jface.viewers.ISelection;
37
import org.eclipse.ui.IWorkbenchPart;
38
import org.eclipse.ui.handlers.HandlerUtil;
39
import org.eclipse.ui.ide.FileStoreEditorInput;
40
import org.txm.rcp.RCPMessages;
41
import org.txm.rcp.editors.TxtEditor;
42

  
43
// TODO: Auto-generated Javadoc
44
/**
45
 * Handler: execute a selection of code from the TxtEditor. It uses the file extension to execute it as Groovy or R.
46
 * 
47
 * 
48
 * @author mdecorde.
49
 */
50
public class ExecuteText extends AbstractHandler {
51

  
52
	/* (non-Javadoc)
53
	 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
54
	 */
55
	@Override
56
	public Object execute(ExecutionEvent event) throws ExecutionException {
57

  
58
		ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
59
				.getActivePage().getSelection();
60
		if(!(selection instanceof TextSelection)) return null;
61
		
62
		TextSelection t = (TextSelection) selection;
63
		String result = t.getText();
64

  
65
		IWorkbenchPart page = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getActivePart();
66
		if (!(page instanceof TxtEditor)) return null;
67
		TxtEditor te = (TxtEditor) page;
68
		FileStoreEditorInput input = (FileStoreEditorInput) te.getEditorInput();
69

  
70
		File f = new File(input.getURI().getPath());
71
		String scriptfile = f.getAbsolutePath();
72

  
73
		return executeText(result, scriptfile);
74
	}
75

  
76
	/**
77
	 * Execute text.
78
	 *
79
	 * @param text the text
80
	 * @param scriptfile the scriptfile
81
	 * @return the object
82
	 */
83
	public static Object executeText(final String text, final String scriptfile) {
84
		File script = new File(scriptfile);
85
		if (script.getName().endsWith(".groovy")) { //$NON-NLS-1$
86
			ExecuteGroovyText.executeText(text, scriptfile);
87
		} else if (script.getName().endsWith(".R")) { //$NON-NLS-1$
88
			System.out.println("Execute R text selection not implemented");
89
			//ExecuteRText.executeText(text);
90
		} else {
91
			System.out.println(RCPMessages.ExecuteScript_3+script.getName());
92
		}
93
		return null;
94
	}
95
}
0 96

  
tmp/org.txm.rcp/src/main/java/org/txm/rcp/handlers/scripts/ExecuteGroovyMacro.java (revision 669)
1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
// 
22
// 
23
// 
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$ 
27
//
28
package org.txm.rcp.handlers.scripts;
29

  
30
import java.io.File;
31
import java.util.Iterator;
32

  
33
import org.eclipse.core.commands.AbstractHandler;
34
import org.eclipse.core.commands.ExecutionEvent;
35
import org.eclipse.core.commands.ExecutionException;
36
import org.eclipse.core.runtime.Platform;
37
import org.eclipse.core.runtime.preferences.IPreferencesService;
38
import org.eclipse.jface.viewers.ISelection;
39
import org.eclipse.jface.viewers.IStructuredSelection;
40
import org.eclipse.swt.SWT;
41
import org.eclipse.swt.widgets.FileDialog;
42
import org.eclipse.swt.widgets.Shell;
43
import org.eclipse.ui.IWorkbenchPart;
44
import org.eclipse.ui.handlers.HandlerUtil;
45
import org.eclipse.ui.ide.FileStoreEditorInput;
46
import org.txm.Toolbox;
47
import org.txm.core.preferences.TBXPreferences;
48
import org.txm.rcp.editors.TxtEditor;
49
import org.txm.rcp.swt.dialog.LastOpened;
50
// TODO: Auto-generated Javadoc
51
/**
52
 * Handler: execute a Macro (Groovy script that opens an UI for parameters)
53
 */
54
public class ExecuteGroovyMacro extends AbstractHandler {
55

  
56
	private static final String ID = "org.txm.rcp.commands.ExecuteMacro"; //$NON-NLS-1$
57
	/* (non-Javadoc)
58
	 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
59
	 */
60
	@Override
61
	public Object execute(ExecutionEvent event) throws ExecutionException {
62

  
63
		String filenameParameter = event
64
				.getParameter("org.txm.rcp.command.parameter.file"); //$NON-NLS-1$
65
		
66
		
67
		// the file to execute
68
		String result = ""; //$NON-NLS-1$
69
		IWorkbenchPart page = HandlerUtil.getActiveWorkbenchWindow(event)
70
				.getActivePage().getActivePart();
71
		ISelection selection = HandlerUtil.getCurrentSelection(event);
72
		if (page != null && page instanceof TxtEditor) {
73
			TxtEditor te = (TxtEditor) page;
74
			selection = HandlerUtil.getActiveWorkbenchWindow(event)
75
					.getActivePage().getSelection();
76
			FileStoreEditorInput input = (FileStoreEditorInput) te
77
					.getEditorInput();
78

  
79
			File f = new File(input.getURI().getPath());
80
			result = f.getAbsolutePath();
81
		} else if (filenameParameter != null) {
82
			result = filenameParameter;
83
		} else if (selection != null
84
				& selection instanceof IStructuredSelection) {
85
			File file = null;
86
			IStructuredSelection strucSelection = (IStructuredSelection) selection;
87
			for (Iterator<Object> iterator = strucSelection.iterator(); iterator
88
					.hasNext();) {
89
				file = (File) iterator.next();
90
				result = file.getAbsolutePath();
91
				break;
92
			}
93
		}
94

  
95
		if (result == "") { //$NON-NLS-1$
96
			Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event)
97
					.getShell();
98
			FileDialog dialog = new FileDialog(shell, SWT.OPEN);
99
			if (LastOpened.getFile(ID) != null) {
100
				dialog.setFilterPath(LastOpened.getFolder(ID));
101
				dialog.setFileName(LastOpened.getFile(ID));
102
			}
103
			dialog.setFilterExtensions(new String[] { "*.groovy" }); //$NON-NLS-1$
104

  
105
			IPreferencesService service = Platform.getPreferencesService();
106
			String scriptRootDir = Toolbox.getTXMHOMEPATH() + "/scripts"; //$NON-NLS-1$
107

  
108
			dialog.setFilterPath(scriptRootDir);
109
			result = dialog.open();
110
			if (result == null) return null;
111
			LastOpened.set(ID, new File(result));
112
		}
113

  
114
		execute(result, page, selection);
115
		return null;
116
	}
117
	
118
	public static void execute(String scriptpath, IWorkbenchPart page, ISelection selection) {
119
		IPreferencesService service = Platform.getPreferencesService();
120
		String scriptRootDir = Toolbox.getTXMHOMEPATH() + "/scripts"; //$NON-NLS-1$
121
		File currentRootDir = new File(scriptRootDir, "macro"); //$NON-NLS-1$
122
		
123
		ExecuteGroovyScript.executeScript(currentRootDir, scriptpath, page, selection, false);
124
	}
125
}
0 126

  
tmp/org.txm.rcp/src/main/java/org/txm/rcp/handlers/scripts/ExecuteGroovyScript.java (revision 669)
1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
// 
22
// 
23
// 
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$ 
27
//
28
package org.txm.rcp.handlers.scripts;
29

  
30
import groovy.lang.Binding;
31

  
32
import java.io.File;
33
import java.io.FilenameFilter;
34
import java.io.IOException;
35
import java.net.MalformedURLException;
36
import java.util.HashMap;
37
import java.util.HashSet;
38
import java.util.Iterator;
39
import java.util.List;
40

  
41
import org.eclipse.core.commands.AbstractHandler;
42
import org.eclipse.core.commands.ExecutionEvent;
43
import org.eclipse.core.commands.ExecutionException;
44
import org.eclipse.core.internal.runtime.InternalPlatform;
45
import org.eclipse.core.runtime.IProgressMonitor;
46
import org.eclipse.core.runtime.IStatus;
47
import org.eclipse.core.runtime.Platform;
48
import org.eclipse.core.runtime.Status;
49
import org.eclipse.core.runtime.preferences.IPreferencesService;
50
import org.eclipse.jface.viewers.ISelection;
51
import org.eclipse.jface.viewers.IStructuredSelection;
52
import org.eclipse.osgi.util.NLS;
53
import org.eclipse.swt.SWT;
54
import org.eclipse.swt.widgets.FileDialog;
55
import org.eclipse.swt.widgets.Shell;
56
import org.eclipse.ui.IWorkbenchPart;
57
import org.eclipse.ui.handlers.HandlerUtil;
58
import org.eclipse.ui.ide.FileStoreEditorInput;
59
import org.osgi.framework.Bundle;
60
import org.osgi.framework.BundleContext;
61
import org.txm.Toolbox;
62
import org.txm.core.preferences.TBXPreferences;
63
import org.txm.rcp.RCPMessages;
64
import org.txm.rcp.TXMClassLoader;
65
import org.txm.rcp.editors.TxtEditor;
66
import org.txm.rcp.swt.dialog.LastOpened;
67
import org.txm.rcp.swt.widget.parameters.UIParameterException;
68
import org.txm.rcp.utils.GSERunner;
69
import org.txm.rcp.utils.JobHandler;
70
import org.txm.rcp.views.corpora.CorporaView;
71
import org.txm.utils.logger.Log;
72
// TODO: Auto-generated Javadoc
73
/**
74
 * Execute a Groovy script
75
 * 
76
 * @author mdecorde
77
 */
78
public class ExecuteGroovyScript extends AbstractHandler {
79

  
80
	private static final String ID = "org.txm.rcp.commands.ExecuteGroovyScript"; //$NON-NLS-1$
81
	private static GSERunner gse;
82
	private static String previousScriptRootDir;
83

  
84
	/* (non-Javadoc)
85
	 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
86
	 */
87
	@Override
88
	public Object execute(ExecutionEvent event) throws ExecutionException {
89

  
90
		String filenameParameter = event
91
				.getParameter("org.txm.rcp.command.parameter.file"); //$NON-NLS-1$
92
		
93
		// the file to execute
94
		String result = ""; //$NON-NLS-1$
95
		IWorkbenchPart page = HandlerUtil.getActiveWorkbenchWindow(event)
96
				.getActivePage().getActivePart();
97

  
98
		ISelection sel =  HandlerUtil.getCurrentSelection(event); 
99

  
100
		if (page != null && page instanceof TxtEditor) {
101
			TxtEditor te = (TxtEditor) page;
102
			FileStoreEditorInput input = (FileStoreEditorInput) te
103
					.getEditorInput();
104

  
105
			File f = new File(input.getURI().getPath());
106
			result = f.getAbsolutePath();
107
		} else if (filenameParameter != null) {
108
			result = filenameParameter;
109
		} else if (sel != null
110
				& sel instanceof IStructuredSelection) {
111
			File file = null;
112
			IStructuredSelection strucSelection = (IStructuredSelection) sel;
113
			for (Iterator<Object> iterator = strucSelection.iterator(); iterator
114
					.hasNext();) {
115
				file = (File) iterator.next();
116
				result = file.getAbsolutePath();
117
				break;
118
			}
119
		}
120

  
121
		if ("".equals(result)) { //$NON-NLS-1$
122
			Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event)
123
					.getShell();
124
			FileDialog dialog = new FileDialog(shell, SWT.OPEN);
125
			if (LastOpened.getFile(ID) != null) {
126
				dialog.setFilterPath(LastOpened.getFolder(ID));
127
				dialog.setFileName(LastOpened.getFile(ID));
128
			}
129
			dialog.setFilterExtensions(new String[] { "*.groovy" }); //$NON-NLS-1$
130

  
131
			IPreferencesService service = Platform.getPreferencesService();
132
			String scriptCurrentDir = Toolbox.getTXMHOMEPATH() + "/scripts"; //$NON-NLS-1$
133

  
134
			dialog.setFilterPath(scriptCurrentDir);
135
			result = dialog.open();
136
			if (result == null) return null;
137
			LastOpened.set(ID, new File(result));
138
		}
139
		executeScript(result, page, sel);
140

  
141
		return null;
142
	}
143

  
144
	/**
145
	 * 
146
	 * @param currentRootDir
147
	 * @param scriptpath
148
	 * @param page current active page, might be null
149
	 * @param sel current selection, might be null
150
	 */
151
	public static void executeScript(String scriptpath, final  IWorkbenchPart page, final ISelection selection, boolean modal) {
152
		IPreferencesService service = Platform.getPreferencesService();
153
		//		String scriptRootDir = service.getString(Application.PLUGIN_ID,
154
		//				ScriptPreferencePage.SCRIPT_ROOT_DIR,
155
		//				ScriptPreferenceInitializer.SCRIPT_ROOT_DIR_DEFAULT, null);
156
		//		
157
		String scriptRootDir = Toolbox.getTXMHOMEPATH() + "/scripts"; //$NON-NLS-1$
158
		File currentRootDir = new File(scriptRootDir, "user"); //$NON-NLS-1$
159

  
160
		executeScript(currentRootDir, scriptpath, page, selection, modal);
161
	}
162

  
163
	/**
164
	 * 
165
	 * @param currentRootDir
166
	 * @param scriptpath
167
	 * @param page current active page, might be null
168
	 * @param sel current selection, might be null
169
	 */
170
	public static void executeScript(String scriptpath, final  IWorkbenchPart page, final ISelection selection) {
171
		executeScript(scriptpath, page, selection, false);
172
	}
173

  
174
	/**
175
	 * 
176
	 * @param currentRootDir
177
	 * @param scriptpath
178
	 * @param page current active page, might be null
179
	 * @param sel current selection, might be null
180
	 */
181
	public static void executeScript(final File currentRootDir, String scriptpath, final  IWorkbenchPart page, final ISelection sel, final boolean modal) {
182
		// check script
183
		final File scriptfile = new File(scriptpath);
184
		if (!scriptfile.exists()) {
185
			System.err.println(NLS.bind(RCPMessages.ExecuteScriptImport_1, scriptpath));
186
			return;
187
		}
188
		ExecuteLastGroovyScript.setLastScript(scriptfile, currentRootDir.getName().equals("macro")); // register last groovy script //$NON-NLS-1$
189

  
190
		// get current selection of the corpora view
191
		final Object corpusViewSelection = CorporaView.getSelection();
192
		final Object corpusViewSelections = CorporaView.getSelections();
193

  
194
		final Object selection;
195
		final List selections;
196
		if (sel != null && sel instanceof IStructuredSelection) {
197
			selection = ((IStructuredSelection)sel).getFirstElement();
198
			selections = ((IStructuredSelection)sel).toList();
199
		} else {
200
			selection = sel;
201
			selections = null;
202
		}
203

  
204
		String tmp = Toolbox.getTXMHOMEPATH() + "/scripts"; //$NON-NLS-1$
205
		if (previousScriptRootDir == null || !tmp.equals(previousScriptRootDir)) {
206
			previousScriptRootDir = tmp;
207
			gse = null;
208
			//System.out.println("SCRIPT ROOT DIR CHANGED tmp="+tmp);
209
		}
210

  
211
		String dir = currentRootDir.getAbsolutePath();
212
		String path = scriptfile.getAbsolutePath();
213
		if (!path.startsWith(dir)) {
214
			System.out.println(RCPMessages.bind(RCPMessages.ExecuteGroovyScriptFromFile_2, dir));
215
			return;
216
		}
217
		final String relativepath = path.substring(dir.length()+1);
218

  
219
		if (gse == null) {
220
			gse = buildGSE(previousScriptRootDir, scriptfile);
221
			if (gse == null) {
222
				System.out.println("Could not create the Groovy script engine. Aborting.");
223
				return;
224
			}
225
		}
226

  
227
		JobHandler jobhandler = new JobHandler(NLS.bind(RCPMessages.ExecuteRScript_1,scriptfile.getName())) {
228
			@Override
229
			protected IStatus run(IProgressMonitor monitor) {
230
				this.runInit(monitor);
231
				//this.setCurrentMonitor(monitor);
232
				monitor.beginTask(NLS.bind(RCPMessages.ExecuteRScript_1, scriptfile.getName()), 100);
233

  
234
				try {
235
					Binding binding = new Binding();
236
					binding.setVariable("editor", page); //$NON-NLS-1$
237
					binding.setVariable("selection", selection); //$NON-NLS-1$
238
					binding.setVariable("selections", selections); //$NON-NLS-1$
239
					binding.setVariable("corpusViewSelection", corpusViewSelection); //$NON-NLS-1$
240
					binding.setVariable("corpusViewSelections", corpusViewSelections); //$NON-NLS-1$
241
					binding.setProperty("monitor", this); //$NON-NLS-1$
242
					binding.setProperty("gse", gse);
243
					
244
					System.out.println(NLS.bind(RCPMessages.ExecuteRScript_1, scriptfile.getName()));
245
					long time = System.currentTimeMillis();
246
					gse.run(relativepath, binding);
247
					
248
					System.out.println(RCPMessages.ExecuteRScript_2+(System.currentTimeMillis()-time)+RCPMessages.ExecuteGroovyScript_0);
249
				} catch (ThreadDeath td) {
250
					return Status.CANCEL_STATUS;
251
				} catch (UIParameterException e) {
252
					if (e.isError()) {
253
						System.out.println(RCPMessages.ExecuteGroovyScript_1);
254
					} else {
255
						System.out.println(RCPMessages.ExecuteGroovyScript_2);
256
					}
257
				} catch (Exception e) {
258
					System.out.println(RCPMessages.ExecuteGroovyScript_9+e);
259
					org.txm.rcp.utils.Logger.printStackTrace(e);
260
				} finally {
261
					monitor.done();			
262
				}
263
				return Status.OK_STATUS;
264
			}
265
		};
266

  
267
		jobhandler.startJob(modal);
268
		//        //gse.loadScriptByName("scripts/B.groovy");
269
		//		gse.run("A.groovy", new Binding());
270

  
271
		//		ClassLoader parent = getClass().getClassLoader();
272
		//		GroovyClassLoader loader = new GroovyClassLoader(parent);
273
		//
274
		//		List<File> classPath = new ArrayList<File>();
275
		//		classPath.add(new File(scriptRootDir));
276
		/*classPath.add(new File(scriptfile));
277
		classPath.add(new File(scriptfile).getParentFile().getParentFile());
278
		classPath.add(new File("")); //$NON-NLS-1$*/
279
		// System.out.println(classPath);
280
		//System.out.println("Build groovy scripts - Start");
281
		//org.txm.Build.run();
282
		//System.out.println("Build groovy scripts - Done");
283

  
284
		//		Class groovyClass = null;
285
		//		for (File f : classPath)
286
		//			loader.addClasspath(f.getAbsolutePath());
287
		//		loader.setShouldRecompile(true);
288
		//
289
		//		try {
290
		//			groovyClass = loader.parseClass(new File(scriptfile));
291
		//		} catch (CompilationFailedException e) {
292
		//			org.txm.rcp.utils.Logger.printStackTrace(e);
293
		//		} catch (IOException e) {
294
		//			org.txm.rcp.utils.Logger.printStackTrace(e);
295
		//		}
296
		//
297
		//		// let's call some method on an instance
298
		//		GroovyObject groovyObject = null;
299
		//		try {
300
		//			groovyObject = (GroovyObject) groovyClass.newInstance();
301
		//		} catch (InstantiationException e) {
302
		//			org.txm.rcp.utils.Logger.printStackTrace(e);
303
		//		} catch (IllegalAccessException e) {
304
		//			org.txm.rcp.utils.Logger.printStackTrace(e);
305
		//		}
306
		//
307
		//		Object[] args = {};
308
		//		try {
309
		//			groovyObject.invokeMethod("run", args); //$NON-NLS-1$
310
		//		} catch (Exception e) {
311
		//			System.err.println(e);
312
		//			org.txm.rcp.utils.Logger.printStackTrace(e);
313
		//		}
314

  
315
		/*
316
		 * try { File script = new File(scriptfile); // GroovyImportScriptRunner
317
		 * g = new GroovyImportScriptRunner(new File(""),new ArrayList<File>());
318
		 * 
319
		 * GroovyClassLoader classLoader = new
320
		 * GroovyClassLoader(this.getClass().getClassLoader());
321
		 * classLoader.addURL(script.getParentFile().toURI().toURL());
322
		 * 
323
		 * GroovyScriptEngine gse = new
324
		 * GroovyScriptEngine(script.getParent(),classLoader);
325
		 * gse.run(script.getAbsolutePath(), new Binding());
326
		 * System.out.println("END : execute script RCP "); } catch (Exception
327
		 * e) { // TODO Auto-generated catch block org.txm.rcp.utils.Logger.printStackTrace(e); }
328
		 * 
329
		 * /* if (scriptfile == "") return; final File script = new
330
		 * File(scriptfile);
331
		 * 
332
		 * String[] roots = new String[] {script.getParent()};
333
		 * System.out.println("SCRIPTROOTDIR "+script.getParent());
334
		 * System.out.println("SCRIPT "+script); GroovyScriptEngine gse; try {
335
		 * gse = new GroovyScriptEngine(roots); for(File f :
336
		 * script.getParentFile().listFiles())
337
		 * gse.loadScriptByName(f.getName()); Binding binding = new Binding();
338
		 * 
339
		 * System.out.println("start execution"); gse.run(script.getName(),
340
		 * binding); System.out.println("end execution"); } catch (Exception e)
341
		 * {org.txm.rcp.utils.Logger.printStackTrace(e);}
342
		 */
343
	}
344
	
345
public static GSERunner buildGSE(String scriptRootDir, File script) {
346
		
347
		GSERunner gse;
348
		
349
		String[] roots = new String[] { 
350
				scriptRootDir+"/user/", //$NON-NLS-1$
351
				scriptRootDir+"/macro/", //$NON-NLS-1$
352
				scriptRootDir+"/import/", //$NON-NLS-1$
353
				script.getParent(),
354
		}; 
355

  
356
		HashMap<String, Bundle> bundleWithActivators = new HashMap<String, Bundle>();
357
		HashSet<ClassLoader> loaders = new HashSet<ClassLoader>();
358
		BundleContext bundleContext = InternalPlatform.getDefault().getBundleContext();
359
		for (Bundle b : bundleContext.getBundles()) {
360
			String acName = b.getHeaders().get("Bundle-Activator");
361
			//System.out.println(b.getSymbolicName()+":"+b.getHeaders());
362
			if (acName != null) {
363
				if (!bundleWithActivators.containsKey(b.getSymbolicName())) {
364
					bundleWithActivators.put(b.getSymbolicName(),b);
365
				} else {
366
					Bundle b2 = bundleWithActivators.get(b.getSymbolicName());
367
					if (b2.getVersion().compareTo(b.getVersion()) < 0) {
368
						bundleWithActivators.put(b.getSymbolicName(),b);
369
					}
370
				}
371
			}
372
		}
373
		
374
		for (Bundle b : bundleWithActivators.values()) {
375
			try {
376
				String acName = b.getHeaders().get("Bundle-Activator");
377
				Class<?> ac = b.loadClass(acName);
378
				loaders.add(ac.getClassLoader());
379
			} catch (ClassNotFoundException e) {
380
				e.printStackTrace();
381
			}
382
		}
383
		Log.info("Initialized TXMClassLoader with "+loaders.size()+ " bundles.");
384
		TXMClassLoader scl = new TXMClassLoader(loaders);
385

  
386
		try {
387
			gse = new GSERunner(roots, scl);
388
		} catch (IOException e) {
389
			e.printStackTrace();
390
			return null;
391
		}
392

  
393
		File jardir = new File(scriptRootDir,"lib"); //$NON-NLS-1$
394
		if (jardir.exists() && jardir.isDirectory()) {
395
			for (File f: jardir.listFiles(new FilenameFilter() {
396
				@Override
397
				public boolean accept(File dir, String name) {
398
					return name.endsWith(".jar"); //$NON-NLS-1$
399
				}
400
			}))
401
			{
402
				try {
403
					gse.getGroovyClassLoader().addURL(f.toURI().toURL());
404
				} catch (MalformedURLException e) {
405
					// TODO Auto-generated catch block
406
					e.printStackTrace();
407
				}
408
			}
409
		}	
410
		gse.getGroovyClassLoader().addClasspath(scriptRootDir); // for the .class files if any
411
		
412
		return gse;
413
	}
414
}
0 415

  
tmp/org.txm.rcp/src/main/java/org/txm/rcp/handlers/scripts/ExecuteGroovyText.java (revision 669)
1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
// 
22
// 
23
// 
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$ 
27
//
28
package org.txm.rcp.handlers.scripts;
29

  
30
import groovy.lang.Binding;
31
import groovy.lang.GroovyShell;
32

  
33
import java.io.File;
34
import java.net.MalformedURLException;
35
import java.util.ArrayList;
36
import java.util.List;
37

  
38
import org.codehaus.groovy.control.CompilationFailedException;
39
import org.eclipse.core.commands.AbstractHandler;
40
import org.eclipse.core.commands.ExecutionEvent;
41
import org.eclipse.core.commands.ExecutionException;
42
import org.eclipse.core.runtime.IProgressMonitor;
43
import org.eclipse.core.runtime.IStatus;
44
import org.eclipse.core.runtime.Platform;
45
import org.eclipse.core.runtime.Status;
46
import org.eclipse.core.runtime.preferences.IPreferencesService;
47
import org.eclipse.jface.text.TextSelection;
48
import org.eclipse.jface.viewers.ISelection;
49
import org.eclipse.osgi.util.NLS;
50
import org.eclipse.ui.IWorkbenchPart;
51
import org.eclipse.ui.handlers.HandlerUtil;
52
import org.eclipse.ui.ide.FileStoreEditorInput;
53
import org.txm.core.preferences.TBXPreferences;
54
import org.txm.core.preferences.TXMPreferences;
55
import org.txm.rcp.Application;
56
import org.txm.rcp.RCPMessages;
57
import org.txm.rcp.editors.TxtEditor;
58
import org.txm.rcp.utils.JobHandler;
59
import org.txm.rcp.views.corpora.CorporaView;
60

  
61
// TODO: Auto-generated Javadoc
62
/**
63
 * execute a selection of Groovy code from the TxtEditor 
64
 * 
65
 * @author mdecorde.
66
 */
67
public class ExecuteGroovyText extends AbstractHandler {
68

  
69
	/*
70
	 * (non-Javadoc)
71
	 * 
72
	 * @see
73
	 * org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands
74
	 * .ExecutionEvent)
75
	 */
76
	@Override
77
	public Object execute(ExecutionEvent event) throws ExecutionException {
78

  
79
		ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
80
				.getActivePage().getSelection();
81
		if (!(selection instanceof TextSelection))
82
			return null;
83
		TextSelection t = (TextSelection) selection;
84
		String result = t.getText();
85

  
86
		IWorkbenchPart page = HandlerUtil.getActiveWorkbenchWindow(event)
87
				.getActivePage().getActivePart();
88
		TxtEditor te = (TxtEditor) page;
89
		FileStoreEditorInput input = (FileStoreEditorInput) te.getEditorInput();
90

  
91
		File f = new File(input.getURI().getPath());
92
		String scriptfile = f.getAbsolutePath();
93

  
94
		executeText(result, scriptfile);
95
		return null;
96
	}
97

  
98
	/**
99
	 * Execute text.
100
	 * 
101
	 * @param text
102
	 *            the text
103
	 * @param scriptfile
104
	 *            the scriptfile
105
	 * @return the object
106
	 */
107
	public static Object executeText(final String text, final String scriptfile) {
108
		if (!new File(scriptfile).exists()) {
109
			System.err.println(NLS.bind(RCPMessages.ExecuteScriptImport_1,
110
					scriptfile));
111
			return null;
112
		}
113
		final String scriptRootDir = TXMPreferences.getString(TBXPreferences.USER_TXM_HOME, TBXPreferences.PREFERENCES_NODE)+ "/scripts";
114

  
115
		// get current selection of the corpora view
116
				final Object corpusViewSelection = CorporaView.getSelection();
117
				final Object corpusViewSelections = CorporaView.getSelections();
118
				final Object selection = null;
119
				final List selections = null;
120
				
121
		JobHandler jobhandler = new JobHandler("run") { //$NON-NLS-1$
122
			@Override
123
			protected IStatus run(IProgressMonitor monitor) {
124
				// this.setCurrentMonitor(monitor);
125
				this.runInit(monitor);
126

  
127
				try {
128
					List<File> classPath = new ArrayList<File>();
129
					classPath.add(new File(scriptRootDir));
130
					classPath.add(new File(scriptfile));
131
					classPath.add(new File(scriptfile).getParentFile());
132
					classPath.add(new File(".")); //$NON-NLS-1$
133
					// System.out.println(classPath);
134
					
135
					Binding binding = new Binding();
136
					//binding.setVariable("editor", page); //$NON-NLS-1$
137
					binding.setVariable("selection", selection); //$NON-NLS-1$
138
					binding.setVariable("selections", selections); //$NON-NLS-1$
139
					binding.setVariable("corpusViewSelection", corpusViewSelection); //$NON-NLS-1$
140
					binding.setVariable("corpusViewSelections", corpusViewSelections); //$NON-NLS-1$
141
					binding.setProperty("monitor", this); //$NON-NLS-1$
142
					//binding.setProperty("gse", gse);
143
					GroovyShell shell = new GroovyShell(this.getClass()
144
							.getClassLoader(), binding);
145

  
146
					for (File file : classPath) {
147
						try {
148
							shell.getClassLoader().addURL(file.toURI().toURL());
149
							shell.getClassLoader().addClasspath(
150
									file.getAbsolutePath());
151
						} catch (MalformedURLException e) {
152
							org.txm.rcp.utils.Logger.printStackTrace(e);
153
							return null;
154
						}
155
					}
156

  
157
					// try {
158
					System.out.println("Executing text selection..."); //$NON-NLS-1$
159
					long time = System.currentTimeMillis();
160
					shell.evaluate(text);
161
					System.out
162
							.println("Done: " + (System.currentTimeMillis() - time) + RCPMessages.ExecuteRScript_3); //$NON-NLS-1$
163
				} catch (ThreadDeath td) { 
164
					return Status.CANCEL_STATUS;
165
				} catch (CompilationFailedException e) {
166
					System.err.println(e);
167
				} catch (Exception e) {
168
					org.txm.rcp.utils.Logger.printStackTrace(e);
169
				}
170
				return Status.OK_STATUS;
171
			}
172
		};
173

  
174
		jobhandler.startJob();
175
		return null;
176
	}
177
}
0 178

  
tmp/org.txm.rcp/src/main/java/org/txm/rcp/handlers/scripts/ExecuteGroovyScriptFromFile.java (revision 669)
1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
// 
22
// 
23
// 
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$ 
27
//
28
package org.txm.rcp.handlers.scripts;
29

  
30
import java.io.File;
31

  
32
import org.eclipse.core.commands.AbstractHandler;
33
import org.eclipse.core.commands.ExecutionEvent;
34
import org.eclipse.core.commands.ExecutionException;
35
import org.eclipse.jface.viewers.ISelection;
36
import org.eclipse.swt.SWT;
37
import org.eclipse.swt.widgets.FileDialog;
38
import org.eclipse.swt.widgets.Shell;
39
import org.eclipse.ui.IWorkbenchPart;
40
import org.eclipse.ui.handlers.HandlerUtil;
41
import org.txm.core.preferences.TBXPreferences;
42
import org.txm.core.preferences.TXMPreferences;
43
import org.txm.rcp.swt.dialog.LastOpened;
44
/**
45
 *  Ask the user to select a Groovy script using a File dialog and execute it
46
 */
47
public class ExecuteGroovyScriptFromFile extends AbstractHandler {
48

  
49
	private static final String ID = "org.txm.rcp.commands.ExecuteGroovyScriptFromFile"; //$NON-NLS-1$
50

  
51
	/* (non-Javadoc)
52
	 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
53
	 */
54
	@Override
55
	public Object execute(ExecutionEvent event) throws ExecutionException {
56

  
57
		// the file to execute
58
		String result; //$NON-NLS-1$
59
		IWorkbenchPart page = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getActivePart();
60
		ISelection selection = HandlerUtil.getCurrentSelection(event);
61
		Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell();
62

  
63
		String scriptCurrentDir = TXMPreferences.getString(TBXPreferences.USER_TXM_HOME, TBXPreferences.PREFERENCES_NODE)+ "/scripts";
64

  
65
		FileDialog dialog = new FileDialog(shell, SWT.OPEN);
66

  
67
		if (LastOpened.getFile(ID) != null) {
68
			dialog.setFilterPath(LastOpened.getFolder(ID));
69
			dialog.setFileName(LastOpened.getFile(ID));
70
		} else {
71
			dialog.setFilterPath(scriptCurrentDir);
72
		}
73

  
74
		dialog.setFilterExtensions(new String[] { "*.groovy" }); //$NON-NLS-1$
75
		dialog.setFilterPath(scriptCurrentDir);
76
		result = dialog.open();
77
		if (result == null) return null;
78
		
79
		LastOpened.set(ID, new File(result));
80
		ExecuteGroovyScript.executeScript(result, page, selection);
81
		return null;
82
	}
83
}
0 84

  
tmp/org.txm.rcp/src/main/java/org/txm/rcp/handlers/scripts/ExecuteLastGroovyScript.java (revision 669)
1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
// 
22
// 
23
// 
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$ 
27
//
28
package org.txm.rcp.handlers.scripts;
29

  
30
import java.io.File;
31

  
32
import org.eclipse.core.commands.AbstractHandler;
33
import org.eclipse.core.commands.ExecutionEvent;
34
import org.eclipse.core.commands.ExecutionException;
35
import org.eclipse.jface.viewers.ISelection;
36
import org.eclipse.ui.IWorkbenchPart;
37
import org.eclipse.ui.handlers.HandlerUtil;
38
import org.txm.Toolbox;
39
import org.txm.core.preferences.TBXPreferences;
40
import org.txm.rcp.RCPMessages;
41
// TODO: Auto-generated Javadoc
42
/**
43
 * Handler: execute a Macro (Groovy script that opens an UI for parameters)
44
 */
45
public class ExecuteLastGroovyScript extends AbstractHandler {
46

  
47
	private static final String ID = "org.txm.rcp.commands.ExecuteMacro"; //$NON-NLS-1$
48
	public static File lastScript;
49
	public static boolean macro = false;
50
	
51
	/* (non-Javadoc)
52
	 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
53
	 */
54
	@Override
55
	public Object execute(ExecutionEvent event) throws ExecutionException {
56

  
57
		IWorkbenchPart page = HandlerUtil.getActiveWorkbenchWindow(event)
58
				.getActivePage().getActivePart();
59
		ISelection selection = HandlerUtil.getCurrentSelection(event);
60
		
61
		execute(page, selection);
62
		
63
		return null;
64
	}
65
	
66
	public static void setLastScript(File s, boolean b) {
67
		lastScript = s;
68
		macro = b;
69
	}
70
	
71
	/**
72
	 * Execute last Groovy script
73
	 * 
74
	 * @param page
75
	 * @param selection
76
	 */
77
	public static void execute(IWorkbenchPart page, ISelection selection) {
78
		if (lastScript == null) return; // no last script
79
		if (macro)
80
			System.out.println(RCPMessages.ExecuteLastGroovyScript_0+lastScript);
81
		else 
82
			System.out.println(RCPMessages.ExecuteLastGroovyScript_1+lastScript);
83
		
84
		//IPreferencesService service = Platform.getPreferencesService();
85
		String scriptRootDir = Toolbox.getTXMHOMEPATH() + "/scripts"; //$NON-NLS-1$
86
		File currentRootDir;
87
		if (macro) {
88
			currentRootDir = new File(scriptRootDir, "macro"); //$NON-NLS-1$
89
		} else {
90
			currentRootDir = new File(scriptRootDir, "user"); //$NON-NLS-1$
91
		}
92
		
93
		if (!currentRootDir.exists()) {
94
			System.out.println(RCPMessages.ExecuteLastGroovyScript_3+currentRootDir);
95
			return;
96
		}
97
		
98
		ExecuteGroovyScript.executeScript(currentRootDir, lastScript.getAbsolutePath(), page, selection, false);
99
	}
100
}
0 101

  
... Ce différentiel a été tronqué car il excède la taille maximale pouvant être affichée.

Formats disponibles : Unified diff