📄 configtree.java
字号:
/* * ==================================================================== * The Vovida Software License, Version 1.0 * * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The names "VOCAL", "Vovida Open Communication Application Library", * and "Vovida Open Communication Application Library (VOCAL)" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor * may "VOCAL" appear in their name, without prior written * permission of Vovida Networks, Inc. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc. For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * *//* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xerces" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, International * Business Machines, Inc., http://www.apache.org. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * Based on DOMTree by Andy Clark, IBM * Note that the Apache license applies to the original * software and the Vovida license applies to changes * to that software. */package vocal.data;import java.util.Vector;import java.io.Serializable;import javax.swing.JTree;import javax.swing.tree.TreePath;import javax.swing.event.TreeModelEvent;import javax.swing.event.TreeSelectionEvent;import javax.swing.event.TreeModelListener;import javax.swing.tree.DefaultTreeModel;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.MutableTreeNode;import javax.swing.tree.TreeNode;import javax.swing.tree.TreeSelectionModel;import org.w3c.dom.Document;import org.w3c.dom.DocumentFragment;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import org.w3c.dom.Attr;import org.w3c.dom.traversal.NodeFilter;import java.util.Hashtable;import java.util.Enumeration;/** * This class manages the JTree that is the basis of * navigation through server-side provisioning. * This class has an outer and inner component. The outer * component provides an API for the GUI classes in the ui package. * Each type of screen (ie Marshal, Feature, Group, etc) * has its own pair of methods to call, either to populate * its textfields with data from an existing server or to set * it up for creating a new server. * <p> * The inner class component contains a Document Object Model * that holds all server data in a tree and the classes for * converting the nodes of this DOM to be rendered as JTree * nodes. * * @author Barbara Samson, Vovida Networks */public class ConfigTree extends JTree implements TreeNodeTypes{ /** * The data manager that gets passed from all requests. */ DataToDOM dataManager = new DataToDOM(this); // // Constructors // /** * Default Constructor. */ public ConfigTree() { super(new Model()); // set tree properties setRootVisible(false); getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); Vector nodes = ((Model) getModel()).getTreeNodesToExpand(); for (int i = 0; i < nodes.size(); i++) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes.elementAt(i); expandPath(new TreePath(node.getPath())); } } // <init>() // // Public methods // /** * Returns the document's root element. */ public Element getRootElement() { return getTreeDOM().getDocumentElement(); } /** * returns the DOM created from the document */ public Document getTreeDOM() { return ((Model) getModel()).getTreeDOM(); } /** * get the org.w3c.Node for a MutableTreeNode. */ public Node getNode(Object treeNode) { return ((Model) getModel()).getNode(treeNode); } /** * Checks to see if the same host:port combination already * exist in the tree. * @param host hostname to check * @param port port number to check * @return the Element that matches host:port, or null if * no Element matches */ public Element hostPortExists(String host, String port) { System.out.println("calling hostPortExists with host " + host + ", port " + port); Element rootElement = getRootElement(); System.out.println(new XMLUtils().buildXMLString(rootElement)); return (traverseChildren(rootElement, host, port)); } private Element traverseChildren(Element parent, String host, String port) { NodeList children = parent.getChildNodes(); if (children != null) { for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element child = (Element)node; String hostString = child.getAttribute("host"); String portString = child.getAttribute("port"); // we won't check empty strings if( (!hostString.equals("")) && (!portString.equals("")) ) { if (hostString.equals(host) && portString.equals(port)) { return child; } } Element testVal = traverseChildren(child, host, port); if (testVal != null) { return testVal; } } } } return null; } /* * Causes the TreeSelectionEvent to fire again for the current node. */ public void refreshCurrentSelection() { DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) getLastSelectedPathComponent(); TreePath path = new TreePath(currentNode.getPath()); // this causes the node to be reselected, which magically // sets all fields back to their last saved values. fireValueChanged(new TreeSelectionEvent(this, path, false, path, path)); } /** * Replace selected node. * @param replacement replacement node */ public void replaceSelectedNode(Node replacement) { DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) getLastSelectedPathComponent(); ((Model) getModel()).replaceNodeInMap(currentNode, replacement); setSelectionPath(new TreePath(currentNode.getPath())); DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) getLastSelectedPathComponent(); } /** * We can only do this if the node has no children. * @return true if the node could be deleted, false otherwise. */ public boolean deleteData() { Element element = getCurrentElement(); int elementType = getSelectedNodeType(); switch (elementType) { case FEATURE_GROUP: case MARSHAL_GROUP: case CDR_GROUP: case HEARTBEAT_GROUP: case PDP_GROUP: case DVR_GROUP: case ENV_GROUP: case REDIRECT_GROUP: case JTAPI_GROUP: { // cannot do delete if there are element child nodes. boolean canDelete = true; if (element.hasChildNodes()) { NodeList childNodes = element.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if (childNode.getNodeType() == Node.ELEMENT_NODE) { canDelete = false; } } } if (canDelete) { deleteSelectedNode(); return true; } return false; } // note that the user must understand the consequences of // deleting a feature server. case FEATURE_SERVER: case MARSHAL_SERVER: case CDR_SERVER: case HEARTBEAT_SERVER: case PDP_SERVER: case DVR: case ENV: case REDIRECT_SERVER: case JTAPI_SERVER: { deleteSelectedNode(); return true; } default: { return false; } } } /** * delete the selected tree node and its associated org.w3c.node */ protected void deleteSelectedNode() { DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) getLastSelectedPathComponent(); DOMToFile.deleteFile((Element) getNode(currentNode)); DefaultMutableTreeNode newSelection = ((Model) getModel()).deleteNode(currentNode); setSelectionPath(new TreePath(newSelection.getPath())); try { DOMToFile.writeAllFilesInPath(getCurrentElement()); } catch (FailedToWriteFileException e) { // not much we can do about it. refreshCurrentSelection(); } } /** * remove the children of the selected node */ public void deleteChildrenOfSelectedNode() { DefaultMutableTreeNode parent = (DefaultMutableTreeNode) getLastSelectedPathComponent(); if (parent == null) { System.out.println("parent is null"); return; } ((Model) getModel()).deleteChildrenOfNode(parent); // we must set selection because we fired a nodeChanged event setSelectionPath(new TreePath(parent.getPath())); if (parent != getLastSelectedPathComponent()) { System.out.println("Error selection mismatch"); } } /** * Rebuild the tree from the currently selected TreeNode * down. */ public void saveSelectedNode() { DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) getLastSelectedPathComponent(); DefaultMutableTreeNode newSelection = ((Model) getModel()).rebuildTree(currentNode); setSelectionPath(new TreePath(newSelection.getPath())); } /** * Rebuild the children of the currently selected TreeNode. */ public void saveChildrenOfSelectedNode() { DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) getLastSelectedPathComponent(); Node parent = ((Model) getModel()).getNode(treeNode); NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node child = childNodes.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { ((Model) getModel()).addElementToParent(child, treeNode); } } setSelectionPath(new TreePath(treeNode)); } /** * Insert a DocumentFragment as first child of the specified TreeNode. */ public void insertDocumentFragment(DocumentFragment fragment, MutableTreeNode where) { DefaultMutableTreeNode newSelection = ((Model) getModel()).insertFragmentNode(fragment, where); setSelectionPath(new TreePath(newSelection.getPath())); } /** * get Element corresponding to currently selected TreeNode */ public Element getCurrentElement() { DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) getLastSelectedPathComponent(); Node node = getNode(currentNode); if (node == null) { System.out.println("DOM node corresponding to currently selected treeNode is null"); return null; } Element element = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -