下面列出了javax.swing.tree.DefaultMutableTreeNode#remove ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Removes the selected module in the tvProcessingList from the list
*/
private void removeModule() {
DefaultMutableTreeNode selected = getSelectedItem(tvProcessing);
if (selected == null)
return;
if (selected instanceof DPPModuleTreeNode) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent();
if (parent instanceof DPPMSLevelTreeNode && ((DPPMSLevelTreeNode) parent).isEnabled()) {
parent.remove(selected);
logger.finest("Removed module " + ((DPPModuleTreeNode) selected).getModule().getName()
+ " from processing list.");
}
} else {
logger.finest("Cannot remove item " + selected.toString() + " from processing list.");
}
((DefaultTreeModel) tvProcessing.getModel()).reload();
expandAllNodes(tvProcessing);
}
/**
* Removes empty categories. This method can create some new empty
* categories!
*
* @param current
* the current node
* @return true if an empty category was removed
*/
private boolean removeEmptyCategories(DefaultMutableTreeNode current) {
if (!current.children().hasMoreElements()) {
return false;
}
TreeNode node = current.getFirstChild();
boolean removed = false;
while (node != null) {
DefaultMutableTreeNode n = (DefaultMutableTreeNode) node;
node = current.getChildAfter(n);
if (n.isLeaf()) {
if (n instanceof CategoryTreeNode && n.getChildCount() == 0) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) n.getParent();
parent.remove(n);
removed = true;
}
} else {
removed = removed || removeEmptyCategories(n);
}
}
return removed;
}
@Override
public final void dataRemoved(DataSet data) {
DefaultMutableTreeNode root = (DefaultMutableTreeNode) ((DefaultTreeModel) tree.getModel()).getRoot();
for (int i = 0; i < root.getChildCount(); i++) {
DataSet toSearch = (DataSet) ((DefaultMutableTreeNode) root.getChildAt(i)).getUserObject();
if (toSearch.equals(data)) {
Object removed = root.getChildAt(i);
root.remove(i);
((javax.swing.tree.DefaultTreeModel) tree.getModel()).nodesWereRemoved(root, new int[] { i },
new Object[] { removed });
// to remove a node requires it to be selected, so move the selection to the root
tree.setSelectionPath(new TreePath(root));
break;
}
}
}
/**
* delete all the selected nodes
*/
public void deleteSelectedNodes() {
DefaultTreeModel model = (DefaultTreeModel) dataTree.getModel();
TreePath[] selectedPaths = dataTree.getSelectionPaths();
if (selectedPaths != null) {
for (TreePath selectedPath : selectedPaths) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectedPath.getLastPathComponent();
node.removeAllChildren();
model.nodeStructureChanged(node);
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
if (parent != null) {
parent.remove(node);
model.nodeStructureChanged(parent);
}
}
}
}
private void renormalizeMonitorDepth(DefaultMutableTreeNode monitorNode, int index) {
// First, remove all duplicates of the item at index "index"
DefaultMutableTreeNode threadNode1 = (DefaultMutableTreeNode) monitorNode.getChildAt(index);
ThreadInfo mi1 = (ThreadInfo) threadNode1.getUserObject();
int i = index + 1;
while (i < monitorNode.getChildCount()) {
DefaultMutableTreeNode threadNode2 = (DefaultMutableTreeNode) monitorNode.getChildAt(i);
ThreadInfo mi2 = (ThreadInfo) threadNode2.getUserObject();
if (mi1.getName().equals(mi2.getName())) {
if (threadNode2.getChildCount() > 0) {
threadNode1.add((DefaultMutableTreeNode) threadNode2.getFirstChild());
monitorNode.remove(i);
continue;
}
}
i++;
}
// Second, recurse into item "index"
renormalizeThreadDepth(threadNode1);
}
/**
* Removes the selected module in the tvProcessingList from the list
*/
private void removeModule() {
DefaultMutableTreeNode selected = getSelectedItem(tvProcessing);
if (selected == null)
return;
if (selected instanceof DPPModuleTreeNode) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent();
if (parent instanceof DPPMSLevelTreeNode && ((DPPMSLevelTreeNode) parent).isEnabled()) {
parent.remove(selected);
logger.finest("Removed module " + ((DPPModuleTreeNode) selected).getModule().getName()
+ " from processing list.");
}
} else {
logger.finest("Cannot remove item " + selected.toString() + " from processing list.");
}
((DefaultTreeModel) tvProcessing.getModel()).reload();
expandAllNodes(tvProcessing);
}
@SuppressWarnings("unchecked")
protected void populate(Set<URI> uris, DefaultMutableTreeNode node) {
if (node instanceof TreeNodeExpandable) {
((TreeNodeExpandable)node).populateTreeNode(api);
int i = node.getChildCount();
while (i-- > 0) {
T child = (T)node.getChildAt(i);
if (uris.contains(child.getUri())) {
populate(uris, child);
} else {
node.remove(i);
}
}
}
}
private DefaultMutableTreeNode deleteNode(AnonymousClassDeclaration childAnonymous) {
Enumeration enumeration = root.postorderEnumeration();
DefaultMutableTreeNode childNode = findNode(childAnonymous);
DefaultMutableTreeNode parentNode = root;
while(enumeration.hasMoreElements()) {
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)enumeration.nextElement();
AnonymousClassDeclarationObject currentAnonymous = (AnonymousClassDeclarationObject)currentNode.getUserObject();
if(currentAnonymous != null && isParent(childAnonymous, currentAnonymous.getAstNode())) {
parentNode = currentNode;
break;
}
}
parentNode.remove(childNode);
AnonymousClassDeclarationObject childAnonymousObject = (AnonymousClassDeclarationObject)childNode.getUserObject();
childAnonymousObject.setAstNode(null);
return parentNode;
}
public void removeSongNodeInTreeList(JTree tree, int index) {
DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree
.getModel().getRoot();
DefaultMutableTreeNode list = (DefaultMutableTreeNode) root
.getChildAt(index);
list.remove(songNode);
// 列表名更新
String listName = (String) list.getUserObject();
listName = listName.substring(0, listName.lastIndexOf("[")) + "["
+ list.getChildCount() + "]";
list.setUserObject(listName);
// 如果这里不更新树的话 会不正确显示
tree.updateUI();
}
public void removeSongNodeInTreeList(JTree tree, int index,
SongNode songNode) {
DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel()
.getRoot();
DefaultMutableTreeNode list = (DefaultMutableTreeNode) root
.getChildAt(index);
list.remove(songNode);
// 列表名更新
String listName = (String) list.getUserObject();
listName = listName.substring(0, listName.lastIndexOf("[")) + "["
+ list.getChildCount() + "]";
list.setUserObject(listName);
// 如果这里不更新树的话 会不正确显示
tree.updateUI();
}
@Test
public void remove() {
final TreeNode<Integer> tree = newTree(5, new Random(123));
final DefaultMutableTreeNode stree = newSwingTree(5, new Random(123));
tree.remove(0);
stree.remove(0);
Assert.assertTrue(equals(tree, stree));
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
selectedNode = (DefaultMutableTreeNode) treeMem.getLastSelectedPathComponent();
if (selectedNode != null){
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
parent.remove(selectedNode);
tree.reload(parent);
}
}
private static void removePathFromActionsTree(JTree tree, ActionUrl url) {
if (url.myComponent == null) return;
final TreePath treePath = CustomizationUtil.getTreePath(tree, url);
if (treePath == null) return;
DefaultMutableTreeNode node = (DefaultMutableTreeNode)treePath.getLastPathComponent();
final int absolutePosition = url.getAbsolutePosition();
if (node.getChildCount() > absolutePosition && absolutePosition >= 0) {
DefaultMutableTreeNode child = (DefaultMutableTreeNode)node.getChildAt(absolutePosition);
if (child.getUserObject().equals(url.getComponent())) {
node.remove(child);
}
}
}
public void expandTreeToDepth(DefaultMutableTreeNode node, int depth, String userId, List<ListOptionSerialized> blankRestrictedTools, List<String> accessAdminNodeIds, boolean onlyAccessNodes, boolean shopping, boolean shoppingPeriodTool, String filterSearch){
projectLogic.addChildrenNodes(node, userId, blankRestrictedTools, onlyAccessNodes, accessAdminNodeIds, shopping, shoppingPeriodTool);
//set expand flag to true so to not look for children again:
((NodeModel) node.getUserObject()).setAddedDirectChildrenFlag(true);
getTree().getTreeState().expandNode(node);
if(depth > 0){
//recursive function stopper:
int newDepth = depth - 1;
//count down backwards since we could be deleting these children nodes
for(int i = node.getChildCount() - 1; i >= 0; i--){
expandTreeToDepth((DefaultMutableTreeNode) node.getChildAt(i), newDepth, userId, blankRestrictedTools, accessAdminNodeIds, onlyAccessNodes, shopping, shoppingPeriodTool, filterSearch);
}
}else{
//make sure all children are collapsed and filter out the ones that need to be filtered
//count down backwards since we could be deleting these children nodes
for(int i = node.getChildCount() - 1; i >= 0; i--){
getTree().getTreeState().collapseNode(node.getChildAt(i));
String nodeTitle = ((NodeModel) ((DefaultMutableTreeNode) node.getChildAt(i)).getUserObject()).getNode().description.toLowerCase();
if(filterSearch != null && !"".equals(filterSearch.trim()) && !nodeTitle.contains(filterSearch.toLowerCase())){
//delete this child:
node.remove(i);
}
}
}
//check if all of the children have been removed (but don't delete root node)
if(node.getParent() != null && node.getChildCount() == 0){
((DefaultMutableTreeNode) node.getParent()).remove(node);
}
}
private void removeEmptyGroups(DefaultMutableTreeNode root, Map<String, Integer> groupNodeMap) {
DefaultMutableTreeNode rootChild = (DefaultMutableTreeNode) root.getFirstChild();
while (rootChild != null) {
DefaultMutableTreeNode nextChild = rootChild.getNextSibling();
if (rootChild.getChildCount() == 0 && groupNodeMap.containsKey(rootChild.getUserObject().toString())) {
root.remove(rootChild);
}
rootChild = nextChild;
}
}
public DefaultMutableTreeNode getRemainingExpression(Expression expressionToBeRemoved) {
DefaultMutableTreeNode newRoot = new DefaultMutableTreeNode();
processExpression(newRoot, completeExpression);
DefaultMutableTreeNode leaf = newRoot.getFirstLeaf();
while(leaf != null) {
Expression expression = (Expression)leaf.getUserObject();
if(expression.equals(expressionToBeRemoved)) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode)leaf.getParent();
if(parent != null) {
DefaultMutableTreeNode grandParent = (DefaultMutableTreeNode)parent.getParent();
DefaultMutableTreeNode sibling = null;
if(leaf.getNextSibling() != null) {
sibling = leaf.getNextSibling();
}
else if(leaf.getPreviousSibling() != null) {
sibling = leaf.getPreviousSibling();
}
if(grandParent != null) {
int parentIndex = grandParent.getIndex(parent);
grandParent.remove(parent);
grandParent.insert(sibling, parentIndex);
}
else {
newRoot = sibling;
}
break;
}
else {
newRoot = null;
break;
}
}
leaf = leaf.getNextLeaf();
}
return newRoot;
}
public void expandTreeToDepth(DefaultMutableTreeNode node, int depth, String userId, List<ListOptionSerialized> blankRestrictedTools, List<String> accessAdminNodeIds, boolean onlyAccessNodes, boolean shopping, boolean shoppingPeriodTool, String filterSearch){
projectLogic.addChildrenNodes(node, userId, blankRestrictedTools, onlyAccessNodes, accessAdminNodeIds, shopping, shoppingPeriodTool);
//set expand flag to true so to not look for children again:
((NodeModel) node.getUserObject()).setAddedDirectChildrenFlag(true);
getTree().getTreeState().expandNode(node);
if(depth > 0){
//recursive function stopper:
int newDepth = depth - 1;
//count down backwards since we could be deleting these children nodes
for(int i = node.getChildCount() - 1; i >= 0; i--){
expandTreeToDepth((DefaultMutableTreeNode) node.getChildAt(i), newDepth, userId, blankRestrictedTools, accessAdminNodeIds, onlyAccessNodes, shopping, shoppingPeriodTool, filterSearch);
}
}else{
//make sure all children are collapsed and filter out the ones that need to be filtered
//count down backwards since we could be deleting these children nodes
for(int i = node.getChildCount() - 1; i >= 0; i--){
getTree().getTreeState().collapseNode(node.getChildAt(i));
String nodeTitle = ((NodeModel) ((DefaultMutableTreeNode) node.getChildAt(i)).getUserObject()).getNode().description.toLowerCase();
if(filterSearch != null && !"".equals(filterSearch.trim()) && !nodeTitle.contains(filterSearch.toLowerCase())){
//delete this child:
node.remove(i);
}
}
}
//check if all of the children have been removed (but don't delete root node)
if(node.getParent() != null && node.getChildCount() == 0){
((DefaultMutableTreeNode) node.getParent()).remove(node);
}
}
/**
*
* @param root
* @return boolean bad leaves were returned
*/
private boolean removeBadLeaves(DefaultMutableTreeNode root) {
// no bad leaves yet
boolean badLeaves = false;
// reference first leaf
DefaultMutableTreeNode leaf = root.getFirstLeaf();
// if leaf is root then its the only node
if (leaf.isRoot())
return false;
int leafCount = root.getLeafCount(); // this get method changes if in for loop so have to define outside of it
for (int i = 0; i < leafCount; i++) {
DefaultMutableTreeNode nextLeaf = leaf.getNextLeaf();
// if it does not start with the text then snip it off its parent
if (!leaf.getUserObject().toString().startsWith(textToMatch)) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) leaf.getParent();
if (parent != null)
parent.remove(leaf);
badLeaves = true;
}
leaf = nextLeaf;
}
return badLeaves;
}
/**
*
* @param root
* @return boolean bad leaves were returned
*/
private boolean removeBadLeaves(DefaultMutableTreeNode root) {
// no bad leaves yet
boolean badLeaves = false;
// reference first leaf
DefaultMutableTreeNode leaf = root.getFirstLeaf();
// if leaf is root then its the only node
if (leaf.isRoot())
return false;
int leafCount = root.getLeafCount(); // this get method changes if in for loop so have to define outside of it
for (int i = 0; i < leafCount; i++) {
DefaultMutableTreeNode nextLeaf = leaf.getNextLeaf();
// if it does not start with the text then snip it off its parent
if (!filter.accepts(filterText, leaf.getUserObject().toString())) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) leaf.getParent();
if (parent != null)
parent.remove(leaf);
badLeaves = true;
}
leaf = nextLeaf;
}
return badLeaves;
}
private void renormalizeBlockingThreadTree(MonitorMap mmap, Map directChildMap) {
Map allBlockingThreadsMap = new HashMap(directChildMap); // All threads that are blocking at least one other thread
// First, renormalize based on monitors to get our unique tree
// Tree will be unique as long as there are no deadlocks aka monitor loops
for (Iterator iter = mmap.iterOfKeys(); iter.hasNext();) {
String monitor1 = (String) iter.next();
Map[] threads1 = mmap.getFromMonitorMap(monitor1);
DefaultMutableTreeNode thread1Node = (DefaultMutableTreeNode) allBlockingThreadsMap.get(monitor1);
if (thread1Node == null) {
continue;
}
// Get information on the one thread holding this lock
Iterator it = threads1[MonitorMap.LOCK_THREAD_POS].keySet().iterator();
if (!it.hasNext()) {
continue;
}
String threadLine1 = (String) it.next();
for (Iterator iter2 = mmap.iterOfKeys(); iter2.hasNext();) {
String monitor2 = (String) iter2.next();
if (monitor1 == monitor2) {
continue;
}
Map[] threads2 = mmap.getFromMonitorMap(monitor2);
if (threads2[MonitorMap.WAIT_THREAD_POS].containsKey(threadLine1)) {
// Get the node of the thread that is holding this lock
DefaultMutableTreeNode thread2Node = (DefaultMutableTreeNode) allBlockingThreadsMap.get(monitor2);
// Get the node of the monitor itself
DefaultMutableTreeNode monitor2Node = (DefaultMutableTreeNode) thread2Node.getFirstChild();
// If a redundant node for thread2 exists with no children, remove it
// To compare, we have to remove "Thread - " from the front of display strings
for (int i = 0; i < monitor2Node.getChildCount(); i++) {
DefaultMutableTreeNode child2 = (DefaultMutableTreeNode) monitor2Node.getChildAt(i);
if (child2.toString().substring(9).equals(threadLine1) && child2.getChildCount() == 0) {
monitor2Node.remove(i);
break;
}
}
// Thread1 is blocked by monitor2 held by thread2, so move thread1 under thread2
monitor2Node.insert(thread1Node, 0);
directChildMap.remove(monitor1);
break;
}
}
}
allBlockingThreadsMap.clear();
// Second, renormalize top level based on threads for cases where one thread holds multiple monitors
boolean changed = false;
do {
changed = false;
for (Iterator iter = directChildMap.entrySet().iterator(); iter.hasNext();) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) ((Map.Entry) iter.next()).getValue();
if (checkForDuplicateThreadItem(directChildMap, node)) {
changed = true;
break;
}
}
} while (changed);
// Third, renormalize lower levels of the tree based on threads for cases where one thread holds multiple monitors
for (Iterator iter = directChildMap.entrySet().iterator(); iter.hasNext();) {
renormalizeThreadDepth((DefaultMutableTreeNode) ((Map.Entry) iter.next()).getValue());
}
}