📄 sampletree.java
字号:
* Determines the selection from the Tree and adds an item * after that. If nothing is selected, an item is added to * the root. */ public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode lastItem = getSelectedNode(); DefaultMutableTreeNode parent; /* Determine where to create the new node. */ if(lastItem != null) { parent = (DefaultMutableTreeNode)lastItem.getParent(); if(parent == null) { parent = (DefaultMutableTreeNode)treeModel.getRoot(); lastItem = null; } } else parent = (DefaultMutableTreeNode)treeModel.getRoot(); if (parent == null) { // new root treeModel.setRoot(createNewNode("Added " + Integer.toString(addCount++))); } else { int newIndex; if(lastItem == null) newIndex = treeModel.getChildCount(parent); else newIndex = parent.getIndex(lastItem) + 1; /* Let the treemodel know. */ treeModel.insertNodeInto(createNewNode("Added " + Integer.toString(addCount++)), parent, newIndex); } } } // End of SampleTree.AddAction /** * InsertAction is used to insert a new item before the selected item. */ class InsertAction extends Object implements ActionListener { /** Number of nodes that have been added. */ public int insertCount; /** * Messaged when the user clicks on the Insert menu item. * Determines the selection from the Tree and inserts an item * after that. If nothing is selected, an item is added to * the root. */ public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode lastItem = getSelectedNode(); DefaultMutableTreeNode parent; /* Determine where to create the new node. */ if(lastItem != null) { parent = (DefaultMutableTreeNode)lastItem.getParent(); if(parent == null) { parent = (DefaultMutableTreeNode)treeModel.getRoot(); lastItem = null; } } else parent = (DefaultMutableTreeNode)treeModel.getRoot(); if (parent == null) { // new root treeModel.setRoot(createNewNode("Inserted " + Integer.toString(insertCount++))); } else { int newIndex; if(lastItem == null) newIndex = treeModel.getChildCount(parent); else newIndex = parent.getIndex(lastItem); /* Let the treemodel know. */ treeModel.insertNodeInto(createNewNode("Inserted " + Integer.toString(insertCount++)), parent, newIndex); } } } // End of SampleTree.InsertAction /** * ReloadAction is used to reload from the selected node. If nothing * is selected, reload is not issued. */ class ReloadAction extends Object implements ActionListener { /** * Messaged when the user clicks on the Reload menu item. * Determines the selection from the Tree and asks the treemodel * to reload from that node. */ public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode lastItem = getSelectedNode(); if(lastItem != null) treeModel.reload(lastItem); } } // End of SampleTree.ReloadAction /** * RemoveAction removes the selected node from the tree. If * The root or nothing is selected nothing is removed. */ class RemoveAction extends Object implements ActionListener { /** * Removes the selected item as long as it isn't root. */ public void actionPerformed(ActionEvent e) { TreePath[] selected = getSelectedPaths(); if (selected != null && selected.length > 0) { TreePath shallowest; // The remove process consists of the following steps: // 1 - find the shallowest selected TreePath, the shallowest // path is the path with the smallest number of path // components. // 2 - Find the siblings of this TreePath // 3 - Remove from selected the TreePaths that are descendants // of the paths that are going to be removed. They will // be removed as a result of their ancestors being // removed. // 4 - continue until selected contains only null paths. while ((shallowest = findShallowestPath(selected)) != null) { removeSiblings(shallowest, selected); } } } /** * Removes the sibling TreePaths of <code>path</code>, that are * located in <code>paths</code>. */ private void removeSiblings(TreePath path, TreePath[] paths) { // Find the siblings if (path.getPathCount() == 1) { // Special case, set the root to null for (int counter = paths.length - 1; counter >= 0; counter--) { paths[counter] = null; } treeModel.setRoot(null); } else { // Find the siblings of path. TreePath parent = path.getParentPath(); MutableTreeNode parentNode = (MutableTreeNode)parent. getLastPathComponent(); ArrayList toRemove = new ArrayList(); int depth = parent.getPathCount(); // First pass, find paths with a parent TreePath of parent for (int counter = paths.length - 1; counter >= 0; counter--) { if (paths[counter] != null && paths[counter]. getParentPath().equals(parent)) { toRemove.add(paths[counter]); paths[counter] = null; } } // Second pass, remove any paths that are descendants of the // paths that are going to be removed. These paths are // implicitly removed as a result of removing the paths in // toRemove int rCount = toRemove.size(); for (int counter = paths.length - 1; counter >= 0; counter--) { if (paths[counter] != null) { for (int rCounter = rCount - 1; rCounter >= 0; rCounter--) { if (((TreePath)toRemove.get(rCounter)). isDescendant(paths[counter])) { paths[counter] = null; } } } } // Sort the siblings based on position in the model if (rCount > 1) { Collections.sort(toRemove, new PositionComparator()); } int[] indices = new int[rCount]; Object[] removedNodes = new Object[rCount]; for (int counter = rCount - 1; counter >= 0; counter--) { removedNodes[counter] = ((TreePath)toRemove.get(counter)). getLastPathComponent(); indices[counter] = treeModel.getIndexOfChild (parentNode, removedNodes[counter]); parentNode.remove(indices[counter]); } treeModel.nodesWereRemoved(parentNode, indices, removedNodes); } } /** * Returns the TreePath with the smallest path count in * <code>paths</code>. Will return null if there is no non-null * TreePath is <code>paths</code>. */ private TreePath findShallowestPath(TreePath[] paths) { int shallowest = -1; TreePath shallowestPath = null; for (int counter = paths.length - 1; counter >= 0; counter--) { if (paths[counter] != null) { if (shallowest != -1) { if (paths[counter].getPathCount() < shallowest) { shallowest = paths[counter].getPathCount(); shallowestPath = paths[counter]; if (shallowest == 1) { return shallowestPath; } } } else { shallowestPath = paths[counter]; shallowest = paths[counter].getPathCount(); } } } return shallowestPath; } /** * An Comparator that bases the return value on the index of the * passed in objects in the TreeModel. * <p> * This is actually rather expensive, it would be more efficient * to extract the indices and then do the comparision. */ private class PositionComparator implements Comparator { public int compare(Object o1, Object o2) { TreePath p1 = (TreePath)o1; int o1Index = treeModel.getIndexOfChild(p1.getParentPath(). getLastPathComponent(), p1.getLastPathComponent()); TreePath p2 = (TreePath)o2; int o2Index = treeModel.getIndexOfChild(p2.getParentPath(). getLastPathComponent(), p2.getLastPathComponent()); return o1Index - o2Index; } public boolean equals(Object obj) { return super.equals(obj); } } } // End of SampleTree.RemoveAction /** * ShowHandlesChangeListener implements the ChangeListener interface * to toggle the state of showing the handles in the tree. */ class ShowHandlesChangeListener extends Object implements ChangeListener { public void stateChanged(ChangeEvent e) { tree.setShowsRootHandles(((JCheckBox)e.getSource()).isSelected()); } } // End of class SampleTree.ShowHandlesChangeListener /** * ShowRootChangeListener implements the ChangeListener interface * to toggle the state of showing the root node in the tree. */ class ShowRootChangeListener extends Object implements ChangeListener { public void stateChanged(ChangeEvent e) { tree.setRootVisible(((JCheckBox)e.getSource()).isSelected()); } } // End of class SampleTree.ShowRootChangeListener /** * TreeEditableChangeListener implements the ChangeListener interface * to toggle between allowing editing and now allowing editing in * the tree. */ class TreeEditableChangeListener extends Object implements ChangeListener { public void stateChanged(ChangeEvent e) { tree.setEditable(((JCheckBox)e.getSource()).isSelected()); } } // End of class SampleTree.TreeEditableChangeListener static public void main(String args[]) { new SampleTree(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -