Drag and drop between TreeGrid (Ext GWT) : Table Drag Drop « GWT « Java

Drag and drop between TreeGrid (Ext GWT)

Drag and drop between TreeGrid (Ext GWT)
 * Ext GWT - Ext for GWT
 * Copyright(c) 2007-2009, Ext JS, LLC.
 * licensing@extjs.com
 * http://extjs.com/license

package com.google.gwt.sample.hello.client;

import java.io.Serializable;
import java.util.Arrays;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BaseTreeModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.dnd.TreeGridDragSource;
import com.extjs.gxt.ui.client.dnd.TreeGridDropTarget;
import com.extjs.gxt.ui.client.dnd.DND.Feedback;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayoutData;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout.VBoxLayoutAlign;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGrid;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGridCellRenderer;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.RootPanel;

public class Hello implements EntryPoint {
  public void onModuleLoad() {
    RootPanel.get().add(new TreeGridToTreeGridExample());
class TreeGridToTreeGridExample extends LayoutContainer {

  private TreeGridDropTarget target;
  protected void onRender(Element parent, int index) {
    super.onRender(parent, index);

    VerticalPanel vp = new VerticalPanel();

    Folder model = getTreeModel();

    TreeStore<ModelData> store = new TreeStore<ModelData>();
    store.add(model.getChildren(), true);

    ColumnConfig name = new ColumnConfig("name", "Name", 100);
    name.setRenderer(new TreeGridCellRenderer<ModelData>());

    ColumnConfig date = new ColumnConfig("author", "Author", 100);
    ColumnConfig size = new ColumnConfig("genre", "Genre", 100);
    ColumnModel cm = new ColumnModel(Arrays.asList(name, date, size));

    ContentPanel cp = new ContentPanel();
    cp.setHeading("TreeGrid to TreeGrid Example");
    VBoxLayout layout = new VBoxLayout();
    cp.setSize(500, 500);
    ToolBar toolBar = new ToolBar();
    toolBar.add(new LabelToolItem("Feedback: "));
    final SimpleComboBox<String> type = new SimpleComboBox<String>();
    type.addListener(Events.Change, new Listener<FieldEvent>() {
      public void handleEvent(FieldEvent be) {
        boolean append = type.getSimpleValue().equals("Append");
        Feedback f = append ? Feedback.APPEND : Feedback.INSERT;
    toolBar.add(new SeparatorToolItem());

    TreeGrid<ModelData> tree = new TreeGrid<ModelData>(store, cm);

    new TreeGridDragSource(tree);

    VBoxLayoutData data = new VBoxLayoutData(6, 0, 6, 0);
    cp.add(tree, data);

    Folder f = new Folder("My Music");
    TreeStore<ModelData> dropStore = new TreeStore<ModelData>();
    dropStore.add(f, false);


    tree = new TreeGrid<ModelData>(dropStore, cm) {
      protected boolean hasChildren(ModelData model) {
        if ("My Music".equals(model.get("name")) || model instanceof Folder) {
          return true;
        return super.hasChildren(model);

    target = new TreeGridDropTarget(tree);
    data = new VBoxLayoutData();
    cp.add(tree, data);



  public static Folder getTreeModel() {
    Folder[] folders = new Folder[] {
        new Folder("Beethoven",
            new Folder[] {

                new Folder("Quartets",
                    new Music[] {
                        new Music("Six String Quartets", "Beethoven", "Quartets"),
                        new Music("Three String Quartets", "Beethoven", "Quartets"),
                        new Music("Grosse Fugue for String Quartets", "Beethoven",
                new Folder("Sonatas", new Music[] {
                    new Music("Sonata in A Minor", "Beethoven", "Sonatas"),
                    new Music("Sonata in F Major", "Beethoven", "Sonatas"),}),

                new Folder("Concertos", new Music[] {
                    new Music("No. 1 - C", "Beethoven", "Concertos"),
                    new Music("No. 2 - B-Flat Major", "Beethoven", "Concertos"),
                    new Music("No. 3 - C Minor", "Beethoven", "Concertos"),
                    new Music("No. 4 - G Major", "Beethoven", "Concertos"),
                    new Music("No. 5 - E-Flat Major", "Beethoven", "Concertos"),}),

                new Folder("Symphonies", new Music[] {
                    new Music("No. 1 - C Major", "Beethoven", "Symphonies"),
                    new Music("No. 2 - D Major", "Beethoven", "Symphonies"),
                    new Music("No. 3 - E-Flat Major", "Beethoven", "Symphonies"),
                    new Music("No. 4 - B-Flat Major", "Beethoven", "Symphonies"),
                    new Music("No. 5 - C Minor", "Beethoven", "Symphonies"),
                    new Music("No. 6 - F Major", "Beethoven", "Symphonies"),
                    new Music("No. 7 - A Major", "Beethoven", "Symphonies"),
                    new Music("No. 8 - F Major", "Beethoven", "Symphonies"),
                    new Music("No. 9 - D Minor", "Beethoven", "Symphonies"),}),}),
        new Folder("Brahms",
            new Folder[] {
                new Folder("Concertos", new Music[] {
                    new Music("Violin Concerto", "Brahms", "Concertos"),
                    new Music("Double Concerto - A Minor", "Brahms", "Concertos"),
                    new Music("Piano Concerto No. 1 - D Minor", "Brahms", "Concertos"),
                    new Music("Piano Concerto No. 2 - B-Flat Major", "Brahms",
                new Folder("Quartets",
                    new Music[] {
                        new Music("Piano Quartet No. 1 - G Minor", "Brahms", "Quartets"),
                        new Music("Piano Quartet No. 2 - A Major", "Brahms", "Quartets"),
                        new Music("Piano Quartet No. 3 - C Minor", "Brahms", "Quartets"),
                        new Music("String Quartet No. 3 - B-Flat Minor", "Brahms",
                new Folder("Sonatas", new Music[] {
                    new Music("Two Sonatas for Clarinet - F Minor", "Brahms", "Sonatas"),
                    new Music("Two Sonatas for Clarinet - E-Flat Major", "Brahms",
                new Folder("Symphonies", new Music[] {
                    new Music("No. 1 - C Minor", "Brahms", "Symphonies"),
                    new Music("No. 2 - D Minor", "Brahms", "Symphonies"),
                    new Music("No. 3 - F Major", "Brahms", "Symphonies"),
                    new Music("No. 4 - E Minor", "Brahms", "Symphonies"),}),}),
        new Folder("Mozart", new Folder[] {new Folder("Concertos", new Music[] {
            new Music("Piano Concerto No. 12", "Mozart", "Concertos"),
            new Music("Piano Concerto No. 17", "Mozart", "Concertos"),
            new Music("Clarinet Concerto", "Mozart", "Concertos"),
            new Music("Violin Concerto No. 5", "Mozart", "Concertos"),
            new Music("Violin Concerto No. 4", "Mozart", "Concertos"),}),}),};

    Folder root = new Folder("root");
    for (int i = 0; i < folders.length; i++) {
      root.add((Folder) folders[i]);

    return root;
class Music extends BaseTreeModel {

  public Music() {


  public Music(String name) {
    set("name", name);

  public Music(String name, String author, String genre) {
    set("name", name);
    set("author", author);
    set("genre", genre);

  public String getName() {
    return (String) get("name");

  public String getAuthor() {
    return (String) get("author");

  public String getGenre() {
    return (String) get("genre");

  public String toString() {
    return getName();

class Folder extends BaseTreeModel implements Serializable {

  private static int ID = 0;
  public Folder() {
    set("id", ID++);

  public Folder(String name) {
    set("id", ID++);
    set("name", name);

  public Folder(String name, BaseTreeModel[] children) {
    for (int i = 0; i < children.length; i++) {

  public Integer getId() {
    return (Integer) get("id");

  public String getName() {
    return (String) get("name");

  public String toString() {
    return getName();



Ext-GWT.zip( 4,297 k)

Related examples in the same category

1.Drag and drop between table (Smart GWT)Drag and drop between table (Smart GWT)
2.Drag and copy between tables (Smart GWT)Drag and copy between tables (Smart GWT)
3.Drag and move between tables (Smart GWT)Drag and move between tables (Smart GWT)
4.Drop and drop to a sorted ListView (Ext GWT)Drop and drop to a sorted ListView (Ext GWT)
5.Drop and drop to insert to a ListView (Ext GWT)Drop and drop to insert to a ListView (Ext GWT)
6.Drag and drop between ListView, Grid,TreeGrid (Ext GWT)Drag and drop between ListView, Grid,TreeGrid (Ext GWT)
7.Drag and drop between grids (Ext GWT)Drag and drop between grids (Ext GWT)