Package totalcross.ui.tree
Class Node
- java.lang.Object
-
- totalcross.util.Vector
-
- totalcross.ui.tree.Node
-
- All Implemented Interfaces:
Comparable
public class Node extends Vector implements Comparable
This class defines the requirements for an object that can be used as a tree node in a Tree.
-
-
Field Summary
Fields Modifier and Type Field Description booleanallowsChildrenflag to determine if this node can have childrenintbackColorThe background color, or -1 to use the default.intforeColorThe foreground color, or -1 to use the default.booleanisCheckedflag to determine if this node is checked (in multiple-selection trees).protected NodeparentThis node's parent (root node is when the parent node = null).intuserIntThe user's int that can be set with anything you want.java.lang.ObjectuserObjectThe user's object that can be set with anything you want.booleanvisitedflag to determine if the leaf node has been clicked before
-
Constructor Summary
Constructors Constructor Description Node()Default constructor to create a tree node that has no parent and no children, but which allows children.Node(java.lang.Object userObject)Default constructor to create a tree node with no parent, no children, but which allows children, and initializes it with the specified user object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(Node newChild)Method to remove newChild from its parent and makes it a child of this node by adding it to the end of this node's child vector.VectorbreadthFirstVector()Method to create and return a vector that traverses the subtree rooted at this node in breadth-first order.intcompareTo(java.lang.Object o)Must return > 0 if this object is greater than the other one, < 0 if its smaller, and 0 if they are equal.booleanequals(java.lang.Object o)NodegetChildAfter(Node aChild)Method to returns the child in this node's child array that immediately follows aChild, which must be a child of this node; otherwise, retrun null.NodegetChildBefore(Node aChild)Method to returns the child in this node's child array that immediately precedes aChild, which must be a child of this node; otherwise, retrun null.NodegetFirstChild()Method to return this node's first child.NodegetLastChild()Method to return this node's last child.intgetLevel()Method to return the number of levels above this node -- the distance from the root to this node.NodegetNextSibling()Method to return the next sibling of this node in the parent's children array.java.lang.StringgetNodeName()Method to return the node name of this node.NodegetParent()Method to return this node's parent or null if this node has no parent.Node[]getPath()Method to return the path from the root, to get to this node.protected Node[]getPathToRoot()Method to builds the parents of node up to and including the root node, where the original node is the last element in the returned array.NodegetPreviousSibling()Method to return the previous sibling of this node in the parent's children array.NodegetRoot()Method to return the root of the tree that contains this node.java.lang.Object[]getUserObjectPath()Method to return the user object path, from the root, to get to this node.intinsert(Node newChild, int childIndex)Method to removes newChild from its present parent (if it has a parent), sets the child's parent to this node, and then adds the child to this node's child array at index childIndex.booleanisLeaf()Method to return true if this node has no children.booleanisLeaf(boolean useAllowsChildren)Method to return true if this node has no children.booleanisNodeChild(Node aNode)Method to return true if aNode is a child of this node.booleanisNodeSibling(Node anotherNode)Method to return true if anotherNode is a sibling of (has the same parent as) this node.booleanisRoot()Method to return true if this node is a root.voidremove(int childIndex)Method to remove the child at the specified index from this node's children and sets that node's parent to null.voidremove(Node aChild)Method to remove aChild from this node's child array, giving it a null parent.voidremoveAllChildren()Method to remove all of this node's children, setting their parents to null.voidremoveFromParent()Method to remove the subtree rooted at this node from the tree, giving this node a null parent.voidsetParent(Node parent)Method to sets this node's parent to newParent but does not change the parent's child array.java.lang.StringtoString()Method to return the result of sending toString() to this node's user object, or null if this node has no user object.-
Methods inherited from class totalcross.util.Vector
addElement, addElements, addElementsNotNull, contains, copyInto, dump, elementAt, ensureCapacity, indexOf, indexOf, insertElementAt, isEmpty, peek, peek, peek, pop, pop, pop, push, qsort, qsort, qsort, qsort, removeAllElements, removeElement, removeElementAt, reverse, setSize, size, toObjectArray, toString
-
-
-
-
Field Detail
-
parent
protected Node parent
This node's parent (root node is when the parent node = null).
-
userObject
public java.lang.Object userObject
The user's object that can be set with anything you want. The userObject can also be a Control (or Container); in this case, all controls inside of it are drawn. The control itself is added to the Tree at an invisible location and is painted at the right position. This means that events are somewhat limited. You should change the lineH to increase the line to the desired control's height. Here's a sample:class Item extends Container { String txt; public Item(String s) {txt = s;} public void initUI() { add(new Check("Here"),LEFT,CENTER,PARENTSIZE+50,PREFERRED); add(new Button(txt),AFTER,CENTER,PARENTSIZE+50,PREFERRED); } } public void initUI() { try { setUIStyle(Settings.Android); TreeModel tmodel = new TreeModel(); Tree tree = new Tree(tmodel); tree.setCursorColor(Color.RED); tree.setLineHeight(fmH*3/2); add(tree,LEFT+50,TOP+50,FILL-50,FILL-50); Node root = new Node("Tree"); tmodel.setRoot(root); Node n; root.add(n = new Node("Branch1")); n.add(new Node(new Item("b1"))); n.add(new Node(new Item("b2"))); } catch (Exception ee) { MessageBox.showException(ee,true); } }
-
userInt
public int userInt
The user's int that can be set with anything you want.- Since:
- TotalCross 1.25
-
allowsChildren
public boolean allowsChildren
flag to determine if this node can have children
-
visited
public boolean visited
flag to determine if the leaf node has been clicked before
-
isChecked
public boolean isChecked
flag to determine if this node is checked (in multiple-selection trees).
-
backColor
public int backColor
The background color, or -1 to use the default.- Since:
- TotalCross 1.2
-
foreColor
public int foreColor
The foreground color, or -1 to use the default.- Since:
- TotalCross 1.2
-
-
Constructor Detail
-
Node
public Node()
Default constructor to create a tree node that has no parent and no children, but which allows children.
-
Node
public Node(java.lang.Object userObject)
Default constructor to create a tree node with no parent, no children, but which allows children, and initializes it with the specified user object.- Parameters:
userObject- the user object.
-
-
Method Detail
-
add
public void add(Node newChild)
Method to remove newChild from its parent and makes it a child of this node by adding it to the end of this node's child vector.- Parameters:
newChild- the new child node to add to the end of the child vector.
-
breadthFirstVector
public Vector breadthFirstVector()
Method to create and return a vector that traverses the subtree rooted at this node in breadth-first order.- Returns:
- the children vector of this node.
-
getChildAfter
public Node getChildAfter(Node aChild)
Method to returns the child in this node's child array that immediately follows aChild, which must be a child of this node; otherwise, retrun null.- Returns:
- the child node that immediately follows aChild node.
-
getChildBefore
public Node getChildBefore(Node aChild)
Method to returns the child in this node's child array that immediately precedes aChild, which must be a child of this node; otherwise, retrun null.- Returns:
- the child node that immediately precede aChild node.
-
getFirstChild
public Node getFirstChild()
Method to return this node's first child.- Returns:
- this node's first child, or null if there's none.
-
getLastChild
public Node getLastChild()
Method to return this node's last child.- Returns:
- this node's last child, or null if there's none.
-
getLevel
public int getLevel()
Method to return the number of levels above this node -- the distance from the root to this node.- Returns:
- the number of levels above this node -- the distance from the root to this node
-
getNodeName
public java.lang.String getNodeName()
Method to return the node name of this node.- Returns:
- the node name of this node.
-
getNextSibling
public Node getNextSibling()
Method to return the next sibling of this node in the parent's children array. Returns null if this node has no parent or is the parent's last child. This method performs a linear search that is O(n) where n is the number of children- Returns:
- the next sibling of this node in the parent's children array. Returns null if this node has no parent or is the parent's last child.
-
getPreviousSibling
public Node getPreviousSibling()
Method to return the previous sibling of this node in the parent's children array. Returns null if this node has no parent or is the parent's first child. This method performs a linear search that is O(n) where n is the number of children- Returns:
- the previous sibling of this node in the parent's children array. Returns null if this node has no parent or is the parent's in the tree.
-
getParent
public Node getParent()
Method to return this node's parent or null if this node has no parent.
-
getPath
public Node[] getPath()
Method to return the path from the root, to get to this node.- Returns:
- the path from the root, to get to this node.
-
getPathToRoot
protected Node[] getPathToRoot()
Method to builds the parents of node up to and including the root node, where the original node is the last element in the returned array.- Returns:
- the path from this node to the root node, including the root node.
-
getRoot
public Node getRoot()
Method to return the root of the tree that contains this node.- Returns:
- the root of the tree that contains this node.
-
getUserObjectPath
public java.lang.Object[] getUserObjectPath()
Method to return the user object path, from the root, to get to this node.- Returns:
- the user object path, from the root, to get to this node.
-
insert
public int insert(Node newChild, int childIndex)
Method to removes newChild from its present parent (if it has a parent), sets the child's parent to this node, and then adds the child to this node's child array at index childIndex. If childINdex is out of bound, newChild will be inserted at the end of the children vector- Parameters:
newChild- the new child to remove from this subtree and add to this node children vector at the specified index.childIndex- the position in the children vector to insert newChild.- Returns:
- the child index.
-
isLeaf
public boolean isLeaf()
Method to return true if this node has no children.- Returns:
- true if this node has no children.
-
isLeaf
public boolean isLeaf(boolean useAllowsChildren)
Method to return true if this node has no children.- Returns:
- true if this node has no children.
-
isRoot
public boolean isRoot()
Method to return true if this node is a root. Root node is node that has a null parent node.
-
isNodeChild
public boolean isNodeChild(Node aNode)
Method to return true if aNode is a child of this node.- Parameters:
aNode- the node to deterimine if it's a shild of this node.- Returns:
- true if aNode is a child of this node.
-
isNodeSibling
public boolean isNodeSibling(Node anotherNode)
Method to return true if anotherNode is a sibling of (has the same parent as) this node.- Returns:
- true if anotherNode is a sibling of (has the same parent as) this node.
-
remove
public void remove(int childIndex)
Method to remove the child at the specified index from this node's children and sets that node's parent to null.- Parameters:
childIndex- the index of the this node's children to be removed
-
remove
public void remove(Node aChild)
Method to remove aChild from this node's child array, giving it a null parent.- Parameters:
aChild- the child node to remove.
-
removeAllChildren
public void removeAllChildren()
Method to remove all of this node's children, setting their parents to null. If you don't want to set their parents to null, call removeAllElements instead.
-
removeFromParent
public void removeFromParent()
Method to remove the subtree rooted at this node from the tree, giving this node a null parent.
-
setParent
public void setParent(Node parent)
Method to sets this node's parent to newParent but does not change the parent's child array.- Parameters:
parent- the newParent node
-
toString
public java.lang.String toString()
Method to return the result of sending toString() to this node's user object, or null if this node has no user object.
-
compareTo
public int compareTo(java.lang.Object o)
Description copied from interface:ComparableMust return > 0 if this object is greater than the other one, < 0 if its smaller, and 0 if they are equal.- Specified by:
compareToin interfaceComparable
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
-