开发者

How to override/remove default handler in GWT widget?

I would like to change a behavior of SuggestBox in GWT. I want submit value by pressing ENTER. However the SuggestBox have a KeyHandler, which catches any enter key and sets a new Selection. I am looking for a solution to remove/overide this Handler.

This method is called by constructor in SuggestBox

private void addEventsToTextBox() {
class TextBoxEvents extends HandlesAllKeyEvents implements
    ValueChangeHandler<String> {

  public void onKeyDown(KeyDownEvent event) {
    switch (event.getNativeKeyCode()) {
      case KeyCodes.KEY_DOWN:
        display.moveSelectionDown();
        break;
      case KeyCodes.KEY_UP:
        display.moveSelectionUp();
        break;
      case KeyCodes.KEY_ENTER:
      case KeyCodes.KEY_TAB:
        Suggestion suggestion = display.getCurrentSelection();
        if (suggestion == null) {
          display.hideSuggestions();
        } else {
          setNewSelection(suggestion);
        }
        break;
    }
    delegateEvent(SuggestBox.this, event);
  }

  public void onKeyPress(KeyPressEvent event) {
    delegateEvent(SuggestBox.this, event);
  }

  public void onKeyUp(Key开发者_高级运维UpEvent event) {
    // After every user key input, refresh the popup's suggestions.
    refreshSuggestions();
    delegateEvent(SuggestBox.this, event);
  }

  public void onValueChange(ValueChangeEvent<String> event) {
    delegateEvent(SuggestBox.this, event);
  }
}

TextBoxEvents events = new TextBoxEvents();
events.addKeyHandlersTo(box);
box.addValueChangeHandler(events);

}

Is it possible to remove this Handler from box by using HandlerRegistration? Is there any nice solution by extending class and overriding private method?


Unfortunately as the method is private, unless you copy the source directly and change the method that way you won't be able to override the method. I think your best option is to create an instance of SuggestBox and passing it a custom implementation of TextBoxBase which you can then override event/handler behaviours. Notice that the addEventsToTextBox() hooks up that inner class to the box member variable that is set in the constructor prior to the call to addEventsToTextBox().

Something similar to the following:

public class MyTextBox extends TextBox{
...
// Override the handlers/events
...
}

and then use it with your instance of a SuggestBox:

MyTextBox myBox = new MyTextBox(...);
SuggestBox sBox = new SuggestBox(oracle, myBox);

Depending on your specific situation this might not be an ideal solution in which case it would be helpful to see some code snippets showing your use of it.

EDIT:

I should mention that you probably actually want to extend a subclass of TextBoxBase such as TextBox or TextArea and not TextBoxBase directly so you don't have to re-implement the basic text input functionality of those widgets.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜