/*******************************************************************************
*
* 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.
*
******************************************************************************/
package org.spiffyui.hellospiffyphp.client;
import org.spiffyui.client.JSONUtil;
import org.spiffyui.client.MainFooter;
import org.spiffyui.client.MainHeader;
import org.spiffyui.client.MessageUtil;
import org.spiffyui.client.rest.RESTCallback;
import org.spiffyui.client.rest.RESTException;
import org.spiffyui.client.rest.RESTility;
import org.spiffyui.client.widgets.LongMessage;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
/**
* This class is the main entry point for our GWT module.
*/
public class Index implements EntryPoint, ClickHandler, KeyPressHandler
{
private static Index g_index;
private TextBox m_text = new TextBox();
private Button m_submit = new Button("Submit");
private Button m_submitWithError = new Button("Submit and trigger error");
private LongMessage m_longMessage = new LongMessage("longMsg");
/**
* The Index page constructor
*/
public Index()
{
g_index = this;
}
@Override
public void onModuleLoad()
{
MainHeader header = new MainHeader();
header.setHeaderTitle("Hello Spiffy PHP!" );
MainFooter footer = new MainFooter();
footer.setFooterString("This application is a <a href=\"http://www.spiffyui.org\">Spiffy UI Framework</a> application");
FlowPanel panel = new FlowPanel()
{
@Override
public void onLoad()
{
super.onLoad();
/*
Let's set focus into the text field when the page first loads
*/
m_text.setFocus(true);
}
};
panel.add(m_longMessage);
final InlineLabel label = new InlineLabel("What's your name? ");
label.setHeight("1em");
panel.add(label);
panel.add(m_text);
panel.add(m_submit);
panel.add(m_submitWithError);
m_submit.addClickHandler(this);
m_submitWithError.addClickHandler(this);
m_text.addKeyPressHandler(this);
RootPanel.get("mainContent").add(panel);
}
@Override
public void onClick(ClickEvent event)
{
// this is either from the "Submit" button or the "Submit and trigger error" button
boolean triggerError = (event.getSource() == m_submitWithError);
sendRequest(triggerError);
}
@Override
public void onKeyPress(KeyPressEvent event)
{
/*
We want to submit the normal request if the user pressed enter
*/
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
sendRequest(false);
}
}
/**
* Send the REST request to the server and read the response back.
*/
private void sendRequest(boolean triggerError)
{
String name = m_text.getValue().trim();
if (name.equals("")) {
MessageUtil.showWarning("Please enter your name in the text field.", false);
return;
}
String restURL = "hellospiffyphp";
restURL = restURL + "/" + name; // we know we have a name because we passed the check above
if (triggerError) {
// this is not a very RESTful way to do this but it's just to show the error handling
restURL = restURL + "?triggererror=TRUE";
}
RESTility.callREST(restURL, new RESTCallback() {
@Override
public void onSuccess(JSONValue val)
{
showSuccessMessage(val);
}
@Override
public void onError(int statusCode, String errorResponse)
{
MessageUtil.showError("Error. Status Code: " + statusCode + " " + errorResponse);
}
@Override
public void onError(RESTException e)
{
MessageUtil.showError(e.getReason());
}
});
}
/**
* Show the successful message result of our REST call.
*
* @param val the value containing the JSON response from the server
*/
private void showSuccessMessage(JSONValue val)
{
JSONObject obj = val.isObject();
String name = JSONUtil.getStringValue(obj, "user");
String browser = JSONUtil.getStringValue(obj, "userAgent");
String server = JSONUtil.getStringValue(obj, "serverInfo");
String message = "Hello, " + name + "! <br/>You are using " + browser + ".<br/>The server is " + server + ".";
m_longMessage.setHTML(message);
}
}