|
|
@@ -0,0 +1,197 @@
|
|
|
+package com.mes.print.ui;
|
|
|
+
|
|
|
+import com.mes.print.Printer;
|
|
|
+import com.mes.print.model.FlowCardData;
|
|
|
+import com.mes.print.model.LabelData;
|
|
|
+
|
|
|
+import javax.swing.*;
|
|
|
+import java.awt.*;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 打印机设置对话框(通用版,跟随 mes-print.jar 一起发布)。
|
|
|
+ * 任何 Swing 客户端都可以直接使用:
|
|
|
+ * <pre>
|
|
|
+ * new PrinterSettingDialog(parentFrame).setVisible(true);
|
|
|
+ * </pre>
|
|
|
+ * 保存后立即生效,无需重启。
|
|
|
+ */
|
|
|
+public class PrinterSettingDialog extends JDialog {
|
|
|
+
|
|
|
+ private static final Font FONT_LABEL = new Font("微软雅黑", Font.PLAIN, 18);
|
|
|
+ private static final Font FONT_INPUT = new Font("微软雅黑", Font.PLAIN, 16);
|
|
|
+
|
|
|
+ private JRadioButton hpRadio;
|
|
|
+ private JRadioButton tscRadio;
|
|
|
+ private JRadioButton noneRadio;
|
|
|
+ private JComboBox<String> printerCombo;
|
|
|
+
|
|
|
+ public PrinterSettingDialog(Frame owner) {
|
|
|
+ super(owner, "打印机设置", true);
|
|
|
+ buildUI();
|
|
|
+ loadCurrent();
|
|
|
+ reloadPrinters();
|
|
|
+ pack();
|
|
|
+ setLocationRelativeTo(owner);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void buildUI() {
|
|
|
+ setLayout(new BorderLayout(0, 0));
|
|
|
+
|
|
|
+ JPanel center = new JPanel(new GridBagLayout());
|
|
|
+ center.setBorder(BorderFactory.createEmptyBorder(20, 25, 10, 25));
|
|
|
+ GridBagConstraints c = new GridBagConstraints();
|
|
|
+ c.insets = new Insets(10, 8, 10, 8);
|
|
|
+ c.anchor = GridBagConstraints.WEST;
|
|
|
+
|
|
|
+ c.gridx = 0; c.gridy = 0;
|
|
|
+ center.add(newLabel("打印类型:"), c);
|
|
|
+
|
|
|
+ hpRadio = newRadio("流转卡 (HP)");
|
|
|
+ tscRadio = newRadio("标签 (TSC)");
|
|
|
+ noneRadio = newRadio("不打印");
|
|
|
+ ButtonGroup g = new ButtonGroup();
|
|
|
+ g.add(hpRadio); g.add(tscRadio); g.add(noneRadio);
|
|
|
+ JPanel typePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 15, 0));
|
|
|
+ typePanel.add(hpRadio); typePanel.add(tscRadio); typePanel.add(noneRadio);
|
|
|
+ c.gridx = 1; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0;
|
|
|
+ center.add(typePanel, c);
|
|
|
+
|
|
|
+ c.gridx = 0; c.gridy = 1; c.fill = GridBagConstraints.NONE; c.weightx = 0;
|
|
|
+ center.add(newLabel("目标打印机:"), c);
|
|
|
+
|
|
|
+ printerCombo = new JComboBox<>();
|
|
|
+ printerCombo.setEditable(true);
|
|
|
+ printerCombo.setFont(FONT_INPUT);
|
|
|
+ printerCombo.setPreferredSize(new Dimension(320, 32));
|
|
|
+
|
|
|
+ JButton refreshBtn = newButton("刷新");
|
|
|
+ refreshBtn.addActionListener(e -> reloadPrinters());
|
|
|
+
|
|
|
+ JPanel printerPanel = new JPanel(new BorderLayout(6, 0));
|
|
|
+ printerPanel.add(printerCombo, BorderLayout.CENTER);
|
|
|
+ printerPanel.add(refreshBtn, BorderLayout.EAST);
|
|
|
+ c.gridx = 1; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0;
|
|
|
+ center.add(printerPanel, c);
|
|
|
+
|
|
|
+ JLabel hint = new JLabel("<html><span style='color:gray'>下拉列出本机所有系统打印机,也可手动输入名字片段(支持模糊匹配)</span></html>");
|
|
|
+ hint.setFont(new Font("微软雅黑", Font.PLAIN, 12));
|
|
|
+ c.gridx = 0; c.gridy = 2; c.gridwidth = 2; c.fill = GridBagConstraints.HORIZONTAL;
|
|
|
+ center.add(hint, c);
|
|
|
+
|
|
|
+ add(center, BorderLayout.CENTER);
|
|
|
+
|
|
|
+ JPanel bottom = new JPanel(new FlowLayout(FlowLayout.RIGHT, 8, 10));
|
|
|
+ JButton testBtn = newButton("测试打印");
|
|
|
+ JButton okBtn = newButton("保存");
|
|
|
+ JButton cancelBtn = newButton("取消");
|
|
|
+ testBtn.addActionListener(e -> doTest());
|
|
|
+ okBtn.addActionListener(e -> doSave());
|
|
|
+ cancelBtn.addActionListener(e -> dispose());
|
|
|
+ bottom.add(testBtn); bottom.add(okBtn); bottom.add(cancelBtn);
|
|
|
+ add(bottom, BorderLayout.SOUTH);
|
|
|
+ }
|
|
|
+
|
|
|
+ private JLabel newLabel(String text) {
|
|
|
+ JLabel l = new JLabel(text);
|
|
|
+ l.setFont(FONT_LABEL);
|
|
|
+ return l;
|
|
|
+ }
|
|
|
+
|
|
|
+ private JRadioButton newRadio(String text) {
|
|
|
+ JRadioButton r = new JRadioButton(text);
|
|
|
+ r.setFont(FONT_LABEL);
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ private JButton newButton(String text) {
|
|
|
+ JButton b = new JButton(text);
|
|
|
+ b.setFont(FONT_INPUT);
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void loadCurrent() {
|
|
|
+ String type = Printer.getCurrentType();
|
|
|
+ String name = Printer.getCurrentName();
|
|
|
+ if ("hp".equals(type)) hpRadio.setSelected(true);
|
|
|
+ else if ("tsc".equals(type)) tscRadio.setSelected(true);
|
|
|
+ else noneRadio.setSelected(true);
|
|
|
+ if (name != null && !name.isEmpty()) printerCombo.setSelectedItem(name);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void reloadPrinters() {
|
|
|
+ Object cur = printerCombo.getSelectedItem();
|
|
|
+ printerCombo.removeAllItems();
|
|
|
+ List<String> all = Printer.listAvailablePrinters();
|
|
|
+ for (String n : all) printerCombo.addItem(n);
|
|
|
+ if (cur != null && !cur.toString().isEmpty()) {
|
|
|
+ printerCombo.setSelectedItem(cur);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String currentType() {
|
|
|
+ if (hpRadio.isSelected()) return "hp";
|
|
|
+ if (tscRadio.isSelected()) return "tsc";
|
|
|
+ return "none";
|
|
|
+ }
|
|
|
+
|
|
|
+ private String currentName() {
|
|
|
+ Object v = printerCombo.getSelectedItem();
|
|
|
+ return v == null ? "" : v.toString().trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void doTest() {
|
|
|
+ String type = currentType();
|
|
|
+ String name = currentName();
|
|
|
+ if ("none".equals(type)) { msg("请先选择打印类型(流转卡/标签)"); return; }
|
|
|
+ if (name.isEmpty()) { msg("请先选择或填写目标打印机"); return; }
|
|
|
+ try {
|
|
|
+ Printer.setPrinter(type, name);
|
|
|
+ if ("hp".equals(type)) sendTestFlowCard();
|
|
|
+ else sendTestLabel();
|
|
|
+ msg("测试打印已发送");
|
|
|
+ } catch (Exception ex) {
|
|
|
+ err("测试打印失败:" + ex.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void sendTestFlowCard() {
|
|
|
+ FlowCardData d = new FlowCardData();
|
|
|
+ d.setSn("TEST0001");
|
|
|
+ d.setProduct("+TEST");
|
|
|
+ d.setOpNo("TEST-OP");
|
|
|
+ d.setBatch("TEST-BATCH");
|
|
|
+ d.setDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
|
|
|
+ d.setOperator("tester");
|
|
|
+ d.setRemark("这是一张测试流转卡");
|
|
|
+ Printer.printFlowCard(d);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void sendTestLabel() {
|
|
|
+ LabelData l = new LabelData();
|
|
|
+ l.setTitle("TEST");
|
|
|
+ l.setSn("TEST0001");
|
|
|
+ l.setBarcode("TEST0001");
|
|
|
+ Printer.printLabel(l);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void doSave() {
|
|
|
+ String type = currentType();
|
|
|
+ String name = currentName();
|
|
|
+ if (!"none".equals(type) && name.isEmpty()) {
|
|
|
+ msg("请选择或填写目标打印机"); return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Printer.setPrinter(type, "none".equals(type) ? "" : name);
|
|
|
+ msg("打印机配置已保存");
|
|
|
+ dispose();
|
|
|
+ } catch (Exception ex) {
|
|
|
+ err("保存失败:" + ex.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void msg(String s) { JOptionPane.showMessageDialog(this, s, "提示", JOptionPane.INFORMATION_MESSAGE); }
|
|
|
+ private void err(String s) { JOptionPane.showMessageDialog(this, s, "错误", JOptionPane.ERROR_MESSAGE); }
|
|
|
+}
|