package com.digitbit.client; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.json.client.JSONArray; import com.google.gwt.json.client.JSONException; import com.google.gwt.json.client.JSONObject; import com.google.gwt.json.client.JSONParser; 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.ChangeListener; import com.google.gwt.user.client.ui.ClickListener; import com.google.gwt.user.client.ui.FlexTable; import com.google.gwt.user.client.ui.HasVerticalAlignment; import com.google.gwt.user.client.ui.HorizontalPanel; import com.google.gwt.user.client.ui.Image; import com.google.gwt.user.client.ui.KeyboardListener; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.ListBox; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.Widget; /* * Copyright 2007 Chris Fong * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class GoogleAPITester implements EntryPoint { private TextBox searchBox; private Button goButton; private FlexTable resultsTable; private Image progressImage; private ListBox functionListBox; private Label helpLabel; /** * This is the entry point method. */ public void onModuleLoad() { HorizontalPanel panel = new HorizontalPanel(); functionListBox = new ListBox(); functionListBox.addItem("search"); functionListBox.addItem("spelling"); functionListBox.setVisibleItemCount(1); searchBox = new TextBox(); goButton = new Button(); goButton.setText("Go"); progressImage = new Image(); helpLabel = new Label(""); panel.add(functionListBox); panel.add(searchBox); panel.add(goButton); panel.add(progressImage); panel.add(helpLabel); panel.setSpacing(5); panel.setCellVerticalAlignment(progressImage, HasVerticalAlignment.ALIGN_MIDDLE); panel.setCellVerticalAlignment(helpLabel, HasVerticalAlignment.ALIGN_MIDDLE); resultsTable = new FlexTable(); RootPanel.get("slot1").add(panel); RootPanel.get("slot2").add(resultsTable); addListeners(); updateHelpLabel(); showSearchingNotInProgress(); } private void addListeners() { searchBox.addKeyboardListener(new KeyboardListener() { public void onKeyDown(Widget sender, char keyCode, int modifiers) { // Handle enter in the search box if (keyCode == 13) { startSearch(); } } public void onKeyPress(Widget sender, char keyCode, int modifiers) {} public void onKeyUp(Widget sender, char keyCode, int modifiers) {} }); goButton.addClickListener(new ClickListener() { public void onClick(Widget sender) { startSearch(); } }); functionListBox.addChangeListener(new ChangeListener() { public void onChange(Widget sender) { updateHelpLabel(); } }); } private void updateHelpLabel() { int selectedIndex = functionListBox.getSelectedIndex(); String selectedFunction = functionListBox.getItemText(selectedIndex); if (selectedFunction.equals("search")) { helpLabel.setText("Enter a search query"); } else if (selectedFunction.equals("spelling")) { helpLabel.setText("Enter a phrase. (ie. 'speeling')"); } } private class JSONResponseTextHandler implements ResponseTextHandler { public void onCompletion(String responseText) { try { showSearchingNotInProgress(); // Check if we get a bogus response from the server. if (responseText.trim().equals("false") || responseText.trim().equals("")) { clearResultsTable(); resultsTable.setHTML(0, 0, "We are having problems connecting with google's servers. Please try again later."); return; } String function = getSelectedFunction(); if (function.equals("spelling")) { handleSpellingResponse(responseText); } else if (function.equals("search")) { handleSearchResponse(responseText); } } catch (JSONException e) { Window.alert("Exception handling JSON Response: " + e.getMessage()); } } private void handleSpellingResponse(String responseText) { clearResultsTable(); // This weird string seems to indicate no spelling suggestion if (responseText.trim().equals("{\"!xsi:null\":\"true\"}")) { resultsTable.setHTML(0, 0, "No spelling suggestions found"); } else { resultsTable.setHTML(0, 0, responseText); } } private void handleSearchResponse(String responseText) throws JSONException { JSONObject jsonObject = (JSONObject) JSONParser.parse(responseText); JSONArray resultElements = (JSONArray) jsonObject.get("resultElements"); for (int i = 0; i < resultElements.size(); i++) { JSONObject resultElement = (JSONObject) resultElements.get(i); String link = ""+ resultElement.get("title")+""; resultsTable.setHTML(i*3, 0, link); resultsTable.setHTML(i*3+1, 0, resultElement.get("snippet").toString()); resultsTable.setHTML(i*3+2, 0, "
"+resultElement.get("URL").toString()+"

"); } } } private void startSearch() { showSearchingInProgress(); String function = getSelectedFunction(); if (!HTTPRequest.asyncGet( "http://www.gwtsite.com/demos/googleapi/google-api-server.php?function=" +function+"&query="+searchBox.getText(), new JSONResponseTextHandler())) { Window.alert("Error making request to server"); } } private String getSelectedFunction() { String function = functionListBox.getItemText( functionListBox.getSelectedIndex()); return function; } public void clearResultsTable() { for (int i = 0; i < resultsTable.getRowCount(); i++) { resultsTable.removeCell(i, 0); } } private void showSearchingNotInProgress() { progressImage.setUrl("images/blanksearching.gif"); helpLabel.setVisible(true); } private void showSearchingInProgress() { progressImage.setUrl("images/searching.gif"); helpLabel.setVisible(false); } }