Package net.mygwt.ui.client.util

Source Code of net.mygwt.ui.client.util.TreeBuilder

/*
* MyGWT Widget Library
* Copyright(c) 2007, MyGWT.
* licensing@mygwt.net
*
* http://mygwt.net/license
*/
package net.mygwt.ui.client.util;

import net.mygwt.ui.client.data.Model;
import net.mygwt.ui.client.widget.tree.Tree;
import net.mygwt.ui.client.widget.tree.TreeItem;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

/**
* Various ways of populating trees.
*
* @see Tree
*/
public class TreeBuilder {
 
  /**
   * Populates a tree from existing dom elements. The tree item text is taken
   * from the 'title' attribute of the element.
   *
   * @param tree the tree
   * @param root the root element
   */
  public static void buildTree(Tree tree, Element root) {
    process(tree.getRootItem(), root);
  }

  /**
   * Populates a tree from the given model.
   *
   * @param tree the tree
   * @param model the model
   */
  public static void buildTree(Tree tree, Model model) {
    TreeItem root = tree.getRootItem();
    for (int i = 0; i < model.getChildCount(); i++) {
      Model m = model.getChild(i);
      TreeItem item = new TreeItem();
      item.setText(m.toString());
      root.add(item);
      process(item, m);
    }
  }

  private static void process(TreeItem parentItem, Model model) {
    for (int i = 0; i < model.getChildCount(); i++) {
      Model m = model.getChild(i);
      TreeItem item = new TreeItem();
      item.setText(m.toString());
      parentItem.add(item);
      process(item, m);
    }
  }

  private static void process(TreeItem item, Element parent) {
    int size = DOM.getChildCount(parent);
    for (int i = 0; i < size; i++) {
      Element li = DOM.getChild(parent, i);
      TreeItem childItem = new TreeItem();
      String id = DOM.getElementAttribute(li, "id");
      if (id != null && !id.equals("")) {
        childItem.setId(id);
      }
      childItem.setText(DOM.getElementProperty(li, "title"));
      item.add(childItem);
      for (int j = 0; j < DOM.getChildCount(li); j++) {
        Element subList = DOM.getChild(li, j);
        process(childItem, subList);
      }
    }
  }

}
TOP

Related Classes of net.mygwt.ui.client.util.TreeBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.