Révision 3431

TXM/trunk/org.txm.analec.rcp/src/org/txm/annotation/urs/commands/URSToolsMenuContribution.java (revision 3431)
139 139
		public void widgetSelected(SelectionEvent e) {
140 140
			IWorkbenchWindow acWindow = TXMWindows.getActiveWindow();
141 141
			IWorkbenchPart page = acWindow.getActivePage().getActivePart();
142
			ExecuteGroovyMacro.execute(macro.getAbsolutePath(), page, CorporaView.getInstance().getTreeViewer().getSelection(), "", null);
142
			ExecuteGroovyMacro.execute(macro.getAbsolutePath(), page, CorporaView.getInstance().getTreeViewer().getSelection(), "", null, null);
143 143
		}
144 144
	}
145 145

  
TXM/trunk/org.txm.groovy.core/src/java/org/txm/groovy/core/GSERunner.java (revision 3431)
235 235
	 * @throws ScriptException
236 236
	 */
237 237
	@SuppressWarnings("rawtypes")
238
	public Object run(Class clazz, Map args) throws ResourceException, ScriptException {
239
		return run(clazz.getCanonicalName().replace(".", "/") + ".groovy", new Binding(args));
238
	public Object run(Class clazz, Map bindingArgs) throws ResourceException, ScriptException {
239
		return run(clazz.getCanonicalName().replace(".", "/") + ".groovy", new Binding(bindingArgs));
240 240
	}
241 241
	
242 242
	/**
......
250 250
	 * @throws ScriptException
251 251
	 */
252 252
	@SuppressWarnings("rawtypes")
253
	public Object runMacro(Class clazz, Map args) throws ResourceException, ScriptException {
253
	public Object runMacro(Class clazz, Map args, Map defaultArgs) throws ResourceException, ScriptException {
254 254
		HashMap<String, Object> arrangedMap = new HashMap<>();
255
		arrangedMap.put("args", args);
255
		if (args != null) {
256
			arrangedMap.put("args", args);
257
		}
258
		if (defaultArgs != null) {
259
			arrangedMap.put("defaultArgs", defaultArgs);
260
		}
256 261
		
257 262
		
258 263
		// Find the Groovy Script
......
272 277
	
273 278
	/**
274 279
	 * Convenience method exactly the same as calling
280
	 * run(clazz, ["args":args])
281
	 * 
282
	 * @param clazz
283
	 * @param args
284
	 * @return
285
	 * @throws ResourceException
286
	 * @throws ScriptException
287
	 */
288
	@SuppressWarnings("rawtypes")
289
	public Object runMacro(Class clazz, Map args) throws ResourceException, ScriptException {
290
		return runMacro(clazz, args, null);
291
	}
292
	
293
	/**
294
	 * Convenience method exactly the same as calling
275 295
	 * run(clazz, ["args":[:]])
276 296
	 * 
277 297
	 * @param clazz
......
282 302
	 */
283 303
	@SuppressWarnings("rawtypes")
284 304
	public Object runMacro(Class clazz) throws ResourceException, ScriptException {
285
		HashMap<String, Object> arrangedMap = new HashMap<>();
286
		arrangedMap.put("args", new HashMap<String, Object>());
287
		return run(clazz.getCanonicalName().replace(".", "/") + ".groovy", new Binding(arrangedMap));
305
//		HashMap<String, Object> arrangedMap = new HashMap<>();
306
//		arrangedMap.put("args", new HashMap<String, Object>());
307
//		return run(clazz.getCanonicalName().replace(".", "/") + ".groovy", new Binding(arrangedMap));
308
		return runMacro(clazz, null, null);
288 309
	}
289 310
	
290 311
	//// TODO ScriptCacheEntry is a private class...
TXM/trunk/org.txm.groovy.core/src/groovy/org/txm/macro/annotation/ImportWordPropertiesFromTableMacro.groovy (revision 3431)
18 18
@Field @Option(name="properties", usage="columns to inject separated by commas", widget="String", required=true, def="p1, p2, ... , pn")
19 19
def properties
20 20

  
21
@Field @Option(name="update_edition", usage="Update the edition pages", widget="Boolean", required=true, def="false")
22
def update_edition
23

  
21 24
@Field @Option(name="debug", usage="Debug de the macro", widget="Boolean", required=false, def="false")
22 25
def debug
23 26

  
......
32 35
	, "outputDirectory": inputDirectory
33 36
	, "tsvFile": tsvFile
34 37
	, "properties": properties
35
	, "debug":debug])
38
	, "debug":debug] ,
39
)
36 40

  
37
org.txm.rcp.commands.workspace.UpdateCorpus.update(selection, TBXPreferences.getInstance().getBoolean(TBXPreferences.UPDATEEDITIONS))
41
org.txm.rcp.commands.workspace.UpdateCorpus.update(selection, update_edition)
TXM/trunk/org.txm.libs.args4j/src/org/kohsuke/args4j/NamedOptionDef.java (revision 3431)
5 5
 */
6 6
public final class NamedOptionDef extends OptionDef {
7 7
	
8
	private final String name;
8
	private String name;
9 9
	
10
	private final String def;
10
	public String def;
11 11
	
12
	private final String widget;
12
	private String widget;
13 13
	
14
	private final String[] aliases;
14
	private String[] aliases;
15 15
	
16 16
	public NamedOptionDef(Option o, boolean forceMultiValued) {
17 17
		super(o.usage(), o.metaVar(), o.required(), o.handler(), o.multiValued() || forceMultiValued);
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/menu/MacrosMenuContribution.java (revision 3431)
120 120
		for (File f : files) {
121 121
			if (f.getName().endsWith("Macro.groovy")) {
122 122
				MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
123
				menuItem.setText(f.getName().substring(0, f.getName().length()-12).replaceAll("([A-Z])([^A-Z])", " $1$2"));
123
				String name = f.getName().substring(0, f.getName().length()-12);
124
				name = name.replaceAll("([0-9]++)", " $1 ");
125
				name = name.replaceAll("(To)", " To ");
126
				name = name.replaceAll("([A-Z]++)([^A-Z])", " $1$2");
127
				
128
				menuItem.setText(name);
124 129
				menuItem.setImage(IImageKeys.getImage(IImageKeys.SCRIPT_RUN));
125 130
				menuItem.addSelectionListener(new MacroSelectionAdapter(f));
126 131
			} else if (f.isDirectory()) {
......
145 150
		public void widgetSelected(SelectionEvent e) {
146 151
			IWorkbenchWindow acWindow = TXMWindows.getActiveWindow();
147 152
			IWorkbenchPart page = acWindow.getActivePage().getActivePart();
148
			ExecuteGroovyMacro.execute(macro.getAbsolutePath(), page, CorporaView.getInstance().getTreeViewer().getSelection(), "", null);
153
			ExecuteGroovyMacro.execute(macro.getAbsolutePath(), page, CorporaView.getInstance().getTreeViewer().getSelection(), "", null, null);
149 154
		}
150 155
	}
151 156
	
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/swt/widget/parameters/ParametersDialog.java (revision 3431)
58 58
	
59 59
	protected Map args; // store values given by another Macro, if contains all the needed values, no dialog is shown
60 60
	
61
	protected Map defaultArgs; // store additional default values of parameters (replace default values of script)
62
	
61 63
	// protected int numberOfParameters; // number of parameters to show in the dialog box (not set by 'args'), if 0 parameters no dialog is shown
62 64
	
63 65
	protected ArrayList<String> parametersToSetByTheUser;
......
83 85
	 * @param allParameters all the scripts annotations
84 86
	 * @param args pre-filled fields
85 87
	 */
86
	public ParametersDialog(Shell parentShell, Object bean, List<OptionHandler> allParameters, Map args) {
88
	public ParametersDialog(Shell parentShell, Object bean, List<OptionHandler> allParameters, Map args, Map defaultArgs) {
89

  
87 90
		super(parentShell);
88 91
		if (bean != null) {
89 92
			this.script = bean.getClass().getName();
......
91 94
		this.parameters = new ArrayList<>();
92 95
		this.bean = bean;
93 96
		this.args = args;
97
		this.defaultArgs = defaultArgs;
94 98
		setShellStyle(getShellStyle() | SWT.RESIZE);
95 99
		
96 100
		// count the number of fields to show. If "0" then don't show the dialog, see the "open()" function
......
143 147
		for (NamedOptionDef option : this.parameters) {
144 148
			String widgetName = option.widget();
145 149
			String stringValue = option.def();
150
			
146 151
			Object value = null;
147 152
			if ("File".equals(widgetName) || "FileOpen".equals(widgetName)) { //$NON-NLS-1$
148 153
				value = new File(stringValue);
......
553 558
			}
554 559
		}
555 560
		
561
		Object oDefaultArgs = null;
562
		if (bean instanceof groovy.lang.Script) {
563
			groovy.lang.Script script = (groovy.lang.Script) bean;
564
			
565
			// if the "defaultArgs" variable exists then use its values. Can be used to set a default value by another Macro
566
			if (script.getBinding().hasVariable("defaultArgs")) {
567
				oDefaultArgs = script.getBinding().getVariable("defaultArgs"); //$NON-NLS-1$
568
			}
569
		}
570
		
556 571
		final Map args;
557 572
		if (oArgs == null || !(oArgs instanceof Map)) {
558 573
			args = new HashMap<String, Object>();
......
567 582
				script.getBinding().setVariable("args", args);
568 583
			}
569 584
		}
585
		
586
		final Map defaultArgs;
587
		if (oDefaultArgs == null || !(oDefaultArgs instanceof Map)) {
588
			defaultArgs = new HashMap<String, Object>();
589
		}
590
		else {
591
			defaultArgs = (Map) oDefaultArgs;
592
		}
593
		
594
		if (bean instanceof groovy.lang.Script) { // create the args binding if not already existing
595
			groovy.lang.Script script = (groovy.lang.Script) bean;
596
			if (!script.getBinding().hasVariable("defaultArgs")) {
597
				script.getBinding().setVariable("defaultArgs", defaultArgs);
598
			}
599
		}
600
		
570 601
		Log.finest("Calling syncExec on display: " + Display.getDefault());
571 602
		Display.getDefault().syncExec(new Runnable() {
572 603
			
......
587 618
							if (opt.option instanceof NamedOptionDef) {
588 619
								NamedOptionDef option = (NamedOptionDef) opt.option;
589 620
								c.getDeclaredField(option.name());
621
								if (defaultArgs.containsKey(option.name())) {
622
									option.def = ""+defaultArgs.get(option.name());
623
								}
590 624
							}
591 625
						}
592 626
						catch (NoSuchFieldException e) {
......
595 629
						}
596 630
					}
597 631
					if (okNames) {
598
						ParametersDialog dialog = new ParametersDialog(shell, bean, parser.getOptions(), args);
632
						ParametersDialog dialog = new ParametersDialog(shell, bean, parser.getOptions(), args, defaultArgs);
599 633
						if (dialog.getShell() != null) {
600 634
							Log.finest("Activating shell:" + shell);
601 635
							shell.setActive();
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/commands/function/WordPropertiestoTable.java (revision 3431)
3 3
import java.io.File;
4 4
import java.util.HashMap;
5 5

  
6
import org.apache.commons.lang.StringUtils;
7 6
import org.eclipse.core.commands.AbstractHandler;
8 7
import org.eclipse.core.commands.ExecutionEvent;
9 8
import org.eclipse.core.commands.ExecutionException;
......
11 10
import org.eclipse.ui.IWorkbenchPart;
12 11
import org.eclipse.ui.handlers.HandlerUtil;
13 12
import org.txm.Toolbox;
14
import org.txm.objects.CorpusBuild;
15 13
import org.txm.rcp.handlers.scripts.ExecuteGroovyMacro;
16
import org.txm.searchengine.cqp.clientExceptions.CqiClientException;
17 14
import org.txm.searchengine.cqp.corpus.MainCorpus;
18
import org.txm.searchengine.cqp.corpus.query.CQLQuery;
19 15
import org.txm.utils.logger.Log;
20 16

  
21 17
public class WordPropertiestoTable extends AbstractHandler{
......
34 30
		MainCorpus corpus = (MainCorpus)first;
35 31

  
36 32
		File script = new File(Toolbox.getTxmHomePath(), "/scripts/groovy/user/org/txm/macro/annotation/ExportWordPropertiesToTableMacro.groovy");
37
		File parametersFile = new File(Toolbox.getTxmHomePath(), "/scripts/groovy/user/org/txm/macro/annotation/ExportWordPropertiesToTableMacro.properties");
33
		//File parametersFile = new File(Toolbox.getTxmHomePath(), "/scripts/groovy/user/org/txm/macro/annotation/ExportWordPropertiesToTableMacro.properties");
38 34
		
39
		ExecuteGroovyMacro.execute(script.getAbsolutePath(), part, selection, "", null);
35
		HashMap<String, Object> parameters = new HashMap<String, Object>();
36
		
37
		HashMap<String, Object> defaultParameters = new HashMap<String, Object>();
38
		defaultParameters.put("properties", "frpos,frlemma");
39
		defaultParameters.put("tsvFile", corpus.getName()+"_annotations.tsv");
40
		
41
		ExecuteGroovyMacro.execute(script.getAbsolutePath(), part, selection, "", parameters, defaultParameters);
40 42
		return null;
41 43
	}
42 44
}
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/commands/function/WordPropertiesFromTable.java (revision 3431)
34 34
		File script = new File(Toolbox.getTxmHomePath(),         "/scripts/groovy/user/org/txm/macro/annotation/ImportWordPropertiesFromTableMacro.groovy");
35 35
		File parametersFile = new File(Toolbox.getTxmHomePath(), "/scripts/groovy/user/org/txm/macro/annotation/ImportWordPropertiesFromTableMacro.properties");
36 36

  
37
		ExecuteGroovyMacro.execute(script.getAbsolutePath(), part, selection, "", null);
37
		HashMap<String, Object> defaultParameters = new HashMap<String, Object>();
38
		defaultParameters.put("properties", "frpos,frlemma");
39
		defaultParameters.put("tsvFile", corpus.getName()+"_annotations.tsv");
40
		
41
		ExecuteGroovyMacro.execute(script.getAbsolutePath(), part, selection, "", null, defaultParameters);
38 42
		return null;
39 43
	}
40 44
	
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/views/fileexplorer/MacroExplorer.java (revision 3431)
271 271
					IWorkbenchPart page = acWindow.getActivePage().getActivePart();
272 272
					
273 273
					if (selectedItem.getName().toLowerCase().endsWith(".groovy")) {
274
						ExecuteGroovyMacro.execute(selectedItem.getAbsolutePath(), page, selection, "", null);
274
						ExecuteGroovyMacro.execute(selectedItem.getAbsolutePath(), page, selection, "", null, null);
275 275
					}
276 276
					else {
277 277
						String ext = FileUtils.getExtension(selectedItem);
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/handlers/scripts/ExecuteLastGroovyScript.java (revision 3431)
90 90
			return;
91 91
		}
92 92
		
93
		ExecuteGroovyScript.executeScript(currentRootDir, lastScript.getAbsolutePath(), page, selection, false, "", null);
93
		ExecuteGroovyScript.executeScript(currentRootDir, lastScript.getAbsolutePath(), page, selection, false, "", null, null);
94 94
	}
95 95
}
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/handlers/scripts/ExecuteGroovyMacro.java (revision 3431)
113 113
			LastOpened.set(ID, new File(result));
114 114
		}
115 115

  
116
		return execute(result, page, selection, stringArgs, null);
116
		return execute(result, page, selection, stringArgs, null, null);
117 117
	}
118 118
	
119
	public static IStatus execute(String scriptpath, IWorkbenchPart page, ISelection selection, String stringArgs, HashMap<String, Object> parameters) {
119
	public static IStatus execute(String scriptpath, IWorkbenchPart page, ISelection selection, String stringArgs, HashMap<String, Object> parameters, HashMap<String, Object> defaultParameters) {
120 120
		//IPreferencesService service = Platform.getPreferencesService();
121 121
		String scriptRootDir = Toolbox.getTxmHomePath() + "/scripts"; //$NON-NLS-1$
122 122
		File currentRootDir = new File(scriptRootDir, "groovy/user"); //$NON-NLS-1$
123 123
		
124 124
		if (scriptpath.endsWith(".groovy")) {
125
			return ExecuteGroovyScript.executeScript(currentRootDir, scriptpath, page, selection, false, stringArgs, parameters);
125
			return ExecuteGroovyScript.executeScript(currentRootDir, scriptpath, page, selection, false, stringArgs, parameters, defaultParameters);
126 126
		} else {
127 127
			return ExecuteScript.executeScript(scriptpath, page, selection, stringArgs);
128 128
		}
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/handlers/scripts/ExecuteGroovyScript.java (revision 3431)
164 164
		String scriptRootDir = Toolbox.getTxmHomePath() + "/scripts"; //$NON-NLS-1$
165 165
		File currentRootDir = new File(scriptRootDir, "groovy/user"); //$NON-NLS-1$
166 166

  
167
		return executeScript(currentRootDir, scriptpath, page, selection, modal, stringArgs, null);
167
		return executeScript(currentRootDir, scriptpath, page, selection, modal, stringArgs, null, null);
168 168
	}
169 169

  
170 170
	/**
......
196 196
	 * @param page current active page, might be null
197 197
	 * @param sel current selection, might be null
198 198
	 */
199
	public static IStatus executeScript(final File currentRootDir, String scriptpath, final  IWorkbenchPart page, final ISelection sel, final boolean modal, final String stringArgs, HashMap<String, Object> parameters) {
199
	public static IStatus executeScript(final File currentRootDir, String scriptpath, final  IWorkbenchPart page, final ISelection sel, final boolean modal, final String stringArgs, HashMap<String, Object> parameters, HashMap<String, Object> defaultParameters) {
200 200
		// check script
201 201
		final File scriptfile = new File(scriptpath);
202 202
		if (!scriptfile.exists()) {
......
261 261
					binding.setProperty("MONITOR", this); //$NON-NLS-1$
262 262
					binding.setProperty("stringArgs", stringArgs); //$NON-NLS-1$
263 263
					if (parameters != null) binding.setProperty("args", parameters); //$NON-NLS-1$
264
					if (defaultParameters != null) binding.setProperty("defaultArgs", defaultParameters); //$NON-NLS-1$
264 265
					binding.setProperty("gse", gse);
265 266
					Timer timer = new Timer();
266 267
					binding.setProperty("timer", timer); //$NON-NLS-1$

Formats disponibles : Unified diff