001: package jimm.datavision.gui.cmd;
002:
003: import jimm.datavision.Parameter;
004: import jimm.util.I18N;
005: import java.util.List;
006: import java.util.ArrayList;
007: import java.util.Iterator;
008:
009: /**
010: * A command for changing a {@link Parameter}'s values---not the runtime
011: * values, but the default values presented to the user as initial choices.
012: *
013: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
014: */
015: public class ParamEditCommand extends CommandAdapter {
016:
017: Parameter param;
018: String newName;
019: String newQuestion;
020: int newType;
021: int newArity;
022: List newDefaultValues;
023: String oldName;
024: String oldQuestion;
025: int oldType;
026: int oldArity;
027: List oldDefaultValues;
028:
029: /**
030: * Constructor.
031: *
032: * @param param the parameter
033: * @param name the new name of this parameter
034: * @param question the new question
035: * @param type the new type; one of the <code>Parameter</code> constants
036: * <code>TYPE_BOOLEAN</code>, <code>TYPE_STRING</code>,
037: * <code>TYPE_NUMERIC</code>, or <code>TYPE_DATE</code>
038: * @param arity one of the <code>Parameter</code> constants
039: * <code>ARITY_ONE</code>, <code>ARITY_RANGE</code>,
040: * <code>ARITY_LIST_SINGLE</code>, or <code>ARITY_LIST_MULTIPLE</code>
041: * @param defaultValues the new list of parameter default values
042: */
043: public ParamEditCommand(Parameter param, String name,
044: String question, int type, int arity, List defaultValues) {
045: super (I18N.get("ParamEditCommand.name"));
046:
047: this .param = param;
048: newName = name;
049: newQuestion = question;
050: newType = type;
051: newArity = arity;
052: newDefaultValues = defaultValues;
053:
054: oldName = param.getName();
055: oldQuestion = param.getQuestion();
056: oldType = param.getType();
057: oldArity = param.getArity();
058: oldDefaultValues = new ArrayList();
059: for (Iterator iter = param.defaultValues(); iter.hasNext();)
060: oldDefaultValues.add(iter.next());
061: }
062:
063: public void perform() {
064: editParam(newName, newQuestion, newType, newArity,
065: newDefaultValues);
066: }
067:
068: public void undo() {
069: editParam(oldName, oldQuestion, oldType, oldArity,
070: oldDefaultValues);
071: }
072:
073: protected void editParam(String name, String question, int type,
074: int arity, List defaultValues) {
075: param.setName(name);
076: param.setQuestion(question);
077: param.setType(type);
078: param.setArity(arity);
079:
080: param.removeDefaultValues();
081: switch (arity) {
082: case Parameter.ARITY_ONE:
083: // Dates don't store a default value
084: if (type != Parameter.TYPE_DATE)
085: param.addDefaultValue(defaultValues.get(0));
086: break;
087: case Parameter.ARITY_RANGE:
088: // Dates don't store a default value
089: if (type != Parameter.TYPE_DATE) {
090: param.addDefaultValue(defaultValues.get(0));
091: param.addDefaultValue(defaultValues.get(1));
092: }
093: break;
094: case Parameter.ARITY_LIST_SINGLE:
095: case Parameter.ARITY_LIST_MULTIPLE:
096: for (Iterator iter = defaultValues.iterator(); iter
097: .hasNext();)
098: param.addDefaultValue(iter.next());
099: break;
100: }
101: }
102:
103: }
|