KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* XMLAttribute.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 import java.util.*;
17 import javax.swing.*;
18 import java.awt.*;
19
20 import java.io.*;
21 import org.w3c.dom.*;
22
23 /**
24 * Represents any attribute in a sence of the XML. This class
25 * extends the {@link XMLChoice} class in order to be able to
26 * select some predefined values for attribute if such exists,
27 * otherwise, it is intended to hold the attribute name
28 * and it's user defined (or automaticaly) defined value.
29 * <p> Like any other class which base class is <code>
30 * XMLElement</code> class, this class provides visual way to
31 * change the value that it is supposed to take care of.
32 */

33 public class XMLAttribute extends XMLChoice {
34    /**
35    * The type of textual XMLPanel:0-XMLTextPanel,
36    * XMLLocationPanel otherwise.
37    */

38    private int XMLTextPanelType=0;
39    /** Language depended names of choices-read from property file. */
40    private String JavaDoc[] choiceNames=null;
41    /** Indicates if attribut uses XMLComboPanel or XMLRadioPanel. */
42    private boolean isComboPanel=true;
43    /**
44    Indicates if element's label and edit box will be shown
45    one above other, or one beside other.
46    */

47    private boolean isVertical=false;
48
49    /**
50    * Creates an instance of class that represents specified
51    * XML attribute, with default value for text panel
52    * type (XMLTextPanel).
53    *
54    * @param name The XML attribute name.
55    */

56    public XMLAttribute (String JavaDoc name) {
57       super(name,null);
58    }
59
60    /**
61    * Creates an instance of class that represents specified
62    * XML attribute, with specified value of text panel.
63    *
64    * @param name The XML attribute name.
65    * @param tpType The type of text panel, 1=XMLLocationPanel,
66    * 2=XMLTextPanel with password text field, 0 or
67    * anything else=ordinary XMLTextPanel
68    */

69    public XMLAttribute (String JavaDoc name,int tpType) {
70       super(name,null);
71       this.XMLTextPanelType=tpType;
72    }
73
74    /**
75    * Creates an instance of class that represents specified
76    * XML attribute that has a given set of choices to choose
77    * for attribute value, and with specified index of choice
78    * to be choosen after creation. The default setting's for
79    * panel type is used-XMLComboPanel, and default layout for
80    * label and edit box will be used (one beside other). The
81    * choices are used as a keys for search within a property
82    * file and the values read from property file are used when
83    * showing choices within combo box or at radio button labels.
84    *
85    * @param name The XML attribute name.
86    * @param choices The valid choices for this attribute value.
87    * @param choosenIndex The index of value to be choosen after creation.
88    */

89    public XMLAttribute (String JavaDoc name,String JavaDoc[] choices,int choosenIndex) {
90       this(name,choices,choosenIndex,true,false);
91    }
92
93    /**
94    * Creates an instance of class that represents specified
95    * XML attribute that has a given set of choices to choose
96    * for attribute value, the specified index of choice
97    * to be choosen after creation, the specified setting for
98    * panel type and specified layout for label and edit box.
99    * The choices are used as a keys for search within a property
100    * file and the values read from property file are used when
101    * showing choices within combo box or at radio button labels.
102    *
103    * @param name The XML attribute name.
104    * @param choices The valid choices for this attribute value.
105    * @param choosenIndex The index of value to be choosen after creation.
106    * @param isComboPanel The type of panel:<tt>true</tt> if XMLComboPanel
107    * will be used, <tt>false</tt> if XMLRadioPanel will
108    * be used.
109    * @param isVertical The type of layout:<tt>true</tt> if vertical layout
110    * will be used, <tt>false</tt> if horizontal layout will
111    * be used.
112    */

113    public XMLAttribute (String JavaDoc name,String JavaDoc[] choices,int choosenIndex,
114       boolean isComboPanel,boolean isVertical) {
115
116       super(name,(Object JavaDoc[])choices,choosenIndex);
117       this.choiceNames=new String JavaDoc[choices.length];
118       String JavaDoc nm;
119       for (int i=0; i<choices.length; i++) {
120          nm=XMLUtil.getLanguageDependentString(choices[i]+"Key");
121          if (nm!=null) {
122             this.choiceNames[i]=nm;
123          } else {
124             this.choiceNames[i]=choices[i];
125          }
126       }
127       nm=XMLUtil.getLanguageDependentString(choices[choosenIndex]+"Key");
128       if (nm!=null) {
129          this.choosen=nm;
130       } else {
131          choosen=choices[choosenIndex];
132       }
133       this.isComboPanel=isComboPanel;
134       this.isVertical=isVertical;
135    }
136
137    /**
138    * Overrides super-class method to return this class specific
139    * representation of choices.
140    *
141    * @return The language specific array of strings that correspodent
142    * to real allowed values for this attribute. If this is
143    * not an attribute with restricted set values, <tt>null</tt>
144    * is returned.
145    */

146    public Object JavaDoc[] getChoices () {
147       return choiceNames;
148    }
149
150    /**
151    * Gets the choosen value for attribute.
152    *
153    * @return The language specific presentation for
154    * one of allowed values that is choosen or
155    * <tt>null</tt> if this is not an attribute
156    * with restricted set of values.
157    */

158    public Object JavaDoc getChoosen() {
159       return choosen;
160    }
161
162    /**
163    * Indicates if element is empty.
164    *
165    * @return <tt>true</tt> if user haven't defined the element
166    * value within dialog, <tt>false</tt> otherwise.
167    */

168    public boolean isEmpty () {
169       if (choices==null) {
170          return super.isEmpty();
171       } else {
172          if (choosen==null || ((choosen instanceof String JavaDoc) && ((String JavaDoc)choosen).length()==0)) {
173             return true;
174          } else {
175             return false;
176          }
177       }
178    }
179
180    /**
181    * Overrides super-class method.
182    *
183    * @return The same as the super-class if this is not an
184    * attribute with restricted set of values,
185    * string representation of choosen value otherwise.
186    */

187    public String JavaDoc toString () {
188       if (choices==null) {
189          return super.toString();
190       } else {
191          if (choosen!=null) {
192             return choosen.toString();
193          } else {
194             return super.toString();
195          }
196       }
197    }
198
199    /**
200    * Overrides super-class method to return an attribute
201    * specific XML output.
202    */

203    public void toXML(Node parent) throws DOMException {
204       //System.out.println("Element="+name);
205
if (!isEmpty() || isRequired()) {
206          if (parent!=null) {
207             Attr node = (parent.getOwnerDocument()).createAttribute(name);
208             node.setValue(value.toString().trim());
209             ((Element) parent).setAttributeNode(node);
210          }
211       }
212    }
213
214    /**
215    * Overrides super-class method to return an attribute
216    * specific XML input.
217    */

218    public void fromXML(Node node) {
219       super.fromXML(node);
220       if (choices!=null) {
221          for (int i=0; i<choices.length; i++) {
222             if (choices[i].equals(value)) {
223                choosen=choiceNames[i];
224                break;
225             }
226          }
227       }
228    }
229
230
231    /**
232    * Returns the panel for editing an attribute element value.
233    *
234    * @return <ul>
235    * <li> {@link XMLTextPanel} or {@link XMLLocationPanel} if
236    * attribute is not an attribute with restricted values.
237    * Which of those two types of panel will be returned
238    * depends of how particular object of this class is created.
239    * <li> {@link XMLComboPanel} or {@link XMLRadioPanel} if attribute
240    * has a set of restricted values. Which of those two types of
241    * panel will be returned depends of how particular object of
242    * this class is created.
243    * </ul>
244    */

245    public XMLPanel getPanel () {
246       if (choices==null) {
247          if (XMLTextPanelType==1) {
248             return new XMLLocationPanel(this);
249          } else if (XMLTextPanelType==2) {
250             return new XMLTextPanel(this,XMLPanel.BOX_LAYOUT,false,true);
251          } else {
252             return new XMLTextPanel(this,XMLPanel.BOX_LAYOUT,
253                isVertical,false);
254          }
255       }
256       else {
257          if (isComboPanel) {
258             return new XMLComboPanel(this,XMLPanel.BOX_LAYOUT,isVertical);
259          }
260          else {
261             return new XMLRadioPanel(this,toLabel(),
262                XMLPanel.BOX_LAYOUT,isVertical);
263          }
264       }
265    }
266
267    /**
268    * If this is not an attribute with restricted set of values,
269    * the super-class method is called, and if it is not, the
270    * value that is passed is the value of language specific
271    * choice, so this value is put into the <code>choosen</code>
272    * member of this class, and the real value that corresponds to
273    * this language specific value is put into <code>value</code>member.
274    * of this class.
275    */

276    public void setValue(Object JavaDoc v) {
277       if (choices==null) {
278          super.setValue(v);
279       }
280       else {
281          if (v!=null) {
282             choosen=v.toString();
283             boolean choiceFound=false;
284             for (int i=0; i<choiceNames.length; i++) {
285                if (choiceNames[i].equals(choosen.toString())) {
286                   value=choices[i];
287                   choiceFound=true;
288                   break;
289                }
290             }
291             if (!choiceFound) {
292                value=v;
293             }
294          } else {
295             if (choices!=null && choices.length>0) {
296                value=choices[0];
297             }
298          }
299       }
300    }
301
302    /**
303    * Used to create exact copy of instance of this class.
304    * The newly created instance will have all the properties
305    * same as the copied one.
306    *
307    * @return The newly created instance of this class.
308    */

309    public Object JavaDoc clone () {
310       XMLAttribute d=(XMLAttribute)super.clone();
311       d.XMLTextPanelType=this.XMLTextPanelType;
312       d.choiceNames=this.choiceNames;
313       d.isComboPanel=this.isComboPanel;
314       return d;
315    }
316
317    public void refreshLabelName () {
318       super.refreshLabelName();
319       if (choices!=null) {
320          String JavaDoc nm, oldnm;
321          for (int i=0; i<choices.length; i++) {
322             nm=XMLUtil.getLanguageDependentString(choices[i]+"Key");
323             oldnm=choiceNames[i];
324             if (nm!=null) {
325                choiceNames[i]=nm;
326             } else {
327                choiceNames[i]=(String JavaDoc)choices[i];
328             }
329             if (choosen!=null && choosen.toString().equals(oldnm)) {
330                choosen=choiceNames[i];
331             }
332          }
333       }
334    }
335
336 }
337
338
Popular Tags