1 10 11 12 package org.enhydra.jawe.xml; 13 14 import org.enhydra.jawe.xml.ToNameMutableTreeNode; 15 import org.enhydra.jawe.xml.panels.*; 16 17 import javax.swing.tree.*; 18 import javax.swing.text.*; 19 import java.util.*; 20 import org.w3c.dom.*; 21 import org.apache.xerces.util.XMLChar; 22 23 33 public class XMLCollection extends XMLElement { 34 35 protected transient String IDPrefix=""; 36 37 private transient long nextID=0; 38 39 40 protected transient ArrayList refCollectionElements=new ArrayList(); 41 45 protected transient XMLPanel controlledPanel; 46 51 protected transient XMLControlPanel controlPanel; 52 53 54 protected transient XMLComplexElement myOwner; 55 56 protected boolean coloringTable=false; 57 58 65 public XMLCollection (XMLComplexElement myOwner) { 66 super(); 67 this.myOwner=myOwner; 68 } 69 70 public XMLCollection (XMLComplexElement myOwner,String name) { 71 super(name); 72 this.myOwner=myOwner; 73 } 74 75 public XMLComplexElement getOwner () { 76 return myOwner; 77 } 78 79 80 public void add (XMLElement el) { 81 refCollectionElements.add(el); 82 } 83 84 85 public void remove (Object el) { 86 refCollectionElements.remove(el); 87 } 88 89 90 public Object get (int no) { 91 try { 92 return refCollectionElements.get(no); 93 } catch (Exception ex) { 94 return null; 95 } 96 } 97 98 99 public int size () { 100 return refCollectionElements.size(); 101 } 102 103 104 public void clear () { 105 refCollectionElements.clear(); 106 } 107 108 116 public void refreshCollection (Set elementsToAddOrRemove,boolean append) { 117 if (append) { 118 refCollectionElements.addAll(elementsToAddOrRemove); 119 } else { 120 refCollectionElements.removeAll(elementsToAddOrRemove); 121 } 122 } 123 124 132 public XMLComplexElement getCollectionElement (String IDOrName) { 133 XMLComplexElement ce=null; 134 String ceIDOrName; 135 Iterator it=refCollectionElements.iterator(); 136 while (it.hasNext()) { 137 try { 138 XMLComplexElement cetmp=(XMLComplexElement)it.next(); 139 if (cetmp instanceof XMLCollectionElement) { 140 ceIDOrName=((XMLCollectionElement)cetmp).getID(); 141 } else { 142 ceIDOrName=cetmp.get("Name").toString(); 143 } 144 if (ceIDOrName.equals(IDOrName)) { 145 ce=cetmp; 146 break; 147 } 148 } catch (ClassCastException cce) {} } 150 return ce; 151 } 152 153 157 public Collection getElementStructure () { 158 XMLElement el=generateNewElement(); 159 if (el instanceof XMLCollectionElement) { 160 try { 161 decrementID(); 162 ((XMLCollectionElement)el).set("Id","-1"); 163 } 164 catch (Exception ex) {} 165 } 166 if (el instanceof XMLComplexElement) { 167 return ((XMLComplexElement)el).toComplexType(); 168 } else { 169 java.util.List l=new ArrayList(); 170 l.add(el); 171 return l; 172 } 173 } 174 175 181 public int[] getInvisibleTableFieldOrdinals () { 182 return null; 183 } 184 185 188 public void setReadOnly (boolean ro) { 189 isReadOnly=ro; 190 Iterator it=refCollectionElements.iterator(); 191 while (it.hasNext()) { 192 XMLElement el=(XMLElement)it.next(); 193 el.setReadOnly(ro); 194 } 195 } 196 197 198 public Collection toCollection() { 199 return refCollectionElements; 200 } 201 202 206 public Collection getTableElements() { 207 return refCollectionElements; 208 } 209 210 214 public Collection getChoosable() { 215 return refCollectionElements; 216 } 217 218 222 public XMLElement generateNewElement() { 223 return new XMLElement(); 224 } 225 226 231 public void onElementCreated (XMLElement el) { 232 return; 233 } 234 235 240 public void onElementInserted (XMLElement el) { 241 return; 242 } 243 244 249 public void onElementModified (XMLElement el) { 250 return; 251 } 252 253 258 public void onElementDeleted (XMLElement el) { 259 return; 260 } 261 262 267 public void onElementRemoved (XMLElement el) { 268 return; 269 } 270 271 276 public boolean canInsertElement (XMLElement el) { 277 return true; 278 } 279 280 285 public boolean canRemoveElement (XMLElement el) { 286 return true; 287 } 288 289 public XMLPanel getControlledPanel () { 290 return controlledPanel; 291 } 292 293 public XMLPanel getControlPanel () { 294 return controlPanel; 295 } 296 297 300 public boolean isEmpty () { 301 return size()==0; 302 } 303 304 public XMLPanel getPanel () { 306 controlledPanel=new XMLTablePanel(this,"",false,false,false,coloringTable,true); 307 controlPanel=new XMLTableControlPanel(this,"",true,false); 308 return new XMLGroupPanel(this,new XMLPanel[]{ 309 controlledPanel,controlPanel},toLabel(),XMLPanel.BOX_LAYOUT, 310 false,true); 311 } 312 313 public void toXML(Node parent) throws DOMException { 314 if (!isEmpty() || isRequired()) { 315 if (parent!=null) { 316 Node node = (parent.getOwnerDocument()).createElement(name); 317 for (Iterator it = refCollectionElements.iterator(); it.hasNext();) { 318 ((XMLElement) it.next()).toXML(node); 319 } 320 parent.appendChild(node); 321 } 322 } 323 } 324 325 public void fromXML(Node node) { 326 String nameSpacePrefix=node.getPrefix(); 327 if (nameSpacePrefix!=null) { 328 nameSpacePrefix+=":"; 329 } else { 330 nameSpacePrefix=""; 331 } 332 333 XMLElement newOne=generateNewElement(); 334 if (newOne instanceof XMLCollectionElement) { 335 try { 336 decrementID(); 337 ((XMLCollectionElement)newOne).set("Id","-1"); 338 } 339 catch (Exception ex) {} 340 } 341 342 String elName=newOne.name; 343 344 if (node!=null) { 345 if (node.hasChildNodes()) { 346 NodeList children = node.getChildNodes(); 347 int lng=children.getLength(); 348 for (int i = 0; i<lng; i++) { 350 Node child=children.item(i); 351 if (child.getNodeName().equals(nameSpacePrefix+elName)) { 352 newOne = generateNewElement(); 354 decrementID(); 355 newOne.fromXML(children.item(i)); 357 add(newOne); } 360 } 361 } 372 } 373 } 375 376 public String toString () { 377 if (labelName!=null) { 378 return labelName; 379 } else { 380 return ""; 381 } 382 } 383 384 389 public String getReadOnlyMessageName (XMLElement el) { 390 return "WarningCannotDeleteReadOnlyElement"; 391 } 392 393 398 public String getInUseMessageName (XMLElement el) { 399 return "WarningCannotDeleteElementThatIsInUse"; 400 } 401 402 public Object clone () { 405 XMLCollection d=(XMLCollection)super.clone(); 406 d.refCollectionElements=new ArrayList(); 407 Iterator it=this.refCollectionElements.iterator(); 408 while (it.hasNext()) { 409 XMLElement el=(XMLElement)it.next(); 410 Object cloned=el.clone(); 411 d.refCollectionElements.add(cloned); 412 if (cloned instanceof XMLCollectionElement) { 413 ((XMLCollectionElement)cloned).myCollection=d; 414 } 415 } 416 d.myOwner=this.myOwner; 417 d.coloringTable=this.coloringTable; 418 return d; 419 } 420 421 423 public void setIDPrefix (String idPref) { 424 IDPrefix=idPref; 425 } 426 427 428 public String getIDPrefix () { 429 return IDPrefix; 430 } 431 432 433 protected void setCurrentID (long ID) { 434 nextID=ID; 435 } 436 437 438 public long getCurrentID () { 439 return nextID; 440 } 441 442 450 public String generateID () { 451 if (IDPrefix==null) IDPrefix=""; 452 String ID; 453 do { 454 ID=IDPrefix+new Long (++nextID).toString(); 455 } while (getCollectionElement(ID)!=null); 456 return ID; 457 } 458 459 463 protected void resetID () { 464 nextID=0; 465 } 466 467 471 protected void decrementID () { 472 nextID--; 473 } 474 475 481 protected void updateID (String someID) { 482 try { 484 long val; 485 if (someID.startsWith(IDPrefix)) { 486 String ID=someID.substring(IDPrefix.length(),someID.length()); 487 val=Long.parseLong(ID); 488 if (val>nextID) { 489 nextID=val; 490 } 491 } 492 } catch (Exception ex) { 494 return; 495 } 496 } 497 498 public void refreshLabelName() { 499 super.refreshLabelName(); 500 Iterator it = refCollectionElements.iterator(); 501 while(it.hasNext()) { 502 XMLElement el = (XMLElement) it.next(); 503 el.refreshLabelName(); 504 } 505 } 506 507 510 public static boolean isIdValid (String id) { 511 return XMLChar.isValidNmtoken(id); 512 } 513 514 public DefaultMutableTreeNode getNode() { 515 DefaultMutableTreeNode node = new ToNameMutableTreeNode(this); 516 for(int i = 0; i < this.refCollectionElements.size(); i++) { 517 node.add( 518 ((XMLElement)this.refCollectionElements.get(i)).getNode() 519 ); 520 } 521 return node; 522 } 523 524 528 public String getPrintDescription( String indent, StyledDocument doc ) { 529 String retVal = ""; 530 try { 531 StringBuffer all = new StringBuffer (); 532 all.append( toName() + " : " ); 533 doc.insertString( doc.getLength(), all.toString(), atts ); 534 535 if ( !this.isCollapsed() ) { 537 String value; 539 for ( int i = 0; i < this.refCollectionElements.size(); i++ ) { 540 value = ( ( XMLElement )this.refCollectionElements.get( i ) ).getPrintDescription( 541 indent + OFFSET, doc ); 542 if ( value != null && !value.trim().equals( "" ) ) { 543 all.append( NEWLINE ); 544 all.append( indent ); 545 all.append( value ); 546 } 547 } 548 } 549 retVal = all.toString(); 550 }catch(Exception e) { 551 e.printStackTrace(); 552 } 553 return retVal; 554 } 555 556 557 558 559 } 560 | Popular Tags |