1 package com.calipso.xmleditor; 2 3 import org.w3c.dom.Node ; 4 import org.w3c.dom.NodeList ; 5 import org.w3c.dom.NamedNodeMap ; 6 7 import javax.swing.*; 8 import javax.swing.tree.*; 9 import javax.swing.event.TreeSelectionListener ; 10 import javax.swing.event.TreeSelectionEvent ; 11 import java.util.*; 12 import java.awt.*; 13 14 21 22 public class XmlEditorPanel extends JPanel implements TreeSelectionListener { 23 24 private String name; 25 private JPanel pnlMain; 26 private CardLayout cardLayout; 27 private JTree tree; 28 private DefaultTreeModel model; 29 private XmlEditorTreeModelNode currentNode; 30 private XmlEditorTreeModelNode lastNode; 31 private HashMap subPanels; 32 private XmlEditorTreeDefinitionNode nodeDefinition; 33 private Vector treeNodesHistory; 34 private boolean isBlocked; 35 private Node domRoot; 36 private boolean isJustXml; 37 private boolean isOnlyXsd = false; 38 39 public void setOnlyXsd(boolean onlyXsd) { 40 this.isOnlyXsd = onlyXsd; 41 } 42 43 48 public XmlEditorPanel(XmlEditorTreeDefinitionNode nodeDefinition) throws XmlEditorException{ 49 name = nodeDefinition.getValue(); 50 isBlocked = false; 51 isJustXml = false; 52 initialize(nodeDefinition); 53 } 54 55 60 public XmlEditorPanel(DefaultTreeModel model, Node domRoot) { 61 this.model = model; 62 isJustXml = true; 63 this.domRoot = domRoot; 64 initialize(); 65 } 66 67 70 private void initialize() { 71 createCardLayoutPanels(); 72 setLayout(new BorderLayout()); 73 JSplitPane splitPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT, createCenterPanel(), createSouthPanel()); 74 splitPanel.setDividerLocation(200); 75 splitPanel.setDividerSize(2); 76 add(splitPanel, BorderLayout.CENTER); 77 } 78 79 82 private void createCardLayoutPanels() { 83 cardLayout = new CardLayout(); 84 pnlMain = new JPanel(cardLayout); 85 HashMap subPanels = new HashMap(); 86 getLeafsFrom(domRoot, subPanels); 87 Iterator iterator = subPanels.keySet().iterator(); 88 while(iterator.hasNext()) { 89 String currentKey = iterator.next().toString(); 90 pnlMain.add(((JPanel)subPanels.get(currentKey)), currentKey); 91 } 92 this.subPanels = (HashMap) subPanels.clone(); 93 } 94 95 100 private void getLeafsFrom(Node node, HashMap subPanels) { 101 if(node.getAttributes().getLength() > 0) { 102 subPanels.put(node.getNodeName(), new XmlEditorSubPanel(getAttrsNamesFrom(node))); 103 getSubPanelsFrom(node.getChildNodes(), subPanels); 104 } else { 105 getSubPanelsFrom(node.getChildNodes(), subPanels); 106 } 107 } 108 109 114 private void getSubPanelsFrom(NodeList children, HashMap subPanels) { 115 for(int i = 0 ; i < children.getLength(); i++) { 116 Node node = children.item(i); 117 if(node.getNodeType() == Node.ELEMENT_NODE) { 118 if(node.getAttributes().getLength() > 0) { 119 subPanels.put(node.getNodeName(), new XmlEditorSubPanel(getAttrsNamesFrom(node))); 120 } else { 121 subPanels.put(node.getNodeName(), new XmlEditorSubPanel(new Vector())); 122 getSubPanelsFrom(node.getChildNodes(), subPanels); 123 } 124 } 125 } 126 } 127 128 133 private Vector getAttrsNamesFrom(Node node) { 134 Vector vector = new Vector(); 135 NamedNodeMap attrs = node.getAttributes(); 136 for(int i = 0 ; i < attrs.getLength() ; i++) { 137 Node attr = attrs.item(i); 138 vector.add(attr.getNodeName()); 139 } 140 return vector; 141 } 142 143 148 private void initialize(XmlEditorTreeDefinitionNode nodeDefinition) throws XmlEditorException{ 149 this.nodeDefinition = nodeDefinition; 150 model = getModelFrom(nodeDefinition); 151 createCardLayoutPanels(nodeDefinition); 152 setLayout(new BorderLayout()); 153 JSplitPane splitPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT, createCenterPanel(), createSouthPanel()); 154 splitPanel.setDividerLocation(200); 155 splitPanel.setDividerSize(2); 156 add(splitPanel, BorderLayout.CENTER); 157 } 158 159 164 private DefaultTreeModel getModelFrom(XmlEditorTreeDefinitionNode nodeDefinition) { 165 XmlEditorTreeModelNode node = new XmlEditorTreeModelNode(nodeDefinition.getValue()); 166 fillTreeModel(nodeDefinition, node); 167 return new DefaultTreeModel(node); 168 } 169 170 175 private void fillTreeModel(XmlEditorTreeDefinitionNode nodeDefinition, XmlEditorTreeModelNode node) { 176 Iterator iterator = nodeDefinition.getSubnodes().keySet().iterator(); 177 while(iterator.hasNext()) { 178 XmlEditorTreeDefinitionNode definition = (XmlEditorTreeDefinitionNode) nodeDefinition.getSubnodes().get(iterator.next()); 179 XmlEditorTreeModelNode newNode = new XmlEditorTreeModelNode(definition.getValue()); 180 node.add(newNode); 181 fillTreeModel(definition, newNode); 182 } 183 } 184 185 private JPanel createSouthPanel() { 186 JPanel pnlSouth = new JPanel(new BorderLayout()); 187 JScrollPane pane = new JScrollPane(pnlMain); 188 pnlSouth.add(pane, BorderLayout.CENTER); 189 return pnlSouth; 190 } 191 192 197 private void createCardLayoutPanels(XmlEditorTreeDefinitionNode nodeDefinition) throws XmlEditorException{ 198 cardLayout = new CardLayout(); 199 pnlMain = new JPanel(cardLayout); 200 HashMap subPanels = new HashMap(); 201 getLeafsFrom(nodeDefinition, subPanels); 202 Iterator iterator = subPanels.keySet().iterator(); 203 while(iterator.hasNext()) { 204 String currentKey = iterator.next().toString(); 205 pnlMain.add(((JPanel)subPanels.get(currentKey)), currentKey); 206 } 207 this.subPanels = (HashMap) subPanels.clone(); 208 } 209 210 216 private void getLeafsFrom(XmlEditorTreeDefinitionNode nodeDefinition, HashMap subPanels) throws XmlEditorException{ 217 if(nodeDefinition.getItemsKeys().size() > 0) { 218 subPanels.put(nodeDefinition.getValue(), new XmlEditorSubPanel(nodeDefinition.getItemsKeys(), nodeDefinition.getItems())); 219 getSubPanelsFrom(nodeDefinition.getSubnodes(), subPanels); 220 } else { 221 getSubPanelsFrom(nodeDefinition.getSubnodes(), subPanels); 222 } 223 } 224 225 private void getSubPanelsFrom(Map nodes, HashMap subPanels) throws XmlEditorException{ 226 Iterator iterator = nodes.keySet().iterator(); 227 while(iterator.hasNext()) { 228 XmlEditorTreeDefinitionNode node = (XmlEditorTreeDefinitionNode) nodes.get(iterator.next().toString()); 229 if(node.getItemsKeys().size() > 0) { 230 subPanels.put(node.getValue(), new XmlEditorSubPanel(node.getItemsKeys(), node.getItems())); 231 Map newNodes = node.getSubnodes(); 232 getSubPanelsFrom(newNodes, subPanels); 233 } else { 234 subPanels.put(node.getValue(), new XmlEditorSubPanel(new Vector(), new HashMap())); 235 Map newNodes = node.getSubnodes(); 236 getSubPanelsFrom(newNodes, subPanels); 237 } 238 } 239 } 240 241 private JPanel createCenterPanel() { 242 JPanel centerPanel = new JPanel(new BorderLayout()); 243 tree = new JTree(model); 244 tree.addTreeSelectionListener(this); 245 JScrollPane scrollTree = new JScrollPane(tree); 246 centerPanel.add(scrollTree, BorderLayout.CENTER); 247 return centerPanel; 248 } 249 250 public void valueChanged(TreeSelectionEvent e) { 251 currentNode = (XmlEditorTreeModelNode) (e.getPath().getLastPathComponent()); 252 valueChanged(); 253 } 254 255 public void valueChanged(){ 256 getTreeNodesHistory().add(currentNode); 257 if(getTreeNodesHistory().size() > 1) { 258 lastNode = (XmlEditorTreeModelNode) getTreeNodesHistory().elementAt(getTreeNodesHistory().size() - 2); 259 } 260 if( currentNode.isLeaf()) { 261 leafHasBeenSelected(); 262 } else if(!currentNode.isLeaf() && currentNode.getChildCount() > 0) { 263 folderHasBeenSelected(); 264 } else { 265 cardLayout.show(pnlMain, currentNode.toString()); 266 } 267 268 } 269 270 273 private void folderHasBeenSelected() { 274 if(!isJustXml) { 275 if(lastNode != currentNode) { 276 verifyModelNodeChanges(false); 277 } 278 if(!isBlocked) { 279 if(currentNode.getAttributes()!= null && currentNode != getTreeModel().getRoot()){ 281 cardLayout.show(pnlMain, currentNode.toString()); 282 } } 286 } else { 287 verifyModelNodeChanges(false); 288 ((XmlEditorSubPanel) subPanels.get(currentNode.toString())).eraseInputFields(); 289 cardLayout.show(pnlMain, currentNode.toString()); 290 } 291 } 292 293 296 private void leafHasBeenSelected() { 297 if(!isJustXml) { 298 if(lastNode != currentNode) { 299 verifyModelNodeChanges(true); 300 } 301 if(!isBlocked) { 302 XmlEditorTreeModelNode parent = (XmlEditorTreeModelNode) currentNode.getParent(); 303 XmlEditorSubPanel subPanel = null; 304 if(parent!=null){ 305 subPanel = (XmlEditorSubPanel) subPanels.get(parent.toString()); 306 if(subPanel!=null){ 307 if(!isOnlyXsd){ 308 subPanel.fillFrom(currentNode); 309 cardLayout.show(pnlMain, parent.toString()); 310 } 311 cardLayout.show(pnlMain, currentNode.toString()); 312 } 313 } 314 } 315 } else { 316 verifyModelNodeChanges(true); 317 XmlEditorTreeModelNode parent = (XmlEditorTreeModelNode) currentNode.getParent(); 318 if(parent!=null){ 319 XmlEditorSubPanel subPanel = (XmlEditorSubPanel) subPanels.get(parent.toString()); 320 subPanel.fillFrom(currentNode); 321 cardLayout.show(pnlMain, parent.toString()); 322 } 323 } 324 } 325 326 330 private void verifyModelNodeChanges(boolean isLeaf) { 331 if(getTreeNodesHistory().size() > 1) { 332 if(isLeaf) { 333 verifyModelNodeChangesIfLeaf(); 334 } else { 335 verifyModelNodeChangesIfFolder(); 336 } 337 } 338 } 339 340 344 private void verifyModelNodeChangesIfFolder() { 345 if(currentNode == lastNode.getParent() && lastNode.isLeaf()) { 346 XmlEditorSubPanel subPanel = (XmlEditorSubPanel) subPanels.get(currentNode.toString()); 347 updateModelNodeFrom(subPanel, lastNode); 348 } else if(currentNode != lastNode.getParent() && lastNode.isLeaf()) { 349 XmlEditorTreeModelNode parent = (XmlEditorTreeModelNode) lastNode.getParent(); 350 XmlEditorSubPanel subPanel = (XmlEditorSubPanel) subPanels.get(parent.toString()); 351 updateModelNodeFrom(subPanel, lastNode); 352 } 353 } 354 355 359 private void verifyModelNodeChangesIfLeaf() { 360 if((lastNode.getParent() == currentNode.getParent()) && lastNode.isLeaf()) { 361 XmlEditorTreeModelNode parent = (XmlEditorTreeModelNode) lastNode.getParent(); 362 if(parent!=null){ 363 XmlEditorSubPanel subPanel = (XmlEditorSubPanel) subPanels.get(parent.toString()); 364 updateModelNodeFrom(subPanel, lastNode); 365 } 366 } else if(lastNode.getParent() != null) { 367 if(lastNode.getParent() != currentNode.getParent() && lastNode.isLeaf()) { 368 XmlEditorTreeModelNode parent = (XmlEditorTreeModelNode) lastNode.getParent(); 369 XmlEditorSubPanel subPanel = (XmlEditorSubPanel) subPanels.get(parent.toString()); 370 updateModelNodeFrom(subPanel, lastNode); 371 } 372 } 373 } 374 375 380 private void updateModelNodeFrom(XmlEditorSubPanel subPanel, XmlEditorTreeModelNode node) { 381 String id = ""; 382 if(node.getId() != null){ 383 id = node.getId(); 384 } 385 if(!isJustXml && !isOnlyXsd) { 386 String wrongElement = validateFrom(subPanel, (XmlEditorTreeModelNode) node); 387 if(wrongElement != null) { 388 JOptionPane.showMessageDialog(this, "Falta completar el campo " + wrongElement); 389 isBlocked = true; 390 getTreeNodesHistory().removeElementAt(getTreeNodesHistory().size() - 1); 391 tree.setSelectionPath(new TreePath(node)); 392 } else { 393 isBlocked = false; 394 node.updateAttributesFrom(subPanel); 395 } 396 } else if(!isOnlyXsd){ 397 node.updateAttributesFrom(subPanel); 398 } 399 String newId = ""; 400 if(node.getId()!=null){ 401 newId = node.getId(); 402 } 403 if(!id.equalsIgnoreCase(newId)){ 404 node.setUserObject(newId); 405 this.invalidate(); 406 } 407 } 408 409 414 private XmlEditorTreeModelNode getRootSpecs(Enumeration children) { 415 while(children.hasMoreElements()) { 416 XmlEditorTreeModelNode modelNode = (XmlEditorTreeModelNode) children.nextElement(); 417 if(modelNode.isLeaf()) { 418 return modelNode; 419 } 420 } 421 return null; 422 } 423 424 427 public void deleteNode() { 428 if(!isJustXml){ 429 if(currentNode == getRootSpecs(((XmlEditorTreeModelNode)model.getRoot()).children()) || currentNode == model.getRoot()){ 430 JOptionPane.showMessageDialog(this, "Debe existir una instancia de " + ((XmlEditorTreeModelNode)model.getRoot()).getUserObject()); 431 return; 432 } 433 } 434 DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); 435 TreeNode parent = currentNode.getParent(); 436 if(parent!=null){ 437 ((XmlEditorSubPanel) subPanels.get(parent.toString())).eraseInputFields(); 438 model.removeNodeFromParent(currentNode); 439 } 440 } 442 443 446 public void addNode() { 447 if(!isJustXml) { 448 if(currentNode != getRootSpecs(((XmlEditorTreeModelNode)model.getRoot()).children()) && currentNode != model.getRoot()){ 449 if(subPanels.get(currentNode.toString())==null){ 452 currentNode = (XmlEditorTreeModelNode)currentNode.getParent(); 453 } 454 XmlEditorSubPanel subPanel = getSubPanelFor(currentNode); 455 if(subPanel!=null){ 456 String wrongElement = validateFrom(subPanel, currentNode); 457 if(wrongElement != null) { 458 JOptionPane.showMessageDialog(this, "Falta completar el campo " + wrongElement); 459 } else { 460 updateTreeModel(getId(currentNode), subPanel); 461 } 462 } 463 } else { 464 JOptionPane.showMessageDialog(this, "Solo debe existir una instancia de " + ((XmlEditorTreeModelNode)model.getRoot()).getUserObject()); 465 } 466 } else { 467 XmlEditorSubPanel subPanel = getSubPanelFor(currentNode); 468 if(subPanel!=null){ 469 updateTreeModel(getId(subPanel), subPanel); 470 } 471 } 472 } 473 474 private String getId(XmlEditorTreeModelNode node) { 475 if(subPanels.get(node.toString())!=null){ 476 return node.toString(); 477 }else{ 478 return node.getParent().toString(); 479 } 480 } 481 482 487 private XmlEditorSubPanel getSubPanelFor(XmlEditorTreeModelNode node) { 488 XmlEditorSubPanel subPanel = (XmlEditorSubPanel)subPanels.get(node.toString()); 489 if(subPanel==null){ 490 subPanel = (XmlEditorSubPanel)subPanels.get(node.getParent().toString()); 491 } 492 return subPanel; 493 } 494 495 private String validateFrom(XmlEditorSubPanel subPanel, XmlEditorTreeModelNode modelNode) { 496 XmlEditorTreeModelNode auxModelNode; 499 if(subPanels.get(modelNode.toString())==null){ 500 auxModelNode = (XmlEditorTreeModelNode)modelNode.getParent(); 501 }else{ 502 auxModelNode = modelNode; 503 } 504 XmlEditorTreeDefinitionNode definition = getNodeDefinitionFrom(nodeDefinition, auxModelNode); 505 if(definition!=null && !modelNode.toString().equalsIgnoreCase(definition.getValue())){ 506 return XmlEditorSubPanelValidator.validateFrom(subPanel, definition); 507 } 508 return null; 509 } 510 511 private String getId(XmlEditorSubPanel subPanel) { 512 if(subPanel.getInputComponents()!=null && !subPanel.getInputComponents().isEmpty()){ 513 return ((JTextField)subPanel.getInputComponents().elementAt(0)).getText(); 514 }else{ 515 return ""; 516 } 517 } 518 519 524 private void updateTreeModel(String nodeName, XmlEditorSubPanel subPanel) { 525 XmlEditorTreeModelNode modelNode = new XmlEditorTreeModelNode("New " + nodeName); 526 modelNode.addAttributesFrom(subPanel); 527 ((XmlEditorSubPanel) subPanels.get(getId(currentNode))).eraseInputFields(); 528 DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); 529 model.insertNodeInto(modelNode, currentNode, 0); 530 int [] selRows = tree.getSelectionRows(); 531 for(int i = 0 ; i < selRows.length ; i++) { 532 tree.expandRow(selRows [i]); 533 } 534 } 535 536 542 private XmlEditorTreeDefinitionNode getNodeDefinitionFrom(XmlEditorTreeDefinitionNode definition, XmlEditorTreeModelNode modelNode) { 543 if(definition.getValue().equals(modelNode.toString())) { 544 return definition; 545 } else { 546 return getNodeDefinitionFrom(definition.getSubnodes(), false, modelNode); 547 } 548 } 549 550 private XmlEditorTreeDefinitionNode getNodeDefinitionFrom(Map subNodes, boolean found, XmlEditorTreeModelNode modelNode) { 551 XmlEditorTreeDefinitionNode returnVal = null; 552 Iterator iterator = subNodes.keySet().iterator(); 553 while(iterator.hasNext() && !found) { 554 XmlEditorTreeDefinitionNode definition = (XmlEditorTreeDefinitionNode) subNodes.get(iterator.next().toString()); 555 if(definition.getValue().equals(modelNode.toString())) { 556 return definition; 557 } else { 558 returnVal = getNodeDefinitionFrom(definition.getSubnodes(), found, modelNode); 559 if(returnVal != null) { 560 found = true; 561 } 562 } 563 } 564 return returnVal; 565 } 566 567 public DefaultTreeModel getTreeModel() { 568 return model; 569 } 570 571 public String getName() { 572 return name; 573 } 574 575 public Map getSubPanels() { 576 return subPanels; 577 } 578 579 private Vector getTreeNodesHistory() { 580 if(treeNodesHistory == null) { 581 treeNodesHistory = new Vector(); 582 } 583 return treeNodesHistory; 584 } 585 586 public JTree getTree() { 587 return tree; 588 } 589 590 public XmlEditorTreeDefinitionNode getNodeDefinition() { 591 return nodeDefinition; 592 } 593 594 597 public void collapseAllNodes() { 598 int j = tree.getRowCount(); 599 for(int i=0; j==tree.getRowCount() && i<tree.getRowCount(); i++){ 600 tree.collapseRow(tree.getRowCount()-i); 601 } 602 if(j!=tree.getRowCount()){ 603 collapseAllNodes(); 604 } 605 } 606 607 private XmlEditorTreeModelNode getRootNode() { 608 XmlEditorTreeModelNode node = currentNode; 609 while(node.getParent() != null){ 610 node = (XmlEditorTreeModelNode)node.getParent(); 611 } 612 return node; 613 } 614 615 } 616 | Popular Tags |