Révision 2727

tmp/org.txm.rcp/src/main/java/org/txm/rcp/swt/widget/parameters/ParametersDialog.java (revision 2727)
41 41

  
42 42
@SuppressWarnings("rawtypes")
43 43
public class ParametersDialog extends Dialog {
44
	
44 45
	Object bean;
46
	
45 47
	String script;
48
	
46 49
	String title;
47

  
48
	List<NamedOptionDef> parameters;	
49
	List<String> fieldNames = new ArrayList<String>(); // store all the declared parameters names 
50
	LinkedHashMap<String, ParameterField> widgets = new LinkedHashMap<String, ParameterField>();
50
	
51
	List<NamedOptionDef> parameters;
52
	
53
	List<String> fieldNames = new ArrayList<>(); // store all the declared parameters names
54
	
55
	LinkedHashMap<String, ParameterField> widgets = new LinkedHashMap<>();
56
	
51 57
	protected boolean error = false;
58
	
52 59
	protected Map args; // store values given by another Macro, if contains all the needed values, no dialog is shown
60
	
53 61
	protected int numberOfParameters; // number of parameters to show in the dialog box (not set by 'args'), if 0 parameters no dialog is shown
62
	
54 63
	protected boolean mustShowDialog; // show dialog if args.size == 0 OR if there is at least one mandatory field to set
55

  
56
	HashMap<String, Object> values = new HashMap<String, Object>(); // store the values of the parameters (may not contains all the values needed
64
	
65
	HashMap<String, Object> values = new HashMap<>(); // store the values of the parameters (may not contains all the values needed
66
	
57 67
	Properties defaultValues; // default values set by the .properties file
68
	
58 69
	Properties defaultScriptValues = new Properties(); // default values set by the Groovy script @Field@def annotation attribute
59

  
70
	
60 71
	File propFile;
61
	//	private int titleWidth;
62
	//	private Composite parent;
63

  
72
	// private int titleWidth;
73
	// private Composite parent;
74
	
64 75
	/**
65 76
	 * 
66 77
	 * @param parentShell
......
72 83
		super(parentShell);
73 84
		if (bean != null)
74 85
			this.script = bean.getClass().getName();
75
		this.parameters = new ArrayList<NamedOptionDef>();
86
		this.parameters = new ArrayList<>();
76 87
		this.bean = bean;
77 88
		this.args = args;
78 89
		setShellStyle(getShellStyle() | SWT.RESIZE);
79

  
90
		
80 91
		// count the number of fields to show. If "0" then don't show the dialog, see the "open()" function
81 92
		// and select only the NamedOptionDef annotations
82 93
		numberOfParameters = 0; // number of parameters not set by args
83 94
		int numberOfMandatoryFields = 0;
84 95
		for (OptionHandler arg : allParameters) {
85 96
			if (arg.option instanceof NamedOptionDef) {
86
				NamedOptionDef option = (NamedOptionDef)arg.option;
97
				NamedOptionDef option = (NamedOptionDef) arg.option;
87 98
				this.parameters.add(option);
88 99
				fieldNames.add(option.name());
89 100
				if (args.containsKey(option.name())) {
90 101
					continue; // don't show the field
91 102
				}
92 103
				
93
				//count number of mandatory fields set AND add exception for "Separator" widget types
104
				// count number of mandatory fields set AND add exception for "Separator" widget types
94 105
				if (option.required() || "Separator".equals(option.widget())) numberOfMandatoryFields++;
95 106
				numberOfParameters++;
96 107
			}
97 108
		}
98

  
109
		
99 110
		mustShowDialog = numberOfMandatoryFields > 0 || (args.size() == 0 && numberOfParameters > 0);
100

  
111
		
101 112
		// initialize the properties file default values store
102 113
		defaultValues = new Properties();
103
		String scriptRootDir = Toolbox.getTxmHomePath() + "/scripts"; //$NON-NLS-1$
104
		File currentRootDir = new File(scriptRootDir, "macro"); //$NON-NLS-1$
105

  
106
		propFile = new File(currentRootDir, this.script.replace(".", "/")+".properties");
107

  
114
		String scriptRootDir = Toolbox.getTxmHomePath() + "/scripts/groovy/user"; //$NON-NLS-1$
115
		
116
		propFile = new File(scriptRootDir, this.script.replace(".", "/") + ".properties");
117
		
108 118
		loadDefaultValuesFromPropFile();
109

  
110 119
		
120
		
111 121
	}
112

  
113

  
122
	
123
	
114 124
	private void loadDefaultValuesFromPropFile() {
115 125
		if (propFile.exists()) {
116 126
			try {
117 127
				defaultValues.load(new FileReader(propFile));
118
				Log.info("Retrieving previous macro parameters from "+propFile+" values="+defaultValues.entrySet() + "...");
119
			} catch (Exception e) {
120
				Log.severe("Failed to load previous values from "+propFile+": "+e);
128
				Log.info("Retrieving previous macro parameters from " + propFile + " values=" + defaultValues.entrySet() + "...");
129
			}
130
			catch (Exception e) {
131
				Log.severe("Failed to load previous values from " + propFile + ": " + e);
121 132
				Log.printStackTrace(e);
122 133
			}
123 134
		}
124 135
		
125
		//initialize the script default values
126
				for (NamedOptionDef option : this.parameters) {
127
					String widgetName = option.widget();
128
					String stringValue = option.def();
129
					Object value = null;
130
					if ("File".equals(widgetName) || "FileOpen".equals(widgetName)) { //$NON-NLS-1$
131
						value = new File(stringValue);
132
					} else if ("CreateFile".equals(widgetName) || "FileSave".equals(widgetName)) { //$NON-NLS-1$
133
						value = new File(stringValue);
134
					} else if ("Folder".equals(widgetName)) { //$NON-NLS-1$
135
						value = new File(stringValue);
136
					} else if ("Query".equals(widgetName)) { //$NON-NLS-1$
137
						value = new CQLQuery(stringValue);
138
					} else if ("Date".equals(widgetName)) { //$NON-NLS-1$
139
						try {
140
							value = DateField.formater.parse(stringValue);
141
						} catch (ParseException e) {
142
							Log.severe("Wrong default date format: "+stringValue+". Waiting for: "+DateField.STRINGFORMAT+". Error = "+e + ".");
143
							value = new Date();
144
						}
145
					} else if ("Time".equals(widgetName)) { //$NON-NLS-1$
146
						value = Integer.parseInt(stringValue);
147
					} else if ("String".equals(widgetName)) { //$NON-NLS-1$
148
						value = stringValue;
149
					} else if ("Separator".equals(widgetName)) { //$NON-NLS-1$
150
						value = stringValue;
151
					} else if ("Password".equals(widgetName)) { //$NON-NLS-1$
152
							value = stringValue;
153
					} else if ("StringArray".equals(widgetName)) { //$NON-NLS-1$
154
						value = stringValue;
155
					} else if ("StringArrayMultiple".equals(widgetName)) { //$NON-NLS-1$
156
						value = stringValue;
157
					} else if ("StructuralUnits".equals(widgetName)) { //$NON-NLS-1$
158
						value = stringValue;
159
					} else if ("Text".equals(widgetName)) { //$NON-NLS-1$
160
						value = stringValue;
161
					} else if ("Integer".equals(widgetName)) { //$NON-NLS-1$
162
						value = Integer.parseInt(stringValue);
163
					} else if ("Float".equals(widgetName)) { //$NON-NLS-1$
164
						value = Float.parseFloat(stringValue);
165
					} else if ("Boolean".equals(widgetName)) { //$NON-NLS-1$
166
						value = Boolean.parseBoolean(stringValue);
167
					} else {
168
						System.out.println(TXMUIMessages.unknowedWidgetNameColon+widgetName+TXMUIMessages.FileTreeContentProvider_4);
169
					}
136
		// initialize the script default values
137
		for (NamedOptionDef option : this.parameters) {
138
			String widgetName = option.widget();
139
			String stringValue = option.def();
140
			Object value = null;
141
			if ("File".equals(widgetName) || "FileOpen".equals(widgetName)) { //$NON-NLS-1$
142
				value = new File(stringValue);
143
			}
144
			else if ("CreateFile".equals(widgetName) || "FileSave".equals(widgetName)) { //$NON-NLS-1$
145
				value = new File(stringValue);
146
			}
147
			else if ("Folder".equals(widgetName)) { //$NON-NLS-1$
148
				value = new File(stringValue);
149
			}
150
			else if ("Query".equals(widgetName)) { //$NON-NLS-1$
151
				value = new CQLQuery(stringValue);
152
			}
153
			else if ("Date".equals(widgetName)) { //$NON-NLS-1$
154
				try {
155
					value = DateField.formater.parse(stringValue);
170 156
				}
157
				catch (ParseException e) {
158
					Log.severe("Wrong default date format: " + stringValue + ". Waiting for: " + DateField.STRINGFORMAT + ". Error = " + e + ".");
159
					value = new Date();
160
				}
161
			}
162
			else if ("Time".equals(widgetName)) { //$NON-NLS-1$
163
				value = Integer.parseInt(stringValue);
164
			}
165
			else if ("String".equals(widgetName)) { //$NON-NLS-1$
166
				value = stringValue;
167
			}
168
			else if ("Separator".equals(widgetName)) { //$NON-NLS-1$
169
				value = stringValue;
170
			}
171
			else if ("Password".equals(widgetName)) { //$NON-NLS-1$
172
				value = stringValue;
173
			}
174
			else if ("StringArray".equals(widgetName)) { //$NON-NLS-1$
175
				value = stringValue;
176
			}
177
			else if ("StringArrayMultiple".equals(widgetName)) { //$NON-NLS-1$
178
				value = stringValue;
179
			}
180
			else if ("StructuralUnits".equals(widgetName)) { //$NON-NLS-1$
181
				value = stringValue;
182
			}
183
			else if ("Text".equals(widgetName)) { //$NON-NLS-1$
184
				value = stringValue;
185
			}
186
			else if ("Integer".equals(widgetName)) { //$NON-NLS-1$
187
				value = Integer.parseInt(stringValue);
188
			}
189
			else if ("Float".equals(widgetName)) { //$NON-NLS-1$
190
				value = Float.parseFloat(stringValue);
191
			}
192
			else if ("Boolean".equals(widgetName)) { //$NON-NLS-1$
193
				value = Boolean.parseBoolean(stringValue);
194
			}
195
			else {
196
				System.out.println(TXMUIMessages.unknowedWidgetNameColon + widgetName + TXMUIMessages.FileTreeContentProvider_4);
197
			}
198
		}
171 199
	}
172

  
173

  
174
	/* (non-Javadoc)
200
	
201
	
202
	/*
203
	 * (non-Javadoc)
175 204
	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
176 205
	 */
177 206
	@Override
......
179 208
		super.configureShell(newShell);
180 209
		int idx2 = script.lastIndexOf("Macro");
181 210
		if (idx2 < 0) idx2 = script.length();
182
		title = TXMUIMessages.bind(TXMUIMessages.p0ParametersInput,script.substring(script.lastIndexOf(".")+1, idx2));
211
		title = TXMUIMessages.bind(TXMUIMessages.p0ParametersInput, script.substring(script.lastIndexOf(".") + 1, idx2));
183 212
		newShell.setText(title);
184 213
	}
185

  
214
	
215
	@Override
186 216
	protected void createButtonsForButtonBar(Composite parent) {
187
		//create the reset button (hack of the Back button)
188
//		Button propButton = createButton(parent, IDialogConstants.BACK_ID,
189
//				"...", false);
190
//		propButton.removeListener(SWT.Selection, propButton.getListeners(SWT.Selection)[0]);
191
//
192
//		propButton.addSelectionListener(new SelectionAdapter() {
193
//			@Override
194
//			public void widgetSelected(SelectionEvent e) {
195
//				FileDialog dialog = new FileDialog(e.display.getActiveShell());
196
//				dialog.setFilterPath(propFile.getParent());
197
//				dialog.setFilterExtensions(new String[] {"*.properties"});
198
//				dialog.setFileName(propFile.getName());
199
//				String path = dialog.open();
200
//				if (path != null) {
201
//					propFile = new File(path);
202
//					loadDefaultValuesFromPropFile();
203
//				}
204
//			}
205
//		});
217
		// create the reset button (hack of the Back button)
218
		// Button propButton = createButton(parent, IDialogConstants.BACK_ID,
219
		// "...", false);
220
		// propButton.removeListener(SWT.Selection, propButton.getListeners(SWT.Selection)[0]);
221
		//
222
		// propButton.addSelectionListener(new SelectionAdapter() {
223
		// @Override
224
		// public void widgetSelected(SelectionEvent e) {
225
		// FileDialog dialog = new FileDialog(e.display.getActiveShell());
226
		// dialog.setFilterPath(propFile.getParent());
227
		// dialog.setFilterExtensions(new String[] {"*.properties"});
228
		// dialog.setFileName(propFile.getName());
229
		// String path = dialog.open();
230
		// if (path != null) {
231
		// propFile = new File(path);
232
		// loadDefaultValuesFromPropFile();
233
		// }
234
		// }
235
		// });
206 236
		
207 237
		Button button = createButton(parent, IDialogConstants.BACK_ID,
208 238
				"Reset values", false);
209 239
		button.removeListener(SWT.Selection, button.getListeners(SWT.Selection)[0]);
210

  
240
		
211 241
		button.addSelectionListener(new SelectionAdapter() {
242
			
212 243
			@Override
213 244
			public void widgetSelected(SelectionEvent e) {
214
				//System.out.println("Reseting fields to default values");
245
				// System.out.println("Reseting fields to default values");
215 246
				for (String k : widgets.keySet()) {
216 247
					widgets.get(k).resetToDefault();
217 248
				}
218 249
			}
219 250
		});
220 251
		if (numberOfParameters == 0) button.setEnabled(false);
221

  
252
		
222 253
		super.createButtonsForButtonBar(parent);
223 254
		this.getButton(IDialogConstants.OK_ID).setText(TXMUIMessages.run);
224 255
	}
225

  
256
	
257
	@Override
226 258
	protected Control createButtonBar(Composite parent) {
227 259
		Control c = super.createButtonBar(parent);
228

  
229
		//		int barWidth = c.getSize().x;
230
		//		if (barWidth > titleWidth) {
231
		//			GridData cdata = (GridData)(this.parent.getLayoutData());
232
		//			cdata.widthHint = barWidth;
233
		//		}
234

  
260
		
261
		// int barWidth = c.getSize().x;
262
		// if (barWidth > titleWidth) {
263
		// GridData cdata = (GridData)(this.parent.getLayoutData());
264
		// cdata.widthHint = barWidth;
265
		// }
266
		
235 267
		return c;
236 268
	}
237

  
238
	/* (non-Javadoc)
269
	
270
	/*
271
	 * (non-Javadoc)
239 272
	 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
240 273
	 */
241 274
	@Override
242 275
	protected Control createDialogArea(Composite composite) {
243
		//this.parent = composite;
276
		// this.parent = composite;
244 277
		super.createDialogArea(composite);
245

  
278
		
246 279
		composite.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, true));
247 280
		
248 281
		GridLayout layout = new GridLayout(1, true);
249 282
		composite.setLayout(layout);
250

  
251
		//TODO: this width initialization does not works : it does not depends on the buttonBar width
252
		//		GridData cdata = (GridData)(composite.getLayoutData());
253
		//		titleWidth = FitFontSize.calculate(composite, title) + 100;
254
		//		cdata.widthHint = titleWidth;
255

  
283
		
284
		// TODO: this width initialization does not works : it does not depends on the buttonBar width
285
		// GridData cdata = (GridData)(composite.getLayoutData());
286
		// titleWidth = FitFontSize.calculate(composite, title) + 100;
287
		// cdata.widthHint = titleWidth;
288
		
256 289
		// initialize each widget + default value *of the widget*
257 290
		for (NamedOptionDef option : parameters) {
258
			//if (arg.option instanceof NamedOptionDef) {
259

  
260
			//NamedOptionDef option = (NamedOptionDef)arg.option;
261

  
291
			// if (arg.option instanceof NamedOptionDef) {
292
			
293
			// NamedOptionDef option = (NamedOptionDef)arg.option;
294
			
262 295
			// if the 'args' contains a value filled by another Macro, don't show the widget
263 296
			if (args.containsKey(option.name())) {
264
				//System.out.println("Args contains '"+option.name()+"' with value '"+args.get(option.name())+"'");
297
				// System.out.println("Args contains '"+option.name()+"' with value '"+args.get(option.name())+"'");
265 298
				continue; // don't show the field
266 299
			}
267

  
300
			
268 301
			// each widget is initialized with de default value set in @Field@def attribute
269 302
			String widgetName = option.widget();
270 303
			ParameterField widget = null;
271 304
			if ("File".equals(widgetName) || "FileOpen".equals(widgetName)) { //$NON-NLS-1$
272 305
				widget = new FileField(composite, SWT.NONE, option);
273
			} else if ("CreateFile".equals(widgetName) || "FileSave".equals(widgetName)) { //$NON-NLS-1$
306
			}
307
			else if ("CreateFile".equals(widgetName) || "FileSave".equals(widgetName)) { //$NON-NLS-1$
274 308
				widget = new CreateFileField(composite, SWT.NONE, option);
275
			} else if ("Folder".equals(widgetName)) { //$NON-NLS-1$
309
			}
310
			else if ("Folder".equals(widgetName)) { //$NON-NLS-1$
276 311
				widget = new FolderField(composite, SWT.NONE, option);
277
			} else if ("Query".equals(widgetName)) { //$NON-NLS-1$
312
			}
313
			else if ("Query".equals(widgetName)) { //$NON-NLS-1$
278 314
				widget = new QueryField(composite, SWT.NONE, option);
279
			} else if ("Date".equals(widgetName)) { //$NON-NLS-1$
315
			}
316
			else if ("Date".equals(widgetName)) { //$NON-NLS-1$
280 317
				widget = new DateField(composite, SWT.NONE, option);
281
			}  else if ("Time".equals(widgetName)) { //$NON-NLS-1$
318
			}
319
			else if ("Time".equals(widgetName)) { //$NON-NLS-1$
282 320
				widget = new TimeField(composite, SWT.NONE, option);
283
			} else if ("Separator".equals(widgetName)) { //$NON-NLS-1$
284
				widget = new SeparatorField(composite, SWT.NONE, option);	
285
			} else if ("String".equals(widgetName)) { //$NON-NLS-1$
286
				widget = new StringField(composite, SWT.NONE, option);	
287
			} else if ("Password".equals(widgetName)) { //$NON-NLS-1$
288
					widget = new PasswordField(composite, SWT.NONE, option);
289
			} else if ("StringArray".equals(widgetName)) { //$NON-NLS-1$
321
			}
322
			else if ("Separator".equals(widgetName)) { //$NON-NLS-1$
323
				widget = new SeparatorField(composite, SWT.NONE, option);
324
			}
325
			else if ("String".equals(widgetName)) { //$NON-NLS-1$
326
				widget = new StringField(composite, SWT.NONE, option);
327
			}
328
			else if ("Password".equals(widgetName)) { //$NON-NLS-1$
329
				widget = new PasswordField(composite, SWT.NONE, option);
330
			}
331
			else if ("StringArray".equals(widgetName)) { //$NON-NLS-1$
290 332
				widget = new StringArrayField(composite, SWT.NONE, option);
291
			} else if ("StringArrayMultiple".equals(widgetName)) { //$NON-NLS-1$
333
			}
334
			else if ("StringArrayMultiple".equals(widgetName)) { //$NON-NLS-1$
292 335
				widget = new StringArrayMultipleField(composite, SWT.NONE, option);
293
			} else if ("StructuralUnits".equals(widgetName)) { //$NON-NLS-1$
336
			}
337
			else if ("StructuralUnits".equals(widgetName)) { //$NON-NLS-1$
294 338
				widget = new CospusStructuresField(composite, SWT.NONE, option);
295
			} else if ("Text".equals(widgetName)) { //$NON-NLS-1$
339
			}
340
			else if ("Text".equals(widgetName)) { //$NON-NLS-1$
296 341
				widget = new LongStringField(composite, SWT.NONE, option);
297
			} else if ("Integer".equals(widgetName)) { //$NON-NLS-1$
342
			}
343
			else if ("Integer".equals(widgetName)) { //$NON-NLS-1$
298 344
				widget = new IntegerField(composite, SWT.NONE, option);
299
			} else if ("Float".equals(widgetName)) { //$NON-NLS-1$
345
			}
346
			else if ("Float".equals(widgetName)) { //$NON-NLS-1$
300 347
				widget = new FloatField(composite, SWT.NONE, option);
301
			} else if ("Boolean".equals(widgetName)) { //$NON-NLS-1$
348
			}
349
			else if ("Boolean".equals(widgetName)) { //$NON-NLS-1$
302 350
				widget = new BooleanField(composite, SWT.NONE, option);
303
			} else {
304
				System.out.println(TXMUIMessages.unknowedWidgetNameColon+widgetName+TXMUIMessages.FileTreeContentProvider_4);
305 351
			}
306

  
352
			else {
353
				System.out.println(TXMUIMessages.unknowedWidgetNameColon + widgetName + TXMUIMessages.FileTreeContentProvider_4);
354
			}
355
			
307 356
			if (widget != null) {
308 357
				widgets.put(option.name(), widget);
309

  
358
				
310 359
				// set default value in the fields, not enough if args contains the value
311 360
				String value = defaultValues.getProperty(option.name());
312 361
				if (value != null) { // set the default value using the properties stored
313 362
					widget.setDefault(value);
314 363
				}
315 364
			}
316
			//			}
365
			// }
317 366
		}
318 367
		new Label(composite, SWT.NONE).setText(TXMUIMessages.mandatoryFields);
319

  
368
		
320 369
		return composite;
321 370
	}
322

  
323
	/* (non-Javadoc)
371
	
372
	/*
373
	 * (non-Javadoc)
324 374
	 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
325 375
	 */
326 376
	@Override
327 377
	protected void okPressed() {
328 378
		error = false;
329

  
379
		
330 380
		// get widget values
331 381
		for (String field : widgets.keySet()) {
332 382
			ParameterField w = widgets.get(field);
333 383
			Object value = w.getWidgetValue();
334

  
384
			
335 385
			// is widget set if mandatory EXCEPT "Separator" widgets
336 386
			if (w.isWidgetMandatory() && value == null && !("Separator".equals(w.getWidgetType()))) {
337
				System.out.println(TXMUIMessages.errorColonMissingValueColon+field);
387
				System.out.println(TXMUIMessages.errorColonMissingValueColon + field);
338 388
				error = true;
339
			} else if (value != null) {
389
			}
390
			else if (value != null) {
340 391
				values.put(field, value);
341
				//System.out.println(""+field+"\t"+values.get(field));
392
				// System.out.println(""+field+"\t"+values.get(field));
342 393
			} // else value is not filled
343 394
		}
344

  
345
		//initialize variables
395
		
396
		// initialize variables
346 397
		if (!error) {
347 398
			initializeMacroVariables();
348 399
			saveParameters();
349

  
400
			
350 401
			if (!error) {
351 402
				super.okPressed();
352 403
			}
353
		} 
404
		}
354 405
	}
355

  
406
	
356 407
	private void saveParameters() {
357 408
		if (!error) {
358 409
			try {
359 410
				propFile.getParentFile().mkdirs();
360 411
				defaultValues.store(new FileWriter(propFile), "");
361
			} catch (IOException e) {
362
				System.out.println("Warning: failed to store default macro valuesin "+propFile+": "+e);
412
			}
413
			catch (IOException e) {
414
				System.out.println("Warning: failed to store default macro valuesin " + propFile + ": " + e);
363 415
				Log.printStackTrace(e);
364 416
			}
365 417
		}
366 418
	}
367

  
368

  
419
	
420
	
369 421
	/**
370 422
	 * can use the args map to initialize variable not set with the dialog box
371 423
	 */
372 424
	private void initializeMacroVariables() {
373
		//		System.out.println("Initialize macro variables of "+fieldNames);
425
		// System.out.println("Initialize macro variables of "+fieldNames);
374 426
		if (bean == null) return;
375 427
		
376 428
		Class c = bean.getClass();
377
		for (String name :fieldNames) {
429
		for (String name : fieldNames) {
378 430
			Object value = values.get(name);
379
//			if (value == null) { // no widget or not set by user
380
//				value = defaultScriptValues.get(name);
381
//			}
382
//			if (value == null) { // no default set by script
383
//				value = defaultValues.get(name);
384
//			}
385
			//System.out.println("test if args contains name="+name+" "+args.containsKey(name));
431
			// if (value == null) { // no widget or not set by user
432
			// value = defaultScriptValues.get(name);
433
			// }
434
			// if (value == null) { // no default set by script
435
			// value = defaultValues.get(name);
436
			// }
437
			// System.out.println("test if args contains name="+name+" "+args.containsKey(name));
386 438
			if (args.containsKey(name)) { // the 'args' variable may contains values set by another Macro
387 439
				value = args.get(name);
388
			} else { // store the default value only if user set it in the dialog box
440
			}
441
			else { // store the default value only if user set it in the dialog box
389 442
				if (value != null) {
390 443
					defaultValues.setProperty(name, value.toString());
391 444
				}
392 445
			}
393

  
446
			
394 447
			try {
395
				//System.out.println("setting "+name+ " with "+values.get(name));
448
				// System.out.println("setting "+name+ " with "+values.get(name));
396 449
				Field field = c.getDeclaredField(name);
397 450
				field.setAccessible(true);
398 451
				field.set(bean, value);
399
			}  catch (Exception e) {
400
				System.out.println(TXMUIMessages.errorInAtOptionNameDeclarationColon+e.getMessage());
452
			}
453
			catch (Exception e) {
454
				System.out.println(TXMUIMessages.errorInAtOptionNameDeclarationColon + e.getMessage());
401 455
				Log.printStackTrace(e);
402 456
				error = true;
403 457
			}
404

  
458
			
405 459
		}
406 460
	}
407

  
408

  
461
	
462
	
409 463
	protected boolean isError() {
410 464
		return error;
411 465
	}
412

  
466
	
413 467
	public HashMap<String, Object> getValues() {
414 468
		return values;
415 469
	}
416

  
417
	static boolean errorOpen,retOpen;
470
	
471
	static boolean errorOpen, retOpen;
472
	
418 473
	/**
419 474
	 * Conveniance method to initialize the parameters dialog.
420 475
	 * Can detect the "args" Groovy binding and use it to set variables
......
427 482
		errorOpen = true;
428 483
		retOpen = false;
429 484
		
430
//		if (!(bean instanceof groovy.lang.Script)) {
431
//			System.out.println(Messages.ParametersDialog_15);
432
//			return false;
433
//		}
434

  
485
		// if (!(bean instanceof groovy.lang.Script)) {
486
		// System.out.println(Messages.ParametersDialog_15);
487
		// return false;
488
		// }
489
		
435 490
		Object oArgs = null;
436 491
		if (bean instanceof groovy.lang.Script) {
437 492
			groovy.lang.Script script = (groovy.lang.Script) bean;
438

  
493
			
439 494
			// if the "args" variable exists then use its values. Can be used to set a default value by another Macro
440 495
			if (script.getBinding().hasVariable("args")) {
441 496
				oArgs = script.getBinding().getVariable("args"); //$NON-NLS-1$
442 497
			}
443 498
		}
444

  
499
		
445 500
		final Map args;
446 501
		if (oArgs == null || !(oArgs instanceof Map)) {
447 502
			args = new HashMap<String, Object>();
448
		} else {
449
			args = (Map)oArgs;
450 503
		}
504
		else {
505
			args = (Map) oArgs;
506
		}
451 507
		
452 508
		if (bean instanceof groovy.lang.Script) { // create the args binding if not already existing
453 509
			groovy.lang.Script script = (groovy.lang.Script) bean;
......
457 513
		}
458 514
		
459 515
		Display.getDefault().syncExec(new Runnable() {
460
			public void run() { 
516
			
517
			@Override
518
			public void run() {
461 519
				boolean okNames = true;
462 520
				CmdLineParser parser = new CmdLineParser(bean);
463

  
521
				
464 522
				Display display = Display.getCurrent();
465 523
				Shell shell = display.getActiveShell();
466

  
524
				
467 525
				// check names
468 526
				Class c = bean.getClass();
469 527
				for (OptionHandler opt : parser.getOptions()) {
470 528
					try {
471 529
						if (opt.option instanceof NamedOptionDef) {
472
							NamedOptionDef option = (NamedOptionDef)opt.option;
530
							NamedOptionDef option = (NamedOptionDef) opt.option;
473 531
							c.getDeclaredField(option.name());
474 532
						}
475
					}  catch (NoSuchFieldException e) {
476
						System.out.println(TXMUIMessages.errorInAtOptionNameDeclarationColon+e.getMessage());
533
					}
534
					catch (NoSuchFieldException e) {
535
						System.out.println(TXMUIMessages.errorInAtOptionNameDeclarationColon + e.getMessage());
477 536
						okNames = false;
478 537
					}
479 538
				}
......
484 543
					if (dialog.getMustShowDialog()) {
485 544
						retOpen = dialog.open() == ParametersDialog.OK;
486 545
						errorOpen = dialog.isError();
487
					} else { // fields initialized by 'args'
546
					}
547
					else { // fields initialized by 'args'
488 548
						dialog.initializeMacroVariables(); // with args
489 549
						if (dialog.getNumberOfFields() > 0) dialog.saveParameters();
490 550
						retOpen = true;
......
494 554
					if (bean instanceof groovy.lang.Script) { // reset the timer binding if already existing
495 555
						groovy.lang.Script script = (groovy.lang.Script) bean;
496 556
						if (script.getBinding().hasVariable("timer")) {
497
							Timer timer = (Timer)script.getBinding().getVariable("timer");
557
							Timer timer = (Timer) script.getBinding().getVariable("timer");
498 558
							timer.reset();
499 559
						}
500 560
					}
......
503 563
		});
504 564
		return retOpen && !errorOpen;
505 565
	}
506

  
566
	
507 567
	public boolean getMustShowDialog() {
508 568
		return mustShowDialog;
509 569
	}
510

  
570
	
511 571
	protected int getNumberOfFields() {
512 572
		return numberOfParameters;
513 573
	}
514

  
515
	// TODO: uncomment this to enable "args" detection  
516
	//	public int open() {
517
	//		System.out.println("Number of parameters: "+numberOfParameters);
518
	//		if (numberOfParameters == 0) {
519
	//			return ParametersDialog.OK;
520
	//		} else {
521
	//			return super.open();
522
	//		}
523
	//	}
524
}
574
	
575
	// TODO: uncomment this to enable "args" detection
576
	// public int open() {
577
	// System.out.println("Number of parameters: "+numberOfParameters);
578
	// if (numberOfParameters == 0) {
579
	// return ParametersDialog.OK;
580
	// } else {
581
	// return super.open();
582
	// }
583
	// }
584
}

Formats disponibles : Unified diff