2011-04-14 68 views
1

我想處理cellTree單元格中的雙擊事件,但我無法使其工作。 有什麼想法? 謝謝。gwt處理單元格cellTree中的事件

if(((Node) value).getNodeType().equals(NodeTypes.Folder)) 
{ 
    ListDataProvider<Node> dataProvider = new ListDataProvider<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); 
} 
+0

這不是寫我的代碼給我的網站! – Zasz 2011-04-14 13:38:52

回答

0

您必須比較該事件的類型屬性:

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

你要延長一些其他的Widget,並在構造沉DoubleClick事件:

sinkEvents(Event.ONCLICK | Event.ONDBLCLICK); 

,然後進行編寫代碼來處理沉沒的事件。請注意,在某些瀏覽器的Linux版本中雙擊會出現問題 - 它往往會在Linux版本以及一些較舊的mac/win版本中多次觸發單擊事件。

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; 
    } 
} 
你的情況

理想的情況下,該代碼在頂層NodeTree類本身(或擴展Widget的東西)應該去上雙擊它,會發現有雙擊並做基於它的東西的節點。

0

試試這個:

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; 
    } 
} 

而且電池類:

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()); 
     } 
    } 
` 

在模塊:

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); 
0

如果你的魔杖消耗你的手機一browserevent你需要告訴什麼是要消耗的事件。 顯示示例

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"); 

      }