Mercurial > projects > dwt-addons
comparison dwtx/core/commands/ParameterizedCommand.d @ 71:4878bef4a38e
Some fixing
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 22 May 2008 04:03:58 +0200 |
parents | 46a6e0e6ccd4 |
children | 04b47443bb01 |
comparison
equal
deleted
inserted
replaced
70:46a6e0e6ccd4 | 71:4878bef4a38e |
---|---|
27 import tango.util.collection.model.Set; | 27 import tango.util.collection.model.Set; |
28 import tango.util.collection.ArraySeq; | 28 import tango.util.collection.ArraySeq; |
29 import tango.util.collection.HashSet; | 29 import tango.util.collection.HashSet; |
30 import tango.util.collection.HashMap; | 30 import tango.util.collection.HashMap; |
31 | 31 |
32 import dwtx.core.commands.AbstractParameterValueConverter; | |
32 import dwtx.core.commands.Command; | 33 import dwtx.core.commands.Command; |
33 import dwtx.core.commands.CommandManager; | 34 import dwtx.core.commands.CommandManager; |
34 import dwtx.core.commands.IParameter; | 35 import dwtx.core.commands.IParameter; |
35 import dwtx.core.commands.IParameterValues; | 36 import dwtx.core.commands.IParameterValues; |
37 import dwtx.core.commands.ParameterType; | |
36 import dwtx.core.commands.Parameterization; | 38 import dwtx.core.commands.Parameterization; |
37 import dwtx.core.commands.ParameterValuesException; | 39 import dwtx.core.commands.ParameterValuesException; |
40 import dwtx.core.commands.ParameterValueConversionException; | |
38 import dwtx.core.commands.ExecutionEvent; | 41 import dwtx.core.commands.ExecutionEvent; |
39 import dwtx.core.commands.common.NotDefinedException; | 42 import dwtx.core.commands.common.NotDefinedException; |
40 import dwtx.core.internal.commands.util.Util; | 43 import dwtx.core.internal.commands.util.Util; |
41 | |
42 import dwt.dwthelper.utils; | 44 import dwt.dwthelper.utils; |
43 import tango.text.convert.Format; | 45 import tango.text.convert.Format; |
44 | 46 |
45 static import tango.text.Text; | 47 static import tango.text.Text; |
46 alias tango.text.Text.Text!(char) StringBuffer; | 48 alias tango.text.Text.Text!(char) StringBuffer; |
318 } | 320 } |
319 | 321 |
320 /** | 322 /** |
321 * Take a command and a map of parameter IDs to values, and generate the | 323 * Take a command and a map of parameter IDs to values, and generate the |
322 * appropriate parameterized command. | 324 * appropriate parameterized command. |
323 * | 325 * |
324 * @param command | 326 * @param command |
325 * The command object. Must not be <code>null</code>. | 327 * The command object. Must not be <code>null</code>. |
326 * @param parameters | 328 * @param parameters |
327 * A map of String parameter ids to objects. May be | 329 * A map of String parameter ids to objects. May be |
328 * <code>null</code>. | 330 * <code>null</code>. |
329 * @return the parameterized command, or <code>null</code> if it could not | 331 * @return the parameterized command, or <code>null</code> if it could not |
330 * be generated | 332 * be generated |
331 * @since 3.4 | 333 * @since 3.4 |
332 */ | 334 */ |
333 public static final ParameterizedCommand generateCommand(Command command, | 335 public static final ParameterizedCommand generateCommand(Command command, |
334 Map parameters) { | 336 Map!(String,Object) parameters) { |
335 // no parameters | 337 // no parameters |
336 if (parameters is null || parameters.isEmpty()) { | 338 if (parameters is null || parameters.drained()) { |
337 return new ParameterizedCommand(command, null); | 339 return new ParameterizedCommand(command, null); |
338 } | 340 } |
339 | 341 |
340 try { | 342 try { |
341 ArrayList parms = new ArrayList(); | 343 Parameterization[] parms; |
342 Iterator i = parameters.keySet().iterator(); | |
343 | 344 |
344 // iterate over given parameters | 345 // iterate over given parameters |
345 while (i.hasNext()) { | 346 foreach( key, value; parameters ){ |
346 String key = (String) i.next(); | |
347 IParameter parameter = null; | 347 IParameter parameter = null; |
348 // get the parameter from the command | 348 // get the parameter from the command |
349 parameter = command.getParameter(key); | 349 parameter = command.getParameter(key); |
350 | 350 |
351 // if the parameter is defined add it to the parameter list | 351 // if the parameter is defined add it to the parameter list |
352 if (parameter is null) { | 352 if (parameter is null) { |
353 return null; | 353 return null; |
354 } | 354 } |
355 ParameterType parameterType = command.getParameterType(key); | 355 ParameterType parameterType = command.getParameterType(key); |
356 if (parameterType is null) { | 356 if (parameterType is null) { |
357 parms.add(new Parameterization(parameter, | 357 parms ~= new Parameterization(parameter, |
358 (String) parameters.get(key))); | 358 stringcast(value) ); |
359 } else { | 359 } else { |
360 AbstractParameterValueConverter valueConverter = parameterType | 360 AbstractParameterValueConverter valueConverter = parameterType |
361 .getValueConverter(); | 361 .getValueConverter(); |
362 if (valueConverter !is null) { | 362 if (valueConverter !is null) { |
363 String val = valueConverter.convertToString(parameters | 363 String val = valueConverter.convertToString(value); |
364 .get(key)); | 364 parms ~= new Parameterization(parameter, val); |
365 parms.add(new Parameterization(parameter, val)); | |
366 } else { | 365 } else { |
367 parms.add(new Parameterization(parameter, | 366 parms ~= new Parameterization(parameter, |
368 (String) parameters.get(key))); | 367 stringcast(value)); |
369 } | 368 } |
370 } | 369 } |
371 } | 370 } |
372 | 371 |
373 // convert the parameters to an Parameterization array and create | 372 // convert the parameters to an Parameterization array and create |
374 // the command | 373 // the command |
375 return new ParameterizedCommand(command, (Parameterization[]) parms | 374 return new ParameterizedCommand(command, parms ); |
376 .toArray(new Parameterization[parms.size()])); | |
377 } catch (NotDefinedException e) { | 375 } catch (NotDefinedException e) { |
378 } catch (ParameterValueConversionException e) { | 376 } catch (ParameterValueConversionException e) { |
379 } | 377 } |
380 return null; | 378 return null; |
381 } | 379 } |
429 int parmIndex = 0; | 427 int parmIndex = 0; |
430 Parameterization[] params = new Parameterization[parameterizations.length]; | 428 Parameterization[] params = new Parameterization[parameterizations.length]; |
431 for (int j = 0; j < parms.length; j++) { | 429 for (int j = 0; j < parms.length; j++) { |
432 for (int i = 0; i < parameterizations.length; i++) { | 430 for (int i = 0; i < parameterizations.length; i++) { |
433 Parameterization pm = parameterizations[i]; | 431 Parameterization pm = parameterizations[i]; |
434 if (parms[j].equals(pm.getParameter())) { | 432 if ((cast(Object)parms[j]).opEquals(cast(Object)pm.getParameter())) { |
435 params[parmIndex++] = pm; | 433 params[parmIndex++] = pm; |
436 } | 434 } |
437 } | 435 } |
438 } | 436 } |
439 this.parameterizations = params; | 437 this.parameterizations = params; |
627 return parameterMap; | 625 return parameterMap; |
628 } | 626 } |
629 | 627 |
630 /* | 628 /* |
631 * (non-Javadoc) | 629 * (non-Javadoc) |
632 * | 630 * |
633 * @see java.lang.Object#hashCode() | 631 * @see java.lang.Object#hashCode() |
634 */ | 632 */ |
635 public override final hash_t toHash() { | 633 public override final hash_t toHash() { |
636 if (hashCode is HASH_CODE_NOT_COMPUTED) { | 634 if (hashCode is HASH_CODE_NOT_COMPUTED) { |
637 hashCode = HASH_INITIAL * HASH_FACTOR + Util.toHash(command); | 635 hashCode = HASH_INITIAL * HASH_FACTOR + Util.toHash(command); |