父窗口小部件中的事件处理,用于从CellTable中的AbstractCell“单击”

时间:2011-10-28 03:04:19

标签: gwt

我是一个GWT菜鸟,我很欣赏在这个方向上正确的推动。

使用GWT 2.4

我无法弄清楚如何在我的EntryPoint类中监听一个添加了CellTable的事件。 CellTable有一个自定义的AbstractCell,它使用超链接呈现String。 Cell处理点击事件。

我的EntryPoint类如何处理事件以便它可以执行某些操作(比如隐藏CellTable并显示其他一些小部件)?

我能够成功处理Cell和CellTable中的事件,但无法弄清楚如何获取父窗口小部件(EntryPoint类来处理它)。

EntryPoint类:

public class Tags_GWT implements EntryPoint {

private final TagGwtServiceAsync tagService = GWT.create(TagGwtService.class);
final TagCellTable tagCellTable = new TagCellTable();

public void onModuleLoad() {

    RootPanel.get("tagCellTable").add(tagCellTable);

    // load the table with data
    tagService.getTagsAsList(new AsyncCallback<List<TagDto>>() {
        public void onFailure(Throwable caught) {
            GWT.log("Error loading data");
        }
        public void onSuccess(List<TagDto> tags) {
            tagCellTable.setInput(tags);
        }
    });

CellTable类:

public class TagCellTable extends CellTable<TagDto>{

code removed for simplicity

// Add a field updater to be notified when the user enters a new name.
nameColumn.setFieldUpdater(new FieldUpdater<TagDto, String>() {
  @Override
  public void update(int index, TagDto object, String value) {
    // Inform the user of the change.
    Window.alert("TagCellTable.update is executing for: '" + value + "'");
  }
});

AbstractCell类:

public class LinkCell extends AbstractCell<String>{

code omitted for simplicity

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

  super.onBrowserEvent(context, parent, value, event, valueUpdater);

  if ("click".equals(event.getType())) {
    // Ignore clicks that occur outside of the outermost element.
    EventTarget eventTarget = event.getEventTarget();
    if (parent.getFirstChildElement().isOrHasChild(Element.as(eventTarget))) {
      doAction(value, valueUpdater);
    }
  }
}


@Override
protected void onEnterKeyDown(Context context, Element parent, String value, NativeEvent event, ValueUpdater<String> valueUpdater) {
  doAction(value, valueUpdater);
}


private void doAction(String value, ValueUpdater<String> valueUpdater) {

  Window.alert("LinkCell.doAction is executing for: ' " + value + "'");

  if (valueUpdater != null) {
      valueUpdater.update(value);
  }
}

1 个答案:

答案 0 :(得分:1)

我认为您必须向LinkCell提供EventBus对象的引用。然后,在doAction中,您可以在Event上触发一些EventBus。您的入口点模块可以在相同的EventBus上收听相关事件,并从那里做出响应。

没有一种好方法可以从已经内置的外部代码中挂入单元格,因为CellTable实际上并不是由小部件组成的。永远不会激活正常的窗口小部件事件层次结构,因此无需添加任何侦听器 - 您必须创建自己的EventBus。

相关问题