All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.globalmentor.swing.tree.TreeModelSelectionUpdateAdapter Maven / Gradle / Ivy

The newest version!
/*
 * Copyright © 1996-2009 GlobalMentor, Inc. 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.globalmentor.swing.tree;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

import com.globalmentor.swing.event.*;

/**
 * A class that automatically updates a tree selection when nodes are added or deleted.
 * 
    *
  • If the selected node has been deleted, the selection will be updated to the next appropriate node.
  • *
* @author Garret Wilson */ public class TreeModelSelectionUpdateAdapter extends TreeModelAdapter { /** The tree this adapter updates. */ private final JTree tree; /** @return The tree this adapter updates. */ protected JTree getTree() { return tree; } /** * Tree constructor. * @param tree The tree component that will be updated. */ public TreeModelSelectionUpdateAdapter(final JTree tree) { this.tree = tree; //save the tree } /** *

* Invoked after nodes have been inserted into the tree. *

* *

* Use e.getPath() to get the parent of the new node(s). e.getChildIndices() returns the index(es) of the new node(s) in ascending * order. *

*/ //TODO fix public void treeNodesInserted(final TreeModelEvent treeModelEvent) {} /** * Invoked after nodes have been removed from the tree. If one of the nodes was previously selected, the selection will be moved to the next appropriate node. *

* If the parent node of the deleted node is not a TreeNode, the parent path will be selected. *

* @param treeModelEvent The event, with treeModelEvent.getPath() returning the former parent of the deleted node(s), and * treeModelEvent.getChildIndices() returning, in ascending order, the index(es) the node(s) had before being deleted.

*/ public void treeNodesRemoved(final TreeModelEvent treeModelEvent) { final JTree tree = getTree(); //get our tree component final TreePath parentPath = treeModelEvent.getTreePath(); //get the path of the removed node's parent final Object[] children = treeModelEvent.getChildren(); //get the removed children for(int i = children.length - 1; i >= 0; i--) { //look at each of the deleted children, starting at the last deletion final TreePath childPath = parentPath.pathByAddingChild(children[i]); //get the path to this deleted child if(tree.isPathSelected(childPath)) { //if this path was selected final TreePath remainingPath = Trees.getRemainingPath(parentPath, treeModelEvent.getChildIndices()[i]); //find the path remaining after the child at this index was deleted SwingUtilities.invokeLater(new Runnable() { //select the remaining path, but do it later, so that any other listeners that might be listening for the event that caused the deletion may finish processing using the current selection state public void run() { tree.setSelectionPath(remainingPath); } }); return; //don't look for more selections } } } /** *

* Invoked after the tree has drastically changed structure from a given node down. If the path returned by e.getPath() is of length one and the first element * does not identify the current root node the first element should become the new root of the tree. *

* *

* Use e.getPath() to get the path to the node. e.getChildIndices() returns null. *

*/ //TODO fix public void treeStructureChanged(final TreeModelEvent treeModelEvent) {} }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy