我正在尝试使用Google Web工具包(v2.5)TreeViewModel创建一个单元格树,每个单元格有一个可点击链接。我正确地显示了树,但是在单击单元格时我无法弄清楚如何处理事件。我的类NeoTreeModel实现了TreeViewModel,但是当我在getNodeInfo中创建一个AbstractCell并尝试覆盖onBrowserEvent时,我得到编译错误“方法不会覆盖或实现超类型的方法”。我并不致力于这种方法。我很满意任何解决方案,您可以点击按钮或链接树,并能够处理事件。我一直在努力完成我认为容易的事情。
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;
}
}
答案 0 :(得分:0)
问题在于您的代码。你正在使用的onBrowserEvent方法的原型和AbstractCell中的方法的原型是不同的。投机型shuold是 -
public void onBrowserEvent(Cell.Context context, Element parent, NeoTreeItem value, NativeEvent event, ValueUpdater<NeoTreeItem> valueUpdater )
{
}
差异是ValueUpdater&lt; NeoTreeItem&gt;。在您的代码中,它是ValueUpdater&lt;字符串&gt;
答案 1 :(得分:0)
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);