Search code examples
gwtmvpclasscastexceptiongwt-mvp

Using Something Other Than HasClickHandlers in my GWT MVP


I am facing a similar issue to the problem faced by the similar StackOverflow question Way to specify multiple interfaces in Java but as it specifically applies to the GWT MVP example framework presented in http://code.google.com/webtoolkit/articles/mvp-architecture.html.

In this example, in the /Contacts/src/com/google/gwt/sample/contacts/client/presenter/ContactsPresenter.java file, the interface Display contains the following methods:

public interface Display 
    {
        HasClickHandlers getAddButton();
        HasClickHandlers getDeleteButton();
        HasClickHandlers getList();
        ...
    }

Instead of these buttons, I would like to make a getSuggestBox() method here. In the bind() method in my Presenter file, I would like to invoke

getSuggestBox().addKeyDownHandler({...}) 

and

getSuggestBox().addSelectionHandler({...}) 

and create handlers for these.

To do this, my first solution was to create a HasSearchHandlers interface that looks like this:

public interface HasSearchHandlers extends HasKeyDownHandlers,     
HasSelectionHandlers<SuggestOracle.Suggestion>{}

and then to try to use the method

HasSearchHandlers getSuggestBox()

This, however, has failed so far. I can't use polymorphism and say

private HasSearchHandlers box = new SuggestBox()

in my version of the ContactsView file to implement getSuggestBox(), and I can't cast a SuggestBox into a HasSearchHandlers either -- I get a ClassCastException. The JRE knows that HasSearchHandlers is an empty interface, so I don't get why SuggestBox has to explicitly implement HasSearchHandlers for this casting to work. The class SuggestBox implements everything present in the HasSearchHandlers interface; isn't this enough?

The generic interface presented as the alternative in the first link in this post has given me similar difficulty, since it does the same thing without calling it "HasSearchHandlers". In light of this problem, what is the best design step to take? How should I implement and create a "getSuggestBox() - like" mechanism in the GWT MVP framework?


Solution

  • The class SuggestBox implements everything present in the HasSearchHandlers interface; isn't this enough?

    No, not enough, the class SuggestBox has to implement the HasSearchHandlers interface itself (which it obviously doesn't).

    In light of this problem, what is the best design step to take?

    You can do two things -

    1) Use (modify ur code) whatever interfaces are provided by the suggest box.

    2) Create your own widget which extends the suggest box and implements your handlers.

    I would go with number 1 as I don't believe you want to extend the suggest boxes functionality any further.