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