1 64 65 68 package com.jcorporate.expresso.core.controller; 69 70 import com.jcorporate.expresso.core.cache.Cacheable; 71 import com.jcorporate.expresso.core.dbobj.ValidValue; 72 import com.jcorporate.expresso.core.misc.ConfigManager; 73 import com.jcorporate.expresso.core.misc.StringUtil; 74 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 75 import org.w3c.dom.NamedNodeMap ; 76 import org.w3c.dom.Node ; 77 import org.w3c.dom.NodeList ; 78 79 import java.util.ArrayList ; 80 import java.util.Enumeration ; 81 import java.util.Iterator ; 82 import java.util.List ; 83 import java.util.Vector ; 84 85 86 108 public class Input 109 extends ControllerElement 110 implements Cloneable , Cacheable { 111 112 115 public static final String SELECTED = "selected"; 116 117 120 private ArrayList defaultValue = null; 121 122 125 private Vector validValues = null; 126 127 131 private String lookup = null; 132 133 136 private int maxLength = 80; 137 138 139 142 private String multiple = "false"; 143 144 147 private String key = null; 148 149 152 153 156 public static final String ATTRIBUTE_LISTBOX = "listbox"; 157 158 161 public static final String ATTRIBUTE_DROPDOWN = "dropdown"; 162 163 166 public static final String ATTRIBUTE_CHECKBOX = "checkbox"; 167 168 171 public static final String ATTRIBUTE_CHECKBOX_VERTICAL = "checkbox-vertical"; 172 173 176 public static final String ATTRIBUTE_RADIO = "radio"; 177 178 179 182 public static final String ATTRIBUTE_RADIO_VERTICAL = "radio-vertical"; 183 184 187 public static final String ATTRIBUTE_TEXTAREA = "textarea"; 188 189 192 public static final String ATTRIBUTE_HIDDEN = "hidden"; 193 194 195 198 public static final String ATTRIBUTE_READONLY = "readOnly"; 199 200 201 204 public static final String ATTRIBUTE_FILE = "fileBox"; 205 206 207 210 public static final String ATTRIBUTE_PASSWORD = "password"; 211 212 216 public static final String ATTRIBUTE_ORIGINAL_VALUE = "originalValue"; 217 218 221 public static final String ATTRIBUTE_CSS_STYLE = "styleClass"; 222 223 227 public static final String ATTRIBUTE_MULTIVALUED = "multiValued"; 228 229 232 public static final String ATTRIBUTE_TEXTLINE = "text"; 233 234 235 238 public static final String ATTRIBUTE_TYPE = "type"; 239 240 241 244 public Input() { 245 super(); 246 setType(null); 247 } 248 249 255 public Input(String newName) { 256 super(); 257 setName(newName); 258 setType(null); 259 } 260 261 262 268 public Input(String newName, String newLabel) { 269 super(); 270 setName(newName); 271 setLabel(newLabel); 272 setType(null); 273 } 274 275 280 public Object clone() 281 throws CloneNotSupportedException { 282 Input i; 283 284 synchronized (this) { i = (Input) super.clone(); 286 if (defaultValue != null) { 287 i.defaultValue = (ArrayList ) defaultValue.clone(); 288 } 289 i.maxLength = maxLength; 290 i.lookup = (lookup); 291 i.key = key; 292 if (validValues != null) { 293 i.validValues = (Vector ) validValues.clone(); 294 } 295 if (getAttributes().size() > 0) { 296 i.getAttributes().putAll(getAttributes()); 297 } 298 } 299 300 return i; 301 } 302 303 304 312 public String getDefaultValue() { 313 String result = ""; 314 if (defaultValue != null) { 315 result = (String ) defaultValue.get(0); 316 } 317 318 return result; 319 } 320 321 330 public String getContent() { 331 return getDefaultValue(); 332 } 333 334 341 public ArrayList getDefaultValueList() { 342 if (defaultValue == null) { 343 defaultValue = new ArrayList (); 344 } 345 346 return (ArrayList ) defaultValue.clone(); 347 } 348 349 355 public String getLookup() { 356 return lookup; 357 } 358 359 366 public int getMaxLength() { 367 return maxLength; 368 } 369 370 377 public FastStringBuffer toXML(FastStringBuffer stream) { 378 Vector vv = this.getValidValues(); 379 380 if (vv == null) { 381 vv = new Vector (); 382 } 383 384 stream.append("<input"); 385 386 if (this.getName() != null && this.getName().length() > 0) { 387 stream.append(" name=\"" + StringUtil.xmlEscape(this.getName()) + 388 "\""); 389 } 390 if (vv.size() > 0) { 391 stream.append(" multivalue=\"y\""); 392 } 393 if (defaultValue != null && defaultValue.size() > 0) { 394 stream.append(" default-value=\"" + 395 StringUtil.xmlEscape(this.getDefaultValue()) + "\""); 396 } 397 if (this.getMaxLength() > 0) { 398 stream.append(" max-length=\"" + this.getMaxLength() + "\""); 399 } 400 if (!StringUtil.notNull(this.getLookup()).equals("")) { 401 stream.append(" lookup-object=\"" + 402 StringUtil.xmlEscape(this.getLookup()) + "\""); 403 } 404 if (this.getType() != null && this.getType().length() > 0) { 405 stream.append(" type=\"" + StringUtil.xmlEscape(this.getType()) + 406 "\""); 407 } 408 409 stream.append(">\n"); 410 411 if (vv.size() > 0) { 412 ValidValue oneValue = null; 413 stream.append(" <valid-values>\n"); 414 415 for (Enumeration e = vv.elements(); e.hasMoreElements();) { 416 oneValue = (ValidValue) e.nextElement(); 417 stream.append(" <valid-value value=\"" + 418 StringUtil.xmlEscape(oneValue.getValue()) + 419 "\" description=\"" + 420 StringUtil.xmlEscape(oneValue.getDescription()) + 421 "\" />\n"); 422 } 423 424 stream.append(" </valid-values>\n"); 425 } 426 427 super.toXML(stream); 429 stream.append("</input>\n"); 430 431 return stream; 432 } 433 434 443 public static ControllerElement fromXML(Node n) 444 throws ControllerException { 445 446 if (n.getNodeName().equals("#document")) { 448 return fromXML(n.getChildNodes().item(0)); 449 } 450 if (!n.getNodeName().equals("input")) { 451 throw new ControllerException("Failed To Get DOM Node of " + 452 " type 'input' Got " + 453 n.getNodeName() + " instead."); 454 } 455 456 Input i = new Input(); 457 NamedNodeMap attributes = n.getAttributes(); 458 Node oneAttribute = attributes.getNamedItem("name"); 459 460 if (oneAttribute != null) { 461 i.setName(oneAttribute.getNodeValue()); 462 } 463 464 oneAttribute = attributes.getNamedItem("default-value"); 465 466 if (oneAttribute != null) { 467 i.setDefaultValue(oneAttribute.getNodeValue()); 468 } 469 470 oneAttribute = attributes.getNamedItem("max-length"); 471 472 if (oneAttribute != null) { 473 try { 474 i.maxLength = Integer.parseInt(oneAttribute.getNodeValue()); 475 } catch (NumberFormatException nfe) { 476 } 477 } 478 479 oneAttribute = attributes.getNamedItem("lookup-object"); 480 481 if (oneAttribute != null) { 482 i.lookup = oneAttribute.getNodeValue(); 483 } 484 485 oneAttribute = attributes.getNamedItem("type"); 486 487 if (oneAttribute != null) { 488 i.setType(oneAttribute.getNodeValue()); 489 } 490 491 NodeList children = n.getChildNodes(); 492 493 for (int index = 0; index < children.getLength(); index++) { 494 Node oneChild = children.item(index); 495 String nodeName = oneChild.getNodeName(); 496 497 if (nodeName != null) { 498 if (nodeName.equals("valid-values")) { 499 NodeList vv = oneChild.getChildNodes(); 500 501 for (int j = 0; j < vv.getLength(); j++) { 502 Node onevalue = vv.item(j); 503 504 if (onevalue.getNodeName().equals("valid-value")) { 505 NamedNodeMap valueAttributes = onevalue.getAttributes(); 506 507 if (valueAttributes != null) { 508 String value; 509 String description; 510 Node attribute = valueAttributes.getNamedItem("value"); 511 512 if (attribute != null) { 513 value = StringUtil.notNull(attribute.getNodeValue()); 514 attribute = valueAttributes.getNamedItem("description"); 515 516 if (attribute == null) { 517 description = (""); 518 } else { 519 description = attribute.getNodeValue(); 520 } 521 522 i.addValidValue(value, description); 523 } 524 } 525 } 526 } 527 } else if (nodeName.equals("controller-element")) { 528 i = (Input) ControllerElement.fromXML(oneChild, i); 529 } 530 } 531 } 532 533 return i; 534 } 535 536 546 public Vector getValidValues() { 547 if (validValues == null) { 548 return new Vector (); 549 } 550 return validValues; 551 } 552 553 560 public void setDefaultValue(String newValue) { 561 if (newValue == null) { 562 if (defaultValue != null) { 564 defaultValue.clear(); 565 } 566 567 return; 568 } 569 570 if (defaultValue == null) { 571 defaultValue = new ArrayList (); 572 } 573 574 if (defaultValue.size() == 0) { 575 defaultValue.add(newValue); 576 } else { 577 defaultValue.set(0, newValue); 578 } 579 } 580 581 588 public void setDefaultValue(List list) { 589 if (list == null) { 590 if (defaultValue != null) { 592 defaultValue.clear(); 593 } 594 595 return; 596 } 597 598 if (defaultValue == null) { 599 defaultValue = new ArrayList (list); 600 } else { 601 defaultValue.clear(); 602 defaultValue.addAll(list); 603 } 604 } 605 606 613 public void addDefaultValue(String newValue) { 614 if (newValue == null) { 615 return; } 617 618 if (defaultValue == null) { 619 defaultValue = new ArrayList (); 620 } 621 622 defaultValue.add(newValue); 623 } 624 625 631 public void setDefaultValue(ControllerResponse response) 632 throws ControllerException { 633 setDefaultValue(response.getFormCache(this.getName())); 634 } 635 636 643 public void setLookup(String s) { 644 lookup = s; 645 } 646 647 653 public void setMaxLength(int newMaxLength) { 654 maxLength = newMaxLength; 655 } 656 657 664 public void setName(String newName) { 665 if (ConfigManager.isParameterReservedWord(newName)) { 666 throw new IllegalArgumentException ("You cannot have a input name of " + newName + 667 ". It is a reserved word. Check " + 668 "com.jcorporate.expresso.core.misc.ConfigManager for a full list" + 669 " of reservered words"); 670 } 671 672 super.setName(newName); 673 } 674 675 681 public synchronized void addValidValue(String value, String descrip) { 682 ValidValue v = new ValidValue(value, descrip); 683 684 if (validValues == null) { 685 validValues = new Vector (); 686 } 687 688 validValues.addElement(v); 689 } 690 691 696 public void setType(String newType) { 697 if (newType == null) { 698 super.setType("C"); 699 } else { 700 super.setType(newType); 701 } 702 } 703 704 711 public void setValidValues(Vector v) { 712 validValues = v; 713 } 714 715 720 public synchronized void setKey(String newKey) { 721 key = newKey; 722 } 723 724 729 public String getKey() { 730 return key; 731 } 732 733 734 739 public void setMultiple(String newMultiple) { 740 multiple = newMultiple; 741 } 742 743 748 public String getMultiple() { 749 return multiple; 750 } 751 752 759 public String getSelectedDisplay() { 760 String result = ""; 761 String currentIndex = getDefaultValue(); 762 if (currentIndex != null && validValues != null) { 763 for (Iterator iterator = validValues.iterator(); iterator.hasNext();) { 764 ValidValue vv = (ValidValue) iterator.next(); 765 if (currentIndex.equals(vv.getValue())) { 766 result = vv.getDescription(); 767 break; 768 } 769 } 770 } 771 772 if (result == null) { 773 result = ""; 774 } 775 return result; 776 } 777 } 778 | Popular Tags |