lv.abuzdin.client.simple.SimplePanelExample.java Source code

Java tutorial

Introduction

Here is the source code for lv.abuzdin.client.simple.SimplePanelExample.java

Source

/*
 * Copyright 2014 C.T.Co SIA, Brivibas street 48/50, Riga, Latvia. All rights reserved.
 *
 * 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 lv.abuzdin.client.simple;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import lv.abuzdin.client.Messages;
import lv.abuzdin.shared.FieldVerifier;

public class SimplePanelExample extends Composite {

    GreetingServiceAsync greetingService = GWT.create(GreetingService.class);

    Messages messages = GWT.create(Messages.class);

    public SimplePanelExample() {
        HorizontalPanel panel = new HorizontalPanel();
        panel.setWidth("100%");

        final Button sendButton = new Button(messages.sendButton());
        sendButton.addStyleName("sendButton");
        panel.add(sendButton);

        final TextBox nameField = new TextBox();
        nameField.setText(messages.nameField());
        nameField.setFocus(true);
        nameField.selectAll();
        panel.add(nameField);

        final Label errorLabel = new Label();
        panel.add(errorLabel);

        panel.setCellHorizontalAlignment(sendButton, HasHorizontalAlignment.ALIGN_RIGHT);
        panel.setCellHorizontalAlignment(errorLabel, HasHorizontalAlignment.ALIGN_LEFT);

        final Button closeButton = new Button("Close");
        closeButton.getElement().setId("closeButton");

        final Label textToServerLabel = new Label();
        final HTML serverResponseLabel = new HTML();

        VerticalPanel dialogVPanel = new VerticalPanel();
        dialogVPanel.addStyleName("dialogVPanel");
        dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
        dialogVPanel.add(textToServerLabel);
        dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
        dialogVPanel.add(serverResponseLabel);
        dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
        dialogVPanel.add(closeButton);

        final DialogBox dialogBox = new DialogBox();
        dialogBox.setText("Remote Procedure Call");
        dialogBox.setAnimationEnabled(true);
        dialogBox.setWidget(dialogVPanel);

        // Add a handler to close the DialogBox
        closeButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                dialogBox.hide();
                sendButton.setEnabled(true);
                sendButton.setFocus(true);
            }
        });

        // Create a handler for the sendButton and nameField
        class MyHandler implements ClickHandler, KeyUpHandler {
            /**
             * Fired when the user clicks on the sendButton.
             */
            public void onClick(ClickEvent event) {
                sendNameToServer();
            }

            /**
             * Fired when the user types in the nameField.
             */
            public void onKeyUp(KeyUpEvent event) {
                if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                    sendNameToServer();
                }
            }

            /**
             * Send the name from the nameField to the server and wait for a response.
             */
            private void sendNameToServer() {
                // First, we validate the input.
                errorLabel.setText("");
                String textToServer = nameField.getText();
                if (!FieldVerifier.isValidName(textToServer)) {
                    errorLabel.setText("Please enter at least four characters");
                    return;
                }

                // Then, we send the input to the server.
                sendButton.setEnabled(false);
                textToServerLabel.setText(textToServer);
                serverResponseLabel.setText("");
                greetingService.greetServer(textToServer, new AsyncCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        dialogBox.setText("Remote Procedure Call");
                        serverResponseLabel.removeStyleName("serverResponseLabelError");
                        serverResponseLabel.setHTML(result);
                        dialogBox.center();
                        closeButton.setFocus(true);
                    }

                    @Override
                    public void onFailure(Throwable throwable) {
                    }
                });
            }
        }

        // Add a handler to send the name to the server
        MyHandler handler = new MyHandler();
        sendButton.addClickHandler(handler);
        nameField.addKeyUpHandler(handler);

        initWidget(panel);
    }
}