Justin
Justin

Reputation: 6251

event handling in parent widget for "click" from AbstractCell in CellTable

I'm a bit of a GWT noob and I'd appreciate a nudge in the right direction on this one.

Using GWT 2.4

I'm having trouble figuring out how to listen for an event in my EntryPoint class which adds a CellTable. The CellTable has a custom AbstractCell which renders a String with a hyperlink. The Cell handles a click event.

How can my EntryPoint class handle the event so that it can take some action (like hide the CellTable and show some other widgets)?

I'm able to successfully work with the event in the Cell and CellTable but can't figure out how to get the parent widget (the EntryPoint class to handle it).

The EntryPoint class:

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);
        }
    });

The CellTable class:

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 + "'");
  }
});

The AbstractCell class:

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);
  }
}

Upvotes: 1

Views: 2347

Answers (1)

Riley Lark
Riley Lark

Reputation: 20890

I think you'll have to give your LinkCell a reference to an EventBus object. Then, in doAction, you can fire some Event on the EventBus. Your entrypoint module can be listening on the same EventBus for the relevant events, and respond from there.

There's not a good way to hook into the cell from the outer code that's already built in because the CellTable is not really made out of widgets. The normal widget event hierarchy is never activated, so there's nothing to add a listener to - you have to create your own EventBus.

Upvotes: 1

Related Questions