Example usage for com.google.gwt.user.client.ui ListBox addClickListener

List of usage examples for com.google.gwt.user.client.ui ListBox addClickListener

Introduction

In this page you can find the example usage for com.google.gwt.user.client.ui ListBox addClickListener.

Prototype

@Deprecated
public void addClickListener(ClickListener listener) 

Source Link

Usage

From source file:javawars.client.pages.RobotPage.java

License:Open Source License

RobotPage(final Robot robot, final Workshop workshop) {
    super(robot.getName());
    this.robot = robot;
    this.workshop = workshop;

    /**/*  ww  w  .ja va2s.  c  om*/
     * The robot label - displayed in a side menu.
     */
    robotLabel = new Composite() {

        private VerticalPanel mainPanel = new VerticalPanel();

        {
            FlexTable table = new FlexTable();

            table.setWidget(0, 0, SpacerFactory.getHTMLSpacer(100, 0));
            table.setWidget(0, 1, SpacerFactory.getHTMLSpacer(100, 0));
            Label robotName = new Label(robot.getName());
            robotName.setStyleName("RobotName");
            table.setWidget(1, 0, robotName);
            table.setText(1, 1, "" + robot.getScore());
            table.setText(2, 0, "" + DateFormatter.extractDate(robot.getCreationDate()));
            table.setText(2, 1, "" + DateFormatter.extractDate(robot.getModificationDate()));

            mainPanel.add(table);
            table.setStyleName("Table");
            mainPanel.setStyleName("RobotLabel");
            initWidget(mainPanel);
        }
    };

    /**
     * Content of the statistics subpage
     */
    statisticsContent = new Composite() {

        private VerticalPanel mainPanel = new VerticalPanel();

        {

            mainPanel.add(new HTML("<h2>" + robot.getName() + "</h2>"));

            final FlexTable table = new FlexTable();
            SpacerFactory.adjustFlexTable(table, 120, 50, 50, 120, 50, 50);
            table.setText(1, 0, "Data utworzenia:");
            table.setText(1, 1, "" + DateFormatter.extractDateAndHour(robot.getCreationDate()));
            table.setText(1, 3, "Ostatnia modyfikacja: ");
            table.setText(1, 4, "" + DateFormatter.extractDateAndHour(robot.getModificationDate()));
            table.setText(2, 0, "Punkty: ");
            table.setText(2, 1, "" + robot.getScore());

            // temporary commenting out - the 'choose' button doesn't get back 
            // when another robot is chosen as a representant
            //if (robot.isRepresentant() == false) {
            table.setWidget(3, 0, SpacerFactory.getHTMLSpacer(1, 15));

            table.setText(4, 0, "Ustaw jako reprezentanta:");

            final Button representButton = new Button("reprezentant");
            table.setWidget(4, 1, representButton);

            final AsyncCallback representCallback = new AsyncCallback() {

                public void onFailure(Throwable arg0) {
                    representButton.setEnabled(true);
                }

                public void onSuccess(Object arg0) {
                    representButton.setEnabled(true);
                }
            };
            representButton.addClickListener(new ClickListener() {

                public void onClick(Widget arg0) {
                    representButton.setEnabled(false);
                    DataProvider.getInstance().getService().selectAsRepresentant(robot.getName(),
                            representCallback);
                }
            });

            //  }

            table.setWidget(5, 0, SpacerFactory.getHTMLSpacer(1, 15));

            table.setText(6, 0, "Usu robota:  ");

            final Button deleteButton = new Button("usu");
            table.setWidget(6, 1, deleteButton);

            final AsyncCallback deleteCallback = new AsyncCallback() {

                public void onFailure(Throwable arg0) {
                    Window.alert("Wystpi nieznany bd.");
                    deleteButton.setEnabled(true);
                }

                public void onSuccess(Object arg0) {
                    History.newItem("Workshop");
                    workshop.removeChildPage(getUrl());
                    deleteButton.setEnabled(true);
                }
            };
            deleteButton.addClickListener(new ClickListener() {

                public void onClick(Widget arg0) {
                    deleteButton.setEnabled(false);
                    DataProvider.getInstance().getService().deleteRobot(robot.getName(), deleteCallback);
                }
            });

            mainPanel.add(table);
            initWidget(mainPanel);
        }
    };

    /**
     * The composite which will be used as a content in the Edit subpage
     */
    editContent = new Composite() {

        private final HTML codeTextArea;
        private VerticalPanel mainPanel = new VerticalPanel();

        {

            codeTextArea = new HTML("<textarea id='codeTextArea' " + "class='codepress java' "
                    + "style='width: " + Math.max(300, Window.getClientWidth() - 250) + "px; " + "height: "
                    + Math.max(300, Window.getClientHeight() - 300) + "px; '" + ">" + robot.getCode()
                    + "</textarea>");

            mainPanel.add(codeTextArea);

            final FlowPanel editors = new FlowPanel();

            CheckBox useVi = new CheckBox("Edytor VI  ", false);
            useVi.addClickListener(new ClickListener() {

                private boolean enabled = false;

                public void onClick(Widget arg0) {
                    if (enabled == false) {
                        enabled = true;
                        DOM.getElementById("codeTextArea").setAttribute("onfocus", "editor(this);");
                    } else {
                        enabled = false;
                        DOM.getElementById("codeTextArea").setAttribute("onfocus", ";");
                    }
                }
            });
            editors.add(useVi);
            // editors.add(SpacerFactory.getHTMLSpacer(20, 20));

            final Button launchCodepress = new Button("Edytor CodePress");
            launchCodepress.addClickListener(new ClickListener() {

                public void onClick(Widget arg0) {
                    editors.setVisible(false);
                    initializeCodePress();
                }
            });

            editors.add(launchCodepress);
            mainPanel.add(editors);

            final Button save = new Button("Zapisz");
            final HTML compilerOutput = new HTML();

            final AsyncCallback<String> processOutput = new AsyncCallback<String>() {

                String startingCode;

                {
                    startingCode = robot.getCode();
                }

                public void onFailure(Throwable arg0) {
                    compilerOutput.setText("Bd poczenia z serwerem!");
                    save.setEnabled(true);
                }

                public void onSuccess(String output) {
                    compilerOutput.setHTML("<pre>" + output + "</pre>");
                    robot.setCode(startingCode);
                    save.setEnabled(true);
                }
            };

            save.addClickListener(new ClickListener() {

                public native String getRobotCode() /*-{
                                                    return $wnd.codeTextArea.getCode();
                                                    }-*/;

                public native String storeRobotCode() /*-{
                                                      return $doc.getElementById("codeTextArea_cp").value = $wnd.codeTextArea.getCode()
                                                      }-*/;

                public void onClick(Widget arg0) {
                    save.setEnabled(false);
                    if (editors.isVisible()) {
                        DataProvider.getInstance().getService().updateRobotCode(robot.getName(),
                                DOM.getElementById("codeTextArea").getPropertyString("value"), processOutput);
                    } else {
                        storeRobotCode();
                        DataProvider.getInstance().getService().updateRobotCode(robot.getName(), getRobotCode(),
                                processOutput);
                    }
                }
            });

            mainPanel.add(save);
            mainPanel.add(compilerOutput);

            initWidget(mainPanel);
        }
        boolean initialized = false;

        @Override
        protected void onLoad() {
            ;
        }

        public native void initializeCodePress() /*-{
                                                 $wnd.CodePress.run();
                                                 }-*/;
    };

    // content of the test subpage...

    testContent = new Composite() {

        private final ListBox robotList = new ListBox(true);
        private final VerticalPanel mainPanel = new VerticalPanel();
        private final VerticalPanel matchViewerPanel = new VerticalPanel();
        private final ListBox leaguesList = new ListBox();

        {
            HTML testHeadline = new HTML("<h2>Testuj:</h2>");
            Label testDescription = new Label("Wybierz z listy roboty, ktre "
                    + "chcesz przetestowa na jednej planszy; Ctrl+klik - zaznaczenie wicej ni jednego robota. "
                    + "Po dokonaniu wyboru nacini przycisk.");
            mainPanel.add(testHeadline);
            mainPanel.add(testDescription);
            mainPanel.add(SpacerFactory.getHTMLSpacer(10, 10));
            mainPanel.add(robotList);

            mainPanel.add(SpacerFactory.getHTMLSpacer(10, 10));
            HTML leagueHeadline = new HTML("<h3>Liga:</h3>");
            Label leagueDescription = new Label("Wybierz w jakiej lidze chcesz dokona testu:");

            mainPanel.add(leagueHeadline);
            mainPanel.add(leagueDescription);
            mainPanel.add(leaguesList);
            mainPanel.add(SpacerFactory.getHTMLSpacer(10, 10));

            final Button testButton = new Button("Testuj");

            final AsyncCallback<MatchReport> testCallback = new AsyncCallback<MatchReport>() {

                public void onFailure(Throwable arg0) {
                    Window.alert(("Wystpi nieznany bd."));
                    testButton.setEnabled(true);
                }

                public void onSuccess(MatchReport matchReport) {
                    try {
                        matchViewerPanel.clear();
                        MatchViewer matchViewer = new MatchViewerFast();
                        matchViewerPanel.add(matchViewer);
                        matchViewer.show(matchReport.getXmlDescription());
                        testButton.setEnabled(true);
                    } catch (Exception exception) {
                        Window.alert("Wystpi nieznany bd podczas wywietlania przebiegu pojedynku!");
                    }
                }
            };

            testButton.addClickListener(new ClickListener() {

                public void onClick(Widget arg0) {
                    if (robotList.getSelectedIndex() == -1) {
                        Window.alert("Prosz wybra przynajmniej jednego robota");
                        return;
                    }
                    testButton.setEnabled(false);

                    // adding all the selected items of the listbox to one list
                    List<String> robotNames = new LinkedList<String>();
                    int counter = 0;
                    while (counter < robotList.getItemCount()) {
                        if (robotList.isItemSelected(counter) == true) {
                            robotNames.add(robotList.getItemText(counter));
                        }
                        counter++;
                    }

                    // invoking the method on server
                    DataProvider.getInstance().getService().testRobots(leaguesList.getSelectedIndex(),
                            robotNames, testCallback);
                }
            });

            mainPanel.add(SpacerFactory.getHTMLSpacer(10, 10));
            mainPanel.add(testButton);
            mainPanel.add(matchViewerPanel);
            initWidget(mainPanel);
        } // a boolean that provides us a lock for the listbox
        boolean robotListLocked = false;
        boolean leaguesListLocked = false;

        /**
         * Refreshes the content of the listBox;
         */
        private void populateRobotListBox(List<Robot> list) {
            // if the list is locked, it means there already is an update 
            // in progress; we don't need to update it after such a short
            // time, so we quit updating completely.
            if (robotListLocked == false) {
                robotListLocked = true;
                robotList.clear();
                for (Robot r : list) {
                    robotList.addItem(r.getName());
                }
                robotListLocked = false;
            }
        }

        /**
         * Refreshes the content of the leaguesListBox;
         */
        private void populateLeaguesListBox(List<League> list) {
            // if the list is locked, it means there already is an update 
            // in progress; we don't need to update it after such a short
            // time, so we quit updating completely.
            if (leaguesListLocked == false) {
                leaguesListLocked = true;
                leaguesList.clear();
                for (League l : list) {
                    leaguesList.addItem(l.getName());
                }
                leaguesListLocked = false;
            }
        }

        private final AsyncCallback<List<League>> processLeagueCallback = new AsyncCallback<List<League>>() {

            public void onFailure(Throwable exception) {
                Window.alert("Error, couldn't update leagues list.");
            }

            public void onSuccess(List<League> list) {
                populateLeaguesListBox(list);
            }

        };

        @Override
        protected void onLoad() {
            populateRobotListBox(workshop.getRobotList());
            DataProvider.getInstance().getService().getLeagues(processLeagueCallback);
        }
    };

    // matchesContent

    matchesContent = new Composite() {

        private final List<MatchReport> matches = new ArrayList<MatchReport>();
        private VerticalPanel mainPanel = new VerticalPanel();

        {

            AsyncCallback<List<MatchReport>> processMatches = new AsyncCallback<List<MatchReport>>() {

                public void onFailure(Throwable arg0) {
                    Window.alert("Wystpi bd.");
                }

                public void onSuccess(final List<MatchReport> matchReports) {
                    mainPanel.clear();
                    mainPanel.add(new HTML("<h3>Ostatnie dziesi pojedynkw: </h3>"));
                    mainPanel.add(SpacerFactory.getHTMLSpacer(20, 20));
                    final ListBox matchesListBox = new ListBox();

                    matchesListBox.setVisibleItemCount(5);
                    for (MatchReport m : matchReports) {
                        matchesListBox.addItem(DateFormatter.extractDateAndHour(m.getDate()));
                    }

                    mainPanel.add(matchesListBox);
                    mainPanel.add(SpacerFactory.getHTMLSpacer(20, 20));

                    final VerticalPanel viewerPanel = new VerticalPanel();
                    mainPanel.add(viewerPanel);

                    matchesListBox.addClickListener(new ClickListener() {

                        public void onClick(Widget arg0) {
                            matchesListBox.setEnabled(false);
                            viewerPanel.clear();
                            viewerPanel.add(new Label("Prosz czeka"));
                            MatchViewer mv = new MatchViewerFast();
                            viewerPanel.add(mv);
                            mv.show(matchReports.get(matchesListBox.getSelectedIndex()).getXmlDescription());
                            viewerPanel.getWidget(0).removeFromParent();
                            matchesListBox.setEnabled(true);
                        }
                    });

                }
            };

            mainPanel.add(new Label("prosz czeka..."));

            DataProvider.getInstance().getService().fetchRobotMatchReports(robot.getName(), processMatches);

            initWidget(mainPanel);
        }
    };

    // adding the subpages...
    addChild(new Statistics());
    addChild(new Edit());
    addChild(new Test());
    addChild(new MatchReports());

}