Package DisplayProject.actions

Source Code of DisplayProject.actions.TreeView

/*
Copyright (c) 2003-2008 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o 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.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS 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 COPYRIGHT OWNER OR 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.


*/
package DisplayProject.actions;

import java.awt.Component;
import java.util.Enumeration;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
* Provides a number of EDT safe operations on a JTree
*
*/
public class TreeView extends PendingAction {
    private int Function;
    private int Row;
    private DefaultMutableTreeNode Node;
    private static final int EXPAND_SELECTED = 1;
    private static final int COLLAPSE_SELECTED = 2;
    private static final int EXPAND_NODE = 3;
    private static final int COLLAPSE_NODE = 4;
    private static final int EXPAND_ROW = 5;
    private static final int COLLAPSE_ROW = 6;
    private static final int EXPAND_ALL = 7;
    private static final int COLLAPSE_ALL = 8;

    public TreeView(Component pComponent, int pFunction) {
        super(pComponent);
        this.Function = pFunction;
    }
    public TreeView(Component pComponent, DefaultMutableTreeNode pNode, int pFunction) {
        super(pComponent);
        this.Node = pNode;
        this.Function = pFunction;
    }
    public TreeView(Component pComponent, int pRow, int pFunction) {
        super(pComponent);
        this.Row = pRow;
        this.Function = pFunction;
    }

    public void performAction() {
        switch(this.Function){
        case EXPAND_SELECTED:
            TreePath thePath = ((JTree)this._component).getSelectionPath();
            ((JTree)this._component).expandPath( thePath );
            break;
        case COLLAPSE_SELECTED:
            TreePath thePath2 = ((JTree)this._component).getSelectionPath();
            ((JTree)this._component).collapsePath( thePath2 );
            break;
        case EXPAND_NODE:
            TreePath thePath3 = new TreePath(this.Node.getPath());
            ((JTree)this._component).expandPath( thePath3 );
            break;
        case COLLAPSE_NODE:
            TreePath thePath4 = new TreePath(this.Node.getPath());
            ((JTree)this._component).collapsePath( thePath4 );
            break;
        case EXPAND_ROW:
            ((JTree)this._component).expandRow( this.Row );
            break;
        case COLLAPSE_ROW:
            ((JTree)this._component).collapseRow( this.Row );
            break;
        case EXPAND_ALL:
            expandAll( ((JTree)this._component), true );
            break;
        case COLLAPSE_ALL:
            expandAll( ((JTree)this._component), false );
            break;
        }
    }
    public static void expandSelected(Component tree){
        ActionMgr.addAction(new TreeView(tree, EXPAND_SELECTED));
    }
    public static void collapseSelected(Component tree){
        ActionMgr.addAction(new TreeView(tree, COLLAPSE_SELECTED));
    }
    public static void expandNode(Component tree, DefaultMutableTreeNode node){
        ActionMgr.addAction(new TreeView(tree, node, EXPAND_NODE));
    }
    public static void collapseNode(Component tree, DefaultMutableTreeNode node){
        ActionMgr.addAction(new TreeView(tree, node, COLLAPSE_NODE));
    }
    public static void expandNode(DefaultMutableTreeNode node){
        JTree theTree = _GetTree(node);
        ActionMgr.addAction(new TreeView(theTree, node, EXPAND_NODE));
    }
    public static void collapseNode(DefaultMutableTreeNode node){
        JTree theTree = _GetTree(node);
        ActionMgr.addAction(new TreeView(theTree, node, COLLAPSE_NODE));
    }
    public static void expandRow(Component tree, int row){
        ActionMgr.addAction(new TreeView(tree, row, EXPAND_ROW));
    }
    public static void collapseRow(Component tree, int row){
        ActionMgr.addAction(new TreeView(tree, row, COLLAPSE_ROW));
    }
    public static void expandAll(Component tree){
        ActionMgr.addAction(new TreeView(tree, EXPAND_ALL));
    }
    public static void collapseAll(Component tree){
        ActionMgr.addAction(new TreeView(tree, COLLAPSE_ALL));
    }

    private static JTree _GetTree(DefaultMutableTreeNode node){
        // get root node and then get tree from there
        JTree theTree = null;
        //MHTODO - can we get the tree from the node?????
        return theTree;
    }
    //  If expand is true, expands all nodes in the tree.
    // Otherwise, collapses all nodes in the tree.
    private static void expandAll(JTree tree, boolean expand) {
        TreeNode root = (TreeNode)tree.getModel().getRoot();

        // Traverse tree from root
        expandAll(tree, new TreePath(root), expand);
    }
    @SuppressWarnings("unchecked")
  private static void expandAll(JTree tree, TreePath parent, boolean expand) {
        // Traverse children
        TreeNode node = (TreeNode)parent.getLastPathComponent();
        if (node.getChildCount() >= 0) {
            for (Enumeration e=node.children(); e.hasMoreElements(); ) {
                TreeNode n = (TreeNode)e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                expandAll(tree, path, expand);
            }
        }
        // Expansion or collapse must be done bottom-up
        if (expand) {
            tree.expandPath(parent);
        } else {
            tree.collapsePath(parent);
        }
    }

}
TOP

Related Classes of DisplayProject.actions.TreeView

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.