KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > xml > XMLChoice


1 /* XMLChoice.java
2  *
3  * Authors:
4  * Stefanovic Nenad chupo@iis.ns.ac.yu
5  * Bojanic Sasa sasaboy@neobee.net
6  * Puskas Vladimir vpuskas@eunet.yu
7  * Pilipovic Goran zboniek@uns.ac.yu
8  *
9  */

10
11
12 package org.enhydra.jawe.xml;
13
14 import org.enhydra.jawe.xml.panels.*;
15
16 /**
17  * XMLChoice class instance represents program equivalence to
18  * the 'choice' element defined in some XML XML. It is used
19  * to enable user to visually choose the one of the choices,
20  * and to logically support the choice, which means to
21  * properly hold it, and to properly write it into XML document
22  * (or read it from XML document when the time comes).
23  * <p> For example if XML schema defines some choice as follows:
24  * <pre>
25  * &lt;xsd:choice>
26  * &lt;xsd:element ref="RecordType"/&gt;
27  * &lt;xsd:element ref="UnionType"/&gt;
28  * &lt;xsd:element ref="EnumerationType"/&gt;
29  * &lt;xsd:element ref="ArrayType"/&gt;
30  * &lt;xsd:element ref="ListType"/&gt;
31  * &lt;xsd:element ref="BasicType"/&gt;
32  * &lt;xsd:element ref="PlainType"/&gt;
33  * &lt;xsd:element ref="DeclaredType"/&gt;
34  * &lt;/xsd:choice&gt;
35  * </pre>
36  * such choice is represented by this class instance (actually by
37  * the instance of it's derived class XMLComplexChoice), and choices
38  * would be the instances of appropriate classes derived from
39  * XMLElement class (actually from it's derived class XMLComplexElement),
40  * which names are: 'RecordType', 'UnionType', 'EnumerationType'
41  * , ..., 'DeclaredType'. The combo box that will be shown in the panel
42  * of this element will offer this choices, and the names of the choices
43  * will be shown as language specific presentation of this choices.
44  * <p> The another example of using choices is when choices are
45  * just some predefined strings. This is the case when some
46  * XML 'attribute' has the exact number of possible choices,
47  * for e.g.:
48  * <pre>
49  * &lt;xsd:attribute name="Type" use="required"&gt;
50  * &lt;xsd:simpleType&gt;
51  * &lt;xsd:restriction base="xsd:NMTOKEN"&gt;
52  * &lt;xsd:enumeration value="STRING"/&gt;
53  * &lt;xsd:enumeration value="FLOAT"/&gt;
54  * &lt;xsd:enumeration value="INTEGER"/&gt;
55  * &lt;xsd:enumeration value="REFERENCE"/&gt;
56  * &lt;xsd:enumeration value="DATETIME"/&gt;
57  * &lt;/xsd:restriction&gt;
58  * &lt;/xsd:simpleType&gt;
59  * &lt;/xsd:attribute&gt;
60  * </pre>
61  * such choice is actually represented by the instance of derived class
62  * XMLAttribute, and choices would be the language specific strings that
63  * correspodents to the 'STRING', 'FLOAT',..., 'DATETIME'.
64  *
65  * <p>NOTE: Although this class is not declared abstract, it is
66  * uselles without redefining it's methods. The classes
67  * that are really used are derived classes {@link
68  * XMLAttribute} and {@link XMLComplexChoice}.
69  */

70 public class XMLChoice extends XMLElement {
71    /** The currently choosen element. */
72    protected Object JavaDoc choosen;
73    /** The possible choices. */
74    protected Object JavaDoc[] choices;
75
76    /**
77     * Creates a new instance of element with the specified name, and
78     * specifies the values of choices for the element. Assumes that
79     * the first choice is choosen when object is created.
80     *
81     * @param name The name of the 'choice' XML element
82     * that this class represents.
83     * @param choices The possible choices for this element.
84     */

85    public XMLChoice (String JavaDoc name,Object JavaDoc[] choices) {
86       this(name,choices,0);
87    }
88
89    /**
90     * Creates a new instance of element with the specified name,
91     * specified choices for the element, and with <b>chooseIndex</b>'th
92     * value choosen.
93     *
94     * @param name The name of the 'choice' XML element
95     * that this class represents.
96     * @param choices The possible choices for this element.
97     * @param choosenIndex The index of element to be choosen right after creation.
98     */

99    public XMLChoice (String JavaDoc name,Object JavaDoc[] choices,int choosenIndex) {
100       super(name);
101       try {
102          this.choices=choices;
103          this.choosen=choices[choosenIndex];
104          this.value=choosen;
105       }
106       catch (Exception JavaDoc e) {
107          this.value="";
108       }
109    }
110
111    /**
112     * Overrides super-method to set this element and all it's choice elements
113     * (if choices are instances of XMLElement class) read only value to the
114     * one specified.
115     *
116     * @param ro <tt>true</tt> if element should be <i>read only</i>,
117     * <tt>false</tt> otherwise.
118     */

119    public void setReadOnly (boolean ro) {
120       super.setReadOnly(ro);
121       if (choices!=null) {
122          for (int i=0; i<choices.length; i++) {
123             if (choices[i] instanceof XMLElement) {
124                ((XMLElement)choices[i]).setReadOnly(ro);
125             }
126          }
127       }
128    }
129
130    /**
131     * Overrides the super-method to set the value field and the choosen field
132     * to the specified value. The value is, in fact, one of possible choices.
133     *
134     * @param v the choice to be set.
135     */

136    public void setValue(Object JavaDoc v) {
137       super.setValue(v);
138       choosen=v;
139    }
140
141    /**
142     * The possible choices could be instances of XMLElement class, or
143     * can be instances of String.
144     *
145     * @return the possible choices for this element.
146     */

147    public Object JavaDoc[] getChoices() {
148       return choices;
149    }
150
151    /**
152     * Choosen element is one of the choices.
153     *
154     * @return the choosen element.
155     * @see #getChoices
156     */

157    public Object JavaDoc getChoosen() {
158       return choosen;
159    }
160
161    /**
162     * The default panel that represents this class visually
163     * is instance of {@link XMLComboPanel} class. The panel
164     * has the combo box that is filled with content of
165     * <code>choices</code> field.
166     *
167     * @return the XMLComboPanel which owner is this class instance.
168     */

169    public XMLPanel getPanel () {
170       return new XMLComboPanel(this);
171    }
172
173    /**
174     * Overrides super-method to apply cloning of <code>choices</code>
175     * field content, if it's content are instances of <code>XMLElement
176     * </code> class.
177     *
178     * @return the new instance that has all similar attributes as the
179     * cloned one.
180     */

181    public Object JavaDoc clone () {
182       XMLChoice d=(XMLChoice)super.clone();
183
184       d.choosen=null;
185       d.choices=null;
186
187       if (this.choices!=null && this.choices.length>0) {
188          if (this.choices[0] instanceof String JavaDoc) {
189             d.choices=this.choices;
190             d.choosen=this.choosen;
191          }
192          else {
193             d.choices=new Object JavaDoc[this.choices.length];
194             for (int i=0; i<this.choices.length; i++) {
195                Object JavaDoc och=this.choices[i];
196                if (och instanceof XMLElement) {
197                   XMLElement ch=(XMLElement)this.choices[i];
198                   d.choices[i]=ch.clone();
199                   if (this.choosen==ch) {
200                      d.choosen=d.choices[i];
201                      //System.out.println("d.choosen="+d.choosen);
202
}
203                } else {
204                   d.choices[i]=och;
205                   if (this.choosen!=null && this.choosen.equals(och)) {
206                      d.choosen=d.choices[i];
207                   }
208                }
209             }
210          }
211       }
212       return d;
213    }
214
215    public void refreshLabelName() {
216       super.refreshLabelName();
217       if (choices!=null) {
218          for (int i=0; i<choices.length; i++) {
219             if (choices[i] instanceof XMLElement) {
220                ((XMLElement) choices[i]).refreshLabelName();
221             }
222          }
223       }
224    }
225 }
226
227 /* End of XMLChoice.java */
228
Popular Tags