com.mallentechinc.smartwire.client.SmartWireWeb.java Source code

Java tutorial

Introduction

Here is the source code for com.mallentechinc.smartwire.client.SmartWireWeb.java

Source

/*
 * Copyright 2011 Google Inc. 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 com.mallentechinc.smartwire.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DecoratedTabBar;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
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 SmartWireWeb implements EntryPoint {
    private FlexTable userFlexTable;
    private static final String USER_JSON_URL = "UserServices/";
    private static final String SUBSYSTEM_JSON_URL = "SubSystemServices/";
    private FormPanel addUserFormPanel;
    private TextBox userBox;
    private DockPanel dockPanel;
    private DockPanel userDockPanel;
    private VerticalPanel userMenuVerticalPanel;
    private Button btnGetAllUsers;
    private Button btnAddUsers;
    private VerticalPanel contentPanel;
    private FlexTable flexTable;
    private TextBox userNameTextBox;
    private Button btnAddUser;
    private FormPanel formPanel;
    private Label lblNewLabel;
    private InlineHTML nlnhtmlUsername;
    private Hyperlink logout;

    private String user = null;
    private DockLayoutPanel dockLayoutPanel;
    private DecoratedTabBar decoratedTabBar;
    private TabLayoutPanel tabPanel;
    private DeckPanel deckPanel;
    private FormPanel userformPanel;
    private TextBox userName;
    private PasswordTextBox passWord;
    private TextBox firstName;
    private TextBox lastName;
    private TextBox email;
    private ListBox userRole;
    private ListBox position;

    private Button editUser;

    private DeckPanel subsysContentDeckPanel;
    private FlexTable circuitsFlexTable;

    private String loginUserName;

    private String BASE_URL = "/SmartWireWeb/";

    public void onModuleLoad() {

        RootPanel rootPanel = RootPanel.get();

        dockLayoutPanel = new DockLayoutPanel(Unit.EM);
        rootPanel.add(dockLayoutPanel, 10, 10);
        dockLayoutPanel.setSize("1100px", "900px");

        HorizontalPanel headerPanel = new HorizontalPanel();
        dockLayoutPanel.addNorth(headerPanel, 6);

        headerPanel.setWidth("1200px");
        headerPanel.setBorderWidth(0);

        Image logo = new Image("images/smartwire_logo.jpg");
        headerPanel.setCellWidth(logo, "400px");
        headerPanel.add(logo);

        headerPanel.add(new HTML("jxu"));

        Image logoff = new Image("images/doorlogoff.jpg");
        logoff.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                boolean confirm = Window.confirm("You are about to logoff");
                if (confirm) {
                    //redirect("j_spring_security_logout");
                    logoff();
                }

            }
        });
        headerPanel.add(logoff);

        HTML htmlCopyRight = new HTML("Copy Right 2012 mallentechinc.com", true);
        dockLayoutPanel.addSouth(htmlCopyRight, 7.7);

        tabPanel = new TabLayoutPanel(2.5, Unit.EM);
        tabPanel.setAnimationDuration(1000);
        tabPanel.getElement().getStyle().setMarginBottom(10.0, Unit.PX);
        dockLayoutPanel.add(tabPanel);

        tabPanel.addSelectionHandler(new SelectionHandler() {
            public void onSelection(SelectionEvent event) {
                // Let the user know what they just did.
                // Window.alert("You clicked tab " + event.getSelectedItem().toString());
                if (event.getSelectedItem().toString().equals("0")) {
                    getAllUsers();
                    deckPanel.showWidget(1);
                } else if (event.getSelectedItem().toString().equals("3")) {
                    getAllCircuitsByUser("jxu");
                    subsysContentDeckPanel.showWidget(1);
                }

            }
        });

        //user admin
        DockLayoutPanel userdockLayoutPanel = new DockLayoutPanel(Unit.EM);

        VerticalPanel userAdminMenuPanel = new VerticalPanel();
        userAdminMenuPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

        userdockLayoutPanel.addWest(userAdminMenuPanel, 11);

        Button getAllUsersButton = new Button("Get All Users");
        getAllUsersButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                getAllUsers();
                deckPanel.showWidget(1);
            }
        });
        userAdminMenuPanel.setSpacing(2);
        userAdminMenuPanel.add(getAllUsersButton);

        Button addNewUserButton = new Button("Add New User");
        addNewUserButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                deckPanel.showWidget(2);

            }
        });
        userAdminMenuPanel.setSpacing(2);
        userAdminMenuPanel.add(addNewUserButton);
        userAdminMenuPanel.setSize("130px", "150px");

        //search user
        Button searchUserButton = new Button("Search User");
        searchUserButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                Window.alert("Working on it, stay tune...");

            }
        });
        userAdminMenuPanel.setSpacing(2);
        userAdminMenuPanel.add(searchUserButton);

        //user content area
        deckPanel = new DeckPanel();
        deckPanel.setAnimationEnabled(true);
        userdockLayoutPanel.add(deckPanel);

        //add default page for this
        HTML htmlNewHtml = new HTML("This is admin page", true);
        deckPanel.add(htmlNewHtml);

        //add flex table
        flexTable = new FlexTable();
        flexTable.setCellSpacing(2);
        flexTable.setCellPadding(2);
        deckPanel.add(flexTable);
        flexTable.setSize("700px", "");

        //add user input page
        userformPanel = new FormPanel();
        deckPanel.add(userformPanel);

        final VerticalPanel addUserFormContentPanel = new VerticalPanel();
        addUserFormContentPanel.setSpacing(3);

        addUserFormContentPanel.add(new Label("User Name"));
        userName = new TextBox();
        userName.setName("userName");
        addUserFormContentPanel.add(userName);

        addUserFormContentPanel.add(new Label("Password"));
        passWord = new PasswordTextBox();
        userName.setName("password");
        addUserFormContentPanel.add(passWord);

        addUserFormContentPanel.add(new Label("First Name"));
        firstName = new TextBox();
        firstName.setName("firstName");
        addUserFormContentPanel.add(firstName);

        addUserFormContentPanel.add(new Label("Last Name"));
        lastName = new TextBox();
        lastName.setName("lastName");
        addUserFormContentPanel.add(lastName);

        addUserFormContentPanel.add(new Label("Email"));
        email = new TextBox();
        email.setName("email");
        addUserFormContentPanel.add(email);

        addUserFormContentPanel.add(new Label("User Role"));

        userRole = new ListBox();
        userRole.addStyleName("demo-ListBox");
        userRole.addItem("ADMIN");
        userRole.addItem("USER");
        userRole.setName("userRole");
        addUserFormContentPanel.add(userRole);

        addUserFormContentPanel.add(new Label("Position"));

        position = new ListBox();
        position.addStyleName("demo-ListBox");
        position.addItem("EMPLOYEE");
        position.addItem("MANAGER");
        position.setName("position");
        addUserFormContentPanel.add(position);

        Button insertUserButton = new Button("Add New User");
        insertUserButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {

                //get user input
                String requestData = getAddUserInput();
                //add user
                saveUpdateUser(requestData);

            }
        });
        addUserFormContentPanel.add(insertUserButton);

        userformPanel.add(addUserFormContentPanel);

        tabPanel.add(userdockLayoutPanel, "User Admin");

        //end of user admin

        //arch
        DockLayoutPanel archdockLayoutPanel = new DockLayoutPanel(Unit.EM);
        tabPanel.add(archdockLayoutPanel, "Planning & Architecture");

        VerticalPanel archMenuPanel = new VerticalPanel();
        archMenuPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        archMenuPanel.setSize("160px", "140px");

        Button getAllOption = new Button("All Option Code");
        getAllOption.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                Window.alert("Working on it, stay tune...");
            }
        });
        archMenuPanel.setSpacing(2);
        archMenuPanel.add(getAllOption);

        Button addNewOptionButton = new Button("New Option Code");
        addNewOptionButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                Window.alert("Working on it, stay tune...");

            }
        });
        archMenuPanel.setSpacing(2);
        archMenuPanel.add(addNewOptionButton);

        //search user
        Button searchOptionCodeButton = new Button("Search Option Code");
        searchOptionCodeButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                Window.alert("Working on it, stay tune...");

            }
        });
        archMenuPanel.setSpacing(2);
        archMenuPanel.add(searchOptionCodeButton);

        //search user
        Button functionCodeButton = new Button("Fuction Code");
        functionCodeButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                Window.alert("Working on it, stay tune...");

            }
        });
        archMenuPanel.setSpacing(2);
        archMenuPanel.add(functionCodeButton);

        archdockLayoutPanel.addWest(archMenuPanel, 11);

        //end of arch

        //ee components

        DockLayoutPanel eeCompdockLayoutPanel = new DockLayoutPanel(Unit.EM);

        VerticalPanel eeCompMenuPanel = new VerticalPanel();
        eeCompMenuPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        eeCompMenuPanel.setSize("160px", "120px");

        Button device = new Button("Device");
        device.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                Window.alert("Working on it, stay tune...");
            }
        });
        eeCompMenuPanel.setSpacing(2);
        eeCompMenuPanel.add(device);

        eeCompdockLayoutPanel.addWest(eeCompMenuPanel, 11);
        tabPanel.add(eeCompdockLayoutPanel, "EE Components");

        //end of ee components      

        //sub system
        DockLayoutPanel subSystemdockLayoutPanel = new DockLayoutPanel(Unit.EM);

        VerticalPanel subsysMenuPanel = new VerticalPanel();
        subsysMenuPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        subsysMenuPanel.setSize("160px", "120px");

        Button myCircuits = new Button("My Sub Systems");
        myCircuits.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                getAllCircuitsByUser("jxu");
                subsysContentDeckPanel.showWidget(1);
            }
        });
        subsysMenuPanel.setSpacing(2);
        subsysMenuPanel.add(myCircuits);

        //all sub system
        Button allSubSystems = new Button("Sub Systems");
        allSubSystems.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                Window.alert("Working on it, stay tune...");
            }
        });
        subsysMenuPanel.setSpacing(2);
        subsysMenuPanel.add(allSubSystems);

        //sub system family
        Button allSubSystemFamily = new Button("Sub System Family");
        allSubSystemFamily.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                Window.alert("Working on it, stay tune...");
            }
        });
        subsysMenuPanel.setSpacing(2);
        subsysMenuPanel.add(allSubSystemFamily);

        subSystemdockLayoutPanel.addWest(subsysMenuPanel, 11);

        //sub system content
        subsysContentDeckPanel = new DeckPanel();
        subsysContentDeckPanel.setAnimationEnabled(true);

        //html
        HTML subsystemHtml = new HTML("This is place you can manager your Sub System", true);
        subsysContentDeckPanel.add(subsystemHtml);

        //add circuit page
        circuitsFlexTable = new FlexTable();
        circuitsFlexTable.setSize("500px", "100px");
        circuitsFlexTable.setCellPadding(2);
        circuitsFlexTable.setCellSpacing(2);
        subsysContentDeckPanel.add(this.circuitsFlexTable);

        subSystemdockLayoutPanel.add(subsysContentDeckPanel);

        //end of sub system content

        tabPanel.add(subSystemdockLayoutPanel, "Sub System Design");
        //end of system       

        //config vechile
        DockLayoutPanel vehicledockLayoutPanel = new DockLayoutPanel(Unit.EM);

        VerticalPanel vehicleMenuPanel = new VerticalPanel();
        vehicleMenuPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        vehicleMenuPanel.setSize("160px", "120px");

        Button addVehcile = new Button("Add Vehicle");
        addVehcile.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                Window.alert("Working on it, stay tune...");
            }
        });
        vehicleMenuPanel.setSpacing(2);
        vehicleMenuPanel.add(addVehcile);

        //all sub system
        Button addTrim = new Button("Add Trim");
        addTrim.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                Window.alert("Working on it, stay tune...");
            }
        });
        vehicleMenuPanel.setSpacing(2);
        vehicleMenuPanel.add(addTrim);

        vehicledockLayoutPanel.addWest(vehicleMenuPanel, 11);

        tabPanel.add(vehicledockLayoutPanel, "Vehicle Configurations");
        //end of //config vechile

        DockLayoutPanel qadockLayoutPanel = new DockLayoutPanel(Unit.EM);

        tabPanel.add(qadockLayoutPanel, "Quality Assurance");

        DockLayoutPanel midockLayoutPanel = new DockLayoutPanel(Unit.EM);

        tabPanel.add(midockLayoutPanel, "Manufacture Interface");

        tabPanel.selectTab(0);
        getAllUsers();
        deckPanel.showWidget(1);

    }

    protected String getAddUserInput() {
        String requestData = "{" + "\"role\": \"" + userRole.getItemText(userRole.getSelectedIndex()) + "\","
                + "\"firstName\": \"" + firstName.getText() + "\"," + "\"lastName\": \"" + lastName.getText()
                + "\"," + "\"email\": \"" + email.getText() + "\"," + "\"departmentName\": \"Engineer\","
                + "\"id\": null," + "\"active\": true," + "\"position\": \""
                + position.getItemText(position.getSelectedIndex()) + "\"," + "\"password\": \""
                + passWord.getText() + "\"," + "\"userName\": \"" + userName.getText() + "\"}";
        return requestData;
    }

    protected String getUserData(SmartWireUserData userData) {

        String active = userData.isActive();
        boolean userStatus = false;
        if (active != null && active.equalsIgnoreCase("Active")) {
            userStatus = true;
        }

        String requestData = "{" + "\"role\": \"" + userData.getRole() + "\"," + "\"firstName\": \""
                + userData.getFistName() + "\"," + "\"lastName\": \"" + userData.getLastName() + "\","
                + "\"email\": \"" + userData.getEmail() + "\"," + "\"departmentName\": \"Engineer\"," + "\"id\": "
                + userData.getId() + "," + "\"active\": " + userStatus + "," + "\"position\": \""
                + userData.getPosition() + "\"," + "\"password\": \"" + userData.getPassword() + "\","
                + "\"userName\": \"" + userData.getUsername() + "\"}";
        return requestData;
    }

    protected void getAllUsers() {
        String url = USER_JSON_URL + "getAllUsers.do?";
        url = URL.encode(url);
        // Send request to server and catch any errors.
        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

        try {
            Request request = builder.sendRequest(null, new RequestCallback() {
                public void onError(Request request, Throwable exception) {
                    displayError("Couldn't retrieve JSON");
                }

                public void onResponseReceived(Request request, Response response) {
                    if (200 == response.getStatusCode()) {
                        updateTable(asArrayOfStockData(response.getText()));
                    } else {
                        displayError("Couldn't retrieve JSON (" + response.getStatusText() + ")");
                    }
                }
            });
        } catch (RequestException e) {
            displayError("Couldn't retrieve JSON");
        }

    }

    protected void logoff() {
        String url = BASE_URL + "j_spring_security_logout";
        url = URL.encode(url);
        // Send request to server and catch any errors.
        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

        try {
            Request request = builder.sendRequest(null, new RequestCallback() {
                public void onError(Request request, Throwable exception) {
                    displayError("Couldn't retrieve JSON");
                }

                public void onResponseReceived(Request request, Response response) {
                    if (200 == response.getStatusCode()) {
                        redirect(BASE_URL + "spring_security_login");
                    } else {
                        displayError("Couldn't retrieve JSON (" + response.getStatusText() + ")");
                    }
                }
            });
        } catch (RequestException e) {
            displayError("Couldn't retrieve JSON");
        }

    }

    protected void getAllCircuitsByUser(String userName) {
        String url = SUBSYSTEM_JSON_URL + "getAllCircuitsByUser.do?";
        url = URL.encode(url);
        // Send request to server and catch any errors.
        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);

        try {
            builder.setHeader("Content-Type", "application/json");

            String requestDate = "{\"userName\":\"jxu\"}";

            Request request = builder.sendRequest(requestDate, new RequestCallback() {
                public void onError(Request request, Throwable exception) {
                    displayError("Couldn't retrieve JSON");
                }

                public void onResponseReceived(Request request, Response response) {
                    if (200 == response.getStatusCode()) {
                        updateUserCircuitTable(asArrayOfCircuitData(response.getText()));

                    } else {
                        displayError("Couldn't retrieve JSON (" + response.getStatusText() + ")");
                    }
                }
            });
        } catch (RequestException e) {
            displayError("Couldn't retrieve JSON");
        }

    }

    protected void getLoginUserName() {
        String url = USER_JSON_URL + "getLoginUser.do?";
        url = URL.encode(url);
        // Send request to server and catch any errors.
        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

        try {
            Request request = builder.sendRequest(null, new RequestCallback() {
                public void onError(Request request, Throwable exception) {

                }

                public void onResponseReceived(Request request, Response response) {
                    if (200 == response.getStatusCode()) {
                        loginUserName = response.getText();
                        nlnhtmlUsername = new InlineHTML(response.getText());

                    }
                }
            });
        } catch (RequestException e) {
            //do nothing
        }

    }

    protected void saveUpdateUser(String requestData) {
        String url = USER_JSON_URL + "saveUpdateUser.do?";
        url = URL.encode(url);
        // Send request to server and catch any errors.
        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);

        try {
            builder.setHeader("Content-Type", "application/json");

            System.out.println("RequestData-->" + requestData);
            Request request = builder.sendRequest(requestData, new RequestCallback() {
                public void onError(Request request, Throwable exception) {
                    displayError("Couldn't retrieve JSON");
                }

                public void onResponseReceived(Request request, Response response) {
                    if (200 == response.getStatusCode()) {
                        //reset
                        firstName.setText("");
                        lastName.setText("");
                        email.setText("");
                        passWord.setText("");
                        userName.setText("");

                        getAllUsers();
                        deckPanel.showWidget(1);
                    } else {
                        displayError("Couldn't retrieve JSON (" + response.getStatusText() + ")");
                    }
                }
            });
        } catch (RequestException e) {
            e.printStackTrace();
            displayError("Couldn't retrieve JSON");
        }

    }

    protected void deleteUser(String requestData) {
        String url = USER_JSON_URL + "deleteUser.do?";
        url = URL.encode(url);
        // Send request to server and catch any errors.
        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);

        try {
            builder.setHeader("Content-Type", "application/json");

            System.out.println("RequestData-->" + requestData);
            Request request = builder.sendRequest(requestData, new RequestCallback() {
                public void onError(Request request, Throwable exception) {
                    displayError("Couldn't retrieve JSON");
                }

                public void onResponseReceived(Request request, Response response) {
                    if (200 == response.getStatusCode()) {
                        getAllUsers();
                        deckPanel.showWidget(1);
                    } else {
                        displayError("Couldn't retrieve JSON (" + response.getStatusText() + ")");
                    }
                }
            });
        } catch (RequestException e) {
            e.printStackTrace();
            displayError("Couldn't retrieve JSON");
        }

    }

    protected void updateUserCircuitTable(JsArray<SmartWireUserCircuitsData> asArrayOfCircuitData) {

        for (int i = 0; i < asArrayOfCircuitData.length(); i++) {
            final SmartWireUserCircuitsData cdata = asArrayOfCircuitData.get(i);

            if (i == 0) {
                circuitsFlexTable.setText(i, 0, "Owner");
                circuitsFlexTable.setText(i, 1, "Circuit Name");
                circuitsFlexTable.setText(i, 2, " ");
                circuitsFlexTable.setText(i, 3, " ");
                circuitsFlexTable.setText(i + 1, 0, cdata.getUserName());
                circuitsFlexTable.setText(i + 1, 1, cdata.getCircuitName());

                Button viewCircuit = new Button("View");
                viewCircuit.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent event) {
                        new ViewCircuitPopup(cdata).show();
                    }
                });

                circuitsFlexTable.setWidget(i + 1, 2, viewCircuit);

                Button deleteCircuit = new Button("Delete");
                deleteCircuit.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent event) {
                        boolean confirm = Window.confirm("Are you sure?");
                        if (confirm) {
                            Window.alert("OK, Working on it, stay tune...");
                        }
                    }
                });

                circuitsFlexTable.setWidget(i + 1, 3, deleteCircuit);
            } else {
                circuitsFlexTable.setText(i + 1, 0, cdata.getUserName());
                circuitsFlexTable.setText(i + 1, 1, cdata.getCircuitName());

                Button viewCircuit = new Button("View");
                viewCircuit.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent event) {
                        new ViewCircuitPopup(cdata).show();

                    }
                });

                circuitsFlexTable.setWidget(i + 1, 2, viewCircuit);

                Button deleteCircuit = new Button("Delete");
                deleteCircuit.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent event) {
                        boolean confirm = Window.confirm("Are you sure?");
                        if (confirm) {
                            Window.alert("OK, Working on it, stay tune...");
                        }
                    }
                });

                circuitsFlexTable.setWidget(i + 1, 3, deleteCircuit);
            }

        }

        circuitsFlexTable.getRowFormatter().addStyleName(0, "flexTable");
    }

    protected void updateTable(JsArray<SmartWireUserData> asArrayOfStockData) {

        for (int i = 0; i < asArrayOfStockData.length(); i++) {
            //String username=asArrayOfStockData.get(i).getUsername();
            final SmartWireUserData userData = asArrayOfStockData.get(i);
            if (i == 0) {
                flexTable.setText(i, 0, "UserName");
                flexTable.setText(i, 1, "First Name");
                flexTable.setText(i, 2, "Last Name");
                flexTable.setText(i, 3, "Email");
                flexTable.setText(i, 4, "Position");
                flexTable.setText(i, 5, "Role");
                flexTable.setText(i, 6, "Active");
                flexTable.setText(i, 7, "Edit");
                //flexTable.setText(i, 8, "Delete");
                flexTable.setText(i + 1, 0, asArrayOfStockData.get(i).getUsername());
                flexTable.setText(i + 1, 1, asArrayOfStockData.get(i).getFistName());
                flexTable.setText(i + 1, 2, asArrayOfStockData.get(i).getLastName());
                flexTable.setText(i + 1, 3, asArrayOfStockData.get(i).getEmail());
                flexTable.setText(i + 1, 4, asArrayOfStockData.get(i).getPosition());
                flexTable.setText(i + 1, 5, asArrayOfStockData.get(i).getRole());
                flexTable.setText(i + 1, 6, asArrayOfStockData.get(i).isActive());

                editUser = new Button("Edit");
                //add event to this button
                editUser.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent event) {

                        new MyPopup(userData).show();

                    }
                });

                flexTable.setWidget(i + 1, 7, editUser);

                Button deleteButton = new Button("Delete");
                deleteButton.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent event) {
                        //delete user
                        boolean goahead = Window.confirm("Are you sure?");
                        if (goahead) {

                            deleteUser(getUserData(userData));
                        }

                    }
                });
                //flexTable.setWidget(i+1, 8, deleteButton);

            } else {
                flexTable.setText(i + 1, 0, asArrayOfStockData.get(i).getUsername());
                flexTable.setText(i + 1, 1, asArrayOfStockData.get(i).getFistName());
                flexTable.setText(i + 1, 2, asArrayOfStockData.get(i).getLastName());
                flexTable.setText(i + 1, 3, asArrayOfStockData.get(i).getEmail());
                flexTable.setText(i + 1, 4, asArrayOfStockData.get(i).getPosition());
                flexTable.setText(i + 1, 5, asArrayOfStockData.get(i).getRole());
                flexTable.setText(i + 1, 6, asArrayOfStockData.get(i).isActive());

                editUser = new Button("Edit");
                //add event to this button
                editUser.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent event) {

                        new MyPopup(userData).show();

                    }
                });

                flexTable.setWidget(i + 1, 7, editUser);

                Button deleteButton = new Button("Delete");
                deleteButton.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent event) {
                        //delete user
                        boolean goahead = Window.confirm("Are you sure?");
                        if (goahead) {

                            deleteUser(getUserData(userData));
                        }

                    }
                });
                //flexTable.setWidget(i+1, 8, deleteButton);
            }

        }

        flexTable.getRowFormatter().addStyleName(0, "flexTable");

    }

    private class ViewCircuitPopup extends PopupPanel {

        public ViewCircuitPopup(final SmartWireUserCircuitsData circuitData) {
            // PopupPanel's constructor takes 'auto-hide' as its boolean parameter.
            // If this is set, the panel closes itself automatically when the user
            // clicks outside of it.
            super(true);
            setAnimationEnabled(true);
            setPixelSize(700, 500);
            setPopupPosition(200, 100);
            setWidget(new Image("images/fusion_alarm.jpg"));
        }

    }

    private static class MyDialog extends DialogBox {

        public MyDialog(String message) {
            // Set the dialog box's caption.
            setText(message);
            setAnimationEnabled(true);
            setPixelSize(300, 300);
            setPopupPosition(200, 200);

            // DialogBox is a SimplePanel, so you have to set its widget property to
            // whatever you want its contents to be.
            Button ok = new Button("OK");
            ok.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {

                    redirect(GWT.getModuleBaseURL() + "/j_spring_security_logout");

                }
            });
            setWidget(ok);
        }
    }

    private class MyPopup extends PopupPanel {

        public MyPopup(final SmartWireUserData userData) {
            // PopupPanel's constructor takes 'auto-hide' as its boolean parameter.
            // If this is set, the panel closes itself automatically when the user
            // clicks outside of it.
            super(true);
            setAnimationEnabled(true);
            setPixelSize(600, 400);
            setPopupPosition(200, 100);

            // PopupPanel is a SimplePanel, so you have to set it's widget property to
            // whatever you want its contents to be.
            final VerticalPanel addUserFormContentPanel = new VerticalPanel();
            addUserFormContentPanel.setSpacing(3);
            setWidget(addUserFormContentPanel);
            //add content

            try {
                addUserFormContentPanel.add(new Label("User Name"));
                final TextBox userName4Update = new TextBox();
                userName4Update.setText(userData.getUsername());
                userName4Update.setName("userName");
                userName4Update.setReadOnly(true);
                addUserFormContentPanel.add(userName4Update);

                addUserFormContentPanel.add(new Label("First Name"));
                final TextBox firstName4Update = new TextBox();
                firstName4Update.setName("firstName");
                firstName4Update.setText(userData.getFistName());
                addUserFormContentPanel.add(firstName4Update);

                addUserFormContentPanel.add(new Label("Last Name"));
                final TextBox lastName4Update = new TextBox();
                lastName4Update.setName("lastName");
                lastName4Update.setText(userData.getLastName());
                addUserFormContentPanel.add(lastName4Update);

                addUserFormContentPanel.add(new Label("Email"));
                final TextBox email4Update = new TextBox();
                email4Update.setName("email");
                email4Update.setText(userData.getEmail());
                addUserFormContentPanel.add(email4Update);

                addUserFormContentPanel.add(new Label("User Role"));

                final ListBox userRole4Update = new ListBox();
                userRole4Update.addStyleName("demo-ListBox");
                userRole4Update.addItem("ADMIN");
                userRole4Update.addItem("USER");
                userRole4Update.setName("userRole");
                addUserFormContentPanel.add(userRole4Update);

                addUserFormContentPanel.add(new Label("Position"));

                final ListBox position4Update = new ListBox();
                position4Update.addStyleName("demo-ListBox");
                position4Update.addItem("EMPLOYEE");
                position4Update.addItem("MANAGER");
                position4Update.setName("position");
                addUserFormContentPanel.add(position4Update);

                addUserFormContentPanel.add(new Label("User Status"));

                final ListBox userStatus = new ListBox();
                userStatus.addStyleName("demo-ListBox");
                userStatus.addItem("ACTIVE");
                userStatus.addItem("DISABLE");
                userStatus.setName("userStatus");
                addUserFormContentPanel.add(userStatus);

                Button updateUserButton = new Button("Update User");
                updateUserButton.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent event) {

                        String userStaus = userStatus.getItemText(userStatus.getSelectedIndex());
                        boolean usatus = false;
                        if (userStaus != null && userStaus.equalsIgnoreCase("ACTIVE"))
                            usatus = true;

                        //update user
                        String requestData = "{" + "\"role\": \""
                                + userRole4Update.getItemText(userRole4Update.getSelectedIndex()) + "\","
                                + "\"firstName\": \"" + firstName4Update.getText() + "\"," + "\"lastName\": \""
                                + lastName4Update.getText() + "\"," + "\"email\": \"" + email4Update.getText()
                                + "\"," + "\"departmentName\": \"Engineer\"," + "\"id\": " + userData.getId() + ","
                                + "\"active\": " + usatus + "," + "\"password\": \"" + userData.getPassword()
                                + "\"," + "\"position\": \""
                                + position4Update.getItemText(position4Update.getSelectedIndex()) + "\","
                                + "\"userName\": \"" + userName4Update.getText() + "\"}";

                        hide();
                        saveUpdateUser(requestData);

                    }
                });

                addUserFormContentPanel.add(updateUserButton);

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

    /**
       * Convert the string of JSON into JavaScript object.*/

    private final native JsArray<SmartWireUserData> asArrayOfStockData(String json) /*-{
                                                                                    return eval(json);
                                                                                    }-*/;

    private final native JsArray<SmartWireUserCircuitsData> asArrayOfCircuitData(String json) /*-{
                                                                                              return eval(json);
                                                                                              }-*/;

    /**
     * If can't get JSON, display error message.
     * @param error
     */
    private void displayError(String error) {
        //errorMsgLabel.setText("Error: " + error);
        //errorMsgLabel.setVisible(true);
        Window.alert(error);
    }

    //redirect the browser to the given url
    public static native void redirect(String url)/*-{
                                                  $wnd.location = url;
                                                  }-*/;
}