com.mc.printer.model.StartTask.java Source code

Java tutorial

Introduction

Here is the source code for com.mc.printer.model.StartTask.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mc.printer.model;

import com.mc.printer.model.constants.ClientConstants;
import com.mc.printer.model.constants.Constants;
import com.mc.printer.model.entity.TbBranch;
import com.mc.printer.model.layout.BaseTask;
import com.mc.printer.model.layout.ws.ComResponse;
import com.mc.printer.model.panel.FileTask;
import com.mc.printer.model.panel.GuideBean;
import com.mc.printer.model.panel.client.frame.GuidePanel;
import com.mc.printer.model.panel.client.frame.OpenGuidePaperTask;
import com.mc.printer.model.utils.IPHelper;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;

/**
 *
 * @author 305027939
 */
public class StartTask extends BaseTask {

    private static final Logger logger = LoggerFactory.getLogger(StartTask.class);

    public int topRealHeight = 0;

    public int bottomRealHeight = 0;

    @Override
    public Object doBackgrounp() {

        //        File file = new File(ClientConstants.GUIDE_DOWNLOAD_DIR);
        //        if (file.exists() && file.isDirectory()) {
        //            try {
        //                logger.debug("clean download directory.");
        //                FileUtils.deleteDirectory(file);
        //            } catch (IOException ex) {
        //                logger.error("ex");
        //            }
        //        }
        try {

            TbBranch branch = new TbBranch();
            branch.setAddress(IPHelper.getCurrentIP());
            branch.setName(ClientConstants.LOCAL_NAME);
            branch.setStatus(1);
            ComResponse<TbBranch> response = restTemplate.exchange(
                    ClientConstants.WS_HTTP + ClientConstants.WS_ISREGISTER, HttpMethod.POST,
                    new HttpEntity<TbBranch>(branch), new ParameterizedTypeReference<ComResponse<TbBranch>>() {
                    }).getBody();

            if (response.getResponseStatus() == ComResponse.STATUS_OK) {
                logger.info("checked successfully. client is existing.");
                AutoPrinterApp.isRegister = true;
            } else {
                logger.error(response.getErrorMessage());
            }

        } catch (Exception e) {
            logger.error(e.getMessage());
        }

        return null;
    }

    @Override
    public void onSucceeded(Object object) {
        if (object instanceof Exception) {
            Exception e = (Exception) object;
            logger.error(e.getMessage());
        }
        if (AutoPrinterApp.isRegister) {
            logger.info("connect to remote server,please wait...");
        }
        new FileTask(FileTask.TYPE_DOWNLOAD, ClientConstants.GUIDE_DOWNLOAD_DIR, "") {

            @Override
            public void responseResult(String file) {

                if (AutoPrinterApp.isRegister) {
                    /*video*/
                    new FileTask(FileTask.TYPE_DOWNLOAD, ClientConstants.VIDEO_LIB, "video") {
                        @Override
                        public void responseResult(String file) {
                            logger.info("download video task completed.>" + ClientConstants.VIDEO_LIB);
                        }
                    }.execute();
                } else {
                    logger.debug("client has not register, can't fetch the video.");
                }

                final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                final int maxWidth = (int) screenSize.getWidth();
                final int maxHeight = (int) Math.round(screenSize.getHeight() / 3); //??1/3
                logger.info("load application completed. try to open guide and form...");

                new BaseTask() {
                    @Override
                    public Object doBackgrounp() {
                        new OpenGuidePaperTask(Constants.GUIDE_BOTTOM_FILE) {
                            @Override
                            public void onSucceed(Object object, HashMap<String, Integer> statusMap) {

                                if (object == null || object instanceof Exception) {
                                    topRealHeight = maxHeight;
                                } else if (object instanceof GuideBean) {
                                    GuideBean guideBean = (GuideBean) object;
                                    GuidePanel panel = new GuidePanel(null, guideBean, maxWidth,
                                            guideBean.getHeight()) {

                                        @Override
                                        public GuidePanel parentGuideWindow() {
                                            return null;
                                        }

                                        @Override
                                        public GuidePanel targetWindowAfterAuth() {
                                            return null;
                                        }
                                    };
                                    panel.enableButtonByRemote(statusMap);
                                    AutoPrinterApp.getClientMain().setBottomUI(panel);
                                    topRealHeight = panel.getCurrentGuideBean().getHeight();
                                }

                            }
                        }.run();

                        new OpenGuidePaperTask(Constants.GUIDE_TOP_FILE) {
                            @Override
                            public void onSucceed(Object object, HashMap<String, Integer> statusMap) {
                                if (object == null || object instanceof Exception) {
                                    bottomRealHeight = maxHeight;
                                } else if (object instanceof GuideBean) {

                                    GuideBean guideBean = (GuideBean) object;
                                    GuidePanel panel = new GuidePanel(null, guideBean, maxWidth,
                                            guideBean.getHeight()) {
                                        @Override
                                        public GuidePanel parentGuideWindow() {
                                            return null;
                                        }

                                        @Override
                                        public GuidePanel targetWindowAfterAuth() {
                                            return null;
                                        }
                                    };
                                    panel.enableButtonByRemote(statusMap);
                                    AutoPrinterApp.getClientMain().setTopUI(panel);
                                    bottomRealHeight = panel.getCurrentGuideBean().getHeight();
                                }
                            }
                        }.run();

                        return null;
                    }

                    @Override
                    public void onSucceeded(Object object) {
                        final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                        int maxHeight = screenSize.height - topRealHeight - bottomRealHeight;

                        final int finalHeight = maxHeight > screenSize.height ? screenSize.height : maxHeight;

                        new OpenGuidePaperTask(Constants.GUIDE_MAIN_FILE) {
                            @Override
                            public void onSucceed(Object object, HashMap<String, Integer> statusMap) {
                                if (object == null || object instanceof Exception) {
                                } else if (object instanceof GuideBean) {
                                    GuideBean guideBean = (GuideBean) object;
                                    GuidePanel panel = new GuidePanel(null, guideBean, maxWidth,
                                            finalHeight == 0 ? guideBean.getHeight() : finalHeight) {

                                        @Override
                                        public GuidePanel parentGuideWindow() {
                                            return null;
                                        }

                                        @Override
                                        public GuidePanel targetWindowAfterAuth() {
                                            return null;
                                        }
                                    };
                                    panel.enableButtonByRemote(statusMap);
                                    AutoPrinterApp.getClientMain().setWorkUI(panel);
                                }
                            }

                        }.execute();

                    }

                }.execute();

            }
        }.execute();

    }

}