Results 1 to 2 of 2

Thread: Adding Context Menu to Tree example in java

  1. #1
    Senior Member
    Join Date
    Oct 2012
    Posts
    171
    Blog Entries
    2

    Adding Context Menu to Tree example in java

    Code:
    public class Demo implements EntryPoint {
      public void onModuleLoad() {
        RootPanel.get().add(new ContextMenuTreePanelExample());
      }
    }
    class ContextMenuTreePanelExample extends LayoutContainer {
      private int count = 1;
     
      protected void onRender(Element parent, int pos) {
        super.onRender(parent, pos);
    
        final myRoot model = getTreeModel();
    
        final TreeStore<ModelData> store = new TreeStore<ModelData>();
        store.add(model.getChildren(), true);
    
        add(new Button("Reset", new SelectionListener<ButtonEvent>() {
          public void componentSelected(ButtonEvent ce) {
            store.removeAll();
            store.add(model.getChildren(), true);
          }
        }),new FlowData(10));
    
        final TreePanel<ModelData> tree = new TreePanel<ModelData>(store);
        tree.setDisplayProperty("name"); 
        tree.setWidth(250);
    
        Menu contextMenu = new Menu();
    
        MenuItem insert = new MenuItem();
        insert.setText("Insert Item"); 
        insert.addSelectionListener(new SelectionListener<MenuEvent>() {
          public void componentSelected(MenuEvent ce) {
            ModelData folder = tree.getSelectionModel().getSelectedItem();
            if (folder != null) {
              myRoot child = new myRoot("Add Child " + count++);
              store.add(folder, child, false);
              tree.setExpanded(folder, true);
            }
          }
        });
        contextMenu.add(insert);
    
        MenuItem remove = new MenuItem();
        remove.setText("Remove Selected");
        //remove.setIcon(Resources.ICONS.delete());
        remove.addSelectionListener(new SelectionListener<MenuEvent>() {
          public void componentSelected(MenuEvent ce) {
            List<ModelData> selected = tree.getSelectionModel().getSelectedItems();
            for (ModelData sel : selected) {
              store.remove(sel);
            }
          }
        });
        contextMenu.add(remove);
    
        tree.setContextMenu(contextMenu);
    
        setLayout(new FlowLayout(8));
        add(tree);
    
      }
      public static myRoot getTreeModel() {
        myRoot[] folders = new myRoot[] {
            new myRoot("Example123",
                new myRoot[] {
    
                    new myRoot("Root123",
                        new Music[] {
                            new Music("Six String Root123", "Example123", "Root123"),
                            new Music("Three String Root123", "Example123", "Root123"),
                            new Music("Grosse Fugue for String Root123", "Example123",
                                "Root123"),}),
                    new myRoot("Example1 ", new Music[] {
                        new Music("Sonata in A Minor", "Example123", "Example1 "),
                        new Music("Sonata in F Major", "Example123", "Example1 "),}),
    
                    new myRoot("Example1234", new Music[] {
                        new Music("No. 1 - C", "Example123", "Example1234"),
                        new Music("No. 2 - B-Flat Major", "Example123", "Example1234"),
                        new Music("No. 3 - C Minor", "Example123", "Example1234"),
                        new Music("No. 4 - G Major", "Example123", "Example1234"),
                        new Music("No. 5 - E-Flat Major", "Example123", "Example1234"),}),
    
                    new myRoot("Example", new Music[] {
                        new Music("No. 1 - C Major", "Example123", "Example"),
                        new Music("No. 2 - D Major", "Example123", "Example"),
                        new Music("No. 3 - E-Flat Major", "Example123", "Example"),
                        new Music("No. 4 - B-Flat Major", "Example123", "Example"),
                        new Music("No. 5 - C Minor", "Example123", "Example"),
                        new Music("No. 6 - F Major", "Example123", "Example"),
                        new Music("No. 7 - A Major", "Example123", "Example"),
                        new Music("No. 8 - F Major", "Example123", "Example"),
                        new Music("No. 9 - D Minor", "Example123", "Example"),}),}),
            new myRoot("Root",
                new myRoot[] {
                    new myRoot("Example1234", new Music[] {
                        new Music("Violin Concerto", "Root", "Example1234"),
                        new Music("Double Concerto - A Minor", "Root", "Example1234"),
                        new Music("Piano Concerto No. 1 - D Minor", "Root", "Example1234"),
                        new Music("Piano Concerto No. 2 - B-Flat Major", "Root",
                            "Example1234"),}),
                    new myRoot("Root123",
                        new Music[] {
                            new Music("Piano Quartet No. 1 - G Minor", "Root", "Root123"),
                            new Music("Piano Quartet No. 2 - A Major", "Root", "Root123"),
                            new Music("Piano Quartet No. 3 - C Minor", "Root", "Root123"),
                            new Music("String Quartet No. 3 - B-Flat Minor", "Root",
                                "Root123"),}),
                    new myRoot("Example1 ", new Music[] {
                        new Music("Two Example1  for Clarinet - F Minor", "Root", "Example1 "),
                        new Music("Two Example1  for Clarinet - E-Flat Major", "Root",
                            "Example1 "),}),
                    new myRoot("Example", new Music[] {
                        new Music("No. 1 - C Minor", "Root", "Example"),
                        new Music("No. 2 - D Minor", "Root", "Example"),
                        new Music("No. 3 - F Major", "Root", "Example"),
                        new Music("No. 4 - E Minor", "Root", "Example"),}),}),
            new myRoot("Mozart", new myRoot[] {new myRoot("Example1234", new Music[] {
                new Music("Piano Concerto No. 12", "Mozart", "Example1234"),
                new Music("Piano Concerto No. 17", "Mozart", "Example1234"),
                new Music("Clarinet Concerto", "Mozart", "Example1234"),
                new Music("Violin Concerto No. 5", "Mozart", "Example1234"),
                new Music("Violin Concerto No. 4", "Mozart", "Example1234"),}),}),};
    
        myRoot root = new myRoot("root");
        for (int i = 0; i < folders.length; i++) {
          root.add((myRoot) 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 myRoot extends BaseTreeModel implements Serializable {
    
      private static int ID = 0;
      
      public myRoot() {
        set("id", ID++);
      }
    
      public myRoot(String name) {
        set("id", ID++);
        set("name", name);
      }
    
      public myRoot(String name, BaseTreeModel[] children) {
        this(name);
        for (int i = 0; i < children.length; i++) {
          add(children[i]);
        }
      }
    
      public Integer getId() {
        return (Integer) get("id");
      }
    
      public String getName() {
        return (String) get("name");
      }
    
      public String toString() {
        return getName();
      }

  2. #2
    Senior Member
    Join Date
    Oct 2012
    Posts
    171
    Blog Entries
    2
    Required includes for the above class
    Code:
    package com.google.gwt.sample.hello.client;
    
    import java.io.Serializable;
    import java.util.List;
    
    import com.extjs.gxt.ui.client.data.BaseTreeModel;
    import com.extjs.gxt.ui.client.data.ModelData;
    import com.extjs.gxt.ui.client.event.ButtonEvent;
    import com.extjs.gxt.ui.client.event.MenuEvent;
    import com.extjs.gxt.ui.client.event.SelectionListener;
    import com.extjs.gxt.ui.client.store.TreeStore;
    import com.extjs.gxt.ui.client.widget.LayoutContainer;
    import com.extjs.gxt.ui.client.widget.button.Button;
    import com.extjs.gxt.ui.client.widget.layout.FlowData;
    import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
    import com.extjs.gxt.ui.client.widget.menu.Menu;
    import com.extjs.gxt.ui.client.widget.menu.MenuItem;
    import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
    import com.google.gwt.core.client.EntryPoint;
    import com.google.gwt.user.client.Element;
    import com.google.gwt.user.client.ui.RootPanel;

Similar Threads

  1. Fast tree example in java
    By Sanvi in forum Java programming questions and examples
    Replies: 0
    Last Post: 03-16-2013, 09:38 AM
  2. Example program for sorting the tree(Tree Sort)
    By sarvani in forum C, CPP Programs
    Replies: 0
    Last Post: 03-06-2013, 05:52 AM
  3. Replies: 0
    Last Post: 12-22-2012, 01:05 PM
  4. Use of context menu in Selenium IDE
    By Cora in forum Selenium
    Replies: 1
    Last Post: 10-31-2012, 09:49 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •