开发者

gwt handling events in cell of cellTree

i want to handle double click event in a cell of a cellTree but i can't make it work. Any idea? Thanks.

if(((Node) value).getNodeType().equals(NodeTypes.Folder))
{
    ListDataProvider<Node> dataProvider = new List开发者_如何学CDataProvider<Node>(((Node) value).getChildNode());
    Cell<Node> cell = new AbstractCell<Node>("dblclick") 
    {
          @Override
          public void render(Context context, Node value, SafeHtmlBuilder sb) 
          {
            if (value != null) { sb.appendEscaped(value.getNodeName());    }
          }
          @Override
          public void onBrowserEvent(Context context, Element parent, Node value,
              NativeEvent event, ValueUpdater<Node> valueUpdater) 
            {
                  if (value == null) {   return;          }                         
                  super.onBrowserEvent(context, parent, value, event, valueUpdater);
                  if ("dblclick".equals(event))
                    {
                      ControllerPqm.getInstance().advise(ControllerPqm.ADVISE_ERROR, "Chick");
                    }
            }

    };
    return new DefaultNodeInfo<Node>(dataProvider, cell);
}


You have to extend some other Widget and sink the DoubleClick event in the constructor:

sinkEvents(Event.ONCLICK | Event.ONDBLCLICK); 

and then proceed to write code to handle the sinked events. Be aware that double click has issues in Linux-version of some browsers - it tends to fire the single click event multiple times as well in linux versions, and in some older mac/win versions.

public void onBrowserEvent(Event event) 
{ 
    switch (DOM.eventGetType(event)) 
    { 
    case Event.ONCLICK: 
        // your handler code here
      break; 

    case Event.ONDBLCLICK:   
        // your handler code here     
      break;
    }
} 

Ideally in your case, this code should go in the toplevel NodeTree class itself (or something that extends a Widget), on double click it will find the node that got double-clicked and do something based on it.


You must compare the event's type property:

"dblclick".equals(event.getType())


Try this :

public class MyMenuItem {
  private String name;
  public MyMenuItem(String name) {
    super();
    this.name = name;
    list = new ArrayList<MyMenuItem>();
  }
  //...
  public boolean hasChildrens() {
    return list.size()>0;
  }
}

public class MyTreeModel implements TreeViewModel {
  ArrayList<MyMenuItem> all;
  public MyTreeModel() {
    all = new ArrayList<MyMenuItem>();
    //Default items
    all.add(... //fill

  }
  @Override
  public <T> NodeInfo<?> getNodeInfo(T value) {
    ListDataProvider<MyMenuItem> dataProvider;
    if (value == null) { // root 
        dataProvider = new ListDataProvider<MyMenuItem>(all);
    } else {
        dataProvider = new ListDataProvider<MyMenuItem>(((MyMenuItem) value).getList());
    }
    Cell<MyMenuItem> cell = new MyCell(); //my Cell with dblclick event
    return new DefaultNodeInfo<MyMenuItem>(dataProvider, cell);
  }
  @Override
  public boolean isLeaf(Object value) {
    if (value instanceof MyMenuItem) {
        MyMenuItem t = (MyMenuItem) value;
        if (!t.hasChildrens())
            return true;
        return false;
    }
    return false; 
  }
}

Also Cell class:

   private static class MyCell extends AbstractCell<MyMenuItem> {
      public MyCell() {
        super("keydown","dblclick"); //click?
      }

      @Override
      public void onBrowserEvent(Context context, Element parent, MyMenuItem value,
          NativeEvent event, ValueUpdater<MyMenuItem> valueUpdater) {
        // Check that the value is not null.
        if (value == null) {
          return;
        }
        super.onBrowserEvent(context, parent, value, event, valueUpdater);
        if ("dblclick".equals(event.getType())) {
            this.onEnterKeyDown(context, parent, value, event, valueUpdater);
        }
      }
      @Override
      public void render(Context context, MyMenuItem value, SafeHtmlBuilder sb) {
          if (value == null) {
            return;
          }
          sb.appendEscaped(value.getName());
      }

      @Override
      protected void onEnterKeyDown(Context context, Element parent,
           MyMenuItem value, NativeEvent event, ValueUpdater<MyMenuItem> valueUpdater) {
            Window.alert("You clicked "+event.getType()+" " + value.getName());
      }
    }
`

In the module:

 treeModel = new MyTreeModel();
 CellTree tree = new CellTree(treeModel,null);
 tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
 TreeNode rootNode = tree.getRootTreeNode();
 TreeNode firstPlaylist = rootNode.setChildOpen(0, true);
 //...
 verticalPanelWest.add(tree);


If you wand to consume a browserevent in your Cell you need to tell what are the events to be consumed. Shows example

public class OrderCell extends AbstractCell<Order>  {

    public  OrderCell() {
        super("mousedown","mouseover","mouseup","mousemove");
    }

    @Override
            public void onBrowserEvent(com.google.gwt.cell.client.Cell.Context context,
                    Element parent, Order value, NativeEvent event,
                    ValueUpdater<Order> valueUpdater) {
                System.out.print("event fired");

            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜