Pages

Monday, March 23, 2009

Gwt Suggest Box And Rpc

Gwt Suggest Box :

Hi Every One ,

This is my first blog .so please bare me if i need changes.

so what iam going to give you is a code on suggest box , how to make a callback to server for obtaining the suggestions using Suggest.Response and Request

so first let me create the files that are necessary .

On Client Side :

1. a suggestService which extends Remote Service

public interface SuggestService extends RemoteService {
public SuggestOracle.Response getSuggestions(SuggestOracle.Request req);

public static class Util {
public static SuggestServiceAsync getInstance(){
SuggestServiceAsync suggestService= (SuggestServiceAsync)GWT.create(SuggestService.class);
((ServiceDefTarget)suggestService).setServiceEntryPoint(GWT.getModuleBaseURL()+"/suggestions");
return suggestService;
}
}
}


2.an interface for AsyncCallback

public interface SuggestServiceAsync {
public void getSuggestions(SuggestOracle.Request req,AsyncCallback callback);
}


3.a suggestionClass which implements IsSerializable and Suggestion where this will be usefull in obtaining the suggestion supplied from server

public class Suggestions implements IsSerializable, Suggestion
{
private String suggestions; public String getDisplayString() {
return suggestions;
}

public String getReplacementString() {
return suggestions;
}

public Suggestions() { } //Constructor With Suggestion

public Suggestions(String suggestion) {
this.suggestions=suggestion;
}

}

4. The main important class is SuggestionOracle class which extends SuggestOracle .suggestOracle is used to create suggestions associated with a specific query string

public class SuggestionOracle extends SuggestOracle {

public void requestSuggestions(SuggestOracle.Request req,SuggestOracle.Callback callback) {
SuggestService.Util.getInstance().getSuggestions(req,new ItemRequestCallback(req,callback));
}

public boolean isDisplayStringHTML() {
return true;
}

public class ItemRequestCallback implements AsyncCallback{
private SuggestOracle.Request req;
private SuggestOracle.Callback callback;


public void onFailure(Throwable caught) {
callback.onSuggestionsReady(req,new SuggestOracle.Response()); caught.printStackTrace();
}

public void onSuccess(Object result) {
callback.onSuggestionsReady(req,(SuggestOracle.Response) result);
}

public ItemRequestCallback(SuggestOracle.Request _req,SuggestOracle.Callback _callback) {
this.req=_req; this.callback=_callback;
}
}
}

we have written all the files that are necessary on the client side.now we will move to server side

1. a class that implements The SuggestService and extends the RemoteServiceServlet

public class SuggestServiceImpl extends RemoteServiceServlet implements
SuggestService {

public Response getSuggestions(Request req) {

SuggestOracle.Response response=new SuggestOracle.Response();

ArrayList oracle=new ArrayList();
oracle.add("Afghanistan");
oracle.add("Albania");
oracle.add("Algeria");
oracle.add("American Samoa");
oracle.add("Andorra");
oracle.add("Angola");
oracle.add("Anguilla");
oracle.add("Antarctica");
oracle.add("Antigua And Barbuda");
oracle.add("Argentina");
oracle.add("Belgium");
oracle.add("Belize");
oracle.add("China");
oracle.add("Christmas Island");
oracle.add("Cocos (Keeling) Islands");
oracle.add("Colombia");
oracle.add("Comoros");
oracle.add("Congo, The Democratic Republic Of The");
oracle.add("Gabon");
oracle.add("Gambia");
oracle.add("Georgia");
oracle.add("Germany");
oracle.add("Ghana");

List mySuggestions = new ArrayList();

for(int i=0;i
<oracle.size();i++){
mySuggestions.add(oracle.get(i).toString());
}

response.setSuggestions(mySuggestions);
return response;
}
}




now from your OnModuleLoad() , call as

OnModuleLoad(){

SuggestionOracle oracle=new SuggestionOracle();
SuggestBox box=new SuggestBox(oracle,new MultipleTextBox());
box.setWidth("300px");
HorizontalPanel hPanel=new HorizontalPanel();
hPanel.add(new Label("Suggest Box"));
hPanel.add(box);
RootPanel.get().add(hPanel);

}



u can check the Image for Demonstration





















In My Next Article We Will see how to Create a suggest box which works similarly like the Gmail's "To" Field


Read More