1 10 11 12 package org.enhydra.jawe.xml; 13 14 import org.enhydra.jawe.xml.panels.*; 15 import org.enhydra.jawe.xml.ToNameMutableTreeNode; 16 17 import javax.swing.JPanel ; 18 import javax.swing.JComponent ; 19 import javax.swing.tree.*; 20 import javax.swing.text.*; 21 22 import java.io.*; 23 import java.util.*; 24 import org.w3c.dom.*; 25 26 69 public class XMLElement implements Serializable, Cloneable { 70 71 75 protected String name; 76 80 protected String labelName; 81 86 protected Object value; 87 91 protected boolean isRequired=false; 92 93 protected boolean isReadOnly=false; 94 95 96 protected boolean isCollapsed = true; 97 98 105 public XMLElement () { 106 name=getClass().getName(); 107 int lastDot=name.lastIndexOf("."); 108 if (lastDot>=0) { 109 name=name.substring(lastDot+1,name.length()); 110 } 111 112 labelName=XMLUtil.getLanguageDependentString(name+"Key"); 113 value=new String (); 114 } 116 117 125 public XMLElement (String name) { 126 this.name=name; 127 this.labelName=XMLUtil.getLanguageDependentString(name+"Key"); 128 value=new String (); 129 } 131 132 141 public void setReadOnly (boolean ro) { 142 isReadOnly=ro; 143 } 144 145 153 public boolean isReadOnly () { 154 return isReadOnly; 155 } 156 157 165 public void setRequired (boolean r) { 166 isRequired=r; 167 } 168 169 public boolean isRequired () { 177 return isRequired; 178 } 179 180 188 public boolean isEmpty () { 189 return !(value!=null && value.toString().trim().length()>0); 190 } 191 192 199 public boolean isValid () { 200 return true; 201 } 202 203 211 public boolean isValidEnter (XMLPanel p) { 212 return true; 213 } 214 215 222 public void toXML(Node parent) throws DOMException { 223 if (parent!=null) { 225 if (null != value) { 226 Node node = (parent.getOwnerDocument()).createElement(name); 227 node.setNodeValue(toString()); 228 parent.appendChild(node); 229 } 230 } 231 } 232 233 240 public void fromXML(Node node) { 241 if (node!=null) { 242 Object newVal; 244 if (node.hasChildNodes()) { 245 newVal=node.getChildNodes().item(0).getNodeValue(); 246 } else { 248 newVal=node.getNodeValue(); 249 } 250 if (newVal!=null) { 251 value=newVal; 252 } 253 } 254 } 255 256 263 public void fromXML(String name,Node node) { 264 try { 265 this.name=name; 266 this.labelName=XMLUtil.getLanguageDependentString(name+"Key"); 267 } catch (Exception ex) {} 268 fromXML(node); 269 } 270 271 278 public XMLPanel getPanel () { 279 return new XMLPanel(); 280 } 281 282 public void setLabelName (String lName) { 283 labelName=lName; 284 } 285 286 291 public void setValue(Object v) { 292 value=v; 293 } 294 295 301 public Object toValue() { 302 return value; 303 } 304 305 312 public String toLabel () { 313 return labelName; 314 } 315 316 321 public String toName () { 322 return name; 323 } 324 325 332 public String toString () { 333 if (value!=null) { 334 return value.toString().trim(); 335 } else if (labelName!=null) { 336 return labelName; 337 } else { 338 return ""; 339 } 340 } 341 342 351 public Object clone () { 352 XMLElement d=null; 353 try { 354 d=(XMLElement)super.clone(); 356 d.name=new String (this.name); 357 if (this.labelName!=null) { 358 d.labelName=new String (this.labelName); 359 } else { 360 d.labelName=null; 361 } 362 if (value instanceof XMLElement) { 363 d.value=((XMLElement)value).clone(); 364 } else { 365 if (this.value!=null) { 366 d.value=new String (this.value.toString()); 367 } else { 368 this.value=null; 369 } 370 } 371 d.isRequired=this.isRequired; 372 d.isReadOnly=this.isReadOnly; 373 } catch (CloneNotSupportedException e) { 374 throw new Error (e.toString()); 376 } 377 return d; 378 } 379 380 public void refreshLabelName() { 381 labelName=XMLUtil.getLanguageDependentString(name+"Key"); 382 } 383 384 388 public DefaultMutableTreeNode getNode() { 389 return new ToNameMutableTreeNode(this); 390 } 391 392 protected String OFFSET = " "; 393 protected String NEWLINE = "\n"; 394 protected AttributeSet atts = new SimpleAttributeSet(); 395 396 401 public String getPrintDescription(StyledDocument doc) { 402 return this.getPrintDescription(OFFSET, doc); 403 } 404 405 409 public String getPrintDescription(String indent, StyledDocument doc) { 410 String retVal = ""; 411 try { 412 StringBuffer all = new StringBuffer (); 413 all.append( toName() + " : " + value.toString().replaceAll( "\n", " " ) ); 414 retVal = all.toString(); 415 416 AttributeSet atts = new SimpleAttributeSet(); 417 doc.insertString( doc.getLength(), retVal, atts ); 418 419 }catch(Exception e) { 420 e.printStackTrace(); 421 } 422 423 return retVal; 424 } 425 426 public void setCollapsed(boolean isCollapsed) { 427 this.isCollapsed = isCollapsed; 428 } 429 430 public boolean isCollapsed() { 431 return this.isCollapsed; 432 } 433 434 435 } 436 437 438 | Popular Tags |