Create Tree With Pending Item : Tree « GWT « Java






Create Tree With Pending Item

  

package com.java2s.gwt.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.TreeListener;

public class GWTClient implements EntryPoint,TreeListener {
  class PendingItem extends TreeItem {
    public PendingItem() {
      super("Please wait...");
    }
  }

  class TreeNode {
    public TreeNode[] children;
    public TreeItem item;
    public String text;

    public TreeNode(String text) {
      this.text = text;
    }

    public TreeNode(String text, TreeNode[] children) {
      this(text);
      this.children = children;
    }
  }

  TreeNode[] fTreeNode = new TreeNode[]{
    new TreeNode("A", new TreeNode[]{
      new TreeNode("B", new TreeNode[]{
        new TreeNode("1"), 
        new TreeNode("2"),
        new TreeNode("3"), 
        new TreeNode("4"),
        new TreeNode("5"),}),
      new TreeNode("B", new TreeNode[]{
        new TreeNode("B1"), 
        new TreeNode("B2"),
        new TreeNode("B3"),}),
      new TreeNode("C", new TreeNode[]{
        new TreeNode("C1"), 
        new TreeNode("C2"),}),
      new TreeNode("D", new TreeNode[]{
        new TreeNode("1"), 
        new TreeNode("2"),
        new TreeNode("3"),}),}),
    new TreeNode("AA", new TreeNode[]{
      new TreeNode("A", new TreeNode[]{
        new TreeNode("A1"), 
        new TreeNode("A2"),
        new TreeNode("A3"),
        new TreeNode("A4"),}),
      new TreeNode("B", new TreeNode[]{
        new TreeNode("1"),
        new TreeNode("2"),
        new TreeNode("3"),
        new TreeNode("4"),}),
      new TreeNode("C", new TreeNode[]{
        new TreeNode("F"),
        new TreeNode("E"),}),
      new TreeNode("G", new TreeNode[]{
        new TreeNode("C"), 
        new TreeNode("2"),
        new TreeNode("F"), 
        new TreeNode("E"),}),}),
    new TreeNode("M", new TreeNode[]{
        new TreeNode("C", new TreeNode[]{
      new TreeNode("P"), 
      new TreeNode("V"),}),}),};

  public void onModuleLoad() {
    Tree tree = new Tree();

    for (int i = 0; i < fTreeNode.length; ++i) {
      createItem(fTreeNode[i]);
      tree.addItem(fTreeNode[i].item);
    }

    tree.addTreeListener(this);
    tree.setWidth("20em");

    RootPanel.get().add(tree);
  }
  private void createItem(TreeNode TreeNode) {
    TreeNode.item = new TreeItem(TreeNode.text);
    TreeNode.item.setUserObject(TreeNode);
    if (TreeNode.children != null) {
      TreeNode.item.addItem(new PendingItem());
    }
  }
  public void onTreeItemSelected(TreeItem item) {
    Window.alert(((TreeNode) item.getUserObject()).text);
  }

  public void onTreeItemStateChanged(TreeItem item) {
    TreeItem child = item.getChild(0);
    if (child instanceof PendingItem) {
      item.removeItem(child);

      TreeNode tn = (TreeNode) item.getUserObject();
      for (int i = 0; i < tn.children.length; ++i) {
        createItem(tn.children[i]);
        item.addItem(tn.children[i].item);
      }
    }
  }

  
}



           
         
    
  








GWT-createTreeWithPendingItem.zip( 3 k)

Related examples in the same category

1.Use TreeListener
2.Tree node expanding and collapsing animation (Smart GWT)Tree node expanding and collapsing animation (Smart GWT)
3.Trees can show skinnable connector lines (Smart GWT)Trees can show skinnable connector lines (Smart GWT)
4.Tree with DataArrivedHandler (Smart GWT)Tree with DataArrivedHandler (Smart GWT)
5.Trees with multiple columns of data for each node. (Smart GWT)Trees with multiple columns of data for each node. (Smart GWT)
6.Sortable tree table (Smart GWT)Sortable tree table (Smart GWT)
7.Tree Editing Sample (Smart GWT)Tree Editing Sample (Smart GWT)
8.Parent Linking Tree Sample (Smart GWT)Parent Linking Tree Sample (Smart GWT)
9.Load tree data from a collection of local beans (Smart GWT)Load tree data from a collection of local beans (Smart GWT)
10.Tree data can be specified as a tree of nodes where each node lists its children (Smart GWT)Tree data can be specified as a tree of nodes where each node lists its children (Smart GWT)
11.Drag and drop to move parts and folders within and between the trees (Smart GWT)Drag and drop to move parts and folders within and between the trees (Smart GWT)
12.Tree Drag Reparent Sample (Smart GWT)Tree Drag Reparent Sample (Smart GWT)
13.Closed tree folders automatically open if you hover over them momentarily (Smart GWT)Closed tree folders automatically open if you hover over them momentarily (Smart GWT)
14.Tree view dropdown (Smart GWT)Tree view dropdown (Smart GWT)
15.Trees sort per folder. (Smart GWT)Trees sort per folder. (Smart GWT)
16.Load On Demand Tree Sample (Smart GWT)Load On Demand Tree Sample (Smart GWT)
17.Basic TreePanel Example (Ext GWT)Basic TreePanel Example (Ext GWT)
18.CheckBox Tree Node (Ext GWT)CheckBox Tree Node (Ext GWT)
19.Adding Context Menu to Tree (Ext GWT)Adding Context Menu to Tree (Ext GWT)
20.Fast tree (Ext GWT)Fast tree (Ext GWT)