1 10 11 package org.enhydra.jawe.xml.elements; 12 13 import org.enhydra.jawe.xml.*; 14 import org.enhydra.jawe.xml.panels.*; 15 16 import java.util.*; 17 import javax.swing.*; 18 import javax.swing.tree.*; 19 import org.w3c.dom.*; 20 21 24 public class Package extends XMLComplexElement { 25 private final static String XMLNS = "http://www.wfmc.org/2002/XPDL1.0"; 26 private final static String XMLNS_XPDL = "http://www.wfmc.org/2002/XPDL1.0"; 27 private final static String XMLNS_XSI = "http://www.w3.org/2001/XMLSchema-instance"; 28 private final static String XSI_SCHEMA_LOCATION = "http://www.wfmc.org/2002/XPDL1.0 http://wfmc.org/standards/docs/TC-1025_schema_10_xpdl.xsd"; 29 30 private transient XMLInterface xmlInterface; 31 32 private PackageHeader refPackageHeader=new PackageHeader(); 33 private RedefinableHeader refRedefinableHeader=new RedefinableHeader(this); private ConformanceClass refConformanceClass=new ConformanceClass(); private Script refScript=new Script (); private ExternalPackages refExternalPackages=new ExternalPackages(this); private TypeDeclarations refTypeDeclarations=new TypeDeclarations(this); private Participants refParticipants; 39 private Applications refApplications=new Applications(this); private DataFields refDataFields=new DataFields(this); private WorkflowProcesses refWorkflowProcesses=new WorkflowProcesses(this); private ExtendedAttributes refExtendedAttributes=new ExtendedAttributes(this); 43 44 private Namespaces refNamespaces=new Namespaces(this); 45 46 private XMLAttribute attrId=new XMLAttribute("Id"); private XMLAttribute attrName=new XMLAttribute("Name"); 48 49 52 private String madeBy=""; 53 54 57 private String version=""; 58 59 private Set allMyExternalPackages=new HashSet(); 60 private DefaultTreeModel treeModel; 61 private DefaultMutableTreeNode myNode; 62 63 64 private ExtendedAttributes clonedEAs; 65 66 71 public Package (XMLInterface xmlInterface) { 72 super(); 73 74 this.xmlInterface=xmlInterface; 75 76 myNode=new DefaultMutableTreeNode(this); 77 treeModel=new DefaultTreeModel(myNode); 78 79 refParticipants=new Participants(this); 81 Namespace xpdlNS=new Namespace(); 83 xpdlNS.set("Name","xpdl"); 84 xpdlNS.set("location",XMLNS_XPDL); 85 xpdlNS.setReadOnly(true); 86 refNamespaces.add(xpdlNS); 87 88 fillStructure (); 89 } 90 91 95 protected void fillStructure () { 96 isRequired=true; 97 attrId.setRequired(true); 98 complexStructure.add(attrId); 99 attributes.add(attrId); 100 complexStructure.add(attrName); 101 attributes.add(attrName); 102 refPackageHeader.setRequired(true); 103 complexStructure.add(refPackageHeader); 104 complexStructure.add(refRedefinableHeader); 105 complexStructure.add(refConformanceClass); 106 complexStructure.add(refScript); 107 complexStructure.add(refExternalPackages); 108 complexStructure.add(refTypeDeclarations); 109 complexStructure.add(refParticipants); 110 complexStructure.add(refApplications); 111 complexStructure.add(refDataFields); 112 complexStructure.add(refWorkflowProcesses); 113 complexStructure.add(refExtendedAttributes); 114 } 115 116 public void setReadOnly (boolean ro) { 117 super.setReadOnly(ro); 118 refNamespaces.setReadOnly(ro); 119 } 120 121 126 public boolean isMadeByJaWE () { 127 return madeBy.equals("JaWE"); 128 } 129 130 133 public String getVersion () { 134 return version; 135 } 136 137 140 public String getCurrentVersion () { 141 return "1.4.2"; 142 } 143 144 public XMLInterface getXMLInterface () { 145 return xmlInterface; 146 } 147 148 153 protected void insertFromExternal (ExternalPackage ep) { 154 Package extP=xmlInterface.openPackage(ep.toString(),false); 156 try { 158 System.setProperty("user.dir",xmlInterface.getParentDirectory(this)); 159 } catch (Exception ex) {} 160 Set newlyInsertedPackages=extP.allMyExternalPackages; 161 newlyInsertedPackages.add(extP); 162 newlyInsertedPackages.removeAll(allMyExternalPackages); 163 newlyInsertedPackages.remove(this); 164 allMyExternalPackages.addAll(newlyInsertedPackages); 165 insertFromExternals(newlyInsertedPackages,extP); 166 } 167 171 private void insertFromExternals (Set extPackages,Package beginAt) { 172 Iterator it=extPackages.iterator(); 173 while (it.hasNext()) { 174 Package ext=(Package )it.next(); 175 insertFromExternal(ext); 176 } 177 if (!isReadOnly()) { 178 if (beginAt!=null) { 179 insertNodeIntoModel(beginAt); 180 } else { 181 it=refExternalPackages.toCollection().iterator(); 182 while (it.hasNext()) { 183 ExternalPackage ep=(ExternalPackage)it.next(); 184 Package referencedP=xmlInterface.getExternalPackageByRelativeFilePath(ep.toString(),this); 185 if (referencedP!=null && allMyExternalPackages.contains(referencedP)) { 186 insertNodeIntoModel(referencedP); 187 } 188 } 189 } 190 } 191 192 } 193 194 private void insertFromExternal (Package ep) { 195 refApplications.insertFromExternal(ep); 196 refParticipants.insertFromExternal(ep); 197 refWorkflowProcesses.insertFromExternal(ep); 198 } 199 200 public DefaultTreeModel getTreeModel() { 201 return treeModel; 202 } 203 204 public DefaultTreeModel getPackageTreeModel() { 206 return new DefaultTreeModel(getNode()); 207 } 208 209 private void insertNodeIntoModel(Package extP) { 210 211 DefaultMutableTreeNode rn=new DefaultMutableTreeNode(extP); 212 int hmc=myNode.getChildCount(); 213 214 treeModel.insertNodeInto(rn,myNode,hmc); 215 216 insertNodesRecursivly(rn); 217 } 218 219 private void insertNodesRecursivly(DefaultMutableTreeNode node) { 220 Package insertMyExtPackages=(Package )node.getUserObject(); 221 if (insertMyExtPackages.refExternalPackages.size()>0) { 222 Iterator it=insertMyExtPackages.refExternalPackages. 223 toCollection().iterator(); 224 while (it.hasNext()) { 225 String eps=((ExternalPackage)it.next()).toString(); 226 Package child=xmlInterface.getExternalPackageByRelativeFilePath(eps,insertMyExtPackages); 227 if (child!=null) { 229 DefaultMutableTreeNode jr=new DefaultMutableTreeNode(child); 230 int hmc=node.getChildCount(); 231 treeModel.insertNodeInto(jr,node,hmc); 232 if (!amIAlreadyInserted(jr)) { 233 insertNodesRecursivly(jr); 234 } 235 } 236 } 237 } 238 239 } 240 241 242 private boolean amIAlreadyInserted (DefaultMutableTreeNode dmtn) { 243 Object [] userObjects=dmtn.getUserObjectPath(); 244 Object userObj=dmtn.getUserObject(); 245 if (userObjects==null || userObjects.length<2) { 246 return false; 247 } 248 for (int i=0; i<userObjects.length-1; i++) { 249 if (userObjects[i]==userObj) { 250 return true; 251 } 252 } 253 return false; 254 } 255 256 265 protected boolean canRemoveExternalPackage (ExternalPackage ep) { 266 Set toCheck=findAllPackagesToRemove(ep); 267 Iterator itCheck=toCheck.iterator(); 269 while (itCheck.hasNext()) { 270 Package pkg=(Package )itCheck.next(); 271 WorkflowProcesses wps=(WorkflowProcesses)pkg. 273 get("WorkflowProcesses"); 274 Participants ps=(Participants)pkg. 275 get("Participants"); 276 Applications aps=(Applications)pkg. 277 get("Applications"); 278 279 Iterator it=wps.toCollection().iterator(); 282 while (it.hasNext()) { 283 WorkflowProcess wp=(WorkflowProcess)it.next(); 284 if (!refWorkflowProcesses.canRemoveElement(wp)) { 285 return false; 286 } 287 } 288 it=ps.toCollection().iterator(); 291 while (it.hasNext()) { 292 Participant p=(Participant)it.next(); 293 if (!refParticipants.canRemoveParticipant(p)) { 296 return false; 297 } 298 } 299 it=aps.toCollection().iterator(); 302 while (it.hasNext()) { 303 Application app=(Application)it.next(); 304 if (!refApplications.canRemoveApplication(app)) { 306 return false; 307 } 308 } 309 } 311 312 return true; 313 } 314 315 protected void removeExternal (ExternalPackage ep) { 316 Set toRemove=findAllPackagesToRemove(ep); 317 Package pToRemove=xmlInterface.getExternalPackageByRelativeFilePath(ep.toString(),this); 321 Iterator itRemove=toRemove.iterator(); 323 while (itRemove.hasNext()) { 324 Package pkg=(Package )itRemove.next(); 325 refApplications.removeFromExternal(pkg); 327 refParticipants.removeFromExternal(pkg); 329 refWorkflowProcesses.removeFromExternal(pkg); 331 xmlInterface.closePackage(pkg.get("Id").toString()); 333 allMyExternalPackages.remove(pkg); 335 } 336 337 if (pToRemove!=null) { 339 removeNodeFromModel(pToRemove); 340 allMyExternalPackages.remove(pToRemove); 341 } 343 344 } 349 350 private void removeNodeFromModel(Package extP) { 351 for (Enumeration e=myNode.children(); e.hasMoreElements();) { 352 DefaultMutableTreeNode child=(DefaultMutableTreeNode)e.nextElement(); 353 if (child.getUserObject()==extP) { 354 treeModel.removeNodeFromParent(child); 355 break; 356 } 357 } 358 } 359 360 private Set findAllPackagesToRemove (ExternalPackage ep) { 361 374 Set firstColl=new HashSet(); 376 Package toRemove=xmlInterface.getExternalPackageByRelativeFilePath(ep.toString(),this); 377 firstColl.add(toRemove); 378 firstColl.addAll(toRemove.allMyExternalPackages); 380 381 Set secondColl=new HashSet(); 383 secondColl.add(this); 385 Iterator it=refExternalPackages.toCollection().iterator(); 386 while (it.hasNext()) { 387 ExternalPackage epack=(ExternalPackage)it.next(); 388 if (epack!=ep) { 389 Package notToRemove=xmlInterface.getExternalPackageByRelativeFilePath(epack.toString(),this); 390 secondColl.add(notToRemove); 392 secondColl.addAll(notToRemove.allMyExternalPackages); 393 } 394 } 395 396 firstColl.removeAll(secondColl); 400 return firstColl; 403 } 404 405 421 public WorkflowProcess getWorkflowProcess(String ID) { 422 return refWorkflowProcesses.getWorkflowProcess(ID); 423 } 424 425 433 public String toString() { 434 return attrId.toString(); 435 } 436 437 444 public XMLPanel getPanel (int no) { 445 XMLPanel p; 446 switch (no) { 447 case 1: 448 clonedEAs=(ExtendedAttributes)refExtendedAttributes.clone(); 449 p=new XMLGroupPanel(this, 450 new XMLElement[] { 451 attrId, 452 attrName, 453 refConformanceClass, 454 refScript, 455 clonedEAs 456 },XMLUtil.getLanguageDependentString("GeneralKey")); 457 break; 458 case 2: 459 p=refPackageHeader.getPanel(); 460 break; 461 case 3: 462 p=refRedefinableHeader.getPanel(); 463 break; 464 case 4: 465 p=refExternalPackages.getPanel(); 466 break; 467 case 5: 468 p=refTypeDeclarations.getPanel(); 469 break; 470 case 6: 471 p=refParticipants.getPanel(); 472 break; 473 case 7: 474 p=refApplications.getPanel(); 475 break; 476 case 8: 477 p=refDataFields.getPanel(); 478 break; 479 case 9: 480 p=refWorkflowProcesses.getPanel(); 481 break; 482 case 10: 483 p=refNamespaces.getPanel(); 484 break; 485 default: 486 p=new XMLPanel(); 487 } 488 return p; 489 } 490 491 492 502 public XMLPanel getPanel () { 503 XMLPanel[] p=new XMLPanel[10]; 504 for (int i=0; i<10; i++) { 505 p[i]=getPanel(i+1); 506 } 507 XMLTabbedPanel tp=new XMLTabbedPanel(this,p); 508 509 return tp; 510 } 511 512 520 public void toXML (Node parent) throws DOMException { 521 523 Set easToRemove=new HashSet(); 525 ExtendedAttribute ea=new ExtendedAttribute(refExtendedAttributes); 527 ea.set("Name","Version"); 528 ea.set("Value",getCurrentVersion()); 529 ((ArrayList)refExtendedAttributes.toCollection()).add(0,ea); 530 easToRemove.add(ea); 531 532 ea=new ExtendedAttribute(refExtendedAttributes); 534 ea.set("Name","MadeBy"); 535 ea.set("Value","JaWE"); 536 ((ArrayList)refExtendedAttributes.toCollection()).add(0,ea); 537 easToRemove.add(ea); 538 539 Node node = ((Document) parent).createElement(name); 541 ((Element) node).setAttribute("xmlns", XMLNS); 542 Iterator itNs=refNamespaces.toCollection().iterator(); 545 while (itNs.hasNext()) { 546 Namespace ns=(Namespace)itNs.next(); 547 ((Element) node).setAttribute("xmlns:"+ns.get("Name").toString(), 548 ns.get("location").toString()); 549 } 550 ((Element) node).setAttribute("xmlns:xsi", XMLNS_XSI); 551 ((Element) node).setAttribute("xsi:schemaLocation", XSI_SCHEMA_LOCATION); 552 553 for (Iterator it = complexStructure.iterator(); it.hasNext();) { 554 XMLElement el = (XMLElement) it.next(); 555 el.toXML(node); 556 } 557 parent.appendChild(node); 558 559 refExtendedAttributes.toCollection().removeAll(easToRemove); 561 } 562 563 569 public void fromXML (Node node) { 570 refNamespaces.clear(); 571 refConformanceClass.set("GraphConformance",""); 572 573 processAttributes(node); 574 NamedNodeMap attribs=node.getAttributes(); 575 for (int i=0; i<attribs.getLength(); i++) { 576 Node n=attribs.item(i); 577 String nn=n.getNodeName(); 578 if (nn.startsWith("xmlns:") && !nn.equals("xmlns:xsi")) { 579 Namespace ns=new Namespace(); 580 ns.set("Name",nn.substring(6,nn.length())); 581 ns.get("location").fromXML(n); 582 refNamespaces.add(ns); 583 if (nn.equals("xmlns:xpdl") && ns.get("location").toString().equalsIgnoreCase(XMLNS_XPDL)) { 584 ns.setReadOnly(true); 585 } 586 } 587 } 588 589 attrId.setReadOnly(true); 592 setIDPrefixForCollections (); 593 594 processElements(node); 595 596 Set easToRemove=new HashSet(); 599 600 Iterator it=refExtendedAttributes.toCollection().iterator(); 602 while (it.hasNext()) { 603 ExtendedAttribute eup=(ExtendedAttribute)it.next(); 604 String ID=eup.get("Name").toValue().toString(); 605 606 if (ID.equals("MadeBy") && madeBy.length()==0) { 607 madeBy=eup.get("Value").toValue().toString(); 608 easToRemove.add(eup); 609 } 610 if (ID.equals("Version") && version.length()==0) { 611 version=eup.get("Value").toValue().toString(); 612 easToRemove.add(eup); 613 } 614 } 615 616 refExtendedAttributes.toCollection().removeAll(easToRemove); 619 620 } 621 622 623 public void setIDPrefixForCollections () { 624 String idPref=attrId.toValue().toString()+XMLCollectionElement.ID_DELIMITER; 625 refApplications.setIDPrefix(idPref+"App"); 626 refDataFields.setIDPrefix(idPref+"Dat"); 627 refParticipants.setIDPrefix(idPref+"Par"); 628 refTypeDeclarations.setIDPrefix(idPref+"Typ"); 629 refWorkflowProcesses.setIDPrefix(idPref+"Wor"); 630 } 631 632 637 public Set getAllExternalPackages () { 638 return allMyExternalPackages; 639 } 640 641 645 public void addExternalPackages(Package externalPkg) { 646 allMyExternalPackages.addAll(externalPkg.allMyExternalPackages); 647 allMyExternalPackages.remove(this); 648 } 649 650 653 public void addExternalPackage(Package externalPkg) { 654 allMyExternalPackages.add(externalPkg); 655 } 656 657 663 public void afterImporting () { 664 insertFromExternals(allMyExternalPackages,null); 668 669 refRedefinableHeader.afterImporting(); 670 671 674 Iterator it=refWorkflowProcesses.toCollection().iterator(); 678 while (it.hasNext()) { 679 WorkflowProcess wp=(WorkflowProcess)it.next(); 680 wp.afterImporting(); 682 } 683 684 } 685 686 public boolean isValidEnter (XMLPanel p) { 687 try { 690 XMLTextPanel tp=(XMLTextPanel)((XMLGroupPanel)p).getPanel(0); 691 String IDToCheck=tp.getText(); 692 if (!XMLCollection.isIdValid(IDToCheck)) { 693 String message=XMLUtil.getLanguageDependentString("ErrorIDMustBeValid"); 694 String dialogTitle=XMLUtil.getLanguageDependentString("DialogIDIsNotValid"); 695 XMLPanel.errorMessage(p.getDialog(),dialogTitle,"",message); 696 ((JTextField)tp.getComponent(2)).requestFocus(); 697 return false; 698 } 699 } catch (Exception ex){} 700 try { 701 if (!refRedefinableHeader.isValidEnter(((XMLTabbedPanel)p).getTabbedPanel(2))) { 702 return false; 703 } 704 } catch (Exception ex) {} 705 if (clonedEAs!=null) { 706 complexStructure.remove(refExtendedAttributes); 707 refExtendedAttributes=clonedEAs; 708 complexStructure.add(12,refExtendedAttributes); 709 } 710 return true; 711 } 712 713 } 714 715 | Popular Tags |