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

 /**
 * 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;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值