package com.test.client; import com.google.gwt.user.client.HTTPRequest; import com.google.gwt.user.client.ResponseTextHandler; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.ClickListener; import com.google.gwt.user.client.ui.FocusPanel; import com.google.gwt.user.client.ui.Grid; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.Tree; import com.google.gwt.user.client.ui.TreeItem; import com.google.gwt.user.client.ui.Widget; import java.util.Iterator; import java.util.Set; public class JSON { /** * Class for handling the response text associated with a request for a JSON * object. * */ private class JSONResponseTextHandler implements ResponseTextHandler { /* * (non-Javadoc) * @see com.google.gwt.user.client.ResponseTextHandler#onCompletion(java.lang.String) */ public void onCompletion(String responseText) { /* * When the fetch has been completed we parse the JSON response and * display the result */ //JSONObject jsonObject; try { JSONObject jsonObject = JSONParser.parse(responseText); displayJSONObject(jsonObject); } catch (JSONException e) { } searchButton.setText(SEARCH_BUTTON_DEFAULT_TEXT); } private void displayJSONObject(JSONObject jsonObject) { /* * Here we clear the grid and fill it with the new values. */ childGrid.clear(); requestChildrenGrid(jsonObject); gdOut.setWidget(1,0,childGrid); } private void requestChildrenGrid(JSONValue jsonValue){ /* * Here we fill the grid. */ JSONObject jsonObject; if(jsonValue.isArray() != null){ for(int i = 0; i < jsonValue.isArray().size();i++){ requestChildrenGrid(jsonValue.isArray().get(i)); childGrid.setWidget(itemNumber,0,new HTML("
")); childGrid.setWidget(itemNumber,1,new HTML("
")); itemNumber++; int resizeNumber = itemNumber + 1; childGrid.resize(resizeNumber,2); } } else { if ((jsonObject = jsonValue.isObject()) != null) { String[] keys = jsonObject.getKeys(); for (int i = 0; i < keys.length; ++i) { String key = keys[i]; childGrid.setWidget(itemNumber,0,new HTML(""+ key +":")); childGrid.setWidget(itemNumber,1,new HTML(jsonObject.get(key).toString())); requestChildrenGrid(jsonObject.get(key)); itemNumber++; int resizeNumber = itemNumber + 1; childGrid.resize(resizeNumber,2); } } else if (jsonValue != null) { // Only JSONObject, and JSONArray do anything special with toString, so // it is safe to handle all non-null cases by simply using toString // } else { // } } } } /* * Class for handling the fetch button's click event. */ private class SearchButtonClickListener implements ClickListener { /* * (non-Javadoc) * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget) */ public void onClick(Widget sender) { /* * When the user click the button we fetch the URL. */ itemNumber = 0; doFetchURL(); } } /* * Default URL to use to fetch JSON objects. Note that the contents of this * JSON result were as a result of requesting the following URL: * * http://api.search.yahoo.com/ImageSearchService/V1/imageSearch?appid=Y... * */ private static final String DEFAULT_SEARCH_URL = "http:// www.mywebpage.de/gwt/index.php"; /* * Text displayed on the fetch button when we are in a default state. */ private static final String SEARCH_BUTTON_DEFAULT_TEXT = "Search"; /* * Text displayed on the fetch button when we are waiting for a JSON reply. */ private static final String SEARCH_BUTTON_WAITING_TEXT = "Waiting for JSON Response..."; /** * Entry point for this simple application. Currently, we build the * application's form and wait for events. */ public void onModuleLoad() { initializeMainForm(); } /* * Fetch the requested URL. */ private void doFetchURL() { searchButton.setText(SEARCH_BUTTON_WAITING_TEXT); if (!HTTPRequest.asyncGet(DEFAULT_SEARCH_URL, new JSONResponseTextHandler())) { // Reset the caption. // searchButton.setText(SEARCH_BUTTON_DEFAULT_TEXT); } } /** * Initialize the main form's layout and content. */ private void initializeMainForm() { /* * Here we initialize and setup a panel for use it as container for the search form and * the results. */ FocusPanel fpn = new FocusPanel(); Grid gd = new Grid(1,2); gd.setWidget(0, 0, txtBox); gd.setWidget(0, 1, searchButton); gdOut.setWidget(0,0,gd); gdOut.setBorderWidth(1); gdOut.setWidth("500px"); childGrid.setCellPadding(0); childGrid.setCellSpacing(0); childGrid.setWidth("490px"); searchButton.setText(SEARCH_BUTTON_DEFAULT_TEXT); searchButton.addClickListener(new ClickListener() { public void onClick(Widget sender) { //itemNumber = 0; searchButton.setText(SEARCH_BUTTON_WAITING_TEXT); if (!HTTPRequest.asyncGet(DEFAULT_SEARCH_URL + "? tosearch=" + txtBox.getText(), new JSONResponseTextHandler())) { // Reset the caption. searchButton.setText(SEARCH_BUTTON_DEFAULT_TEXT); } } }); fpn.add(gdOut); RootPanel.get().add(fpn); } private Button searchButton = new Button(); private Grid gdOut = new Grid(2,1); private Grid childGrid = new Grid(5,2); private TextBox txtBox = new TextBox(); private int itemNumber = 0; }