A simple GenericSuggestBox for use with DTO's or POJO's.??
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The TextBoxWithLabel can be found here: http://thinkbelievedo.com/gwt-textbox-and-passwordtextbox-with-label-in | |
*/ | |
public static class SuggestBoxWithLabel extends SuggestBox { | |
public SuggestBoxWithLabel(SuggestOracle oracle, TextBoxWithLabel tb) { | |
super(oracle, tb); | |
} | |
} | |
/** | |
* Generic Multi Word Suggestions for use with DTO's and POJOs Based on example | |
* by James Heggs - jheggs@axonbirch.com Example - | |
* http://eggsylife.co.uk/2008/08/25/gwt-suggestbox-backed-by-dto-model/ | |
* | |
* @author Ram Vijapurapu - ram at thinkbelievedo dot com | |
* | |
* @param <T> | |
*/ | |
public static class GenericMultiWordSuggestion<T> extends MultiWordSuggestion { | |
T dto = null; | |
public GenericMultiWordSuggestion(T dto) { | |
super(dto.toString(), dto.toString()); | |
this.dto = dto; | |
} | |
public T getDTO() { | |
return dto; | |
} | |
} | |
/** | |
* Generic Suggest Oracle for use with DTO's and POJOs Based on example by James | |
* Heggs - jheggs@axonbirch.com - | |
* http://eggsylife.co.uk/2008/08/25/gwt-suggestbox-backed-by-dto-model/ | |
* | |
* @author Ram Vijapurapu - ram at thinkbelievedo dot com | |
* | |
* @param <T> | |
*/ | |
public static class GenericSuggestOracle<T> extends SuggestOracle { | |
private List<GenericMultiWordSuggestion<T>> suggestions = null; | |
@Override | |
public void requestSuggestions(Request request, Callback callback) { | |
Response resp = new Response(matchingQuery(request.getQuery(), | |
request.getLimit())); | |
callback.onSuggestionsReady(request, resp); | |
} | |
public Collection<GenericMultiWordSuggestion<T>> matchingQuery( | |
String query, int limit) { | |
List<GenericMultiWordSuggestion<T>> matchingResults = new ArrayList<GenericMultiWordSuggestion<T>>( | |
limit); | |
if (query.length() > 1) { | |
String prefix = query.toLowerCase(); | |
int i = 0; | |
int s = suggestions.size(); | |
while (i < s | |
&& suggestions.get(i).getDisplayString().toLowerCase() | |
.indexOf(prefix) == -1) { | |
i++; | |
} | |
int count = 0; | |
while (i < s | |
&& suggestions.get(i).getDisplayString().toLowerCase() | |
.indexOf(prefix) != -1 && count < limit) { | |
matchingResults.add(suggestions.get(i)); | |
i++; | |
count++; | |
} | |
} | |
return matchingResults; | |
} | |
/** | |
* @param o | |
* @return | |
* @see java.util.List#add(java.lang.Object) | |
*/ | |
public boolean add(GenericMultiWordSuggestion<T> o) { | |
if (suggestions == null) { | |
suggestions = new ArrayList<GenericMultiWordSuggestion<T>>(); | |
} | |
return suggestions.add(o); | |
} | |
/** | |
* @param o | |
* @return | |
* @see java.util.List#remove(java.lang.Object) | |
*/ | |
public boolean remove(GenericMultiWordSuggestion<T> o) { | |
if (suggestions != null) { | |
return suggestions.remove(o); | |
} | |
return false; | |
} | |
} |
Now to use it within your GWT App:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GenericSuggestOracle<Product> oracle = new GenericSuggestOracle<Product>(); | |
SuggestBoxWithLabel sbProductSearch = new SuggestBoxWithLabel(oracle, new TextBoxWithLabel("Product Search")); | |
// To populate - from Async CallBack.. | |
for (Product product : result) { | |
oracle.add(new GenericMultiWordSuggestion<Product>(product)); | |
} |