1
votes

I am trying to use Google web toolkit (v2.5) TreeViewModel to create a tree of cells with one clickable link per cell. I have the tree displaying properly, but I have been unable to figure out how to handle events when clicking on a cell. My class NeoTreeModel implements TreeViewModel, but when I create an AbstractCell in getNodeInfo and try to override onBrowserEvent, I get the compile error "method does not override or implement a method from a supertype". I am not committed to this approach. I would be happy with ANY solution where you can click on a tree of buttons or links and be able to handle the event. I have been trying several hours to accomplish what I thought would be something easy.

public class NeoTreeModel implements TreeViewModel {
  private List<NeoTreeItem>  _items    = null;
  private Logger             _logger   = Logger.getLogger("NeoTreeModel ");

  public NeoTreeModel(List<NeoTreeItem> items) { 
   _items = items;
  }

  public <T> NodeInfo<?> getNodeInfo(T value) {
    List<NeoTreeItem> list = new ArrayList<NeoTreeItem>();

    if (value == null) {
      for (NeoTreeItem nti: _items) if (nti.getLevel() == 0) list.add(nti);
    } else if (value instanceof NeoTreeItem) {
      list = ((NeoTreeItem)value).getChildren();
    } else {
      _logger.log(Level.SEVERE, "Got request for unknown type: " + value);
    }

    ListDataProvider<NeoTreeItem> provider = 
      new ListDataProvider<NeoTreeItem>(list);

    Cell<NeoTreeItem> cell = new AbstractCell<NeoTreeItem>() {

      public void render(Cell.Context context, NeoTreeItem value, 
                 SafeHtmlBuilder sb) {
        if (value != null) {
          sb.appendHtmlConstant("<a href='javascript:;'>");
          sb.appendEscaped(value.getLongName());
          sb.appendHtmlConstant("</a>");
        }       
      }

      @Override
      public void onBrowserEvent(Cell.Context context, Element parent, 
                     NeoTreeItem value, NativeEvent event, 
                     ValueUpdater<NeoTreeItem> valueUpdater) {

          _logger.log(Level.INFO, "HOWDY2");
        }
      }
    };
    return new DefaultNodeInfo<NeoTreeItem>(provider, cell);
  }
  public boolean isLeaf(Object value) {
    boolean result = false;

    if (value instanceof NeoTreeItem) {
      result = ((NeoTreeItem) value).isLeaf();      
    }

    return result;
  }

}
2

2 Answers

0
votes

The problem is with your code. The prototype of the method onBrowserEvent you are using and the one in AbstractCell are different. The proptotype shuold be -

public void onBrowserEvent(Cell.Context context, Element parent, NeoTreeItem value, NativeEvent event, ValueUpdater<NeoTreeItem> valueUpdater )
{
}

Difference is ValueUpdater< NeoTreeItem >. In your code it is ValueUpdater< String >

0
votes

OK, I think I figured it out. Everything I read about overriding onBrowserEvent had to do with cells in a CellTable, not CellTree. Apparently you need to use the selection model to detect when a CellTree cell is selected. Like this:

SingleSelectionModel<NeoTreeItem> _selectionModel = new SingleSelectionModel<NeoTreeItem>();
_selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
    @Override
    public void onSelectionChange(SelectionChangeEvent event) {
      Object object = _selectionModel.getSelectedObject();
      if (object instanceof NeoTreeItem) {
    Window.alert("HOWDY1: " + object.toString());
      }
    }});

   Cell<NeoTreeItem> cell = new AbstractCell<NeoTreeItem>() {

      public void render(Cell.Context context, NeoTreeItem value, 
             SafeHtmlBuilder sb) {
    if (value != null) {
           // blah
      }
    };

    return new DefaultNodeInfo<NeoTreeItem>(provider, cell,
                        _selectionModel, null);