com.mc.printer.model.panel.task.BuildGuideTask.java Source code

Java tutorial

Introduction

Here is the source code for com.mc.printer.model.panel.task.BuildGuideTask.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.panel.task;

import com.mc.printer.model.AutoPrinterApp;
import com.mc.printer.model.constants.Constants;
import com.mc.printer.model.layout.BaseFileChoose;
import com.mc.printer.model.layout.BaseMessage;
import com.mc.printer.model.layout.BaseTask;
import com.mc.printer.model.panel.GuideBean;
import com.mc.printer.model.panel.GuideCompBean;
import com.mc.printer.model.panel.guide.GuideWork;
import com.mc.printer.model.utils.XMLHelper;
import com.mc.printer.model.utils.ZipHelper;
import java.awt.Component;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import javax.xml.bind.JAXBException;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author woderchen@163.com
 */
public class BuildGuideTask extends BaseTask {

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

    @Override
    public Object doBackgrounp() {
        javax.swing.JTabbedPane tabs = AutoPrinterApp.getMainView().getMainWorkPanel();
        if (tabs != null) {
            if (tabs.getSelectedIndex() >= 0) {
                Component selectedcom = tabs.getSelectedComponent();
                if (selectedcom instanceof GuideWork) {
                    GuideWork selectPanel = (GuideWork) selectedcom;
                    GuideBean presentBeansForm = selectPanel.getBeansForm();

                    logger.debug("selected tab:" + selectPanel.getName());
                    //?
                    GuideBean beansForm;
                    try {
                        beansForm = presentBeansForm.clone();
                    } catch (CloneNotSupportedException ex) {
                        logger.error(ex.getMessage());
                        beansForm = presentBeansForm;
                    }

                    String backGroundImg = beansForm.getBackgoundImg();

                    //
                    Set<String> imgArray = new HashSet();

                    if (backGroundImg != null) {
                        File file = new File(backGroundImg);
                        if (file.exists() && file.isFile()) {
                            beansForm.setBackgoundImg(file.getName());
                            imgArray.add(backGroundImg);
                        } else {
                            beansForm.setBackgoundImg("");
                        }
                    } else {
                        beansForm.setBackgoundImg("");
                    }

                    HashMap<String, GuideCompBean> parameterMap = new HashMap<String, GuideCompBean>();
                    HashMap<String, GuideCompBean> parentParameterMap = selectPanel.getSavedForms();
                    deepCloneMap(parameterMap, parentParameterMap);

                    if (parameterMap.size() > 0) {
                        Set set = parameterMap.keySet();
                        //???
                        List<GuideCompBean> beans = new ArrayList();
                        Iterator it = set.iterator();
                        while (it.hasNext()) {
                            GuideCompBean com = parameterMap.get(it.next().toString());
                            beans.add(com);

                            String pressedIcon = com.getPressedIcon();
                            String icon = com.getIcon();
                            if (pressedIcon != null && !pressedIcon.equals("")) {

                                File pressfile = new File(pressedIcon);
                                if (pressfile.exists() && pressfile.isFile()) {
                                    imgArray.add(pressedIcon);
                                    com.setPressedIcon(pressfile.getName());
                                } else {
                                    com.setPressedIcon("");
                                }

                            }
                            if (icon != null && !icon.equals("")) {
                                File iconfile = new File(icon);
                                if (iconfile.exists() && iconfile.isFile()) {
                                    imgArray.add(icon);
                                    com.setIcon(iconfile.getName());
                                } else {
                                    com.setIcon("");
                                }
                            }
                        }
                        beansForm.setElements(beans);

                        BaseFileChoose fileChoose = new BaseFileChoose("?",
                                new String[] { Constants.MODEL_SUFFIX }, AutoPrinterApp.getMainFrame());
                        String selectedPath = fileChoose.showSaveDialog();
                        if (!selectedPath.trim().equals("")) {

                            //get formname
                            selectedPath = selectedPath + File.separator + beansForm.getGuideName();
                            //String time=DateHelper.format(new Date(), "yyyyMMddHHmmss");
                            String finalZip = selectedPath;
                            if (!selectedPath.endsWith("." + Constants.GUIDE_SUFFIX)) {
                                finalZip = selectedPath + "." + Constants.GUIDE_SUFFIX;
                            }

                            String tempDir = selectedPath + File.separator + Constants.GUIDE_TEMP_DIR;

                            String xmlPath = tempDir + File.separator + beansForm.getGuideName() + ".xml";
                            XMLHelper helper = new XMLHelper(xmlPath, beansForm);
                            try {
                                helper.write();
                            } catch (JAXBException ex) {
                                ex.printStackTrace();
                                logger.error(ex.getMessage());
                                return ex;
                            }

                            File paramFile = new File(xmlPath);
                            if (paramFile.isFile() && paramFile.exists()) {
                                try {
                                    logger.info("copy file and zip temp for:" + tempDir + ",total of attached file:"
                                            + imgArray.size());
                                    File _temp = new File(tempDir);
                                    for (String img : imgArray) {
                                        File imgFile = new File(img);
                                        logger.debug("copy image:" + imgFile.getName());
                                        FileUtils.copyFileToDirectory(imgFile, _temp);
                                    }

                                    ZipHelper.createZip(tempDir, finalZip);
                                    logger.info("zip file successfully.");
                                    return "??.\r\n" + finalZip;
                                } catch (IOException ex) {
                                    ex.printStackTrace();
                                    logger.error(ex.getMessage());
                                    return ex;
                                } finally {
                                    try {
                                        FileUtils.deleteDirectory(new File(selectedPath));
                                    } catch (IOException ex) {
                                        ex.printStackTrace();
                                        logger.error(ex.getMessage());
                                        return ex;
                                    }
                                }
                            } else {
                                logger.error("generator faild for:" + xmlPath);
                            }
                        }
                    } else {
                        return "??.";
                    }
                } else {
                    return "??????.";
                }
            } else {
                return "?.";
            }
        } else {
            return "?.";
        }
        return null;
    }

    @Override
    public void onSucceeded(Object object) {
        if (object != null) {
            if (object instanceof Exception) {
                ((Exception) object).printStackTrace();
                BaseMessage.ERROR("?:" + object);
            } else {
                if (object instanceof String) {
                    String path = object.toString();
                    if (path.trim().equals("")) {
                        BaseMessage.ERROR("?.");
                    } else {
                        BaseMessage.INFO(path);
                    }
                }

            }
        }

    }

    public void deepCloneMap(HashMap<String, GuideCompBean> dest, HashMap<String, GuideCompBean> src) {
        for (String str : src.keySet()) {
            GuideCompBean newbean;
            try {
                newbean = src.get(str).clone();
                dest.put(str, newbean);
            } catch (CloneNotSupportedException ex) {
                logger.error(ex.getMessage());
            }

        }
    }

}