KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > cmd > ParamEditCommand


1 package jimm.datavision.gui.cmd;
2 import jimm.datavision.Parameter;
3 import jimm.util.I18N;
4 import java.util.List JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Iterator JavaDoc;
7
8 /**
9  * A command for changing a {@link Parameter}'s values---not the runtime
10  * values, but the default values presented to the user as initial choices.
11  *
12  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
13  */

14 public class ParamEditCommand extends CommandAdapter {
15
16 Parameter param;
17 String JavaDoc newName;
18 String JavaDoc newQuestion;
19 int newType;
20 int newArity;
21 List JavaDoc newDefaultValues;
22 String JavaDoc oldName;
23 String JavaDoc oldQuestion;
24 int oldType;
25 int oldArity;
26 List JavaDoc oldDefaultValues;
27
28 /**
29  * Constructor.
30  *
31  * @param param the parameter
32  * @param name the new name of this parameter
33  * @param question the new question
34  * @param type the new type; one of the <code>Parameter</code> constants
35  * <code>TYPE_BOOLEAN</code>, <code>TYPE_STRING</code>,
36  * <code>TYPE_NUMERIC</code>, or <code>TYPE_DATE</code>
37  * @param arity one of the <code>Parameter</code> constants
38  * <code>ARITY_ONE</code>, <code>ARITY_RANGE</code>,
39  * <code>ARITY_LIST_SINGLE</code>, or <code>ARITY_LIST_MULTIPLE</code>
40  * @param defaultValues the new list of parameter default values
41  */

42 public ParamEditCommand(Parameter param, String JavaDoc name, String JavaDoc question,
43                 int type, int arity, List JavaDoc defaultValues)
44 {
45     super(I18N.get("ParamEditCommand.name"));
46
47     this.param = param;
48     newName = name;
49     newQuestion = question;
50     newType = type;
51     newArity = arity;
52     newDefaultValues = defaultValues;
53
54     oldName = param.getName();
55     oldQuestion = param.getQuestion();
56     oldType = param.getType();
57     oldArity = param.getArity();
58     oldDefaultValues = new ArrayList JavaDoc();
59     for (Iterator JavaDoc iter = param.defaultValues(); iter.hasNext(); )
60     oldDefaultValues.add(iter.next());
61 }
62
63 public void perform() {
64     editParam(newName, newQuestion, newType, newArity, newDefaultValues);
65 }
66
67 public void undo() {
68     editParam(oldName, oldQuestion, oldType, oldArity, oldDefaultValues);
69 }
70
71 protected void editParam(String JavaDoc name, String JavaDoc question, int type, int arity,
72              List JavaDoc defaultValues)
73 {
74     param.setName(name);
75     param.setQuestion(question);
76     param.setType(type);
77     param.setArity(arity);
78
79     param.removeDefaultValues();
80     switch (arity) {
81     case Parameter.ARITY_ONE:
82     // Dates don't store a default value
83
if (type != Parameter.TYPE_DATE)
84         param.addDefaultValue(defaultValues.get(0));
85     break;
86     case Parameter.ARITY_RANGE:
87     // Dates don't store a default value
88
if (type != Parameter.TYPE_DATE) {
89         param.addDefaultValue(defaultValues.get(0));
90         param.addDefaultValue(defaultValues.get(1));
91     }
92     break;
93     case Parameter.ARITY_LIST_SINGLE:
94     case Parameter.ARITY_LIST_MULTIPLE:
95     for (Iterator JavaDoc iter = defaultValues.iterator(); iter.hasNext(); )
96         param.addDefaultValue(iter.next());
97     break;
98     }
99 }
100
101 }
102
Popular Tags