├── .gitignore ├── src └── main │ └── java │ └── com │ └── lc │ ├── design │ ├── vo │ │ ├── DataItemVO.java │ │ ├── DataVO.java │ │ ├── SysConfigVO.java │ │ ├── ColorVO.java │ │ ├── FontVO.java │ │ ├── PrintContext.java │ │ ├── FontColorVO.java │ │ ├── OccupyVO.java │ │ ├── imp │ │ │ └── DefaultDataItemImpVO.java │ │ └── DesignVO.java │ ├── constant │ │ ├── TPDConsts.java │ │ ├── Direction.java │ │ └── OutType.java │ ├── filter │ │ ├── JPGFileFilter.java │ │ └── TPDFileFilter.java │ ├── component │ │ ├── LCFont.java │ │ ├── OccupyFont.java │ │ ├── OccupyComponent.java │ │ └── JFontChooser.java │ ├── action │ │ ├── DeleteBtnListener.java │ │ ├── OccupyKeyListener.java │ │ ├── ArrowBinding.java │ │ ├── ConfigDlgApplyMouseListener.java │ │ ├── OccupyDlgApplyMouseListener.java │ │ ├── OccupyDlgTypeMouseListener.java │ │ ├── OccupyEditMouseListener.java │ │ ├── OccupyBtnListener.java │ │ ├── ConfigBtnListener.java │ │ ├── OccupyDlgFontMouseListener.java │ │ ├── ImageBtnListener.java │ │ ├── OccupyMouseListener.java │ │ ├── ImportBtnListener.java │ │ ├── PreviewBtnListener.java │ │ └── ExportBtnListener.java │ ├── unit │ │ ├── LogUtil.java │ │ ├── DataItemUtil.java │ │ ├── ValidateHelper.java │ │ ├── FontUtil.java │ │ ├── JsonUtils.java │ │ ├── HtmlUtil.java │ │ ├── DateTimeUtils.java │ │ └── JsonTool.java │ ├── PrintDesignFrame.java │ └── panel │ │ ├── DesignPanel.java │ │ ├── MenuPanel.java │ │ ├── ConfigDialog.java │ │ ├── OccupyDialog.java │ │ └── DesignContainer.java │ ├── PrintDesignMain.java │ └── PrintHtmlMain.java ├── pom.xml └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/vo/DataItemVO.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.vo; 2 | 3 | public interface DataItemVO { 4 | public String getItem(PrintContext vo, OccupyVO occupy); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/vo/DataVO.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.vo; 2 | 3 | public class DataVO { 4 | private DataItemVO[] datas; 5 | 6 | public DataVO(DataItemVO... inDatas) { 7 | datas = inDatas; 8 | } 9 | 10 | public DataItemVO[] getData() { 11 | return datas; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/constant/TPDConsts.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.constant; 2 | 3 | import java.io.File; 4 | 5 | public class TPDConsts { 6 | public static int BORDER = 0; 7 | 8 | public static String F_NAME = ".lc"; 9 | 10 | public static String KEY_FONT = "tempFont"; 11 | 12 | public static File DEAULT_DIR = new File("C:\\Users\\Administrator\\Desktop\\test"); 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/vo/SysConfigVO.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.vo; 2 | 3 | public class SysConfigVO { 4 | private int fillX = 8; 5 | private int fillY = 8; 6 | 7 | public int getFillX() { 8 | return fillX; 9 | } 10 | 11 | public void setFillX(int fillX) { 12 | this.fillX = fillX; 13 | } 14 | 15 | public int getFillY() { 16 | return fillY; 17 | } 18 | 19 | public void setFillY(int fillY) { 20 | this.fillY = fillY; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/filter/JPGFileFilter.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.filter; 2 | 3 | import java.io.File; 4 | 5 | import javax.swing.filechooser.FileFilter; 6 | 7 | public class JPGFileFilter extends FileFilter { 8 | public String getDescription() { 9 | return "*.jpg"; 10 | } 11 | 12 | public boolean accept(File file) { 13 | String name = file.getName(); 14 | return file.isDirectory() || name.toLowerCase().endsWith(".jpg");// 仅显示目录和.jpg文件 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/component/LCFont.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.component; 2 | 3 | import java.awt.Color; 4 | import java.awt.Font; 5 | 6 | public class LCFont { 7 | private Font font; 8 | private Color color; 9 | 10 | public Font getFont() { 11 | return font; 12 | } 13 | 14 | public void setFont(Font font) { 15 | this.font = font; 16 | } 17 | 18 | public Color getColor() { 19 | return color; 20 | } 21 | 22 | public void setColor(Color color) { 23 | this.color = color; 24 | } 25 | } -------------------------------------------------------------------------------- /src/main/java/com/lc/PrintDesignMain.java: -------------------------------------------------------------------------------- 1 | package com.lc; 2 | 3 | import java.awt.EventQueue; 4 | 5 | import com.lc.design.PrintDesignFrame; 6 | 7 | public class PrintDesignMain { 8 | 9 | public static void main(String[] args) { 10 | EventQueue.invokeLater(new Runnable() { 11 | public void run() { 12 | try { 13 | PrintDesignFrame frame = new PrintDesignFrame(); 14 | frame.setVisible(true); 15 | } 16 | catch (Exception e) { 17 | e.printStackTrace(); 18 | } 19 | } 20 | }); 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/com/lc/design/action/DeleteBtnListener.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.action; 2 | 3 | import java.awt.event.MouseAdapter; 4 | import java.awt.event.MouseEvent; 5 | 6 | import com.lc.design.panel.DesignContainer; 7 | 8 | public class DeleteBtnListener extends MouseAdapter { 9 | private DesignContainer container; 10 | 11 | public DeleteBtnListener(DesignContainer container) { 12 | this.container = container; 13 | } 14 | 15 | public void mouseClicked(MouseEvent e) { 16 | container.removeNowTxt(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/action/OccupyKeyListener.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.action; 2 | 3 | import java.awt.event.KeyAdapter; 4 | import java.awt.event.KeyEvent; 5 | 6 | import com.lc.design.panel.DesignContainer; 7 | 8 | public class OccupyKeyListener extends KeyAdapter { 9 | private DesignContainer designContainer; 10 | 11 | public OccupyKeyListener(DesignContainer container) { 12 | designContainer = container; 13 | } 14 | 15 | public void keyPressed(final KeyEvent e) { 16 | designContainer.moveTxt(e); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /src/main/java/com/lc/design/filter/TPDFileFilter.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.filter; 2 | 3 | import java.io.File; 4 | 5 | import javax.swing.filechooser.FileFilter; 6 | 7 | import com.lc.design.constant.TPDConsts; 8 | 9 | public class TPDFileFilter extends FileFilter { 10 | public String getDescription() { 11 | return TPDConsts.F_NAME; 12 | } 13 | 14 | public boolean accept(File file) { 15 | String name = file.getName(); 16 | return file.isDirectory() || name.toLowerCase().endsWith(TPDConsts.F_NAME);// 仅显示目录和.jpg文件 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/unit/LogUtil.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.unit; 2 | 3 | import java.awt.Component; 4 | 5 | import javax.swing.JOptionPane; 6 | 7 | public class LogUtil { 8 | public static void disError(Component parentComponent, String msg) { 9 | JOptionPane.showMessageDialog(parentComponent, msg, "异常提示", JOptionPane.ERROR_MESSAGE); 10 | } 11 | 12 | public static void disError(Component parentComponent, Exception e) { 13 | JOptionPane.showMessageDialog(parentComponent, e.getMessage(), "异常提示", JOptionPane.ERROR_MESSAGE); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/vo/ColorVO.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.vo; 2 | 3 | public class ColorVO { 4 | private int red; 5 | private int green; 6 | private int blue; 7 | 8 | public int getRed() { 9 | return red; 10 | } 11 | 12 | public void setRed(int red) { 13 | this.red = red; 14 | } 15 | 16 | public int getGreen() { 17 | return green; 18 | } 19 | 20 | public void setGreen(int green) { 21 | this.green = green; 22 | } 23 | 24 | public int getBlue() { 25 | return blue; 26 | } 27 | 28 | public void setBlue(int blue) { 29 | this.blue = blue; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/vo/FontVO.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.vo; 2 | 3 | public class FontVO { 4 | private String name; 5 | private int style; 6 | private int size; 7 | 8 | public String getName() { 9 | return name; 10 | } 11 | 12 | public void setName(String name) { 13 | this.name = name; 14 | } 15 | 16 | public int getStyle() { 17 | return style; 18 | } 19 | 20 | public void setStyle(int style) { 21 | this.style = style; 22 | } 23 | 24 | public int getSize() { 25 | return size; 26 | } 27 | 28 | public void setSize(int size) { 29 | this.size = size; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/unit/DataItemUtil.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.unit; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.lc.design.vo.DataItemVO; 7 | import com.lc.design.vo.imp.DefaultDataItemImpVO; 8 | 9 | public class DataItemUtil { 10 | public static DataItemVO[] build(String... datas) { 11 | if (datas == null || datas.length == 0) { 12 | return null; 13 | } 14 | List list = new ArrayList(); 15 | for (String data : datas) { 16 | list.add(new DefaultDataItemImpVO(data)); 17 | } 18 | return list.toArray(new DataItemVO[list.size()]); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/action/ArrowBinding.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.action; 2 | 3 | import java.awt.event.ActionEvent; 4 | 5 | import javax.swing.AbstractAction; 6 | 7 | import com.lc.design.panel.DesignContainer; 8 | 9 | public class ArrowBinding extends AbstractAction { 10 | 11 | private DesignContainer container; 12 | 13 | public ArrowBinding(String text, DesignContainer container) { 14 | super(text); 15 | putValue(ACTION_COMMAND_KEY, text); 16 | this.container = container; 17 | } 18 | 19 | public void actionPerformed(ActionEvent e) { 20 | String actionCommand = e.getActionCommand(); 21 | System.out.println("Key Binding: " + actionCommand); 22 | container.moveTxt(e); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/vo/PrintContext.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.vo; 2 | 3 | public class PrintContext { 4 | private SysConfigVO config; 5 | 6 | private int imgWidth; 7 | private int imgHeight; 8 | 9 | public SysConfigVO getConfig() { 10 | return config; 11 | } 12 | 13 | public void setConfig(SysConfigVO config) { 14 | this.config = config; 15 | } 16 | 17 | public int getImgWidth() { 18 | return imgWidth; 19 | } 20 | 21 | public void setImgWidth(int imgWidth) { 22 | this.imgWidth = imgWidth; 23 | } 24 | 25 | public int getImgHeight() { 26 | return imgHeight; 27 | } 28 | 29 | public void setImgHeight(int imgHeight) { 30 | this.imgHeight = imgHeight; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/action/ConfigDlgApplyMouseListener.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.action; 2 | 3 | import java.awt.event.MouseAdapter; 4 | import java.awt.event.MouseEvent; 5 | 6 | import com.lc.design.panel.ConfigDialog; 7 | import com.lc.design.unit.LogUtil; 8 | 9 | public class ConfigDlgApplyMouseListener extends MouseAdapter { 10 | private ConfigDialog dialog; 11 | 12 | public ConfigDlgApplyMouseListener(ConfigDialog dialog) { 13 | this.dialog = dialog; 14 | } 15 | 16 | public void mouseReleased(MouseEvent e) { 17 | } 18 | 19 | public void mouseClicked(MouseEvent e) { 20 | try { 21 | dialog.setSuccess(); 22 | dialog.setVisible(false); 23 | } 24 | catch (Exception ex) { 25 | LogUtil.disError(dialog, ex); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /src/main/java/com/lc/design/action/OccupyDlgApplyMouseListener.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.action; 2 | 3 | import java.awt.event.MouseAdapter; 4 | import java.awt.event.MouseEvent; 5 | 6 | import com.lc.design.panel.OccupyDialog; 7 | import com.lc.design.unit.LogUtil; 8 | 9 | public class OccupyDlgApplyMouseListener extends MouseAdapter { 10 | private OccupyDialog occupyDialog; 11 | 12 | public OccupyDlgApplyMouseListener(OccupyDialog dialog) { 13 | occupyDialog = dialog; 14 | } 15 | 16 | public void mouseReleased(MouseEvent e) { 17 | } 18 | 19 | public void mouseClicked(MouseEvent e) { 20 | try { 21 | occupyDialog.checkData(); 22 | occupyDialog.refreshOccupy(); 23 | } 24 | catch (Exception e1) { 25 | LogUtil.disError(occupyDialog, e1); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /src/main/java/com/lc/design/component/OccupyFont.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.component; 2 | 3 | import javax.swing.JButton; 4 | 5 | import com.lc.design.unit.FontUtil; 6 | import com.lc.design.vo.FontColorVO; 7 | 8 | /** 9 | * 字体样式控件 10 | * 11 | * @author liubq 12 | */ 13 | public class OccupyFont extends JButton { 14 | private FontColorVO inVO; 15 | 16 | public OccupyFont(String name) { 17 | super(name); 18 | 19 | } 20 | 21 | public void refresh(LCFont font) { 22 | inVO = FontUtil.convert(font); 23 | } 24 | 25 | public FontColorVO getData() { 26 | return inVO; 27 | } 28 | 29 | public void setData(FontColorVO vo) { 30 | if (vo != null) { 31 | this.inVO = vo.clone(); 32 | } 33 | else { 34 | this.inVO = null; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/PrintDesignFrame.java: -------------------------------------------------------------------------------- 1 | package com.lc.design; 2 | 3 | import javax.swing.JFrame; 4 | 5 | import com.lc.design.panel.DesignContainer; 6 | 7 | /** 8 | * 打印设计 9 | * 10 | * @author liubq 11 | */ 12 | public class PrintDesignFrame extends JFrame { 13 | 14 | // 构造 15 | private DesignContainer designContainer; 16 | 17 | /** 18 | * 初始化 19 | */ 20 | public PrintDesignFrame() { 21 | this.setTitle("套打设计器"); 22 | this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 23 | this.setLocationRelativeTo(null); 24 | this.designContainer = new DesignContainer(this); 25 | this.add(designContainer); 26 | this.setLocation(400, 150); 27 | this.setSize(800, 600); 28 | this.setResizable(false); 29 | } 30 | 31 | public DesignContainer getDesignContainer() { 32 | return designContainer; 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /src/main/java/com/lc/design/constant/Direction.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.constant; 2 | 3 | import java.awt.event.KeyEvent; 4 | 5 | import javax.swing.KeyStroke; 6 | 7 | public enum Direction { 8 | 9 | UP("Up", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0)), DOWN("Down", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0)), LEFT("Left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0)), RIGHT("Right", 10 | KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0)); 11 | 12 | Direction(String text, KeyStroke keyStroke) { 13 | this.text = text; 14 | this.keyStroke = keyStroke; 15 | } 16 | 17 | private String text; 18 | private KeyStroke keyStroke; 19 | 20 | public String getText() { 21 | return text; 22 | } 23 | 24 | public KeyStroke getKeyStroke() { 25 | return keyStroke; 26 | } 27 | 28 | @Override 29 | public String toString() { 30 | return text; 31 | } 32 | } -------------------------------------------------------------------------------- /src/main/java/com/lc/design/action/OccupyDlgTypeMouseListener.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.action; 2 | 3 | import java.awt.event.ActionEvent; 4 | 5 | import javax.swing.JComboBox; 6 | 7 | import com.lc.design.panel.OccupyDialog; 8 | import com.lc.design.unit.LogUtil; 9 | 10 | public class OccupyDlgTypeMouseListener implements java.awt.event.ActionListener { 11 | private OccupyDialog occupyDialog; 12 | 13 | public OccupyDlgTypeMouseListener(OccupyDialog dialog) { 14 | this.occupyDialog = dialog; 15 | } 16 | 17 | @Override 18 | public void actionPerformed(ActionEvent e) { 19 | JComboBox outType = (JComboBox) e.getSource(); 20 | String selType = (String) outType.getSelectedItem(); 21 | try { 22 | occupyDialog.refreshOutType(selType); 23 | } 24 | catch (Exception e1) { 25 | LogUtil.disError(occupyDialog, e1); 26 | } 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/action/OccupyEditMouseListener.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.action; 2 | 3 | import java.awt.event.MouseAdapter; 4 | import java.awt.event.MouseEvent; 5 | 6 | import com.lc.design.panel.DesignContainer; 7 | import com.lc.design.panel.OccupyDialog; 8 | import com.lc.design.unit.LogUtil; 9 | import com.lc.design.vo.OccupyVO; 10 | 11 | public class OccupyEditMouseListener extends MouseAdapter { 12 | 13 | private DesignContainer designContainer; 14 | private OccupyDialog dialog; 15 | 16 | public OccupyEditMouseListener(DesignContainer container) { 17 | designContainer = container; 18 | dialog = new OccupyDialog(designContainer); 19 | } 20 | 21 | public void mouseClicked(MouseEvent e) { 22 | OccupyVO vo = designContainer.getNowOccupyVO(); 23 | if (vo != null) { 24 | dialog.show(vo); 25 | } 26 | else { 27 | LogUtil.disError(designContainer, "请选中需要修改的的占位符!"); 28 | } 29 | designContainer.requestFocus(); 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /src/main/java/com/lc/design/vo/FontColorVO.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.vo; 2 | 3 | public class FontColorVO { 4 | private FontVO font; 5 | private ColorVO color; 6 | 7 | public FontVO getFont() { 8 | return font; 9 | } 10 | 11 | public void setFont(FontVO font) { 12 | this.font = font; 13 | } 14 | 15 | public ColorVO getColor() { 16 | return color; 17 | } 18 | 19 | public void setColor(ColorVO color) { 20 | this.color = color; 21 | } 22 | 23 | public FontColorVO clone() { 24 | FontVO fvo = new FontVO(); 25 | fvo.setName(this.getFont().getName()); 26 | fvo.setStyle(this.getFont().getStyle()); 27 | fvo.setSize(this.getFont().getSize()); 28 | ColorVO cvo = new ColorVO(); 29 | cvo.setRed(this.getColor().getRed()); 30 | cvo.setGreen(this.getColor().getGreen()); 31 | cvo.setBlue(this.getColor().getBlue()); 32 | FontColorVO vo = new FontColorVO(); 33 | vo.setColor(cvo); 34 | vo.setFont(fvo); 35 | return vo; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/action/OccupyBtnListener.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.action; 2 | 3 | import java.awt.event.MouseAdapter; 4 | import java.awt.event.MouseEvent; 5 | 6 | import com.lc.design.panel.DesignContainer; 7 | import com.lc.design.vo.OccupyVO; 8 | 9 | public class OccupyBtnListener extends MouseAdapter { 10 | private DesignContainer main; 11 | 12 | public static int index = 0; 13 | 14 | public OccupyBtnListener(DesignContainer design) { 15 | main = design; 16 | } 17 | 18 | public void mouseClicked(MouseEvent e) { 19 | OccupyVO vo = new OccupyVO(); 20 | String name = "name" + index++; 21 | vo.setName("#" + name + "#"); 22 | vo.setWidth(80); 23 | vo.setHeight(18); 24 | OccupyVO nowVO = main.getNowOccupyVO(); 25 | if (nowVO != null) { 26 | vo.setPositionX(nowVO.getPositionX() + nowVO.getWidth() + 10); 27 | vo.setPositionY(nowVO.getPositionY() + nowVO.getHeight() + 10); 28 | } 29 | main.addNewOccupy(vo); 30 | main.requestFocus(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/action/ConfigBtnListener.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.action; 2 | 3 | import java.awt.event.MouseAdapter; 4 | import java.awt.event.MouseEvent; 5 | 6 | import com.lc.design.panel.ConfigDialog; 7 | import com.lc.design.panel.DesignContainer; 8 | import com.lc.design.unit.LogUtil; 9 | import com.lc.design.vo.SysConfigVO; 10 | 11 | public class ConfigBtnListener extends MouseAdapter { 12 | private DesignContainer container; 13 | private ConfigDialog dialog; 14 | 15 | public ConfigBtnListener(DesignContainer container) { 16 | this.container = container; 17 | dialog = new ConfigDialog(container); 18 | } 19 | 20 | public void mouseClicked(MouseEvent e) { 21 | SysConfigVO vo = container.getConfigVO(); 22 | try { 23 | dialog.show(vo); 24 | SysConfigVO configVO = dialog.getData(); 25 | if (configVO != null) { 26 | container.setConfigVO(configVO); 27 | } 28 | } 29 | catch (Exception e1) { 30 | LogUtil.disError(container, e1); 31 | } 32 | container.requestFocus(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/action/OccupyDlgFontMouseListener.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.action; 2 | 3 | import java.awt.event.MouseAdapter; 4 | import java.awt.event.MouseEvent; 5 | 6 | import com.lc.design.component.JFontChooser; 7 | import com.lc.design.component.LCFont; 8 | import com.lc.design.component.OccupyFont; 9 | import com.lc.design.panel.OccupyDialog; 10 | import com.lc.design.unit.FontUtil; 11 | 12 | public class OccupyDlgFontMouseListener extends MouseAdapter { 13 | private OccupyDialog occupyDialog; 14 | private OccupyFont btnObj; 15 | 16 | public OccupyDlgFontMouseListener(OccupyDialog dialog, OccupyFont btnObj) { 17 | this.occupyDialog = dialog; 18 | this.btnObj = btnObj; 19 | } 20 | 21 | public void mouseReleased(MouseEvent e) { 22 | } 23 | 24 | public void mouseClicked(MouseEvent e) { 25 | try { 26 | occupyDialog.setVisible(false); 27 | JFontChooser one = new JFontChooser(); 28 | LCFont lhf = one.showDialog(occupyDialog, FontUtil.convert(btnObj.getData())); 29 | if (lhf != null) { 30 | System.out.println(lhf.getFont()); 31 | btnObj.refresh(lhf); 32 | } 33 | } 34 | finally { 35 | occupyDialog.setVisible(true); 36 | } 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/component/OccupyComponent.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.component; 2 | 3 | import java.awt.Color; 4 | 5 | import javax.swing.JTextArea; 6 | 7 | import com.lc.design.unit.FontUtil; 8 | import com.lc.design.vo.OccupyVO; 9 | 10 | /** 11 | * 占位符对象 12 | * 13 | * @author liubq 14 | */ 15 | public class OccupyComponent extends JTextArea { 16 | 17 | // 数据 18 | private OccupyVO data; 19 | 20 | /** 21 | * 安装信息初始化占位符 22 | * 23 | * @param vo 24 | */ 25 | public void initInfo(OccupyVO vo) { 26 | data = vo; 27 | resetInfo(vo); 28 | 29 | } 30 | 31 | /** 32 | * 安装信息初始化占位符 33 | * 34 | * @param vo 35 | */ 36 | private void resetInfo(OccupyVO vo) { 37 | if (vo != null) { 38 | this.setText(vo.getName()); 39 | this.setBounds(vo.getPositionX(), vo.getPositionY(), vo.getWidth(), vo.getHeight()); 40 | this.setBackground(Color.GRAY); 41 | if (vo.getFc() != null) { 42 | this.setFont(FontUtil.convert(vo.getFc().getFont())); 43 | this.setForeground(FontUtil.convert(vo.getFc().getColor())); 44 | } 45 | this.setEditable(false); 46 | } 47 | } 48 | 49 | /** 50 | * 取得信息 51 | * 52 | * @return 53 | */ 54 | public OccupyVO getInfo() { 55 | data.setPositionX(this.getX()); 56 | data.setPositionY(this.getY()); 57 | return data; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/action/ImageBtnListener.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.action; 2 | 3 | import java.awt.event.MouseAdapter; 4 | import java.awt.event.MouseEvent; 5 | import java.io.File; 6 | import java.nio.file.Files; 7 | import java.nio.file.Paths; 8 | 9 | import javax.swing.ImageIcon; 10 | import javax.swing.JFileChooser; 11 | 12 | import com.lc.design.constant.TPDConsts; 13 | import com.lc.design.filter.JPGFileFilter; 14 | import com.lc.design.panel.DesignContainer; 15 | import com.lc.design.unit.LogUtil; 16 | 17 | public class ImageBtnListener extends MouseAdapter { 18 | private DesignContainer container; 19 | 20 | public ImageBtnListener(DesignContainer container) { 21 | this.container = container; 22 | } 23 | 24 | public void mouseClicked(MouseEvent e) { 25 | JFileChooser chooser = new JFileChooser(); 26 | chooser.setFileFilter(new JPGFileFilter()); 27 | chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 28 | chooser.setMultiSelectionEnabled(false); 29 | chooser.setCurrentDirectory(TPDConsts.DEAULT_DIR); 30 | if (chooser.showOpenDialog(container) == JFileChooser.APPROVE_OPTION) { 31 | File file = chooser.getSelectedFile(); 32 | try { 33 | container.resetImg(new ImageIcon(Files.readAllBytes(Paths.get(file.getAbsolutePath())))); 34 | } 35 | catch (Exception ex) { 36 | LogUtil.disError(container, ex); 37 | } 38 | 39 | } 40 | container.requestFocus(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/panel/DesignPanel.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.panel; 2 | 3 | import java.awt.Color; 4 | import java.awt.Dimension; 5 | import java.awt.Graphics; 6 | 7 | import javax.swing.BorderFactory; 8 | import javax.swing.ImageIcon; 9 | import javax.swing.JPanel; 10 | 11 | import com.lc.design.constant.TPDConsts; 12 | 13 | /** 14 | * 菜单 15 | * 16 | * @author liubq 17 | */ 18 | public class DesignPanel extends JPanel { 19 | 20 | // 图片 21 | private ImageIcon imageicon; 22 | 23 | // 容器 24 | @SuppressWarnings("unused") 25 | private DesignContainer dcesignContainer; 26 | 27 | /** 28 | * 初始化 29 | * 30 | * @param container 31 | */ 32 | public DesignPanel(DesignContainer container) { 33 | dcesignContainer = container; 34 | this.setBorder(BorderFactory.createLineBorder(Color.orange, TPDConsts.BORDER)); 35 | this.setLayout(null); 36 | } 37 | 38 | public void paintComponent(Graphics g) { 39 | super.paintComponent(g); 40 | if (imageicon != null) { 41 | imageicon.paintIcon(this, g, 0, 0); 42 | } 43 | } 44 | 45 | public Dimension getPreferredSize() { 46 | if (imageicon != null) { 47 | return new Dimension(imageicon.getIconWidth(), imageicon.getIconHeight()); 48 | } 49 | return super.getPreferredSize(); 50 | 51 | } 52 | 53 | /** 54 | * 重绘底片 55 | * 56 | * @param image 57 | */ 58 | public void rebuild(ImageIcon image) { 59 | imageicon = image; 60 | this.repaint(); 61 | } 62 | 63 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | com.zy 4 | SuitPrintDesign 5 | 0.0.1-SNAPSHOT 6 | 7 | 8 | junit 9 | junit 10 | 4.10 11 | 12 | 13 | org.slf4j 14 | slf4j-api 15 | 1.7.1 16 | 17 | 18 | ch.qos.logback 19 | logback-core 20 | 1.0.7 21 | 22 | 23 | ch.qos.logback 24 | logback-classic 25 | 1.0.7 26 | 27 | 28 | org.codehaus.jackson 29 | jackson-mapper-asl 30 | 1.9.9 31 | 32 | 33 | org.codehaus.jackson 34 | jackson-core-asl 35 | 1.9.9 36 | 37 | 38 | com.google.guava 39 | guava 40 | 19.0 41 | 42 | 43 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/action/OccupyMouseListener.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.action; 2 | 3 | import java.awt.event.MouseAdapter; 4 | import java.awt.event.MouseEvent; 5 | 6 | import com.lc.design.component.OccupyComponent; 7 | import com.lc.design.constant.TPDConsts; 8 | import com.lc.design.panel.DesignContainer; 9 | 10 | public class OccupyMouseListener extends MouseAdapter { 11 | 12 | private DesignContainer designContainer; 13 | 14 | public OccupyMouseListener(DesignContainer container) { 15 | designContainer = container; 16 | } 17 | 18 | // 这两组x和y为鼠标点下时在屏幕的位置和拖动时所在的位置 19 | int newX, newY, oldX, oldY; 20 | 21 | // 这两个坐标为组件当前的坐标 22 | int startX, startY; 23 | 24 | @Override 25 | public void mousePressed(MouseEvent e) { 26 | // 此为得到事件源组件 27 | OccupyComponent cp = (OccupyComponent) e.getSource(); 28 | // 当鼠标点下的时候记录组件当前的坐标与鼠标当前在屏幕的位置 29 | startX = cp.getX(); 30 | startY = cp.getY(); 31 | oldX = e.getXOnScreen(); 32 | oldY = e.getYOnScreen(); 33 | designContainer.selectPress(cp); 34 | } 35 | 36 | @Override 37 | 38 | public void mouseDragged(MouseEvent e) { 39 | OccupyComponent cp = (OccupyComponent) e.getSource(); 40 | // 拖动的时候记录新坐标 41 | newX = e.getXOnScreen(); 42 | newY = e.getYOnScreen(); 43 | // 设置bounds,将点下时记录的组件开始坐标与鼠标拖动的距离相加 44 | int x = startX + (newX - oldX); 45 | x = x < TPDConsts.BORDER ? TPDConsts.BORDER : x; 46 | int y = startY + (newY - oldY); 47 | y = y < TPDConsts.BORDER ? TPDConsts.BORDER : y; 48 | cp.setBounds(x, y, cp.getWidth(), cp.getHeight()); 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /src/main/java/com/lc/design/action/ImportBtnListener.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.action; 2 | 3 | import java.awt.event.MouseAdapter; 4 | import java.awt.event.MouseEvent; 5 | import java.io.File; 6 | import java.nio.file.Files; 7 | import java.nio.file.Paths; 8 | 9 | import javax.swing.JFileChooser; 10 | 11 | import com.lc.design.constant.TPDConsts; 12 | import com.lc.design.filter.TPDFileFilter; 13 | import com.lc.design.panel.DesignContainer; 14 | import com.lc.design.unit.JsonUtils; 15 | import com.lc.design.unit.LogUtil; 16 | import com.lc.design.vo.DesignVO; 17 | 18 | public class ImportBtnListener extends MouseAdapter { 19 | private DesignContainer container; 20 | 21 | public ImportBtnListener(DesignContainer container) { 22 | this.container = container; 23 | } 24 | 25 | public void mouseClicked(MouseEvent e) { 26 | JFileChooser chooser = new JFileChooser(); 27 | chooser.setFileFilter(new TPDFileFilter()); 28 | chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 29 | chooser.setMultiSelectionEnabled(false); 30 | chooser.setCurrentDirectory(TPDConsts.DEAULT_DIR); 31 | if (chooser.showOpenDialog(container) == JFileChooser.APPROVE_OPTION) { 32 | File file = chooser.getSelectedFile(); 33 | try { 34 | String json = new String(Files.readAllBytes(Paths.get(file.getAbsolutePath()))); 35 | DesignVO design = JsonUtils.convertToObject(json, DesignVO.class); 36 | container.resetDesignVO(design); 37 | } 38 | catch (Exception ex) { 39 | LogUtil.disError(container, ex); 40 | } 41 | } 42 | container.requestFocus(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/constant/OutType.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.constant; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public enum OutType { 7 | 8 | ONE(1, "单个"), LIST(2, "循环"); 9 | 10 | OutType(Integer code, String name) { 11 | this.code = code; 12 | this.name = name; 13 | } 14 | 15 | private Integer code; 16 | private String name; 17 | 18 | public Integer getCode() { 19 | return code; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | @Override 27 | public String toString() { 28 | return name; 29 | } 30 | 31 | public static OutType getByCode(Integer code) { 32 | for (OutType type : OutType.values()) { 33 | if (type.getCode().equals(code)) { 34 | return type; 35 | } 36 | } 37 | return null; 38 | } 39 | 40 | public static OutType getByName(String name) { 41 | for (OutType type : OutType.values()) { 42 | if (type.getName().equals(name)) { 43 | return type; 44 | } 45 | } 46 | return null; 47 | } 48 | 49 | public static String[] getListName() { 50 | List list = new ArrayList(); 51 | for (OutType value : OutType.values()) { 52 | list.add(value.getName()); 53 | } 54 | return list.toArray(new String[list.size()]); 55 | } 56 | 57 | public boolean equals(OutType obj) { 58 | if (obj == null) { 59 | return false; 60 | } 61 | if (obj == this) { 62 | return true; 63 | } 64 | if (obj.getCode().equals(this.getCode())) { 65 | return true; 66 | } 67 | return false; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/action/PreviewBtnListener.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.action; 2 | 3 | import java.awt.event.MouseAdapter; 4 | import java.awt.event.MouseEvent; 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | import com.lc.design.constant.OutType; 9 | import com.lc.design.panel.DesignContainer; 10 | import com.lc.design.unit.DataItemUtil; 11 | import com.lc.design.unit.HtmlUtil; 12 | import com.lc.design.unit.JsonUtils; 13 | import com.lc.design.unit.LogUtil; 14 | import com.lc.design.vo.DataVO; 15 | import com.lc.design.vo.DesignVO; 16 | import com.lc.design.vo.OccupyVO; 17 | 18 | public class PreviewBtnListener extends MouseAdapter { 19 | private DesignContainer container; 20 | 21 | public PreviewBtnListener(DesignContainer container) { 22 | this.container = container; 23 | } 24 | 25 | public void mouseClicked(MouseEvent e) { 26 | DesignVO vo = container.getDesignVO(); 27 | try { 28 | Map dataMap = new HashMap(); 29 | for (OccupyVO oVO : vo.getDatas()) { 30 | if (OutType.LIST.getCode().equals(oVO.getOutType())) { 31 | dataMap.put(oVO.getName(), 32 | new DataVO(DataItemUtil.build(oVO.getName(), oVO.getName(), oVO.getName(), oVO.getName()))); 33 | } 34 | else { 35 | dataMap.put(oVO.getName(), new DataVO(DataItemUtil.build(oVO.getName()))); 36 | } 37 | } 38 | String html = HtmlUtil.buildHtml(JsonUtils.convertToString(vo), dataMap); 39 | HtmlUtil.preview(html); 40 | } 41 | catch (Exception e1) { 42 | LogUtil.disError(container, e1); 43 | } 44 | container.requestFocus(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SuitPrintDesign 2 | 简单的套打打印设计器,主要应用在Web系统中使用 3 | 4 | ## 套打涉及到几个关键的部分 5 | * 1.打印设计,根据底板设计答应模板; 6 | * 2.取得打印模板,填充答应数据; 7 | * 3.打印; 8 | 9 | ## 描述 10 | 整个过程中打印设计最为关键,现在web段做的比较好的设计工具, 11 | 有LODOP和jatoolsPrinter这两个工具,其它工具太弱,基本没有什么功能。 12 | 目前这两个工具都是收费的。费用不低。 13 | 14 | 我使用java开发了一个设计器,通过设计器吧步骤1 完成,把设计模板保存到后台。 15 | 16 | 前台想要打印,发送Ajax请求到后台,后台调用模板,补充数据,然后结果给前台。 17 | 前台直接打印。 18 | 19 | 没有使用web的端设计器直接打印。 20 | 问题 21 | 1. 设计程序和打印程序,相互独立。 22 | 2. 最终效果不是很完美,最终打印的网页毕竟是通过模板的位置和像素计算结果,总是差一点点 23 | 3. 前台可以采用iframe方式,这样看不到弹出的网页可以直接答应。 24 | 25 | ```java 26 | 27 | 发送请求的打印代码 使用jquery 的ajax 28 | function printHtml() { 29 | var url = getRootPath()+"/census/test/manage.action"; 30 | var formData = $("#censusAuthPrintForm").serializeArray(); 31 | jQuery.ajax({ 32 | url : url, 33 | type : 'POST', 34 | cache: false, 35 | data : formData, 36 | success : function(data) { 37 | alert(data); 38 | $(document.body).append(iframe); 39 | var iframe = $(""); 40 | var doc = null; 41 | $(body).append(iframe); 42 | doc = iframe[0].contentWindow.document; 43 | doc.write(data); 44 | doc.close(); 45 | iframe[0].contentWindow.focus(); 46 | iframe[0].contentWindow.print(); 47 | iframe.remove(); 48 | }, 49 | error : function() { 50 | alert('error') 51 | } 52 | }); 53 | } 54 | 55 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/action/ExportBtnListener.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.action; 2 | 3 | import java.awt.event.MouseAdapter; 4 | import java.awt.event.MouseEvent; 5 | import java.io.File; 6 | import java.io.IOException; 7 | import java.nio.file.Files; 8 | import java.nio.file.Paths; 9 | 10 | import javax.swing.JFileChooser; 11 | 12 | import com.lc.design.constant.TPDConsts; 13 | import com.lc.design.panel.DesignContainer; 14 | import com.lc.design.unit.JsonTool; 15 | import com.lc.design.unit.JsonUtils; 16 | import com.lc.design.unit.LogUtil; 17 | import com.lc.design.vo.DesignVO; 18 | 19 | public class ExportBtnListener extends MouseAdapter { 20 | private DesignContainer container; 21 | 22 | public ExportBtnListener(DesignContainer container) { 23 | this.container = container; 24 | } 25 | 26 | public void mouseClicked(MouseEvent e) { 27 | DesignVO vo = container.getDesignVO(); 28 | if (vo != null) { 29 | JFileChooser chooser = new JFileChooser(); 30 | chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 31 | chooser.setCurrentDirectory(TPDConsts.DEAULT_DIR); 32 | if (chooser.showOpenDialog(container) == JFileChooser.APPROVE_OPTION) { 33 | File dir = chooser.getSelectedFile(); 34 | String data = JsonUtils.convertToString(vo); 35 | data = JsonTool.formatJson(data); 36 | File file = new File(dir.getAbsolutePath() + File.separator + System.currentTimeMillis() + TPDConsts.F_NAME); 37 | try { 38 | Files.write(Paths.get(file.getAbsolutePath()), data.getBytes()); 39 | } 40 | catch (IOException e1) { 41 | LogUtil.disError(container, e1); 42 | } 43 | } 44 | 45 | } 46 | container.requestFocus(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/unit/ValidateHelper.java: -------------------------------------------------------------------------------- 1 | 2 | package com.lc.design.unit; 3 | 4 | import java.util.regex.Matcher; 5 | import java.util.regex.Pattern; 6 | 7 | public class ValidateHelper { 8 | // 9 | public static boolean isNull(Object obj) { 10 | if (obj == null) { 11 | return true; 12 | } 13 | if (obj instanceof String) { 14 | String temp = (String) obj; 15 | if ("".equals(temp.trim())) { 16 | return true; 17 | } 18 | } 19 | return false; 20 | } 21 | 22 | public static boolean isNullStr(Object obj) { 23 | if (obj == null) { 24 | return true; 25 | } 26 | if (obj instanceof String) { 27 | String temp = (String) obj; 28 | if ("null".equals(temp.trim().toLowerCase())) { 29 | return true; 30 | } 31 | } 32 | return false; 33 | } 34 | 35 | // 36 | public static boolean isIntNumber(String str) { 37 | 38 | if ((str == null) || "".equals(str.trim())) { 39 | return false; 40 | } 41 | String rep = "^-?\\d+$"; 42 | Pattern p = Pattern.compile(rep); 43 | Matcher m = p.matcher(str.trim()); 44 | return m.matches(); 45 | } 46 | 47 | // 48 | public static boolean isDoubleNumber(String str) { 49 | 50 | if ((str == null) || "".equals(str.trim())) { 51 | return false; 52 | } 53 | String rep = "^([1-9]\\d*\\.\\d*|0\\.\\d+|0)$"; 54 | Pattern p = Pattern.compile(rep); 55 | Matcher m = p.matcher(str.trim()); 56 | return m.matches(); 57 | } 58 | 59 | public static boolean isValidNumber(String str) { 60 | 61 | if ((str == null) || "".equals(str.trim())) { 62 | return false; 63 | } 64 | String rep = "^[0-9]*\\.?[0-9]+$"; 65 | Pattern p = Pattern.compile(rep); 66 | Matcher m = p.matcher(str.trim()); 67 | return m.matches(); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/unit/FontUtil.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.unit; 2 | 3 | import java.awt.Color; 4 | import java.awt.Font; 5 | 6 | import com.lc.design.component.LCFont; 7 | import com.lc.design.vo.ColorVO; 8 | import com.lc.design.vo.FontVO; 9 | import com.lc.design.vo.FontColorVO; 10 | 11 | public class FontUtil { 12 | public static FontVO convert(Font font) { 13 | if (font == null) { 14 | return null; 15 | } 16 | FontVO vo = new FontVO(); 17 | vo.setName(font.getName()); 18 | vo.setStyle(font.getStyle()); 19 | vo.setSize(font.getSize()); 20 | return vo; 21 | } 22 | 23 | public static Font convert(FontVO vo) { 24 | if (vo == null) { 25 | return null; 26 | } 27 | return new Font(vo.getName(), vo.getStyle(), vo.getSize()); 28 | } 29 | 30 | public static ColorVO convert(Color color) { 31 | if (color == null) { 32 | return null; 33 | } 34 | ColorVO vo = new ColorVO(); 35 | vo.setRed(color.getRed()); 36 | vo.setGreen(color.getGreen()); 37 | vo.setBlue(color.getBlue()); 38 | return vo; 39 | } 40 | 41 | public static Color convert(ColorVO vo) { 42 | if (vo == null) { 43 | return null; 44 | } 45 | return new Color(vo.getRed(), vo.getGreen(), vo.getBlue()); 46 | } 47 | 48 | public static FontColorVO convert(LCFont font) { 49 | if (font == null) { 50 | return null; 51 | } 52 | FontColorVO vo = new FontColorVO(); 53 | vo.setFont(convert(font.getFont())); 54 | vo.setColor(convert(font.getColor())); 55 | return vo; 56 | } 57 | 58 | public static LCFont convert(FontColorVO vo) { 59 | if (vo == null) { 60 | return null; 61 | } 62 | LCFont font = new LCFont(); 63 | font.setFont(convert(vo.getFont())); 64 | font.setColor(convert(vo.getColor())); 65 | return font; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/lc/PrintHtmlMain.java: -------------------------------------------------------------------------------- 1 | package com.lc; 2 | 3 | import java.io.File; 4 | import java.nio.file.Files; 5 | import java.nio.file.Paths; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | import javax.swing.JFileChooser; 10 | 11 | import com.lc.design.constant.TPDConsts; 12 | import com.lc.design.filter.TPDFileFilter; 13 | import com.lc.design.unit.DataItemUtil; 14 | import com.lc.design.unit.HtmlUtil; 15 | import com.lc.design.vo.DataVO; 16 | 17 | /** 18 | * 模板答应的Web代码生成 19 | * 20 | * @author liubq 21 | */ 22 | public class PrintHtmlMain { 23 | public static void main(String[] args) { 24 | 25 | try { 26 | 27 | JFileChooser chooser = new JFileChooser(); 28 | chooser.setFileFilter(new TPDFileFilter()); 29 | chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 30 | chooser.setMultiSelectionEnabled(false); 31 | chooser.setCurrentDirectory(TPDConsts.DEAULT_DIR); 32 | if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { 33 | File file = chooser.getSelectedFile(); 34 | Map dataMap = new HashMap(); 35 | dataMap.put("#name0#", new DataVO(DataItemUtil.build("12"))); 36 | dataMap.put("#name1#", new DataVO(DataItemUtil.build("34"))); 37 | dataMap.put("#name2#", new DataVO(DataItemUtil.build(" "))); 38 | dataMap.put("#name3#", new DataVO(DataItemUtil.build(" "))); 39 | dataMap.put("#name4#", new DataVO(DataItemUtil.build(" "))); 40 | dataMap.put("#name5#", new DataVO(DataItemUtil.build(" "))); 41 | String json = new String(Files.readAllBytes(Paths.get(file.getAbsolutePath()))); 42 | String html = HtmlUtil.buildIframe(json, dataMap); 43 | System.out.println(html); 44 | } 45 | 46 | } 47 | catch (Exception ex) { 48 | ex.printStackTrace(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/vo/OccupyVO.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.vo; 2 | 3 | import com.lc.design.constant.OutType; 4 | import com.lc.design.constant.TPDConsts; 5 | 6 | public class OccupyVO { 7 | private String name; 8 | private int width; 9 | private int height; 10 | private Integer outType = OutType.ONE.getCode(); 11 | private int intervalLen; 12 | private int positionX = TPDConsts.BORDER; 13 | private int positionY = TPDConsts.BORDER; 14 | private FontColorVO fc; 15 | 16 | public String getName() { 17 | return name; 18 | } 19 | 20 | public void setName(String name) { 21 | this.name = name; 22 | } 23 | 24 | public int getWidth() { 25 | return width; 26 | } 27 | 28 | public void setWidth(int width) { 29 | this.width = width; 30 | } 31 | 32 | public int getHeight() { 33 | return height; 34 | } 35 | 36 | public void setHeight(int height) { 37 | this.height = height; 38 | } 39 | 40 | public Integer getOutType() { 41 | return outType; 42 | } 43 | 44 | public void setOutType(Integer outType) { 45 | this.outType = outType; 46 | } 47 | 48 | public int getIntervalLen() { 49 | return intervalLen; 50 | } 51 | 52 | public void setIntervalLen(int intervalLen) { 53 | this.intervalLen = intervalLen; 54 | } 55 | 56 | public int getPositionX() { 57 | return positionX; 58 | } 59 | 60 | public void setPositionX(int positionX) { 61 | this.positionX = positionX; 62 | } 63 | 64 | public int getPositionY() { 65 | return positionY; 66 | } 67 | 68 | public void setPositionY(int positionY) { 69 | this.positionY = positionY; 70 | } 71 | 72 | public FontColorVO getFc() { 73 | return fc; 74 | } 75 | 76 | public void setFc(FontColorVO fc) { 77 | this.fc = fc; 78 | } 79 | 80 | public OccupyVO clone() { 81 | OccupyVO vo = new OccupyVO(); 82 | vo.setName(name); 83 | vo.setWidth(width); 84 | vo.setHeight(height); 85 | vo.setOutType(outType); 86 | vo.setIntervalLen(intervalLen); 87 | vo.setPositionX(positionX); 88 | vo.setPositionY(positionY); 89 | vo.setFc(fc.clone()); 90 | return vo; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/panel/MenuPanel.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.panel; 2 | 3 | import java.awt.Color; 4 | import java.awt.FlowLayout; 5 | 6 | import javax.swing.BorderFactory; 7 | import javax.swing.JButton; 8 | import javax.swing.JPanel; 9 | 10 | import com.lc.design.action.ConfigBtnListener; 11 | import com.lc.design.action.DeleteBtnListener; 12 | import com.lc.design.action.ExportBtnListener; 13 | import com.lc.design.action.ImageBtnListener; 14 | import com.lc.design.action.ImportBtnListener; 15 | import com.lc.design.action.OccupyBtnListener; 16 | import com.lc.design.action.OccupyEditMouseListener; 17 | import com.lc.design.action.PreviewBtnListener; 18 | import com.lc.design.constant.TPDConsts; 19 | 20 | /** 21 | * 菜单 22 | * 23 | * @author liubq 24 | */ 25 | public class MenuPanel extends JPanel { 26 | 27 | // 容器 28 | private DesignContainer designContainer; 29 | 30 | /** 31 | * 初始化 32 | * 33 | * @param container 34 | */ 35 | public MenuPanel(DesignContainer container) { 36 | this.designContainer = container; 37 | this.setBorder(BorderFactory.createLineBorder(Color.orange, TPDConsts.BORDER)); 38 | this.setLayout(new FlowLayout(FlowLayout.RIGHT, 2, 1)); 39 | JButton backgroundBtn = new JButton("底片"); 40 | this.add(backgroundBtn); 41 | JButton occupyBtn = new JButton("插入"); 42 | this.add(occupyBtn); 43 | JButton occupyEditBtn = new JButton("编辑"); 44 | this.add(occupyEditBtn); 45 | JButton deleteBtn = new JButton("删除"); 46 | this.add(deleteBtn); 47 | JButton previewBtn = new JButton("预览"); 48 | this.add(previewBtn); 49 | JButton configBtn = new JButton("配置"); 50 | this.add(configBtn); 51 | JButton importBtn = new JButton("导入"); 52 | this.add(importBtn); 53 | JButton exportBtn = new JButton("导出"); 54 | this.add(exportBtn); 55 | backgroundBtn.addMouseListener(new ImageBtnListener(designContainer)); 56 | occupyBtn.addMouseListener(new OccupyBtnListener(designContainer)); 57 | importBtn.addMouseListener(new ImportBtnListener(designContainer)); 58 | exportBtn.addMouseListener(new ExportBtnListener(designContainer)); 59 | occupyEditBtn.addMouseListener(new OccupyEditMouseListener(designContainer)); 60 | previewBtn.addMouseListener(new PreviewBtnListener(designContainer)); 61 | deleteBtn.addMouseListener(new DeleteBtnListener(designContainer)); 62 | configBtn.addMouseListener(new ConfigBtnListener(designContainer)); 63 | } 64 | 65 | } -------------------------------------------------------------------------------- /src/main/java/com/lc/design/vo/imp/DefaultDataItemImpVO.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.vo.imp; 2 | 3 | import java.awt.Font; 4 | 5 | import com.lc.design.vo.DataItemVO; 6 | import com.lc.design.vo.FontColorVO; 7 | import com.lc.design.vo.OccupyVO; 8 | import com.lc.design.vo.PrintContext; 9 | 10 | public class DefaultDataItemImpVO implements DataItemVO { 11 | 12 | private String data; 13 | 14 | public DefaultDataItemImpVO(String inData) { 15 | this.data = inData; 16 | } 17 | 18 | @Override 19 | public String getItem(PrintContext cont, OccupyVO vo) { 20 | StringBuilder div = new StringBuilder(); 21 | // position:fixed;left:425px;top:46px;width:400px;height:207px; 22 | div.append("
"); 31 | if (vo.getFc() != null) { 32 | FontColorVO f = vo.getFc(); 33 | div.append(""); 58 | div.append(data); 59 | div.append(""); 60 | } 61 | else { 62 | div.append(data); 63 | } 64 | 65 | div.append("
"); 66 | return div.toString(); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/vo/DesignVO.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.vo; 2 | 3 | import java.util.List; 4 | 5 | public class DesignVO { 6 | private int width; 7 | private int height; 8 | private int designWidth; 9 | private int designHeight; 10 | 11 | private int imgWidth; 12 | private int imgHeight; 13 | private int menuWidth; 14 | private int menuHeight; 15 | 16 | private List datas; 17 | 18 | private SysConfigVO configVO = new SysConfigVO(); 19 | 20 | public int getWidth() { 21 | return width; 22 | } 23 | 24 | public void setWidth(int width) { 25 | this.width = width; 26 | } 27 | 28 | public int getHeight() { 29 | return height; 30 | } 31 | 32 | public void setHeight(int height) { 33 | this.height = height; 34 | } 35 | 36 | public int getDesignWidth() { 37 | return designWidth; 38 | } 39 | 40 | public void setDesignWidth(int designWidth) { 41 | this.designWidth = designWidth; 42 | } 43 | 44 | public int getDesignHeight() { 45 | return designHeight; 46 | } 47 | 48 | public void setDesignHeight(int designHeight) { 49 | this.designHeight = designHeight; 50 | } 51 | 52 | public int getMenuWidth() { 53 | return menuWidth; 54 | } 55 | 56 | public void setMenuWidth(int menuWidth) { 57 | this.menuWidth = menuWidth; 58 | } 59 | 60 | public int getMenuHeight() { 61 | return menuHeight; 62 | } 63 | 64 | public void setMenuHeight(int menuHeight) { 65 | this.menuHeight = menuHeight; 66 | } 67 | 68 | public List getDatas() { 69 | return datas; 70 | } 71 | 72 | public void setDatas(List datas) { 73 | this.datas = datas; 74 | } 75 | 76 | public SysConfigVO getConfigVO() { 77 | return configVO; 78 | } 79 | 80 | public void setConfigVO(SysConfigVO configVO) { 81 | this.configVO = configVO; 82 | } 83 | 84 | public int getImgWidth() { 85 | return imgWidth; 86 | } 87 | 88 | public void setImgWidth(int imgWidth) { 89 | this.imgWidth = imgWidth; 90 | } 91 | 92 | public int getImgHeight() { 93 | return imgHeight; 94 | } 95 | 96 | public void setImgHeight(int imgHeight) { 97 | this.imgHeight = imgHeight; 98 | } 99 | 100 | public PrintContext build() { 101 | PrintContext context = new PrintContext(); 102 | context.setConfig(configVO); 103 | context.setImgHeight(imgHeight); 104 | context.setImgWidth(imgWidth); 105 | return context; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/unit/JsonUtils.java: -------------------------------------------------------------------------------- 1 | 2 | package com.lc.design.unit; 3 | 4 | import java.text.DateFormat; 5 | import java.text.SimpleDateFormat; 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import org.codehaus.jackson.JsonParser.Feature; 10 | import org.codehaus.jackson.annotate.JsonAutoDetect; 11 | import org.codehaus.jackson.annotate.JsonMethod; 12 | import org.codehaus.jackson.map.DeserializationConfig; 13 | import org.codehaus.jackson.map.ObjectMapper; 14 | import org.codehaus.jackson.map.SerializationConfig; 15 | import org.codehaus.jackson.type.JavaType; 16 | import org.slf4j.Logger; 17 | import org.slf4j.LoggerFactory; 18 | 19 | public class JsonUtils { 20 | 21 | private static ObjectMapper mapper = new ObjectMapper(); 22 | 23 | private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class); 24 | 25 | static { 26 | DateFormat dateFormat = new SimpleDateFormat(DateTimeUtils.YMDHMSS); 27 | SerializationConfig serConfig = mapper.getSerializationConfig().withDateFormat(dateFormat); 28 | DeserializationConfig deserConfig = mapper.getDeserializationConfig().withDateFormat(dateFormat); 29 | mapper.setSerializationConfig(serConfig); 30 | mapper.setDeserializationConfig(deserConfig); 31 | mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true); 32 | mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); 33 | mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false); 34 | } 35 | 36 | public static String convertToString(Object obj) { 37 | if (obj == null) { 38 | return null; 39 | } 40 | try { 41 | mapper.setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY); 42 | return mapper.writeValueAsString(obj); 43 | } 44 | catch (Exception e) { 45 | logger.error("{}转换为JOSN格式出错", obj.toString(), e); 46 | } 47 | return null; 48 | } 49 | 50 | public static T convertToObject(String value, Class clazz) { 51 | if (ValidateHelper.isNull(value)) { 52 | return null; 53 | } 54 | try { 55 | mapper.setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY); 56 | return mapper.readValue(value, clazz); 57 | } 58 | catch (Exception e) { 59 | logger.error("{}转换为JOSN格式出错", value.toString(), e); 60 | } 61 | return null; 62 | } 63 | 64 | @SuppressWarnings("unchecked") 65 | public static List convertToListObject(String value, Class... clazz) throws Exception { 66 | 67 | if (ValidateHelper.isNull(value)) { 68 | return null; 69 | } 70 | try { 71 | JavaType javaType = getCollectionType(ArrayList.class, clazz); 72 | List list = (List) mapper.readValue(value, javaType); 73 | return list; 74 | } 75 | catch (Exception e) { 76 | logger.error("{}转换为JOSN格式出错", value.toString(), e); 77 | } 78 | return null; 79 | } 80 | 81 | /** 82 | * 获取泛型的Collection Type 83 | * 84 | * @param collectionClass 泛型的Collection 85 | * @param elementClasses 元素类 86 | * @return JavaType Java类型 87 | * @since 1.0 88 | */ 89 | private static JavaType getCollectionType(Class collectionClass, Class... elementClasses) { 90 | return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses); 91 | } 92 | 93 | /** 94 | * 调用这个接口只返回list对象,需要用mapConvertor去转 95 | * 96 | * @param value 97 | * @return 98 | */ 99 | @SuppressWarnings("rawtypes") 100 | public static List convertToList(String value) { 101 | if (ValidateHelper.isNull(value)) { 102 | return null; 103 | } 104 | try { 105 | return mapper.readValue(value, List.class); 106 | } 107 | catch (Exception e) { 108 | logger.error("{}转换为JOSN格式出错", value.toString(), e); 109 | } 110 | return null; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/panel/ConfigDialog.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.panel; 2 | 3 | import javax.swing.JButton; 4 | import javax.swing.JDialog; 5 | import javax.swing.JFrame; 6 | import javax.swing.JLabel; 7 | import javax.swing.JPanel; 8 | import javax.swing.JTextField; 9 | import javax.swing.SwingConstants; 10 | 11 | import com.lc.design.action.ConfigDlgApplyMouseListener; 12 | import com.lc.design.vo.SysConfigVO; 13 | 14 | /** 15 | * 配置对话框 16 | * 17 | * @author liubq 18 | */ 19 | public class ConfigDialog extends JDialog { 20 | private static final long serialVersionUID = 1L; 21 | 22 | private JTextField xTxt = new JTextField("8"); 23 | 24 | private JTextField yTxt = new JTextField("8"); 25 | 26 | private JButton okButton = new JButton("确定"); 27 | 28 | private DesignContainer designContainer; 29 | 30 | // 是否按下确认按钮 31 | private boolean ok = false; 32 | 33 | /** 34 | * 容器 35 | * 36 | * @return 37 | */ 38 | public DesignContainer getDesignContainer() { 39 | return designContainer; 40 | } 41 | 42 | /** 43 | * 构造器 44 | * 45 | * @param owner 46 | */ 47 | public ConfigDialog(DesignContainer designContainer) { 48 | super(designContainer.getFrame()); 49 | this.setModal(true); 50 | this.designContainer = designContainer; 51 | this.setTitle("系统配置"); 52 | // 实例化面板 53 | JPanel panel = new JPanel(); 54 | panel.setLayout(null); 55 | JLabel xLabel = new JLabel("横向补偿:"); 56 | xLabel.setHorizontalAlignment(SwingConstants.RIGHT); 57 | xLabel.setBounds(20, 20, 60, 30); 58 | panel.add(xLabel); 59 | xTxt.setBounds(90, 20, 120, 30); 60 | panel.add(xTxt); 61 | 62 | JLabel yLabel = new JLabel("纵向补偿:"); 63 | yLabel.setHorizontalAlignment(SwingConstants.RIGHT); 64 | yLabel.setBounds(20, 60, 60, 30); 65 | panel.add(yLabel); 66 | yTxt.setBounds(90, 60, 120, 30); 67 | panel.add(yTxt); 68 | 69 | okButton.setBounds(110, 100, 60, 30); 70 | okButton.addMouseListener(new ConfigDlgApplyMouseListener(this)); 71 | panel.add(okButton); 72 | 73 | // 将面板添加到帧窗口 74 | add(panel); 75 | setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 76 | 77 | } 78 | 79 | /** 80 | * 初始化显示 81 | * 82 | * @param vo 83 | */ 84 | private void initDisplayValue(SysConfigVO vo) { 85 | this.xTxt.setText(String.valueOf(vo.getFillX())); 86 | this.yTxt.setText(String.valueOf(vo.getFillY())); 87 | } 88 | 89 | /** 90 | * 取得数据 91 | * 92 | * @return 93 | * @throws Exception 94 | */ 95 | public SysConfigVO getData() throws Exception { 96 | if (this.ok) { 97 | inVO.setFillX(Integer.valueOf(xTxt.getText())); 98 | inVO.setFillY(Integer.valueOf(yTxt.getText())); 99 | return inVO; 100 | } 101 | return null; 102 | } 103 | 104 | /** 105 | * 检查画面数据 106 | * 107 | * @throws Exception 108 | */ 109 | public void checkData() throws Exception { 110 | // 横向补充检查 111 | String x = xTxt.getText(); 112 | if (x == null || x.length() == 0) { 113 | throw new Exception("横向补偿不能为空,建议无边框默认为8"); 114 | } 115 | int ix; 116 | try { 117 | ix = Integer.valueOf(x); 118 | } 119 | catch (Exception ex) { 120 | throw new Exception("横向补偿数字:" + x); 121 | } 122 | if (ix < 0 || ix > 1000) { 123 | throw new Exception("横向补偿不合理:" + x); 124 | } 125 | // 横向补充检查 126 | String y = xTxt.getText(); 127 | if (y == null || y.length() == 0) { 128 | throw new Exception("纵向补偿不能为空,建议无边框默认为8"); 129 | } 130 | int iy; 131 | try { 132 | iy = Integer.valueOf(y); 133 | } 134 | catch (Exception ex) { 135 | throw new Exception("纵向补偿数字:" + y); 136 | } 137 | if (iy < 0 || iy > 1000) { 138 | throw new Exception("纵向补偿不合理:" + y); 139 | } 140 | } 141 | 142 | // 插入的数据 143 | private SysConfigVO inVO; 144 | 145 | /* 146 | * (non-Javadoc) 147 | * 148 | * @see java.awt.Dialog#setVisible(boolean) 149 | */ 150 | public void setVisible(boolean b) { 151 | // 每次显示时,先设定按钮未按下 152 | if (b) { 153 | this.ok = false; 154 | } 155 | super.setVisible(b); 156 | } 157 | 158 | /** 159 | * 确认按钮按下 160 | */ 161 | public void setSuccess() { 162 | this.ok = true; 163 | } 164 | 165 | /** 166 | * 显示 167 | * 168 | * @param vo 169 | */ 170 | public void show(SysConfigVO vo) { 171 | this.inVO = vo; 172 | this.setSize(280, 180); 173 | this.setLocation(500, 300); 174 | this.setResizable(false); 175 | this.initDisplayValue(vo); 176 | this.setVisible(true); 177 | } 178 | 179 | } -------------------------------------------------------------------------------- /src/main/java/com/lc/design/unit/HtmlUtil.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.unit; 2 | 3 | import java.io.File; 4 | import java.nio.file.Files; 5 | import java.nio.file.Paths; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | import com.lc.design.constant.TPDConsts; 10 | import com.lc.design.vo.DataItemVO; 11 | import com.lc.design.vo.DataVO; 12 | import com.lc.design.vo.DesignVO; 13 | import com.lc.design.vo.OccupyVO; 14 | 15 | /** 16 | * HTML代码生成 17 | * 18 | * @author liubq 19 | */ 20 | public class HtmlUtil { 21 | 22 | /** 23 | * 组装成html代码 24 | * 25 | * @param pdjFile 26 | * @param dataMap 27 | * @return 28 | * @throws Exception 29 | */ 30 | public static String build(File pdjFile, Map dataMap) throws Exception { 31 | String json = new String(Files.readAllBytes(Paths.get(pdjFile.getAbsolutePath()))); 32 | return buildHtml(json, dataMap); 33 | } 34 | 35 | /** 36 | * 组装成html代码 37 | * 38 | * @param pdjFile 39 | * @param dataMap 40 | * @return 41 | * @throws Exception 42 | */ 43 | public static String buildHtml(String json, Map dataMap) throws Exception { 44 | // 默认非空对象 45 | if (dataMap == null) { 46 | dataMap = new HashMap(); 47 | } 48 | DesignVO design = JsonUtils.convertToObject(json, DesignVO.class); 49 | StringBuilder sb = new StringBuilder(); 50 | sb.append("").append("\n"); 51 | sb.append("").append("\n"); 52 | sb.append(" ").append("\n"); 53 | sb.append(" ").append("\n"); 54 | sb.append(" Document").append("\n"); 55 | sb.append(" ").append("\n"); 56 | sb.append(" ").append("\n"); 57 | DataVO value; 58 | for (OccupyVO vo : design.getDatas()) { 59 | value = dataMap.get(vo.getName()); 60 | value = value == null ? new DataVO(DataItemUtil.build(vo.getName())) : value; 61 | sb.append(" ").append(buildDiv(design, vo, value)).append("\n"); 62 | } 63 | sb.append(" ").append("\n"); 64 | sb.append("").append("\n"); 65 | return sb.toString(); 66 | } 67 | 68 | /** 69 | * 组装成html代码 70 | * 71 | * @param pdjFile 72 | * @param dataMap 73 | * @return 74 | * @throws Exception 75 | */ 76 | public static String buildIframe(String json, Map dataMap) throws Exception { 77 | DesignVO design = JsonUtils.convertToObject(json, DesignVO.class); 78 | StringBuilder sb = new StringBuilder(); 79 | DataVO value; 80 | for (OccupyVO vo : design.getDatas()) { 81 | value = dataMap.get(vo.getName()); 82 | value = value == null ? new DataVO(DataItemUtil.build(vo.getName())) : value; 83 | sb.append(buildDiv(design, vo, value)).append("\n"); 84 | } 85 | return sb.toString(); 86 | } 87 | 88 | /** 89 | * 构建html形式的显示文字 90 | * 91 | * @param vo 92 | * @param txt 93 | * @return 94 | */ 95 | private static String buildDiv(DesignVO design, OccupyVO vo, DataVO dataVO) { 96 | // 空检查 97 | DataItemVO[] items = dataVO.getData(); 98 | StringBuilder div = new StringBuilder(); 99 | OccupyVO occupy; 100 | if (items != null) { 101 | occupy = vo.clone(); 102 | for (int i = 0; i < items.length; i++) { 103 | occupy.setPositionY(vo.getPositionY() + i * (vo.getIntervalLen() + occupy.getHeight())); 104 | div.append(items[i].getItem(design.build(), occupy)).append("\n"); 105 | } 106 | } 107 | return div.toString(); 108 | } 109 | 110 | public static void preview(String html) { 111 | try { 112 | // String url = "http://www.baidu.com"; 113 | String name = System.currentTimeMillis() + ".html"; 114 | File file = new File(TPDConsts.DEAULT_DIR + File.separator + "temp" + File.separator + name); 115 | if (!file.getParentFile().exists()) { 116 | file.getParentFile().mkdirs(); 117 | } 118 | file.createNewFile(); 119 | Files.write(Paths.get(file.getAbsolutePath()), html.getBytes()); 120 | String path = file.getAbsolutePath().replace("\\", "/"); 121 | java.net.URI uri = new java.net.URI("file:/" + path); 122 | // 获取当前系统桌面扩展 123 | java.awt.Desktop dp = java.awt.Desktop.getDesktop(); 124 | // 判断系统桌面是否支持要执行的功能 125 | if (dp.isSupported(java.awt.Desktop.Action.BROWSE)) { 126 | // File file = new File("D:\\aa.txt"); 127 | // dp.edit(file);// 编辑文件 128 | dp.browse(uri);// 获取系统默认浏览器打开链接 129 | // dp.open(file);// 用默认方式打开文件 130 | // dp.print(file);// 用打印机打印文件 131 | } 132 | } 133 | catch (Exception e) { 134 | e.printStackTrace(); 135 | } 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/unit/DateTimeUtils.java: -------------------------------------------------------------------------------- 1 | 2 | package com.lc.design.unit; 3 | 4 | import java.sql.Timestamp; 5 | import java.text.ParseException; 6 | import java.text.SimpleDateFormat; 7 | import java.util.Calendar; 8 | import java.util.Date; 9 | import java.util.regex.Matcher; 10 | import java.util.regex.Pattern; 11 | 12 | public class DateTimeUtils { 13 | 14 | public static final String DEFAULT_START_DATE = "1900-1-1"; 15 | 16 | public static final String YMD = "yyyy-MM-dd"; 17 | 18 | public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss"; 19 | 20 | public static final String YMDHMSS = "yyyy-MM-dd HH:mm:ss:SS"; 21 | 22 | public static final String HMS = "HH:mm:ss"; 23 | 24 | public static final String YMDHMS_STR = "yyyyMMddHHmmss"; 25 | 26 | public static final String Y = "yyyy"; 27 | 28 | public static final String M = "MM"; 29 | 30 | public static final String D = "dd"; 31 | 32 | /** 33 | * 将java.util.Date 按指定格式转化为String 34 | * 35 | * @param date 36 | * @param format 37 | * @return 38 | */ 39 | public static String date2str(Date date, String format) { 40 | if (date == null) { 41 | return null; 42 | } 43 | SimpleDateFormat sdf = new SimpleDateFormat(format); 44 | return sdf.format(date); 45 | } 46 | 47 | /** 48 | * 将string 按指定格式转化为java.util.Date 49 | * 50 | * @param str 51 | * @param format 52 | * @return 53 | * @throws ParseException 54 | */ 55 | public static Date str2Date(String str, String format) throws ParseException { 56 | if (str == null || "".equals(str)) { 57 | return null; 58 | } 59 | SimpleDateFormat sdf = new SimpleDateFormat(format); 60 | return sdf.parse(str); 61 | } 62 | 63 | /** 64 | * 返回当前java.sql.Timestamp类型时间 65 | */ 66 | public static Timestamp getCurrentTime() { 67 | return new Timestamp(System.currentTimeMillis()); 68 | } 69 | 70 | /** 71 | * 将string 按指定格式转化为java.sql.Timestamp 72 | * 73 | * @param str 74 | * @param format 75 | * @return 76 | * @throws ParseException 77 | */ 78 | public static Timestamp str2Timestamp(String str, String format) { 79 | SimpleDateFormat sdf = new SimpleDateFormat(format); 80 | try { 81 | return new Timestamp(sdf.parse(str).getTime()); 82 | } 83 | catch (Exception e) { 84 | return null; 85 | } 86 | } 87 | 88 | /** 89 | * 将Date 按照 format 进行格式化 90 | * @param date 91 | * @param format 92 | * @return 93 | */ 94 | public static String formatDate(Date date, String format) { 95 | 96 | SimpleDateFormat sdf = new SimpleDateFormat(format); 97 | return sdf.format(date); 98 | } 99 | 100 | /** 101 | * 验证字符串是否为合法日期格式 102 | * 支持YYYY-MM-DD OR YYYY-MM-DD HH:mm:ss 103 | * @param dateString 104 | */ 105 | public static boolean validateDateFormat(String dateString) { 106 | Boolean validate = Boolean.FALSE; 107 | String reg1 = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-9]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$"; 108 | String reg2 = "^((\\d{2}(([02468][048])|([13579][26]))" + "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|" 109 | + "(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?" 110 | + "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?(" 111 | + "(((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?" 112 | + "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))"; 113 | 114 | Pattern p1 = Pattern.compile(reg1); 115 | Pattern p2 = Pattern.compile(reg2); 116 | Matcher m1 = p1.matcher(dateString); 117 | Matcher m2 = p2.matcher(dateString); 118 | if (m1.matches() || m2.matches()) { 119 | validate = Boolean.TRUE; 120 | } 121 | return validate; 122 | } 123 | 124 | /** 125 | * 将制定日期向 向后推若干分钟 126 | * @param startTime 日期 127 | * @param compartTime 要推迟的分钟数 正数 向后 负数向前 128 | * @return 129 | */ 130 | public static Date compartDate(Date startTime, int compartTime) { 131 | if (null == startTime) { 132 | return null; 133 | } 134 | Calendar c = Calendar.getInstance(); 135 | c.setTime(startTime); 136 | c.add(Calendar.MINUTE, compartTime); 137 | return c.getTime(); 138 | } 139 | 140 | /** 141 | * 获得对某个时间单位进行偏移之后时间 142 | * 如getDate(new Date(), 1, Calendar.DATE),表示取到当前时间一天之后的时间 143 | * @param date 144 | * @param offset 145 | * @param unit 146 | * @return 147 | */ 148 | public static Date getDate(Date date, int offset, int unit) { 149 | 150 | Calendar c = Calendar.getInstance(); 151 | c.setTime(date); 152 | c.add(unit, offset); 153 | return c.getTime(); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/unit/JsonTool.java: -------------------------------------------------------------------------------- 1 | 2 | package com.lc.design.unit; 3 | 4 | import java.util.ArrayList; 5 | 6 | public class JsonTool { 7 | 8 | //换行符 9 | public static String LINE_SPLIT = "\n"; 10 | 11 | /** 12 | * json格式化 13 | * 14 | * @param jsonString 待格式化的字符串 15 | * @return 16 | */ 17 | public static String formatJson(String jsonString) { 18 | return formatJson(jsonString, " "); 19 | } 20 | 21 | /** 22 | * JSON格式化 23 | * 24 | * @param inJsonString 待格式化的字符串 25 | * @param fillStringUnit 格式化后左侧补充显示的占位符 26 | * @return 格式化后字符串 27 | */ 28 | private static String formatJson(String inJsonString, String fillStringUnit) { 29 | //判空 30 | String jsonString = inJsonString; 31 | if (jsonString == null || jsonString.trim().length() == 0) { 32 | return null; 33 | } 34 | jsonString = jsonString.trim(); 35 | jsonString = jsonString.replace("\\/", "/"); 36 | // 保存所有的Json行 37 | ArrayList eachRowStringList = new ArrayList(); 38 | { 39 | String jsonTemp = jsonString; 40 | //循环拆分出所有的JsonHang 41 | while (jsonTemp.length() > 0) { 42 | // 取得JSON一行数据 43 | String eachRowString = getEachRowOfJsonString(jsonTemp); 44 | eachRowStringList.add(eachRowString.trim()); 45 | // 剩余待分析的数据 46 | jsonTemp = jsonTemp.substring(eachRowString.length()); 47 | } 48 | } 49 | 50 | int fixedLenth = 0; 51 | for (int i = 0; i < eachRowStringList.size(); i++) { 52 | String token = eachRowStringList.get(i); 53 | int length = token.getBytes().length; 54 | if (length > fixedLenth && i < eachRowStringList.size() - 1 && eachRowStringList.get(i + 1).equals(":")) { 55 | fixedLenth = length; 56 | } 57 | } 58 | 59 | StringBuilder buf = new StringBuilder(); 60 | int count = 0; 61 | for (int i = 0; i < eachRowStringList.size(); i++) { 62 | 63 | String token = eachRowStringList.get(i); 64 | 65 | if (token.equals(",")) { 66 | buf.append(token); 67 | doFill(buf, count, fillStringUnit); 68 | continue; 69 | } 70 | if (token.equals(":")) { 71 | buf.append(token); 72 | continue; 73 | } 74 | if (token.equals("{")) { 75 | String nextToken = eachRowStringList.get(i + 1); 76 | if (nextToken.equals("}")) { 77 | i++; 78 | buf.append("{ }"); 79 | } 80 | else { 81 | count++; 82 | buf.append(token); 83 | doFill(buf, count, fillStringUnit); 84 | } 85 | continue; 86 | } 87 | if (token.equals("}")) { 88 | count--; 89 | doFill(buf, count, fillStringUnit); 90 | buf.append(token); 91 | continue; 92 | } 93 | if (token.equals("[")) { 94 | String nextToken = eachRowStringList.get(i + 1); 95 | if (nextToken.equals("]")) { 96 | i++; 97 | buf.append("[ ]"); 98 | } 99 | else { 100 | count++; 101 | buf.append(token); 102 | doFill(buf, count, fillStringUnit); 103 | } 104 | continue; 105 | } 106 | if (token.equals("]")) { 107 | count--; 108 | doFill(buf, count, fillStringUnit); 109 | buf.append(token); 110 | continue; 111 | } 112 | 113 | buf.append(token); 114 | // 对齐: 一般不需要 115 | // if (i < eachRowStringList.size() - 1 && eachRowStringList.get(i + 1).equals(":")) { 116 | // int fillLength = fixedLenth - token.getBytes().length; 117 | // if (fillLength > 0) { 118 | // for (int j = 0; j < fillLength; j++) { 119 | // buf.append(" "); 120 | // } 121 | // } 122 | // } 123 | } 124 | return buf.toString(); 125 | } 126 | 127 | /** 128 | * 取得一行数据 129 | * 130 | * @param jsonString 131 | * @return 132 | */ 133 | private static String getEachRowOfJsonString(String jsonString) { 134 | StringBuilder buf = new StringBuilder(); 135 | boolean isInYinHao = false; 136 | while (jsonString.length() > 0) { 137 | // 取得第一个字符 138 | String firstString = jsonString.substring(0, 1); 139 | //是否是特殊字符: { } [ ] , 140 | if (!isInYinHao 141 | && (firstString.equals(":") || firstString.equals("{") || firstString.equals("}") || firstString.equals("[") || firstString.equals("]") || firstString 142 | .equals(","))) { 143 | // 特殊字符直接加入 144 | if (buf.toString().trim().length() == 0) { 145 | buf.append(firstString); 146 | } 147 | break; 148 | } 149 | // 剩余其它字符 150 | jsonString = jsonString.substring(1); 151 | // 第一个字符 = \ 152 | if (firstString.equals("\\")) { 153 | buf.append(firstString); 154 | buf.append(jsonString.substring(0, 1)); 155 | jsonString = jsonString.substring(1); 156 | continue; 157 | } 158 | // 第一个字符 = " 159 | if (firstString.equals("\"")) { 160 | buf.append(firstString); 161 | if (isInYinHao) { 162 | break; 163 | } 164 | else { 165 | isInYinHao = true; 166 | continue; 167 | } 168 | } 169 | buf.append(firstString); 170 | } 171 | return buf.toString(); 172 | } 173 | 174 | private static void doFill(StringBuilder buf, int count, String fillStringUnit) { 175 | buf.append(LINE_SPLIT); 176 | for (int i = 0; i < count; i++) { 177 | buf.append(fillStringUnit); 178 | } 179 | } 180 | 181 | /** 182 | * JSON格式化后,分行。 183 | * 184 | * @param json 185 | * @return 186 | */ 187 | @SuppressWarnings("unused") 188 | private static String[] formatedJsonToArray(String json) { 189 | if (null == json || json.length() == 0) { 190 | return new String[0]; 191 | } 192 | return json.split("\n"); 193 | } 194 | 195 | } 196 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/panel/OccupyDialog.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.panel; 2 | 3 | import javax.swing.JButton; 4 | import javax.swing.JComboBox; 5 | import javax.swing.JDialog; 6 | import javax.swing.JFrame; 7 | import javax.swing.JLabel; 8 | import javax.swing.JPanel; 9 | import javax.swing.JTextField; 10 | import javax.swing.SwingConstants; 11 | 12 | import com.lc.design.action.OccupyDlgApplyMouseListener; 13 | import com.lc.design.action.OccupyDlgFontMouseListener; 14 | import com.lc.design.action.OccupyDlgTypeMouseListener; 15 | import com.lc.design.component.OccupyFont; 16 | import com.lc.design.constant.OutType; 17 | import com.lc.design.vo.OccupyVO; 18 | 19 | /** 20 | * 占用对话框 21 | * 22 | * @author liubq 23 | */ 24 | public class OccupyDialog extends JDialog { 25 | private static final long serialVersionUID = 1L; 26 | 27 | private JTextField nameTxt = new JTextField(); 28 | 29 | private JTextField widthTxt = new JTextField(); 30 | 31 | private JTextField heightTxt = new JTextField(); 32 | 33 | private OccupyFont fcBtn; 34 | 35 | private JButton okButton = new JButton("应用"); 36 | 37 | private javax.swing.JComboBox outTypeList = new JComboBox(OutType.getListName()); 38 | 39 | private JTextField intervalTxt = new JTextField(); 40 | 41 | private DesignContainer designContainer; 42 | 43 | /** 44 | * 容器 45 | * 46 | * @return 47 | */ 48 | public DesignContainer getDesignContainer() { 49 | return designContainer; 50 | } 51 | 52 | /** 53 | * 构造器 54 | */ 55 | public OccupyDialog() { 56 | init(); 57 | } 58 | 59 | /** 60 | * 构造器 61 | * 62 | * @param owner 63 | */ 64 | public OccupyDialog(DesignContainer designContainer) { 65 | super(designContainer.getFrame()); 66 | this.designContainer = designContainer; 67 | init(); 68 | } 69 | 70 | /** 71 | * 构造器 72 | * 73 | * @param owner 74 | */ 75 | private void init() { 76 | this.setModal(true); 77 | 78 | this.setTitle("占位符编辑"); 79 | // 实例化面板 80 | JPanel panel = new JPanel(); 81 | panel.setLayout(null); 82 | JLabel nameLabel = new JLabel("名称:"); 83 | nameLabel.setHorizontalAlignment(SwingConstants.RIGHT); 84 | nameLabel.setBounds(20, 20, 40, 30); 85 | panel.add(nameLabel); 86 | nameTxt.setBounds(70, 20, 160, 30); 87 | panel.add(nameTxt); 88 | 89 | JLabel widthLabel = new JLabel("宽度:"); 90 | widthLabel.setHorizontalAlignment(SwingConstants.RIGHT); 91 | widthLabel.setBounds(20, 60, 40, 30); 92 | panel.add(widthLabel); 93 | widthTxt.setBounds(70, 60, 50, 30); 94 | panel.add(widthTxt); 95 | 96 | JLabel heightLabel = new JLabel("高度:"); 97 | heightLabel.setHorizontalAlignment(SwingConstants.RIGHT); 98 | heightLabel.setBounds(140, 60, 30, 30); 99 | panel.add(heightLabel); 100 | heightTxt.setBounds(180, 60, 50, 30); 101 | panel.add(heightTxt); 102 | 103 | JLabel styleLabel = new JLabel("字体:"); 104 | styleLabel.setHorizontalAlignment(SwingConstants.RIGHT); 105 | styleLabel.setBounds(20, 100, 40, 30); 106 | panel.add(styleLabel); 107 | fcBtn = new OccupyFont("请选择>>>"); 108 | fcBtn.setBounds(70, 100, 160, 30); 109 | fcBtn.addMouseListener(new OccupyDlgFontMouseListener(this, fcBtn)); 110 | panel.add(fcBtn); 111 | 112 | JLabel typeLabel = new JLabel("类型:"); 113 | typeLabel.setHorizontalAlignment(SwingConstants.RIGHT); 114 | typeLabel.setBounds(20, 140, 40, 30); 115 | panel.add(typeLabel); 116 | outTypeList.setBounds(70, 140, 70, 30); 117 | outTypeList.addActionListener(new OccupyDlgTypeMouseListener(this)); 118 | panel.add(outTypeList); 119 | 120 | JLabel intervalLabel = new JLabel("间距:"); 121 | intervalLabel.setHorizontalAlignment(SwingConstants.RIGHT); 122 | intervalLabel.setBounds(150, 140, 30, 30); 123 | panel.add(intervalLabel); 124 | intervalTxt.setBounds(190, 140, 40, 30); 125 | panel.add(intervalTxt); 126 | 127 | okButton.setBounds(115, 190, 60, 30); 128 | okButton.addMouseListener(new OccupyDlgApplyMouseListener(this)); 129 | panel.add(okButton); 130 | 131 | // 将面板添加到帧窗口 132 | add(panel); 133 | setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 134 | 135 | } 136 | 137 | /** 138 | * 初始化显示 139 | * 140 | * @param vo 141 | */ 142 | private void initDisplayValue(OccupyVO vo) { 143 | if (vo == null) { 144 | return; 145 | } 146 | this.nameTxt.setText(vo.getName()); 147 | this.widthTxt.setText(String.valueOf(vo.getWidth())); 148 | this.heightTxt.setText(String.valueOf(vo.getHeight())); 149 | this.fcBtn.setData(vo.getFc()); 150 | this.outTypeList.setSelectedItem(OutType.getByCode(vo.getOutType()).getName()); 151 | if (vo.getIntervalLen() > 0) { 152 | this.intervalTxt.setText(String.valueOf(vo.getIntervalLen())); 153 | } 154 | } 155 | 156 | /** 157 | * 取得数据 158 | * 159 | * @return 160 | * @throws Exception 161 | */ 162 | public OccupyVO getData() throws Exception { 163 | inVO.setName(nameTxt.getText()); 164 | inVO.setWidth(Integer.valueOf(widthTxt.getText())); 165 | inVO.setHeight(Integer.valueOf(heightTxt.getText())); 166 | inVO.setFc(fcBtn.getData()); 167 | OutType oType = OutType.getByName((String) this.outTypeList.getSelectedItem()); 168 | inVO.setOutType(oType.getCode()); 169 | if (OutType.LIST.equals(oType)) { 170 | inVO.setIntervalLen(Integer.valueOf(this.intervalTxt.getText())); 171 | } 172 | else { 173 | inVO.setIntervalLen(0); 174 | } 175 | return inVO; 176 | } 177 | 178 | /** 179 | * 检查画面数据 180 | * 181 | * @throws Exception 182 | */ 183 | public void checkData() throws Exception { 184 | // 名称检查 185 | String name = nameTxt.getText(); 186 | if (name == null || name.length() == 0) { 187 | throw new Exception("名称不能为空"); 188 | } 189 | // 宽度检查 190 | String width = widthTxt.getText(); 191 | if (width == null || width.length() == 0) { 192 | throw new Exception("宽度不能为空"); 193 | } 194 | int iW; 195 | try { 196 | iW = Integer.valueOf(width); 197 | } 198 | catch (Exception ex) { 199 | throw new Exception("宽度不是数字:" + width); 200 | } 201 | if (iW < 12 || iW > 1200) { 202 | throw new Exception("宽度不合理:" + width); 203 | } 204 | // 高度检查 205 | String height = heightTxt.getText(); 206 | if (height == null || height.length() == 0) { 207 | throw new Exception("高度不能为空"); 208 | } 209 | int iH; 210 | try { 211 | iH = Integer.valueOf(height); 212 | } 213 | catch (Exception ex) { 214 | throw new Exception("高度不是数字:" + height); 215 | } 216 | if (iH < 12 || iH > 1200) { 217 | throw new Exception("高度不合理:" + height); 218 | } 219 | 220 | // 名称检查 221 | OutType oType = OutType.getByName((String) this.outTypeList.getSelectedItem()); 222 | if (OutType.LIST.equals(oType)) { 223 | String intervalLen = this.intervalTxt.getText(); 224 | if (intervalLen == null || height.length() == 0) { 225 | throw new Exception("间距不能为空"); 226 | } 227 | int iIntervalLen; 228 | try { 229 | iIntervalLen = Integer.valueOf(intervalLen); 230 | } 231 | catch (Exception ex) { 232 | throw new Exception("间距不是数字:" + height); 233 | } 234 | if (iIntervalLen < 2 || iIntervalLen > 50) { 235 | throw new Exception("间距不合理:" + height); 236 | } 237 | } 238 | } 239 | 240 | /** 241 | * 选择类型实际处理 242 | * 243 | * @throws Exception 244 | */ 245 | public void refreshOutType(String type) throws Exception { 246 | if (OutType.LIST.getName().equals(type)) { 247 | this.intervalTxt.setText("5"); 248 | this.intervalTxt.setEditable(true); 249 | } 250 | else { 251 | this.intervalTxt.setText(""); 252 | this.intervalTxt.setEditable(false); 253 | } 254 | } 255 | 256 | /** 257 | * 修改画面 258 | * 259 | * @throws Exception 260 | */ 261 | public void refreshOccupy() throws Exception { 262 | OccupyVO vo = getData(); 263 | this.designContainer.resetOccupy(vo); 264 | } 265 | 266 | // 插入的数据 267 | private OccupyVO inVO; 268 | 269 | /** 270 | * 显示 271 | * 272 | * @param vo 273 | */ 274 | public void show(OccupyVO vo) { 275 | this.inVO = vo; 276 | this.setSize(280, 280); 277 | this.setLocation(200, 200); 278 | this.setLocation(500, 300); 279 | this.setResizable(false); 280 | this.initDisplayValue(vo); 281 | this.setVisible(true); 282 | } 283 | 284 | public static void main(String[] args) { 285 | OccupyDialog dialog = new OccupyDialog(); 286 | dialog.show(null); 287 | 288 | } 289 | 290 | } 291 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/panel/DesignContainer.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.panel; 2 | 3 | import java.awt.BorderLayout; 4 | import java.awt.Color; 5 | import java.awt.Component; 6 | import java.awt.Dimension; 7 | import java.awt.event.ActionEvent; 8 | import java.awt.event.KeyEvent; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | import javax.swing.ActionMap; 13 | import javax.swing.BorderFactory; 14 | import javax.swing.ImageIcon; 15 | import javax.swing.InputMap; 16 | import javax.swing.JComponent; 17 | import javax.swing.JPanel; 18 | import javax.swing.JScrollPane; 19 | 20 | import com.lc.design.PrintDesignFrame; 21 | import com.lc.design.action.ArrowBinding; 22 | import com.lc.design.action.OccupyKeyListener; 23 | import com.lc.design.action.OccupyMouseListener; 24 | import com.lc.design.component.OccupyComponent; 25 | import com.lc.design.constant.Direction; 26 | import com.lc.design.constant.TPDConsts; 27 | import com.lc.design.vo.DesignVO; 28 | import com.lc.design.vo.OccupyVO; 29 | import com.lc.design.vo.SysConfigVO; 30 | 31 | /** 32 | * 设计容器,最外层 33 | * 34 | * @author liubq 35 | */ 36 | public class DesignContainer extends JPanel { 37 | 38 | // 设计模板 39 | private DesignPanel designPanel; 40 | 41 | // 菜单模板 42 | private MenuPanel menuPanel; 43 | 44 | // 滚动条 45 | private JScrollPane scrollPane; 46 | 47 | // 当前选中的占位符 48 | private OccupyComponent nowtxt; 49 | 50 | // 窗口 51 | private PrintDesignFrame frame; 52 | 53 | /** 54 | * 窗口 55 | * 56 | * @return 窗口 57 | */ 58 | public PrintDesignFrame getFrame() { 59 | return frame; 60 | } 61 | 62 | /** 63 | * 初始化 64 | */ 65 | public DesignContainer(PrintDesignFrame frame) { 66 | this.frame = frame; 67 | ActionMap actionMap = getActionMap(); 68 | int condition = JComponent.WHEN_FOCUSED; 69 | InputMap inputMap = getInputMap(condition); 70 | 71 | for (Direction direction : Direction.values()) { 72 | inputMap.put(direction.getKeyStroke(), direction.getText()); 73 | actionMap.put(direction.getText(), new ArrowBinding(direction.getText(), this)); 74 | } 75 | this.setLayout(new BorderLayout()); 76 | designPanel = new DesignPanel(this); 77 | menuPanel = new MenuPanel(this); 78 | scrollPane = new JScrollPane(); 79 | scrollPane.setViewportView(designPanel); 80 | this.add(scrollPane, BorderLayout.CENTER); 81 | this.add(menuPanel, BorderLayout.NORTH); 82 | } 83 | 84 | /** 85 | * 加入一个占位符 86 | * 87 | * @param name 88 | */ 89 | public void addNewOccupy(OccupyVO vo) { 90 | OccupyComponent txt = new OccupyComponent(); 91 | OccupyMouseListener m = new OccupyMouseListener(this); 92 | txt.addMouseListener(m); 93 | txt.addMouseMotionListener(m); 94 | txt.addKeyListener(new OccupyKeyListener(this)); 95 | designPanel.add(txt); 96 | txt.initInfo(vo); 97 | } 98 | 99 | /** 100 | * 移动位置 101 | * 102 | * @param e 103 | */ 104 | public void moveTxt(KeyEvent e) { 105 | if (nowtxt != null) { 106 | if (KeyEvent.VK_LEFT == e.getKeyCode()) { 107 | moveTxt("left"); 108 | } 109 | else if (KeyEvent.VK_RIGHT == e.getKeyCode()) { 110 | moveTxt("right"); 111 | } 112 | else if (KeyEvent.VK_DOWN == e.getKeyCode()) { 113 | moveTxt("down"); 114 | } 115 | else if (KeyEvent.VK_UP == e.getKeyCode()) { 116 | moveTxt("up"); 117 | } 118 | } 119 | } 120 | 121 | /** 122 | * 移动位置 123 | * 124 | * @param e 125 | */ 126 | public void moveTxt(ActionEvent e) { 127 | moveTxt(e.getActionCommand()); 128 | } 129 | 130 | /** 131 | * 移动位置 132 | * 133 | * @param commond 134 | */ 135 | private void moveTxt(String commond) { 136 | if (nowtxt != null) { 137 | if ("right".equalsIgnoreCase(commond)) { 138 | nowtxt.setBounds(nowtxt.getX() + 1, nowtxt.getY(), nowtxt.getWidth(), nowtxt.getHeight()); 139 | } 140 | else if ("left".equalsIgnoreCase(commond)) { 141 | int value = nowtxt.getX() - 1; 142 | value = value < TPDConsts.BORDER ? TPDConsts.BORDER : value; 143 | nowtxt.setBounds(value, nowtxt.getY(), nowtxt.getWidth(), nowtxt.getHeight()); 144 | } 145 | else if ("down".equalsIgnoreCase(commond)) { 146 | nowtxt.setBounds(nowtxt.getX(), nowtxt.getY() + 1, nowtxt.getWidth(), nowtxt.getHeight()); 147 | } 148 | else if ("up".equalsIgnoreCase(commond)) { 149 | int value = nowtxt.getY() - 1; 150 | value = value < TPDConsts.BORDER ? TPDConsts.BORDER : value; 151 | nowtxt.setBounds(nowtxt.getX(), value, nowtxt.getWidth(), nowtxt.getHeight()); 152 | } 153 | } 154 | } 155 | 156 | /** 157 | * 选择按下 158 | * 159 | * @param thisTxt 160 | */ 161 | public void selectPress(OccupyComponent thisTxt) { 162 | if (nowtxt != null) { 163 | nowtxt.setBorder(null); 164 | } 165 | thisTxt.setBorder(BorderFactory.createLineBorder(Color.orange, 1)); 166 | nowtxt = (OccupyComponent) thisTxt; 167 | } 168 | 169 | /** 170 | * 选择释放 171 | * 172 | * @param thisTxt 173 | */ 174 | public void selectRelease() { 175 | if (nowtxt != null) { 176 | nowtxt.setBorder(null); 177 | } 178 | } 179 | 180 | /** 181 | * 选择释放 182 | * 183 | * @param thisTxt 184 | */ 185 | public void removeNowTxt() { 186 | selectRelease(); 187 | if (nowtxt != null) { 188 | this.designPanel.remove(this.nowtxt); 189 | this.designPanel.repaint(); 190 | this.designPanel.updateUI(); 191 | } 192 | nowtxt = null; 193 | } 194 | 195 | /** 196 | * 更新配置,弹出对话框点击应用,响应 197 | * 198 | * @param vo 199 | */ 200 | public void resetOccupy(OccupyVO vo) { 201 | nowtxt.initInfo(vo); 202 | } 203 | 204 | /** 205 | * 取得当前选中配置 206 | * 207 | * @param vo 208 | */ 209 | public OccupyVO getNowOccupyVO() { 210 | if (nowtxt == null) { 211 | return null; 212 | } 213 | return nowtxt.getInfo(); 214 | } 215 | 216 | /** 217 | * 获得焦点 218 | */ 219 | public void requestFouse() { 220 | nowtxt.requestFocusInWindow(); 221 | } 222 | 223 | // 图片 224 | private ImageIcon image; 225 | 226 | /** 227 | * 插入底片 228 | * 229 | * @param image 230 | */ 231 | public void resetImg(ImageIcon image) { 232 | this.image = image; 233 | designPanel.rebuild(image); 234 | designPanel.setSize(image.getIconWidth() + 200, image.getIconHeight() + 200); 235 | } 236 | 237 | /** 238 | * 取得设计结果 239 | * 240 | * @return 241 | */ 242 | public DesignVO getDesignVO() { 243 | DesignVO dVO = new DesignVO(); 244 | dVO.setWidth(frame.getWidth()); 245 | dVO.setHeight(frame.getHeight()); 246 | dVO.setDesignWidth(designPanel.getWidth()); 247 | dVO.setDesignHeight(designPanel.getHeight()); 248 | dVO.setMenuWidth(menuPanel.getWidth()); 249 | dVO.setMenuHeight(menuPanel.getHeight()); 250 | dVO.setConfigVO(this.getConfigVO()); 251 | if (image != null) { 252 | dVO.setImgWidth(image.getIconWidth()); 253 | dVO.setImgHeight(image.getIconHeight()); 254 | } 255 | Component[] components = designPanel.getComponents(); 256 | List list = new ArrayList(); 257 | OccupyVO vo; 258 | OccupyComponent txt; 259 | for (Component component : components) { 260 | if (component instanceof OccupyComponent) { 261 | txt = (OccupyComponent) component; 262 | vo = txt.getInfo(); 263 | vo.setPositionX(txt.getX()); 264 | vo.setPositionY(txt.getY()); 265 | list.add(vo); 266 | } 267 | } 268 | dVO.setDatas(list); 269 | return dVO; 270 | } 271 | 272 | /** 273 | * 配置信息 274 | * 275 | * @return 276 | */ 277 | public SysConfigVO getConfigVO() { 278 | return config; 279 | } 280 | 281 | // 配置信息 282 | private SysConfigVO config = new SysConfigVO(); 283 | 284 | /** 285 | * 配置信息 286 | * 287 | * @param vo 288 | */ 289 | public void setConfigVO(SysConfigVO vo) { 290 | if (vo != null) { 291 | config = vo; 292 | } 293 | } 294 | 295 | /** 296 | * 取得设计结果 297 | * 298 | * @return 299 | */ 300 | public void resetDesignVO(DesignVO vo) { 301 | if (vo == null) { 302 | return; 303 | } 304 | frame.setSize(vo.getWidth(), vo.getHeight()); 305 | menuPanel.setSize(vo.getMenuWidth(), vo.getMenuHeight()); 306 | designPanel.removeAll(); 307 | for (OccupyVO data : vo.getDatas()) { 308 | this.addNewOccupy(data); 309 | } 310 | designPanel.setPreferredSize(new Dimension(vo.getDesignWidth(), vo.getDesignHeight())); 311 | this.setConfigVO(vo.getConfigVO()); 312 | scrollPane.updateUI(); 313 | } 314 | 315 | } 316 | -------------------------------------------------------------------------------- /src/main/java/com/lc/design/component/JFontChooser.java: -------------------------------------------------------------------------------- 1 | package com.lc.design.component; 2 | 3 | import java.awt.BorderLayout; 4 | import java.awt.Color; 5 | import java.awt.Component; 6 | import java.awt.Font; 7 | import java.awt.GraphicsEnvironment; 8 | import java.awt.event.ActionEvent; 9 | import java.awt.event.ActionListener; 10 | import java.awt.event.WindowAdapter; 11 | import java.awt.event.WindowEvent; 12 | import java.util.HashMap; 13 | import java.util.Map; 14 | 15 | import javax.swing.JButton; 16 | import javax.swing.JComboBox; 17 | import javax.swing.JDialog; 18 | import javax.swing.JLabel; 19 | import javax.swing.JList; 20 | import javax.swing.JPanel; 21 | import javax.swing.JScrollPane; 22 | import javax.swing.JTextField; 23 | import javax.swing.event.ListSelectionEvent; 24 | import javax.swing.event.ListSelectionListener; 25 | 26 | import com.lc.design.panel.OccupyDialog; 27 | 28 | /** 29 | * 控件 下载的代码,代码写的不好,但是能用,先凑合,以后有时间再修改吧。 30 | * 31 | * @author liubq 32 | */ 33 | public class JFontChooser extends JPanel { 34 | 35 | // 定义变量 36 | private String current_fontName = "宋体";// 当前的字体名称,默认宋体. 37 | private int current_fontStyle = Font.PLAIN;// 当前的字样,默认常规. 38 | private int current_fontSize = 9;// 当前字体大小,默认9号. 39 | private Color current_color = Color.BLACK;// 当前字色,默认黑色. 40 | @SuppressWarnings("unused") 41 | private Component parent;// 弹出dialog的父窗体. 42 | private JDialog dialog;// 用于显示模态的窗体 43 | private LCFont myfont;// 带有Color的字体. 44 | private JTextField txtFont;// 显示选择字体的TEXT 45 | private JTextField txtStyle;// 显示选择字型的TEXT 46 | private JTextField txtSize;// 显示选择字大小的TEXT 47 | private JList lstFont;// 选择字体的列表. 48 | private JList lstStyle;// 选择字型的列表. 49 | private JList lstSize;// 选择字体大小的列表. 50 | private JComboBox cbColor;// 选择Color的下拉框. 51 | private JButton ok, cancel;// "确定","取消"按钮. 52 | private JScrollPane spFont; 53 | private JScrollPane spSize; 54 | private JLabel lblShow;// 显示效果的label. 55 | 56 | private Map sizeMap;// 字号映射表. 57 | private Map colorMap;// 字着色映射表. 58 | // 定义变量_结束________________ 59 | 60 | public JFontChooser() { 61 | init(); 62 | } 63 | 64 | private void init() { 65 | // 实例化变量 66 | 67 | lblShow = new JLabel("字体选择器."); 68 | txtFont = new JTextField("宋体"); 69 | txtStyle = new JTextField("常规"); 70 | txtSize = new JTextField(); 71 | // 取得当前环境可用字体. 72 | GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 73 | String[] fontNames = ge.getAvailableFontFamilyNames(); 74 | lstFont = new JList(fontNames); 75 | // 字形. 76 | lstStyle = new JList(new String[] { 77 | "常规", "斜休", "粗休", "粗斜休" 78 | }); 79 | // 字号. 80 | String[] sizeStr = new String[] { 81 | "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "72", "初号", 82 | "小初", "一号", "小一", "二号", "小二", "三号", "小三", "四号", "小四", "五号", "小五", "六号", "小六", "七号", "八号" 83 | }; 84 | int sizeVal[] = { 85 | 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72, 42, 36, 26, 24, 22, 18, 16, 15, 14, 12, 86 | 11, 9, 8, 7, 6, 5 87 | }; 88 | sizeMap = new HashMap(); 89 | for (int i = 0; i < sizeStr.length; ++i) { 90 | sizeMap.put(sizeStr[i], sizeVal[i]); 91 | } 92 | lstSize = new JList(sizeStr); 93 | spFont = new JScrollPane(lstFont); 94 | spSize = new JScrollPane(lstSize); 95 | 96 | String[] colorStr = new String[] { 97 | "黑色", "蓝色", "青色", "深灰", "灰色", "绿色", "浅灰", "洋红", "桔黄", "粉红", "红色", "白色", "黄色" 98 | }; 99 | Color[] colorVal = new Color[] { 100 | Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, 101 | Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW 102 | }; 103 | colorMap = new HashMap(); 104 | for (int i = 0; i < colorStr.length; i++) { 105 | colorMap.put(colorStr[i], colorVal[i]); 106 | } 107 | cbColor = new JComboBox(colorStr); 108 | ok = new JButton("确定"); 109 | cancel = new JButton("取消"); 110 | // 实例化变量_结束 111 | 112 | // 布局控件 113 | this.setLayout(null);// 不用布局管理器. 114 | JLabel lblFont = new JLabel("字体:"); 115 | add(lblFont); 116 | lblFont.setBounds(12, 10, 30, 20); 117 | txtFont.setEditable(false); 118 | add(txtFont); 119 | txtFont.setBounds(10, 30, 155, 20); 120 | lstFont.setSelectedValue("宋体", true); 121 | add(spFont); 122 | spFont.setBounds(10, 50, 155, 100); 123 | 124 | JLabel lblStyle = new JLabel("字型:"); 125 | add(lblStyle); 126 | lblStyle.setBounds(175, 10, 30, 20); 127 | txtStyle.setEditable(false); 128 | add(txtStyle); 129 | txtStyle.setBounds(175, 30, 130, 20); 130 | lstStyle.setBorder(javax.swing.BorderFactory.createLineBorder(Color.gray)); 131 | add(lstStyle); 132 | lstStyle.setBounds(175, 50, 130, 100); 133 | lstStyle.setSelectedValue("常规", true); 134 | 135 | JLabel lblSize = new JLabel("大小:"); 136 | add(lblSize); 137 | lblSize.setBounds(320, 10, 30, 20); 138 | txtSize.setEditable(false); 139 | add(txtSize); 140 | txtSize.setBounds(320, 30, 60, 20); 141 | add(spSize); 142 | spSize.setBounds(320, 50, 60, 100); 143 | 144 | JLabel lblColor = new JLabel("颜色:"); 145 | add(lblColor); 146 | lblColor.setBounds(13, 170, 30, 20); 147 | add(cbColor); 148 | cbColor.setBounds(10, 195, 130, 22); 149 | cbColor.setMaximumRowCount(5); 150 | // 显示框. 151 | JPanel showPan = new JPanel(); 152 | showPan.setBorder(javax.swing.BorderFactory.createTitledBorder("示例")); 153 | add(showPan); 154 | showPan.setBounds(150, 170, 230, 100); 155 | showPan.setLayout(new BorderLayout()); 156 | lblShow.setBackground(Color.white); 157 | showPan.add(lblShow); 158 | add(ok); 159 | ok.setBounds(10, 240, 60, 20); 160 | add(cancel); 161 | cancel.setBounds(80, 240, 60, 20); 162 | // 布局控件_结束 163 | 164 | // 事件 165 | lstFont.addListSelectionListener(new ListSelectionListener() { 166 | 167 | public void valueChanged(ListSelectionEvent e) { 168 | current_fontName = (String) lstFont.getSelectedValue(); 169 | txtFont.setText(current_fontName); 170 | lblShow.setFont(new Font(current_fontName, current_fontStyle, current_fontSize)); 171 | } 172 | }); 173 | 174 | lstStyle.addListSelectionListener(new ListSelectionListener() { 175 | 176 | public void valueChanged(ListSelectionEvent e) { 177 | String value = (String) ((JList) e.getSource()).getSelectedValue(); 178 | if (value.equals("常规")) { 179 | current_fontStyle = Font.PLAIN; 180 | } 181 | if (value.equals("斜休")) { 182 | current_fontStyle = Font.ITALIC; 183 | } 184 | if (value.equals("粗休")) { 185 | current_fontStyle = Font.BOLD; 186 | } 187 | if (value.equals("粗斜休")) { 188 | current_fontStyle = Font.BOLD | Font.ITALIC; 189 | } 190 | txtStyle.setText(value); 191 | lblShow.setFont(new Font(current_fontName, current_fontStyle, current_fontSize)); 192 | } 193 | }); 194 | 195 | lstSize.addListSelectionListener(new ListSelectionListener() { 196 | 197 | public void valueChanged(ListSelectionEvent e) { 198 | current_fontSize = (Integer) sizeMap.get(lstSize.getSelectedValue()); 199 | txtSize.setText(String.valueOf(current_fontSize)); 200 | lblShow.setFont(new Font(current_fontName, current_fontStyle, current_fontSize)); 201 | } 202 | }); 203 | 204 | cbColor.addActionListener(new ActionListener() { 205 | 206 | public void actionPerformed(ActionEvent e) { 207 | current_color = (Color) colorMap.get(cbColor.getSelectedItem()); 208 | lblShow.setForeground(current_color); 209 | } 210 | }); 211 | 212 | ok.addActionListener(new ActionListener() { 213 | 214 | public void actionPerformed(ActionEvent e) { 215 | myfont = new LCFont(); 216 | myfont.setFont(new Font(current_fontName, current_fontStyle, current_fontSize)); 217 | myfont.setColor(current_color); 218 | dialog.dispose(); 219 | dialog = null; 220 | } 221 | }); 222 | 223 | cancel.addActionListener(new ActionListener() { 224 | 225 | public void actionPerformed(ActionEvent e) { 226 | myfont = null; 227 | dialog.dispose(); 228 | dialog = null; 229 | } 230 | }); 231 | // 事件_结束 232 | 233 | } 234 | 235 | public LCFont showDialog(OccupyDialog parent, LCFont font) { 236 | dialog = new JDialog(parent.getDesignContainer().getFrame(), "字体样式选择器", true); 237 | dialog.add(this); 238 | dialog.setResizable(false); 239 | if (parent != null) { 240 | dialog.setLocation(parent.getX(), parent.getY()); 241 | } 242 | 243 | dialog.setSize(400, 330); 244 | dialog.addWindowListener(new WindowAdapter() { 245 | 246 | @Override 247 | public void windowClosing(WindowEvent e) { 248 | myfont = null; 249 | dialog.removeAll(); 250 | dialog.dispose(); 251 | dialog = null; 252 | } 253 | }); 254 | if (font != null) { 255 | // 字体 256 | txtFont.setText(font.getFont().getName()); 257 | lstFont.setSelectedValue(font.getFont().getName(), true); 258 | // 样式 259 | if (Font.PLAIN == font.getFont().getStyle()) { 260 | txtStyle.setText("常规"); 261 | lstStyle.setSelectedValue("常规", true); 262 | } 263 | else if (Font.ITALIC == font.getFont().getStyle()) { 264 | txtStyle.setText("斜休"); 265 | lstStyle.setSelectedValue("斜休", true); 266 | } 267 | else if (Font.BOLD == font.getFont().getStyle()) { 268 | txtStyle.setText("粗休"); 269 | lstStyle.setSelectedValue("粗休", true); 270 | } 271 | else { 272 | txtStyle.setText("粗斜休"); 273 | lstStyle.setSelectedValue("粗斜休", true); 274 | } 275 | // 字体大小 276 | for (Map.Entry entry : sizeMap.entrySet()) { 277 | if (entry.getValue().equals(font.getFont().getSize())) { 278 | txtSize.setText(entry.getKey()); 279 | lstSize.setSelectedValue(entry.getKey(), true); 280 | break; 281 | } 282 | } 283 | // 颜色 284 | for (Map.Entry entry : colorMap.entrySet()) { 285 | if (entry.getValue().equals(font.getColor())) { 286 | cbColor.setSelectedItem(entry.getKey()); 287 | break; 288 | } 289 | } 290 | } 291 | else { 292 | // 字体 293 | txtFont.setText("宋体"); 294 | lstFont.setSelectedValue("宋体", true); 295 | txtStyle.setText("常规"); 296 | lstStyle.setSelectedValue("常规", true); 297 | txtSize.setText("16"); 298 | lstSize.setSelectedValue("16", true); 299 | } 300 | dialog.setVisible(true); 301 | return myfont; 302 | } 303 | 304 | public static void main(String[] args) { 305 | 306 | JFontChooser one = new JFontChooser(); 307 | LCFont lhf = one.showDialog(null, null); 308 | if (lhf != null) { 309 | System.out.println(lhf.getColor()); 310 | System.out.println(lhf.getFont()); 311 | } 312 | } 313 | } 314 | --------------------------------------------------------------------------------