00001
00002
00003
00004
package org.nees.rbnb;
00005
00006
import java.io.IOException;
00007
00008
import java.util.Hashtable;
00009
import java.util.Enumeration;
00010
00011
import javax.swing.tree.DefaultMutableTreeNode;
00012
import javax.swing.tree.MutableTreeNode;
00013
00014
import org.w3c.dom.Document;
00015
import org.w3c.dom.Element;
00016
import org.w3c.dom.NodeList;
00017
import org.w3c.dom.Node;
00018
00022 public class RBNBBaseParameterHolder
00023 {
00024
00025
final static String XML_TAG_FOR_PARAMETER_NAME =
"name";
00026
final static String XML_TAG_FOR_PARAMETER_TYPE =
"type";
00027
final static String XML_TAG_FOR_PARAMETER_DEFAULT_VALUE =
"defaultValue";
00028
final static String XML_TAG_FOR_PARAMETER_VALUE =
"value";
00029
00030
final static String BOOLEAN_TYPE =
"Boolean";
00031
final static String INTEGER_TYPE =
"Integer";
00032
final static String LONG_TYPE =
"Long";
00033
final static String FLOAT_TYPE =
"Float";
00034
final static String DOUBLE_TYPE =
"Double";
00035
final static String STRING_TYPE =
"String";
00036
00037
private String[] types =
new String[]
00038 {BOOLEAN_TYPE, INTEGER_TYPE, LONG_TYPE,
00039 FLOAT_TYPE, DOUBLE_TYPE, STRING_TYPE};
00040
private Object[] prototypes =
new Object[]
00041 {
00042
new Boolean(
true),
00043
new Integer(0),
00044
new Long(0),
00045
new Float(0.0),
00046
new Double(0.0),
00047
new String(
"")
00048 };
00049
private String name;
00050
private String type;
00051
private Object defaultValue;
00052
private Object value;
00053
00054
public RBNBBaseParameterHolder(Node node)
00055 {
00056 setFromDocNode(node);
00057 }
00058
00059
public RBNBBaseParameterHolder(String n, String t, Object d)
00060
throws IllegalArgumentException
00061 {
00062 initialize(n,t,d);
00063 }
00064
00065
public RBNBBaseParameterHolder(
RBNBBaseParameterHolder model)
00066
throws IllegalArgumentException
00067 {
00068 initialize(model.
getName(), model.
getType(), model.
getDefaultValue());
00069
if (model.
getValue() != null) setValue(model.
getValue());
00070 }
00071
00072
private void initialize(String n, String t, Object d)
00073 {
00074
if ((n == null) || n.equals(
""))
00075
throw new IllegalArgumentException(
"RBNBBase: parameter name must be defined");
00076
if ((t == null) || t.equals(
""))
00077
throw new IllegalArgumentException(
"RBNBBase: parameter type must be defined");
00078 name = n;
00079 type = t;
00080
boolean found =
false;
00081
int index = 0;
00082
00083
for (
int i = 0; (i < types.length) && !found; i++)
00084 {
00085 index = i;
00086
if (types[i].equals(t)) found =
true;
00087 }
00088
if (!found)
00089
throw new IllegalArgumentException(
"RBNBBase: Illegal Parameter Type " + t);
00090
00091
if (d != null)
00092
if (!d.getClass().equals(prototypes[index].getClass()))
00093
throw new IllegalArgumentException(
00094
"RBNBBase: Parameter type and class of default "
00095 +
"do not match; type =" + t
00096 +
"class of default = " + d.getClass());
00097 defaultValue = d;
00098 }
00099
00100
public void setValue(Object v)
00101
throws IllegalArgumentException
00102 {
00103
if (v != null)
00104 {
00105
if (defaultValue != null)
00106 {
00107
if (!defaultValue.getClass().equals(v.getClass()))
00108
throw new IllegalArgumentException(
"RBNBBase: Classes of default value and "
00109 +
"value do not match; "
00110 +
"class of default =" + defaultValue.getClass()
00111 +
"class of value = " + v.getClass());
00112 }
00113
else
00114 {
00115
int index = 0;
00116
boolean found =
false;
00117
for (
int i = 0; (i < types.length) && !found; i++)
00118 {
00119 index = i;
00120
if (types[i].equals(type)) found =
true;
00121 }
00122
if (!found)
00123
throw new IllegalArgumentException(
"RBNBBase: Illegal Parameter Type " + type);
00124
if (!v.getClass().equals(prototypes[index].getClass()))
00125
throw new IllegalArgumentException(
00126
"RBNBBase: Parameter type and class of value "
00127 +
"do not match; type =" + type
00128 +
"class of default = " + v.getClass());
00129 }
00130 }
00131 value = v;
00132 }
00133
00137 public void setValueFromString(String s) {
00138 Object test = makeTypeObject(type, s);
00139
if (test == null)
return;
00140 setValue(test);
00141 }
00142
00143
public Object getValue()
00144 {
00145
return value;
00146 }
00147
00148
public Object getDefaultValue()
00149 {
00150
return defaultValue;
00151 }
00152
00153
public void setDefaultFromTemplate(RBNBBaseParameterHolder t)
00154 {
00155 Object defaultValue = t.getDefaultValue();
00156
if (defaultValue != null)
00157
this.defaultValue = defaultValue;
00158 }
00159
00160
private void setName(String name)
00161 {
00162
this.name = name;
00163 }
00164
00165
public String getName()
00166 {
00167
return name;
00168 }
00169
00170
public String toString()
00171 {
00172
return getName();
00173 }
00174
00175
private void setType(String type)
00176 {
00177
this.type = type;
00178 }
00179
00180
public String getType()
00181 {
00182
return type;
00183 }
00184
00185
public void setFromDocNode(Node node)
00186 {
00187 String name = null;
00188 String type = null;
00189 String defaultValueString = null;
00190 String valueString = null;
00191
00192 NodeList children = node.getChildNodes();
00193
final int length = children.getLength();
00194
for(
int i = 0; i < length; i++)
00195 {
00196 Node child = children.item(i);
00197
if (child.getNodeType() == Node.ELEMENT_NODE)
00198 {
00199 Element element = (Element)child;
00200 String tag = element.getTagName();
00201
if (tag.equals(XML_TAG_FOR_PARAMETER_NAME))
00202 {
00203
00204
00205 name = child.getChildNodes().item(0).getNodeValue();
00206 }
00207
else
00208
if (tag.equals(XML_TAG_FOR_PARAMETER_TYPE))
00209 {
00210
00211
00212 type = child.getChildNodes().item(0).getNodeValue();
00213 }
00214
else
00215
if (tag.equals(XML_TAG_FOR_PARAMETER_DEFAULT_VALUE))
00216 {
00217
if (child.hasChildNodes())
00218
00219
00220 defaultValueString = child.getChildNodes().item(0).getNodeValue();
00221 }
00222
else
00223
if (tag.equals(XML_TAG_FOR_PARAMETER_VALUE))
00224 {
00225
if (child.hasChildNodes())
00226
00227
00228 valueString = child.getChildNodes().item(0).getNodeValue();
00229 }
00230 }
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
if (defaultValueString != null)
00244 initialize(name,type,makeTypeObject(type,defaultValueString));
00245
else
00246 initialize(name,type,null);
00247
if (valueString != null)
00248 setValue(makeTypeObject(type,valueString));
00249 }
00250
00251
private Object makeTypeObject(String type, String value)
00252 {
00253 Object ret = null;
00254
00255
00256
00257
00258
00259
00260
00261
try{
00262
if (type.equals(BOOLEAN_TYPE))
00263 ret =
new Boolean(value);
00264
else if (type.equals(INTEGER_TYPE))
00265 ret =
new Integer(value);
00266
else if (type.equals(LONG_TYPE))
00267 ret =
new Long(value);
00268
else if (type.equals(FLOAT_TYPE))
00269 ret =
new Float(value);
00270
else if (type.equals(DOUBLE_TYPE))
00271 ret =
new Double(value);
00272
else if (type.equals(STRING_TYPE))
00273 ret = value;
00274 }
00275
catch (Exception ignore) {}
00276
return ret;
00277 }
00278
00279
public Node createXmlNode(Document doc)
00280 {
00281 Node node,root = doc.createElement(NeesControlMetadata.XML_TAG_FOR_PARAMETER);
00282
00283 node = doc.createElement(XML_TAG_FOR_PARAMETER_NAME);
00284 node.appendChild(doc.createTextNode(name));
00285 root.appendChild(node);
00286
00287
if ((type != null) && (!type.equals(
"")))
00288 {
00289 node = doc.createElement(XML_TAG_FOR_PARAMETER_TYPE);
00290 node.appendChild(doc.createTextNode(type));
00291 root.appendChild(node);
00292 }
00293
00294
if (getDefaultValue() != null)
00295 {
00296 node = doc.createElement(XML_TAG_FOR_PARAMETER_DEFAULT_VALUE);
00297 node.appendChild(doc.createTextNode(getDefaultValue().toString()));
00298 root.appendChild(node);
00299 }
00300
00301
if (getValue() != null)
00302 {
00303 node = doc.createElement(XML_TAG_FOR_PARAMETER_VALUE);
00304 node.appendChild(doc.createTextNode(getValue().toString()));
00305 root.appendChild(node);
00306 }
00307
00308
return root;
00309 }
00310
00314 public MutableTreeNode
makeSwingTreeNode() {
00315
00316 DefaultMutableTreeNode node, root =
new DefaultMutableTreeNode(
this);
00317
if ((type != null) && (!type.equals(
"")))
00318 {
00319 node =
new DefaultMutableTreeNode(
"Type: " + type);
00320 root.add(node);
00321 }
00322
00323
if (defaultValue != null)
00324 {
00325 node =
new DefaultMutableTreeNode(
"Default: " + defaultValue.toString());
00326 root.add(node);
00327 }
00328
00329
if (value != null)
00330 {
00331 node =
new DefaultMutableTreeNode(
"Value: " + value.toString());
00332 root.add(node);
00333 }
00334
00335
return root;
00336 }
00337
00343 public static MutableTreeNode
makeParamersSwingTreeNode(Hashtable parameters) {
00344 DefaultMutableTreeNode root =
new DefaultMutableTreeNode(
"Parameters");
00345 Enumeration e = parameters.elements();
00346
while (e.hasMoreElements())
00347 {
00348
RBNBBaseParameterHolder p = (
RBNBBaseParameterHolder)e.nextElement();
00349 root.add(p.
makeSwingTreeNode());
00350 }
00351
return (MutableTreeNode)root;
00352 }
00353
00354 }