tv.dyndns.kishibe.qmaclone.client.PanelRatioReport.java Source code

Java tutorial

Introduction

Here is the source code for tv.dyndns.kishibe.qmaclone.client.PanelRatioReport.java

Source

//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.qmaclone.client;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import tv.dyndns.kishibe.qmaclone.client.packet.PacketProblem;
import tv.dyndns.kishibe.qmaclone.client.report.ProblemReportUi;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.RepeatingCommand;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PanelRatioReport extends VerticalPanel implements ClickHandler {
    private static final Logger logger = Logger.getLogger(PanelRatioReport.class.getName());
    private static final int MAX_PROBLEMS_PER_PAGE = 100;
    private final Button buttonAdd = new Button("??", this);
    private final Button buttonRemove = new Button("??", this);
    private final Button buttonUpdate = new Button("", this);
    private final TextBox textBoxProblemNumber = new TextBox();
    private final SimplePanel panelGrid = new SimplePanel();
    private boolean enabled = true;
    private final RepeatingCommand commandCheckProblemId = new RepeatingCommand() {
        @Override
        public boolean execute() {
            final boolean ok = checkProblemId();
            buttonAdd.setEnabled(ok && enabled);
            buttonRemove.setEnabled(ok && enabled);
            return isAttached();
        }
    };

    private void setEnable(boolean enabled) {
        this.enabled = enabled;
        buttonAdd.setEnabled(enabled);
        buttonRemove.setEnabled(enabled);
        buttonUpdate.setEnabled(enabled);
    }

    public PanelRatioReport() {
        setWidth("800px");
        setHorizontalAlignment(ALIGN_CENTER);

        add(new HTML(
                "??????????????????<br/>????????????????<br/>?????????????<br/>??????????"));

        HorizontalPanel idPanel = new HorizontalPanel();
        idPanel.setVerticalAlignment(ALIGN_MIDDLE);

        textBoxProblemNumber.setWidth("240px");
        textBoxProblemNumber.addFocusHandler(focusHandler);

        idPanel.add(textBoxProblemNumber);
        idPanel.add(buttonAdd);
        idPanel.add(buttonRemove);
        add(idPanel);

        add(buttonUpdate);

        add(panelGrid);
    }

    private final FocusHandler focusHandler = new FocusHandler() {
        @Override
        public void onFocus(FocusEvent event) {
            textBoxProblemNumber.selectAll();
        }
    };

    private void update() {
        final int userCode = UserData.get().getUserCode();
        Service.Util.getInstance().getUserProblemReport(userCode, callbackGetProblemList);
    }

    private final AsyncCallback<List<PacketProblem>> callbackGetProblemList = new AsyncCallback<List<PacketProblem>>() {
        @Override
        public void onSuccess(List<PacketProblem> result) {
            if (result == null) {
                logger.log(Level.WARNING, "???????");
                return;
            }

            panelGrid.setWidget(new ProblemReportUi(result, false, true, MAX_PROBLEMS_PER_PAGE));
            setEnable(true);
        }

        @Override
        public void onFailure(Throwable caught) {
            logger.log(Level.WARNING, "????????", caught);
        }
    };

    @Override
    public void onClick(ClickEvent event) {
        final Object sender = event.getSource();
        final String message = "???????????????????????";
        if (sender == buttonAdd) {
            // ??
            try {
                final int problemId = Integer.parseInt(textBoxProblemNumber.getText());
                final int userCode = UserData.get().getUserCode();
                final List<Integer> problemIds = new ArrayList<Integer>();
                problemIds.add(problemId);
                Service.Util.getInstance().addProblemIdsToReport(userCode, problemIds,
                        callbackAddProblemIdsToReport);
            } catch (Exception e) {
                logger.log(Level.WARNING, message);
            }

        } else if (sender == buttonRemove) {
            // ??
            try {
                final int problemID = Integer.parseInt(textBoxProblemNumber.getText());
                final int userCode = UserData.get().getUserCode();
                Service.Util.getInstance().removeProblemIDFromReport(userCode, problemID,
                        callbackRemoveProblemIdFromReport);
            } catch (Exception e) {
                logger.log(Level.WARNING, message);
            }

        } else if (sender == buttonUpdate) {
            // 
            setEnable(false);
            update();
        }
    }

    private final AsyncCallback<Void> callbackAddProblemIdsToReport = new AsyncCallback<Void>() {
        @Override
        public void onSuccess(Void result) {
            textBoxProblemNumber.setText("??????");
        }

        @Override
        public void onFailure(Throwable caught) {
            logger.log(Level.WARNING, "???????", caught);
        }
    };
    private final AsyncCallback<Void> callbackRemoveProblemIdFromReport = new AsyncCallback<Void>() {
        @Override
        public void onSuccess(Void result) {
            textBoxProblemNumber.setText("??????");
        }

        @Override
        public void onFailure(Throwable caught) {
            logger.log(Level.WARNING, "???????", caught);
        }
    };

    private boolean checkProblemId() {
        try {
            Integer.parseInt(textBoxProblemNumber.getText());
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    protected void onLoad() {
        super.onLoad();
        Scheduler.get().scheduleFixedDelay(commandCheckProblemId, 1000);
    }
}