Tree control tool class to convert tree objects and webfxtree/webfxtreeitem

Source: Internet
Author: User
Tags addchild

/**
* Author: crazy_rain
* Date: 2007-2-7
* Time: 01:35:43 pm
* Introduction: tree control that converts tree objects and webfxtree/webfxtreeitem
*/

Public class tree {
/**
* Parent node of the tree
*/
Private tree parent;
/**
* Subnodes of the tree
*/
Private tree child;
/**
* Brother node of the tree
*/
Private tree brother;
/**
* Title displayed on the tree
*/
Private string label;
/**
* Js script triggered when you click the menu
*/
Private string script;
/**
* Define the ID component of the JS variable corresponding to the object
*/
Private int variableid = count ++;

/**
* Variable counters
*/
Private Static int COUNT = 0;

/**
* Tree node JS variable naming prefix
*/
Public static string prefix = "Node _";

/**
* Whether the tree contains only folders (whether this tree contains only folders)
*/
Public static Boolean only_folder = false;

/**
* Default constructor
*/
Private tree (){
}

/**
* Retrieve an empty tree
*
* @ Return tree
*/
Public static tree getemptytree (){
Return new tree ();
}

/**
* Construct a tree control and initialize the actions triggered by the text and clicks displayed above
*
* @ Param label the text displayed on the tree control
* @ Param script the action triggered when you click the tree control
*/
Public tree (string label, string script ){
This. Label = label;
This. Script = script;
}

/**
* Construct a tree control and initialize the text displayed above
*
* @ Param label the text displayed on the tree control
*/
Public tree (string label ){
This. Label = label;
}

/**
* Determine whether the current node is the root node.
*
* @ Return Boolean returns true if the root node is used.
*/
Public Boolean isroot (){
Return null = This. parent;
}

/**
* Get the variable name defined in the tree
*
* @ Return refers to the variable name defined in the tree.
*/
Private string variable (){
If (this. Parent = NULL)
Return "root ";
Return prefix + this. variableid;
}

/**
* Generate the JavaScript code corresponding to the tree control tree
*
* @ Param tree the tree control object to be generated for JS Code
* @ Return JS return the JS Code of the tree control represented by the generated tree
*/
Public static string generatejs (TREE tree ){
Stringbuffer buffer = new stringbuffer ();
If (tree. isroot ()){
// Is root, generate Root
Buffer. append (generateroot (tree ));
} Else {
// Not root, generate item
Buffer. append (generateitem (tree ));
}
If (tree. Child! = NULL) {// has child
Buffer. append (generatejs (tree. Child ));
}
If (tree. Brother! = NULL) {// has brother
Buffer. append (generatejs (tree. Brother ));
}
Return buffer. tostring ();
}

/**
* Obtain the JS Code that generates the tree control.
*
* @ Return JS can generate the JS Code of the tree control represented by this object
*/
Public String gettree (){
Return generatejs (this) + "/ndocument. Write (Root );";
}

/**
* The root of the Spanning Tree
*
* @ Param tree root node
* @ Return root: generate the Js of the root.
*/
Private Static string generateroot (TREE tree ){
String root = "Var root = new webfxtree (/" "+ tree. Label +"/");/N ";
If (only_folder ){
Root + = "root. setbehavior ('explorer ');/N ";
} Else {
Root + = "root. setbehavior ('classic ');/N ";
}
Return root;
}

/**
* Generate a tree node
*
* @ Param tree Node object
* @ Return JS Code of the treeitem Tree node
*/
Private Static string generateitem (TREE tree ){
String treeitem = "/N"
+ "Var" + prefix + tree. variableid + "= new webfxtreeitem (/" "+ tree. Label + "/"";
If (null = tree. Script ){
Treeitem + = ");";
} Else {
Treeitem + = ",/" "+ tree. Script + "/");";
}
Treeitem + = "/N" + tree. Parent. Variable () + ". Add (" + tree. Variable () + ");";
Return treeitem;
}

/**
* Add a subnode to the current node
*
* @ Param child the child node to be added
* @ Return child the added subnode
*/
Public tree addchild (tree child ){
If (this. Child! = NULL ){
// Has child, add the given child as the younger brother of the elder child
Tree elder = This. Child;
Return elder. addbrother (child );
} Else {
// Has no child, add this as the first child
This. Child = child;
Child. Parent = this;
}

Return child;
}

/**
* Add a subnode
*
* @ Param label the name displayed on the subnode
* @ Param script the action triggered when a subnode is clicked
* @ Return the subtree node added to the tree
*/
Public tree addchild (string label, string script ){
Tree son = new tree (Label, script );
Return addchild (son );
}

/**
* Add a subnode
*
* @ Param label the name displayed on the subnode
* @ Return child the subtree node added
*/
Public tree addchild (string label ){
Return addchild (Label, null );
}

/**
* Add the sibling node of the current Tree node
*
* @ Param younger the sibling node to be added
* @ Return the sibling node added to the tree
*/
Public tree addbrother (tree younger ){
If (isroot ()){
// If this is the root, add the given node as brother
Return addchild (younger );
}
Tree elder = this;
// Get the youngest brother
While (elder. Brother! = NULL ){
Elder = elder. brother;
}
// Add the younger as the yongest brother
Elder. Brother = younger;
Younger. Parent = elder. parent;
Return younger;

}

/**
* Add a sibling node to the current node
*
* @ Param label the text displayed on the sibling Node
* @ Param script the action triggered when a sibling node is clicked
* @ Return the sibling node added to the tree
*/
Public tree addbrother (string label, string script ){
Tree younger = new tree (Label, script );
Return addbrother (younger );
}

/**
* Add a sibling node to the current node
*
* @ Param label the text displayed on the sibling Node
* @ Return the sibling node added to the tree
*/
Public tree addbrother (string label ){
Return addbrother (Label, null );
}

/**
* Returns the final root node of the current node.
*
* @ Return the final root node of the current node
*/
Public tree getroot (){
TREE tree = this;
While (tree. parent! = NULL ){
Tree = tree. parent;
}
Return tree;
}

/**
* Get the action triggered when you click the tree control.
*
* @ Return script the action triggered when you click the tree control.
*/
Public String getscript (){
Return script;
}

/**
* Set the action triggered when you click the tree control.
*
* @ Param script the action to be set
*/
Public void setscript (string script ){
This. Script = script;
}

/**
* Get the text displayed on the tree control
*
* @ Return label the text displayed on the tree control
*/
Public String getlabel (){
Return label;
}

/**
* Set the text displayed on the tree control
*
* @ Param label the text to be displayed
*/
Public void setlabel (string label ){
This. Label = label;
}

Public String tostring (){
Return this. label;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.