樹形控制項工具類,實現Tree對象和WebFXTree/WebFXTreeItem的轉換

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.