tags:

views:

65

answers:

2

We're using gwt-presenter, but not really a question specific to that...

I've got a table with users in it. As I build the table in the view (from the data provided by the presenter), I need to add two action buttons ("Edit", and "Delete") at the end of the row.

What's the best way to assign click handlers to these buttons so the presenter knows which was clicked? Previous to this, we could pass a private field from the view to the presenter and attach a discrete click handler to that button. However, this method is rather rigid and doesn't work in this scenario very well.

Thanks in advance.

+2  A: 

How about having the view allowing the subscription for edit/delete click events, registering internally the individual row click events, and then delegating the event handling to the ones registered by the view?

I mean something like the following pesudo code:

View:

addRowEditClickHandler(ClickHandler handler) {
    this.rowEditClickHandler = handler; 
}
addRowDeleteClickHandler(ClickHandler handler) {
    this.rowDeleteClickHandler = handler; 
}

//... somewhere when setting up of the grid...

rowEditButton.addClickHandler = new ClickHandler() {
    onClick(args) {
        this.rowEditClickHandler.onClick(args)

}

rowDeleteButton.addClickHandler = new ClickHandler() {
    onClick(args) {
        this.rowDeleteClickHandler.onClick(args)

}

Presenter:

View view = new View();
view.addRowEditClickHandler( new ClickHandler() {
    onClick(args) {
        doSomething();
    }
});
view.addRowDeleteClickHandler( new ClickHandler() {
    onClick(args) {
        doSomething();
    }
});
Amitay Dobo
Works wonderfully, thanks!
jmccartie
A: 

Hi

Trying Amitay's solution with my tree component. But it doesnt work. getting exceptions. This is my code:

View:

public class MyTree extends Composite implements TreePresenter.Display {

@UiField
Tree tree = new Tree();

SelectionHandler<TreeItem> treeSelectionHandler;

public MyTree() {

    initWidget(tree);

    TreeItem rootNode = new TreeItem("Root");
    rootNode.addItem("contacts");
    rootNode.addItem("edit");
    rootNode.addItem("item2");
    rootNode.addItem("item3");
    rootNode.addItem("item4");
    rootNode.addItem("item5");

    tree.addItem(rootNode);

 tree.addSelectionHandler(new SelectionHandler<TreeItem>() {
        public void onSelection(SelectionEvent<TreeItem> event) {
            treeSelectionHandler.onSelection(event);
        }});

}

@Override
public void setSelectionHandler(SelectionHandler<TreeItem> handler) { 
    this.treeSelectionHandler = handler;  
} 



@Override
public Widget asWidget() {
    return this;
}

@Override
public Tree getTree() {     
    return tree;
}

}

and then in the presenter:

public class TreePresenter implements Presenter{

 public interface Display {

   Tree  getTree();
   Widget asWidget();
   void setSelectionHandler(SelectionHandler<TreeItem> handler);
  }

private final ContactsServiceAsync rpcService; private final HandlerManager eventBus; private final Display display;

public TreePresenter(ContactsServiceAsync rpcService, HandlerManager eventBus, Display view) { this.rpcService = rpcService; this.eventBus = eventBus; this.display = view;

}

public void bind() {

  SelectionHandler<TreeItem> mySelectionHandler = new SelectionHandler<TreeItem>() {
        public void onSelection(SelectionEvent<TreeItem> event) {

             TreeItem item = (TreeItem) event.getSelectedItem();
             GWT.log("TReePresenter item " +item.getText());
            }
    };


  display.setSelectionHandler(mySelectionHandler);

}

public void go(final HasWidgets container) { bind(); container.clear(); container.add(display.asWidget());

}

}

Getting : Caused by: java.lang.NullPointerException: null at com.google.gwt.sample.contacts.client.view.MyTree$1.onSelection(MyTree.java:49)

thanks

Andreas Blomqvist
Please post it as an question if it's still valid. Not sure why the event is triggered before the event handler binding, but there's a simpler solution to your problem. Since you don't need an event handler to each TreeItem, why not just allow the presenter to add a single handler for the tree, by adding:HasSelectionHandlers<TreeItem> getTreeItemSelectionProvider();to the Display interface, and just returning the tree field in the implementation?
Amitay Dobo