001: package org.enhydra.jawe.base.panel;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import org.enhydra.shark.xpdl.XMLComplexChoice;
007: import org.enhydra.shark.xpdl.XMLComplexElement;
008: import org.enhydra.shark.xpdl.XMLElement;
009: import org.enhydra.shark.xpdl.XMLEmptyChoiceElement;
010:
011: /**
012: * Helper element for displaying choices of other elements.
013: *
014: * @author Sasa Bojanic
015: */
016: public class SpecialChoiceElement extends XMLComplexChoice {
017:
018: protected XMLElement controlled;
019: protected String controlledSubElementName;
020: protected String choosenSubElementName;
021: protected XMLEmptyChoiceElement emptyChoice;
022: protected boolean handleEmptyChoice;
023: protected boolean useOriginalElementToName;
024:
025: public SpecialChoiceElement(XMLElement controlled,
026: String controlledSubElementName, List chs, Object chsn,
027: boolean handleEmptyChoice, String chsnSubElementName,
028: String name, boolean isRequired) {
029: this (controlled, controlledSubElementName, chs, chsn,
030: handleEmptyChoice, chsnSubElementName, name,
031: isRequired, true);
032: }
033:
034: public SpecialChoiceElement(XMLElement controlled,
035: String controlledSubElementName, List chs, Object chsn,
036: boolean handleEmptyChoice, String chsnSubElementName,
037: String name, boolean isRequired,
038: boolean useOriginalElementToName) {
039:
040: super (null, name, isRequired);
041: this .controlled = controlled;
042: this .controlledSubElementName = controlledSubElementName;
043: this .choosenSubElementName = chsnSubElementName;
044: this .handleEmptyChoice = handleEmptyChoice;
045: if (handleEmptyChoice || chsn == null) {
046: emptyChoice = new XMLEmptyChoiceElement(null);
047: // if (!handleEmptyChoice) {
048: // emptyChoice.setValue("Unknown!!!");
049: // }
050: }
051: if (chsn == null || chsn instanceof String) {
052: if (emptyChoice == null) {
053: emptyChoice = new XMLEmptyChoiceElement(null);
054: }
055: if (chsn != null) {
056: emptyChoice.setValue((String) chsn);
057: }
058: chs.add(0, emptyChoice);
059: chsn = emptyChoice;
060: }
061: if (chs != null) {
062: this .choices = new ArrayList(chs);
063: }
064: this .choosen = (XMLElement) chsn;
065: this .useOriginalElementToName = useOriginalElementToName;
066: }
067:
068: public void setChoosen(XMLElement ch) {
069: // System.out.println("SC "+ch);
070: if (ch != null) {
071: super .setChoosen(ch);
072: try {
073: String newval = choosen.toValue();
074: if (!choosenSubElementName.equals("")) {
075: if (!(choosen instanceof XMLEmptyChoiceElement)) {
076: newval = ((XMLComplexElement) choosen).get(
077: choosenSubElementName).toValue();
078: }
079: }
080: if (!controlledSubElementName.equals("")) {
081: ((XMLComplexElement) controlled).set(
082: controlledSubElementName, newval);
083: } else {
084: controlled.setValue(newval);
085: }
086: } catch (Exception ex) {
087: ex.printStackTrace();
088: }
089: }
090: }
091:
092: public void setValue(String v) {
093: // System.out.println("SV "+v);
094: super .setValue(v);
095: if (emptyChoice != null) {
096: emptyChoice.setValue(v);
097: choosen = emptyChoice;
098: }
099: if (!handleEmptyChoice)
100: return;
101: if (!controlledSubElementName.equals("")) {
102: ((XMLComplexElement) controlled).set(
103: controlledSubElementName, v);
104: } else {
105: controlled.setValue(v);
106: }
107: }
108:
109: protected void fillChoices() {
110: }
111:
112: public void setReadOnly(boolean readOnly) {
113: this .isReadOnly = readOnly;
114: }
115:
116: public String toName() {
117: if (useOriginalElementToName) {
118: return controlled.toName();
119: }
120: return super .toName();
121: }
122:
123: public XMLElement getControlledElement() {
124: return controlled;
125: }
126:
127: }
|