树形控件工具类,实现Tree对象和WebFXTreeWebFXTreeItem的转换

    技术2022-05-11  68

     /** * Author: crazy_rain * Date: 2007-2-7 * Time: 下午01:35:43 * Introduction:树形控件,实现Tree对象和WebFXTree/WebFXTreeItem的转换 */

    public class Tree { /**  * 该树的父节点  */ private Tree parent; /**  * 该树的子节点  */ private Tree child; /**  * 该树的兄弟节点  */ private Tree brother; /**  * 树上显示的标题  */ private String label; /**  * 点击菜单时触发的js 脚本  */ private String script; /**  * 定义与该对象对应的js变量的标识组成部分之一  */ private int variableId = count++;

     /**  * 变量计数器  */ private static int count = 0;

     /**  * 树形节点js变量命名前缀  */ public static String prefix = "node_";

     /**  * 是否该树只包含文件夹形式(whether this tree contains only folders)  */ public static boolean ONLY_FOLDER = false;

     /**  * 默认构造函数  */ private Tree() { }

     /**  * 获取一棵空树  *  * @return Tree  */ public static Tree getEmptyTree() {  return new Tree(); }

     /**  * 构造一个树形控件并初始化上面显示的文字和点击时触发的动作  *  * @param label  树形控件上显示的文字  * @param script 点击树形控件时触发的动作  */ public Tree(String label, String script) {  this.label = label;  this.script = script; }

     /**  * 构造一个树形控件并初始化上面显示的文字  *  * @param label 树形控件上显示的文字  */ public Tree(String label) {  this.label = label; }

     /**  * 判断当前节点是否是根节点  *  * @return boolean 如果是根节点,返回true  */ public boolean isRoot() {  return null == this.parent; }

     /**  * 获取树定义的变量名称  *  * @return 树定义的变量名称  */ private String variable() {  if (this.parent == null)   return "root";  return prefix + this.variableId; }

     /**  * 生成树形控件Tree 对应的javascript 代码  *  * @param tree 待生成js代码的树形控件对象  * @return js 返回生成 tree 代表的树形控件的js代码  */ 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(); }

     /**  * 获取生成该树形控件的js代码  *  * @return js 能生成该对象所代表的树形控件的js代码  */ public String getTree() {  return generateJS(this) + "/ndocument.write(root);"; }

     /**  * 生成树的根  *  * @param tree 树根节点  * @return root 生成树根的js  */ 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; }

     /**  * 生成一个树节点  *  * @param tree 树节点对象  * @return treeItem 树节点的js代码  */ 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; }

     /**  * 为当前节点添加子节点  *  * @param child 待添加的子节点  * @return child 被添加的子节点  */ 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; }

     /**  * 添加子节点  *  * @param label  子节点上显示的名称  * @param script 点击子节点时触发的动作  * @return Tree 被添加的子树节点  */ public Tree addChild(String label, String script) {  Tree son = new Tree(label, script);  return addChild(son); }

     /**  * 添加子节点  *  * @param label 子节点上显示的名称  * @return child 被添加的子树节点  */ public Tree addChild(String label) {  return addChild(label, null); }

     /**  * 添加当前树节点的兄弟节点  *  * @param younger 待添加的兄弟节点  * @return 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;

     }

     /**  * 为当前节点添加兄弟节点  *  * @param label  兄弟节点上显示的文字  * @param script 点击兄弟节点时触发的动作  * @return Tree 被添加的兄弟节点  */ public Tree addBrother(String label, String script) {  Tree younger = new Tree(label, script);  return addBrother(younger); }

     /**  * 为当前节点添加兄弟节点  *  * @param label 兄弟节点上显示的文字  * @return Tree 被添加的兄弟节点  */ public Tree addBrother(String label) {  return addBrother(label, null); }

     /**  * 返回当前节点的最终根节点  *  * @return root 当前节点的最终根节点  */ public Tree getRoot() {  Tree tree = this;  while (tree.parent != null) {   tree = tree.parent;  }  return tree; }

     /**  * 获取点击树形控件时触发的动作  *  * @return script 点击树形控件时触发的动作  */ public String getScript() {  return script; }

     /**  * 设置点击树形控件时触发的动作  *  * @param script 待设置的动作  */ public void setScript(String script) {  this.script = script; }

     /**  * 获取树形控件上显示的文字  *  * @return label 树形控件上显示的文字  */ public String getLabel() {  return label; }

     /**  * 设置树形控件上显示的文字  *  * @param label 待显示的文字  */ public void setLabel(String label) {  this.label = label; }

     public String toString() {  return this.label; }}


    最新回复(0)