com.google.gwt.sample.homeworkfive.client.HomeworkFive.java Source code

Java tutorial

Introduction

Here is the source code for com.google.gwt.sample.homeworkfive.client.HomeworkFive.java

Source

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

import java.io.File;
import java.util.ArrayList;

import com.google.gwt.sample.homeworkfive.shared.FieldVerifier;
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.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
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.user.client.Window;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class HomeworkFive 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.";

    /**
     * Create a remote service proxy to talk to the server-side Greeting service.
     */
    private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);

    private VerticalPanel mainPanel = new VerticalPanel();
    private FlexTable contactsFlexTable = new FlexTable();
    private HorizontalPanel addPanel = new HorizontalPanel();
    private Button addContactButton = new Button("Add");
    private TextBox newNameTextBox = new TextBox();
    private TextBox newPhoneTextBox = new TextBox();
    private TextBox newEmailTextBox = new TextBox();
    private ArrayList<String> contacts = new ArrayList<String>();

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {

        // Create table for contact data.
        contactsFlexTable.setText(0, 0, "Name");
        contactsFlexTable.setText(0, 1, "Phone");
        contactsFlexTable.setText(0, 2, "E-mail");
        contactsFlexTable.setText(0, 3, "Remove");
        //38,75

        /**
        //Assemble Add New Contact Panel
        final Button sendButton = new Button("Add");
        final TextBox nameField = new TextBox();
        final TextBox phoneField = new TextBox();
        final TextBox emailField = new TextBox();
        nameField.setText("Name");
        phoneField.setText("Phone Number");
        emailField.setText("E-mail Address");
        final Label errorLabel = new Label();
        */

        // Assemble Add Contact panel.
        addPanel.add(newNameTextBox);
        //newNameTextBox.setText("Name");
        addPanel.add(newPhoneTextBox);
        //newPhoneTextBox.setText("Phone");
        addPanel.add(newEmailTextBox);
        //newEmailTextBox.setText("Email");
        addPanel.add(addContactButton);

        // Assemble Main panel.
        mainPanel.add(contactsFlexTable);
        mainPanel.add(addPanel);

        // We can add style names to widgets
        //sendButton.addStyleName("sendButton");
        addContactButton.addStyleName("addButton");

        // Add the nameField and sendButton to the RootPanel
        // Use RootPanel.get() to get the entire body element
        RootPanel.get("contactsFieldContainer").add(mainPanel);

        // Move cursor focus to the input box.
        newNameTextBox.setFocus(true);

        // Listen for mouse events on the Add button.
        addContactButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                addContact();
            }
        });

        // Listen for keyboard events in the input box.
        newNameTextBox.addKeyDownHandler(new KeyDownHandler() {
            public void onKeyDown(KeyDownEvent event) {
                if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                    addContact();
                }
            }
        });

    }

    /**
    * Return to work on this to make sure that exclusings work
    * 
    */
    private void addContact() {

        final String name = newNameTextBox.getText().toUpperCase().trim();
        newNameTextBox.setFocus(true);

        final String phone = newPhoneTextBox.getText().toUpperCase().trim();
        newPhoneTextBox.setFocus(true);

        final String email = newEmailTextBox.getText().toUpperCase().trim();
        newEmailTextBox.setFocus(true);

        // E-mail address must contain numbers, letters, dots, and @.
        if (!name.matches("^[A-Z\\.]{1,50}$")) {
            Window.alert("Enter your name again");
            newNameTextBox.selectAll();
            return;

        } else if (!phone.matches("^[0-9\\.]{1,10}$")) {
            Window.alert("Enter a proper phone number");
            newPhoneTextBox.selectAll();
            return;

        } else if (!email.matches("^[0-9A-Z.@\\.]{12,50}$")) {
            Window.alert("Enter a proper email address");
            newEmailTextBox.selectAll();

            return;
        }

        newEmailTextBox.setText("");
        newPhoneTextBox.setText("");
        newNameTextBox.setText("");

        // Don't add the contact if it's already in the table with the same exact information.
        if (contacts.contains(name) && (contacts.contains(phone) && contacts.contains(email)))
            return;

        if (!name.matches("^[A-Z\\.]{1,50}$"))
            return;

        if (!phone.matches("^[0-9\\.]{1,10}$"))
            return;

        if (!email.matches("^[0-9A-Z@\\.]{12,50}$"))
            return;

        // Add the contact to the table.
        int row = contactsFlexTable.getRowCount();
        contacts.add(name);
        contacts.add(phone);
        contacts.add(email);
        contactsFlexTable.setText(row, 0, name);
        contactsFlexTable.setText(row, 1, phone);
        contactsFlexTable.setText(row, 2, email);

        // Add a button to remove this contact from the table.
        Button removeContactButton = new Button("x");
        removeContactButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                int removedIndex = contacts.indexOf(name);
                contacts.remove(removedIndex);
                contactsFlexTable.removeRow(removedIndex + 1);
            }
        });
        contactsFlexTable.setWidget(row, 3, removeContactButton);

    }

}