1 package jimm.datavision; 2 import jimm.util.XMLWriter; 3 import jimm.util.I18N; 4 import java.util.Observable ; 5 import java.util.ArrayList ; 6 import java.util.Iterator ; 7 import java.util.Date ; 8 import java.io.StringWriter ; 9 import java.text.SimpleDateFormat ; 10 import java.text.ParsePosition ; 11 12 25 public class Parameter 26 extends Observable  27 implements Identity, Nameable, Writeable, Draggable, Cloneable  28 { 29 30 public static final int TYPE_BOOLEAN = 0; 31 public static final int TYPE_STRING = 1; 32 public static final int TYPE_NUMERIC = 2; 33 public static final int TYPE_DATE = 3; 34 35 public static final int ARITY_ONE = 0; 36 public static final int ARITY_RANGE = 1; 37 public static final int ARITY_LIST_SINGLE = 2; 38 public static final int ARITY_LIST_MULTIPLE = 3; 39 40 protected static SimpleDateFormat formatter = 41 new SimpleDateFormat ("yyyy-MM-dd"); 42 protected static ParsePosition parsePosition = new ParsePosition (0); 43 44 protected Long id; 45 protected Report report; 46 protected String name; 47 protected String question; 48 protected int type; 49 protected int arity; 50 protected ArrayList defaultValues; 51 protected ArrayList values; 52 53 61 public Parameter(Long id, Report report) 62 { 63 this(id, report, "string", "", "", "single"); 64 } 65 66 83 public Parameter(Long id, Report report, String typeName, String name, 84 String question, String arityString) 85 { 86 this.report = report; 87 88 if (typeName == null || typeName.length() == 0) { 90 String str = I18N.get("Parameter.param_cap") + " " + id + ": " 91 + I18N.get("Parameter.missing_type"); 92 throw new IllegalArgumentException (str); 93 } 94 95 typeName = typeName.toLowerCase().trim(); 96 if ("boolean".equals(typeName)) type = TYPE_BOOLEAN; 97 else if ("string".equals(typeName)) type = TYPE_STRING; 98 else if ("numeric".equals(typeName)) type = TYPE_NUMERIC; 99 else if ("date".equals(typeName)) type = TYPE_DATE; 100 else { 101 String str = I18N.get("Parameter.param_cap") + " " + id + ": " 102 + I18N.get("Parameter.illegal_type"); 103 throw new IllegalArgumentException (str); 104 } 105 106 this.name = name; 107 this.question = question; 108 109 if (arityString == null || arityString.length() == 0) { 111 String str = I18N.get("Parameter.param_cap") + id + ": " 112 + I18N.get("Parameter.missing_arity"); 113 throw new IllegalArgumentException (str); 114 } 115 arityString = arityString.toLowerCase().trim(); 116 if ("single".equals(arityString)) arity = ARITY_ONE; 117 else if ("range".equals(arityString)) arity = ARITY_RANGE; 118 else if ("list-single".equals(arityString)) arity = ARITY_LIST_SINGLE; 119 else if ("list-multiple".equals(arityString)) arity = ARITY_LIST_MULTIPLE; 120 else { 121 String str = I18N.get("Parameter.param_cap") + id + ": " 122 + I18N.get("Parameter.illegal_arity"); 123 throw new IllegalArgumentException (str); 124 } 125 126 initialize(id); 127 } 128 129 148 public Parameter(Long id, Report report, int type, String name, 149 String question, int arity) 150 { 151 this.report = report; 152 this.type = type; 153 this.name = name; 154 this.question = question; 155 this.arity = arity; 156 157 initialize(id); 158 } 159 160 private void initialize(Long id) { 161 if (id == null) 162 id = report.generateNewParameterId(); 163 this.id = id; 164 165 switch (type) { 167 case TYPE_BOOLEAN: 168 if (arity != ARITY_ONE) { 169 String str = I18N.get("Parameter.param_cap") + id + ": " 170 + I18N.get("Parameter.yesno_single"); 171 throw new IllegalArgumentException (str); 172 } 173 break; 174 case TYPE_DATE: 175 if (arity != ARITY_ONE && arity != ARITY_RANGE) { 176 String str = I18N.get("Parameter.param_cap") + id + ": " 177 + I18N.get("Parameter.date_arity_err"); 178 throw new IllegalArgumentException (str); 179 } 180 break; 181 } 182 183 defaultValues = new ArrayList (); 184 values = new ArrayList (); 185 } 186 187 public Object clone() { 188 Parameter p = new Parameter(null, report, type, name, question, arity); 189 for (Iterator iter = defaultValues.iterator(); iter.hasNext(); ) { 190 Object obj = iter.next(); 191 if (obj instanceof Boolean ) 192 p.defaultValues.add(obj); 193 else if (obj instanceof String ) 194 p.defaultValues.add(new String ((String )obj)); 195 else if (obj instanceof Number ) 196 p.defaultValues.add(obj); 197 else if (obj instanceof Date ) 198 p.defaultValues.add(((Date )obj).clone()); 199 } 200 return p; 201 } 202 203 public Object getId() { return id; } 204 205 210 public String getName() { return name; } 211 212 217 public void setName(String newName) { 218 if (name != newName && (name == null || !name.equals(newName))) { 219 name = newName; 220 setChanged(); 221 notifyObservers(); 222 } 223 } 224 225 230 public String getQuestion() { return question; } 231 232 237 public void setQuestion(String newQuestion) { 238 if (question != newQuestion 239 && (question == null || !question.equals(newQuestion))) 240 { 241 question = newQuestion; 242 setChanged(); 243 notifyObservers(); 244 } 245 } 246 247 254 public int getType() { return type; } 255 256 268 public void setType(int newType) { 269 if (type != newType) { 270 type = newType; 271 272 defaultValues.clear(); 273 values.clear(); 274 275 if (type == TYPE_BOOLEAN) { 276 if (arity != ARITY_ONE) 277 arity = ARITY_ONE; 278 } 279 else if (type == TYPE_DATE) { 280 if (arity == ARITY_LIST_SINGLE || arity == ARITY_LIST_MULTIPLE) 281 arity = ARITY_ONE; 282 } 283 284 setChanged(); 285 notifyObservers(); 286 } 287 } 288 289 296 public int getArity() { return arity; } 297 298 309 public boolean isLegal(int aType, int anArity) { 310 switch (aType) { 311 case TYPE_BOOLEAN: 312 return anArity == ARITY_ONE; 313 case TYPE_DATE: 314 return anArity != ARITY_LIST_SINGLE && anArity != ARITY_LIST_MULTIPLE; 315 case TYPE_STRING: 316 case TYPE_NUMERIC: 317 default: 318 return true; 319 } 320 } 321 322 331 public void setArity(int newArity) { 332 if (arity != newArity) { 333 334 if (type == TYPE_BOOLEAN) { 335 if (newArity != ARITY_ONE) { 336 String str = I18N.get("Parameter.param_cap") + id + ": " 337 + I18N.get("Parameter.yesno_single"); 338 throw new IllegalArgumentException (str); 339 } 340 } 341 else if (type == TYPE_DATE) { 342 if (newArity == ARITY_LIST_SINGLE 343 || newArity == ARITY_LIST_MULTIPLE) 344 { 345 String str = I18N.get("Parameter.param_cap") + id + ": " 346 + I18N.get("Parameter.date_arity_err"); 347 throw new IllegalArgumentException (str); 348 } 349 } 350 351 arity = newArity; 352 353 defaultValues.clear(); 354 values.clear(); 355 356 setChanged(); 357 notifyObservers(); 358 } 359 } 360 361 366 public Iterator defaultValues() { return defaultValues.iterator(); } 367 368 376 public Object getDefaultValue(int i) { 377 Object val; 378 if (i < 0 || i >= defaultValues.size() 379 || (val = defaultValues.get(i)) == null) 380 { 381 return getDefaultForType(type); 382 } 383 else 384 return val; 385 } 386 387 397 public Object getDefaultForType(int type) { 398 switch (type) { 399 case TYPE_BOOLEAN: return Boolean.valueOf(false); 400 case TYPE_STRING: return ""; 401 case TYPE_NUMERIC: return new Integer (0); 402 case TYPE_DATE: return new Date (); 403 default: 404 String str = I18N.get("Paramter.illegal_type_value"); 405 throw new IllegalArgumentException (str + " " + type); 406 } 407 } 408 409 412 public void removeDefaultValues() { 413 if (defaultValues.size() > 0) { 414 defaultValues.clear(); 415 setChanged(); 416 notifyObservers(); 417 } 418 } 419 420 425 public void addDefaultValue(Object newDefaultValue) { 426 newDefaultValue = convertType(newDefaultValue); 428 429 defaultValues.add(newDefaultValue); 430 setChanged(); 431 notifyObservers(); 432 } 433 434 441 public void setDefaultValue(int i, Object newDefaultValue) { 442 newDefaultValue = convertType(newDefaultValue); 444 445 Object defaultValue = null; 446 if (i < defaultValues.size()) 447 defaultValue = getDefaultValue(i); 448 if (defaultValue != newDefaultValue 449 && (defaultValue == null || !defaultValue.equals(newDefaultValue))) 450 { 451 defaultValues.add(i, newDefaultValue); 452 setChanged(); 453 notifyObservers(); 454 } 455 } 456 457 462 public Iterator values() { return values.iterator(); } 463 464 471 public Object getValue() { 472 switch (arity) { 473 case ARITY_ONE: 474 case ARITY_LIST_SINGLE: 475 return getValue(0); 476 case ARITY_RANGE: 477 ArrayList list = new ArrayList (); 478 list.add(getValue(0)); 479 list.add(getValue(1)); 480 return list; 481 case ARITY_LIST_MULTIPLE: 482 return values.clone(); 483 } 484 return null; } 486 487 494 public Object getValue(int i) { 495 Object val = null; 496 if (i < values.size()) 497 val = values.get(i); 498 if (val == null) { 499 if (i < defaultValues.size()) 500 val = defaultValues.get(i); 501 } 502 return val; 503 } 504 505 510 public void addValue(Object newValue) { 511 values.add(convertType(newValue)); 512 setChanged(); 513 notifyObservers(); 514 } 515 516 519 public void removeValues() { 520 if (values.size() > 0) { 521 values.clear(); 522 setChanged(); 523 notifyObservers(); 524 } 525 } 526 527 534 public void setValue(int i, Object newValue) { 535 values.add(i, convertType(newValue)); 537 538 setChanged(); 539 notifyObservers(); 540 } 541 542 559 protected Object convertType(Object val) { 560 if (val == null) 561 return null; 562 563 switch (type) { 564 case TYPE_BOOLEAN: if (val instanceof Boolean ) 566 return val; 567 else if (val instanceof String ) { 568 val = ((String )val).toLowerCase().trim(); 569 if ("true".equals(val) || "t".equals(val) 570 || "yes".equals(val) || "y".equals(val)) 571 return Boolean.valueOf(true); 572 else 573 return Boolean.valueOf(false); 574 } 575 else if (val instanceof Number ) { 576 return Boolean.valueOf(((Number )val).doubleValue() == 0); 577 } 578 else { 579 return Boolean.valueOf(true); } 581 case TYPE_STRING: return val.toString(); 583 case TYPE_NUMERIC: if (val instanceof Number ) 585 return val; 586 else { String str = val.toString(); 588 if (str.length() == 0) 589 return new Integer (0); 590 else if (str.indexOf(".") == -1) 591 return new Integer (str); 592 else 593 return new Double (str); 594 } 595 case TYPE_DATE: if (val instanceof Date ) 597 return val; 598 else { String str = val.toString(); 600 if (str.length() == 0) 601 return new Date (); 602 else { 603 parsePosition.setIndex(0); 604 return formatter.parse(str, parsePosition); 605 } 606 } 607 default: return null; 609 } 610 } 611 612 618 protected String typeString() { 619 switch (type) { 620 case TYPE_BOOLEAN: return "boolean"; 621 case TYPE_STRING: return "string"; 622 case TYPE_NUMERIC: return "numeric"; 623 case TYPE_DATE: return "date"; 624 default: return "unknown"; } 626 } 627 628 public String dragString() { 629 return "parameter:" + getId(); 630 } 631 632 public String designLabel() { 633 return "{?" + getName() + "}"; 634 } 635 636 public String formulaString() { 637 return "{?" + getId() + "}"; 638 } 639 640 645 public void writeXML(XMLWriter out) { 646 String arityString = null; 647 switch (arity) { 648 case ARITY_ONE: arityString = "single"; break; 649 case ARITY_RANGE: arityString = "range"; break; 650 case ARITY_LIST_SINGLE: arityString = "list-single"; break; 651 case ARITY_LIST_MULTIPLE: arityString = "list-multiple"; break; 652 } 653 654 out.startElement("parameter"); 655 out.attr("id", id); 656 out.attr("type", typeString()); 657 out.attr("name", name); 658 out.attr("question", question); 659 out.attr("arity", arityString); 660 661 for (Iterator iter = defaultValues.iterator(); iter.hasNext(); ) 662 out.textElement("default", iter.next().toString()); 663 664 out.endElement(); 665 } 666 667 public String toString() { 668 StringWriter sw = new StringWriter (); 669 writeXML(new XMLWriter(sw)); 670 return sw.toString(); 671 } 672 673 } 674 | Popular Tags |