Java tutorial
/* * 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.canvas.CanvasWork; import com.mc.printer.model.panel.ComponentBean; import com.mc.printer.model.panel.FormBeans; 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.Iterator; import java.util.List; import java.util.Set; import javax.swing.JOptionPane; 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 BuildModelTask extends BaseTask { private static Logger logger = LoggerFactory.getLogger(BuildModelTask.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 CanvasWork) { CanvasWork selectPanel = (CanvasWork) selectedcom; FormBeans beansForm = selectPanel.getBeansForm(); /**/ if (beansForm.getHeightcm() == 0 || beansForm.getWidthcm() == 0) { int res = BaseMessage.CONFIRM( "?\r\nA4."); if (res != JOptionPane.OK_OPTION) { return null; } } String imagePath = beansForm.getImgpath(); HashMap<String, ComponentBean> parameterMap = selectPanel.getSavedForms(); if (parameterMap.size() > 0) { Set set = parameterMap.keySet(); boolean foundKey = false; //??? List<ComponentBean> beans = new ArrayList(); Iterator it = set.iterator(); while (it.hasNext()) { ComponentBean com = parameterMap.get(it.next().toString()); beans.add(com); if (com.isIskey()) { foundKey = true; } } beansForm.setElements(beans); // // if (!foundKey) { // BaseMessage.ERROR("PRIMARY KEY."); // return null; // } 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.getFormname(); //String time=DateHelper.format(new Date(), "yyyyMMddHHmmss"); String finalZip = selectedPath; if (!selectedPath.endsWith("." + Constants.MODEL_SUFFIX)) { finalZip = selectedPath + "." + Constants.MODEL_SUFFIX; } String tempDir = selectedPath + File.separator + Constants.MODEL_TEMP_DIR; File imageFile = new File(imagePath); String xmlPath = tempDir + File.separator + imageFile.getName() + ".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 { FileUtils.copyFileToDirectory(imageFile, new File(tempDir)); ZipHelper.createZip(tempDir, finalZip); 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 { return "??."; } } else { return "??????."; } } else { return "."; } } else { return "."; } return null; } @Override public void onSucceeded(Object object) { if (object != null) { if (object instanceof Exception) { BaseMessage.ERROR("?:" + object); } else { if (object instanceof String) { String path = object.toString(); if (path.trim().equals("")) { BaseMessage.ERROR("?."); } else { BaseMessage.INFO(path); } } } } } }