package client.view.guicomponents;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellRenderer;
/**
* @brief This class creates a JTree, used in Error/Failure window for
* errors and failures.
*
* @author Ronald
*/
public class Tree
{
/**
* Generated serial.
*/
private static final long serialVersionUID = 1983919987487092942L;
/**
* This is the tree which holds the objects as his childs.
*/
private JTree tree = null;
/**
* A default tree model.
*/
private DefaultTreeModel dtmodel = null;
/**
* A node in the tree. This is the root node.
*/
private DefaultMutableTreeNode root = null;
/**
* Signals the tree that he should expand automatic.
*/
private boolean autoExpand = true;
/**
* An empty default constructor.
*/
public Tree()
{ }
/**
* This constructor initializes the JTree with an array.
*
* @param projects The array.
*/
public Tree(Object[] projects)
{
if(tree == null)
{
tree = new JTree(projects);
tree.setModel(getDefaultTreeModel());
}
}
/**
* Creates a JTree if not exist an returns his reference to the caller.
*
* @return The reference to the JTree.
*/
public JTree getJTree()
{
if(tree == null)
{
tree = new JTree(getDefaultTreeModel());
}
return tree;
}
/**
* Initializes the tree with an array of objects. This method is used
* by the workplace tree, because this tree need to initializes with
* custom data. Also the workplace tree doesn't use the
* DefaultTreeModel.
*
* @return void
*/
public void initTree()
{
tree = new JTree();
}
/**
* Initializes the tree with an array of objects. This method is used
* by the workplace tree, because this tree need to initializes with
* custom data. Also the workplace tree doesn't use the
* DefaultTreeModel.
*
* @param projects The array with the objects.
* @return void
*/
public void initTree(Object[] projects)
{
tree = new JTree(projects);
}
/**
* Sets the cell renderer for this Tree to cr.
*
* @return void
*/
public void setRenderer(TreeCellRenderer cr)
{
getJTree().setCellRenderer(cr);
}
/**
* Creates a new model to the tree if not existing and returns the
* reference to the caller.
*
* @return The reference to the tree model.
*/
public DefaultTreeModel getDefaultTreeModel()
{
if(dtmodel == null)
{
dtmodel = new DefaultTreeModel(getRootNode());
}
return dtmodel;
}
/**
* Adds the Object obj to the Node to.
*
* @param to The Node which will contain the obj.
* @param obj The Object which will be added to node to.
* @return void
*/
public void addNode(DefaultMutableTreeNode to,
DefaultMutableTreeNode obj)
{
getDefaultTreeModel().insertNodeInto(
obj,
to,
getRootNode().getChildCount());
}
/**
* Creates the root node of the JTree and returns the reference.
*
* @return The reference to the root node of the JTree.
*/
public DefaultMutableTreeNode getRootNode()
{
if(root == null)
{
root = new DefaultMutableTreeNode();
}
return root;
}
/**
* Returns the auto expand setting to the caller.
*
* @return The auto expand setting.
*/
public boolean getAutoExpand()
{
return autoExpand;
}
/**
* Delivers the Object name to the root node.
*
* @param name The Object used in the root node.
* @return void
*/
public void setTreeName(String name)
{
getRootNode().setUserObject(name);
}
/**
* Signals the client that he should expand automatically or not.
* By default, auto expand is set to true.
*
* @param aexp The new auto expand setting.
* @return void
*/
public void setAutoExpand(boolean aexp)
{
autoExpand = aexp;
}
}