📄 accreditconfigtree.java
字号:
/*
* 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/>.
*
*/
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 AccreditConfigTree extends JTree implements TreeNodeTypes
{
/**
* The data manager that gets passed from all requests.
*/
DataToDOM dataManager = new DataToDOM(this);
//
// Constructors
//
/**
* Default Constructor.
*/
public AccreditConfigTree()
{
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;
try
{
element = (Element) node;
}
catch (ClassCastException e)
{
System.out.println("currently selected node is not an element: "
+ node.getNodeValue());
}
return element;
}
/**
* get type of currently selected TreeNode.
*/
public int getSelectedNodeType()
{
DefaultMutableTreeNode currentNode =
(DefaultMutableTreeNode) getLastSelectedPathComponent();
if (currentNode == null)
{
System.out.println("selected node is null");
return UNKNOWN;
}
System.out.println("this is :" + currentNode.getUserObject());
//System.out.println("_______________________________\n");
AccUserObject theObject = (AccUserObject) (currentNode.getUserObject());
System.out.println("this is :" + theObject.getNodeType());
return theObject.getNodeType();
}
/**
*
*/
public GroupData getGroupData()
{
int type = getSelectedNodeType();
return dataManager;
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -