tags:

views:

63

answers:

0

I need to create a SuggestBox that will show all options on pressing the Enter key. I have written the following implementation, and it seems to be working fine. I would like someone to review my implementation and let me know if it will cause problems in any particular scenario. Also, the SuggestOracle to be passed to this SuggestBox should have the default suggestions set, by calling the method setDefaultSuggestions() on MultiWordSuggestOracle. Any user of my SuggestBox should be transparent to this fact. Hence I guess I will need to wrap (or extend) MultiWordSuggestOracle to do the default suggestions settings. Can you please recommend what will be a good way of doing this?

public class SuggestBoxWithAllOptions extends SuggestBox implements 
    KeyPressHandler { 
    public SuggestBoxWithAllOptions(MultiWordSuggestOracle oracle) { 
            super(oracle); 
            this.addKeyPressHandler(this); 
    } 
    @Override 
    public void onKeyPress(KeyPressEvent event) { 
            char c = event.getCharCode(); 
            int i = this.getText().length(); 
    if (c == KeyboardListener.KEY_ENTER && i == 0) { 
            /* Since the query string is null, the default suggestions 
           will get listed */ 
            this.showSuggestionList(); 
     } 
    } 
   } 

  /* Code for initializing the SuggestBox */ 
            List<String> suggestions = new ArrayList<String>(); 
            suggestions.add("Tablet"); 
            suggestions.add("Capsule"); 
            MultiWordSuggestOracle myOracle = new MultiWordSuggestOracle(); 
            myOracle.addAll(suggestions ); 
            myOracle.setDefaultSuggestionsFromText(suggestions); 
            SuggestBox mySuggest = new SuggestBoxWithAllOptions(myOracle);