/*
Copyright (c) 2003-2009 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.TreeNode;
import javax.swing.tree.TreePath;
import DisplayProject.DisplayNode;
/**
* Expands a node in a JTree or OutlineField
* @author Peter
*
*/
public class NodeExpand extends PendingAction{
private int Function;
private int Row;
private DisplayNode 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 NodeExpand(Component pComponent, int pFunction) {
super(pComponent);
this.Function = pFunction;
}
public NodeExpand(Component pComponent, DisplayNode pNode, int pFunction) {
super(pComponent);
this.Node = pNode;
this.Function = pFunction;
}
public NodeExpand(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 );
this._MaintainIsOpened(thePath);
break;
case COLLAPSE_SELECTED:
TreePath thePath2 = ((JTree)this._component).getSelectionPath();
((JTree)this._component).collapsePath( thePath2 );
this._MaintainIsOpened(thePath2);
break;
case EXPAND_NODE:
TreePath thePath3 = new TreePath(this.Node.getPath());
((JTree)this._component).expandPath( thePath3 );
this._MaintainIsOpened(thePath3);
break;
case COLLAPSE_NODE:
TreePath thePath4 = new TreePath(this.Node.getPath());
((JTree)this._component).collapsePath( thePath4 );
this._MaintainIsOpened(thePath4);
break;
case EXPAND_ROW:
((JTree)this._component).expandRow( this.Row );
TreePath thePath5 = ((JTree)this._component).getPathForRow(this.Row);
this._MaintainIsOpened(thePath5);
break;
case COLLAPSE_ROW:
((JTree)this._component).collapseRow( this.Row );
TreePath thePath6 = ((JTree)this._component).getPathForRow(this.Row);
this._MaintainIsOpened(thePath6);
break;
case EXPAND_ALL:
this._expandAll( ((JTree)this._component), true );
break;
case COLLAPSE_ALL:
this._expandAll( ((JTree)this._component), false );
break;
}
}
public static void expandSelected(Component tree){
ActionMgr.addAction(new NodeExpand(tree, EXPAND_SELECTED));
}
public static void collapseSelected(Component tree){
ActionMgr.addAction(new NodeExpand(tree, COLLAPSE_SELECTED));
}
public static void expandNode(Component tree, DisplayNode node){
ActionMgr.addAction(new NodeExpand(tree, node, EXPAND_NODE));
}
public static void expandNode(Component tree, DisplayNode node, boolean expand){
if(expand){
ActionMgr.addAction(new NodeExpand(tree, node, EXPAND_NODE));
}
else{
ActionMgr.addAction(new NodeExpand(tree, node, COLLAPSE_NODE));
}
}
public static void collapseNode(Component tree, DisplayNode node){
ActionMgr.addAction(new NodeExpand(tree, node, COLLAPSE_NODE));
}
public static void expandNode(DisplayNode node){
if(node != null && node.getUserObject() instanceof JTree){
expandNode(((JTree)(node.getUserObject())), node);
}
}
public static void expandNode(DisplayNode node, boolean expand){
if(node != null && node.getUserObject() instanceof JTree){
expandNode(((JTree)(node.getUserObject())), node, expand);
}
}
public static void collapseNode(DisplayNode node){
if(node != null && node.getUserObject() instanceof JTree){
collapseNode(((JTree)(node.getUserObject())), node);
}
}
public static void expandRow(Component tree, int row){
ActionMgr.addAction(new NodeExpand(tree, row, EXPAND_ROW));
}
public static void collapseRow(Component tree, int row){
ActionMgr.addAction(new NodeExpand(tree, row, COLLAPSE_ROW));
}
public static void expandAll(Component tree){
ActionMgr.addAction(new NodeExpand(tree, EXPAND_ALL));
}
public static void collapseAll(Component tree){
ActionMgr.addAction(new NodeExpand(tree, COLLAPSE_ALL));
}
@SuppressWarnings("unchecked")
public static void expandOpenNodes(JTree tree, DisplayNode root) {
for (Enumeration e = root.breadthFirstEnumeration(); e.hasMoreElements();) {
Object node = e.nextElement();
if (node instanceof DisplayNode) {
DisplayNode dNode = (DisplayNode)node;
if (dNode.isOpened()) {
expandNode(tree, dNode);
}
}
}
}
// If expand is true, expands all nodes in the tree.
// Otherwise, collapses all nodes in the tree.
private void _expandAll(JTree tree, boolean expand) {
TreeNode root = (TreeNode)tree.getModel().getRoot();
this._expandAll(tree, new TreePath(root), expand);
}
@SuppressWarnings("unchecked")
private 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);
this._expandAll(tree, path, expand);
}
}
// Expansion or collapse must be done bottom-up
if (expand) {
tree.expandPath(parent);
} else {
tree.collapsePath(parent);
}
this._MaintainIsOpened(parent);
}
// to maintain the isOpened flag in DisplayNode
// maintain the isOpened flag on DisplayNode
private void _MaintainIsOpened( TreePath path ){
if( path != null){
//check to see if path is expanded
if(((JTree)this._component).isExpanded(path)){
((DisplayNode)path.getLastPathComponent()).setIsOpened(true);
}
else{
((DisplayNode)path.getLastPathComponent()).setIsOpened(false);
}
this._MaintainIsOpened(path.getParentPath());
}
}
}