com.google.gwt.sample.temp_conv.client.GWT_Temp_Conv.java Source code

Java tutorial

Introduction

Here is the source code for com.google.gwt.sample.temp_conv.client.GWT_Temp_Conv.java

Source

package com.google.gwt.sample.temp_conv.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
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.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GWT_Temp_Conv implements EntryPoint {
    /**
     * The message displayed to the user when the server cannot be reached or
     * returns an error.
     */
    //private static final String SERVER_ERROR = "An error occurred while "
    //      + "attempting to contact the server. Please check your network " + "connection and try again.";

    private Button sendButton = new Button("Send");
    private TextBox inputBox = new TextBox();
    private RadioButton fahr = new RadioButton("units", "Fahrenheit");
    private RadioButton celc = new RadioButton("units", "Celcius");
    private Label unitsLabel = new Label("Convert to:");

    private FlexTable outputTable = new FlexTable();
    private VerticalPanel mainPanel = new VerticalPanel();
    private HorizontalPanel hpanel = new HorizontalPanel();
    private HorizontalPanel unitPanel = new HorizontalPanel();

    private TemperatureConverterServiceAsync tempConvSvc = GWT.create(TemperatureConverterService.class);

    public void onModuleLoad() {

        outputTable.setText(0, 0, "Fahrenheit");
        outputTable.setText(0, 1, "Celsius");
        outputTable.setWidget(1, 0, new Label());
        outputTable.setWidget(1, 1, new Label());

        // Add styles to output table
        outputTable.getRowFormatter().addStyleName(0, "outputListHeader");
        outputTable.addStyleName("outputList");

        sendButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                convertTemperature();
            }
        });

        inputBox.addKeyDownHandler(new KeyDownHandler() {
            public void onKeyDown(KeyDownEvent event) {
                if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
                    convertTemperature();
            }
        });

        fahr.setValue(true);
        inputBox.setFocus(true);

        unitPanel.add(unitsLabel);
        unitPanel.add(fahr);
        unitPanel.add(celc);

        hpanel.add(inputBox);
        hpanel.add(sendButton);

        mainPanel.add(outputTable);
        mainPanel.add(hpanel);
        mainPanel.add(unitPanel);

        // Associate the Main Panel with the HTML host page
        RootPanel.get("display").add(mainPanel);
    }

    public void convertTemperature() {
        String input = inputBox.getText().toUpperCase().trim();
        inputBox.setFocus(true);

        if (!input.matches("^-?[0-9]*(.{1}[0-9]*)?$")) {
            Window.alert("Temperature must be numeric");
            inputBox.selectAll();
            return;
        }
        inputBox.setText("");

        // Initialize the service proxy
        if (tempConvSvc == null)
            tempConvSvc = GWT.create(TemperatureConverterService.class);

        // Setup the callback method
        AsyncCallback<Temperature> callback = new AsyncCallback<Temperature>() {
            public void onFailure(Throwable caught) {
                outputTable.setText(1, 0, caught.getMessage());
            }

            public void onSuccess(Temperature temp) {
                updateOutputTable(temp);
            }
        };

        if (fahr.getValue() == true)
            tempConvSvc.toFahrenheit(input, callback);
        else
            tempConvSvc.toCelcius(input, callback);

    }

    public void updateOutputTable(Temperature temp) {
        double ftemp = Double.parseDouble(temp.getFahrenheit());
        double ctemp = Double.parseDouble(temp.getCelcius());
        String changeStyleName = "comfortable";

        // Set css styling
        if (ftemp >= 212) {
            changeStyleName = "boiling";
        } else if (ftemp <= 32) {
            changeStyleName = "freezing";
        }

        // Format the output
        Label flabel = (Label) outputTable.getWidget(1, 0);
        Label clabel = (Label) outputTable.getWidget(1, 1);
        String fahr = NumberFormat.getFormat("#,##0.00").format(ftemp);
        String celc = NumberFormat.getFormat("#,##0.00").format(ctemp);
        flabel.setText(fahr);
        clabel.setText(celc);
        flabel.setStyleName(changeStyleName);
        clabel.setStyleName(changeStyleName);

        // Display the output
        outputTable.setWidget(1, 0, flabel);
        outputTable.setWidget(1, 1, clabel);

    }
}