| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- package com.mes.ui;
- import com.mes.config.MesClientProperties;
- import com.mes.service.DcBatchPrinter;
- import com.mes.util.SingleInstanceLock;
- import com.mes.util.UiImages;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import javax.swing.*;
- import javax.swing.border.EmptyBorder;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.concurrent.atomic.AtomicBoolean;
- /**
- * 自动化大冲工控机主窗:扫批次 → MES允许 → 产量差达标 → 关允许 → 打印。
- */
- public class MainFrame extends JFrame {
- public static final Logger log = LoggerFactory.getLogger(MainFrame.class);
- public static MainFrame mesClientFrame;
- public static String mes_server_ip = "";
- public static String mes_app_title = "MES 自动化大冲";
- private static MesClientProperties props;
- private static final List<StationPanel> stationPanels = new ArrayList<StationPanel>();
- /** 工作记录页(结案后刷新) */
- private static WorkRecordPanel workRecordPanel;
- private static Timer tickTimer;
- private static final AtomicBoolean exiting = new AtomicBoolean(false);
- private static JLabel menuStatus;
- public static boolean launch() {
- if (SingleInstanceLock.getInstance().isAppActive()) {
- log.warn("已有客户端实例在运行,本次启动取消");
- return false;
- }
- EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- try {
- UiTheme.applyLookAndFeel();
- mesClientFrame = new MainFrame();
- mesClientFrame.setVisible(true);
- // 先串行连 PLC,再开 tick,避免抢连
- new Thread(new Runnable() {
- @Override
- public void run() {
- for (StationPanel p : stationPanels) {
- try {
- p.initPlc();
- } catch (Exception ex) {
- log.warn("PLC 初始化异常: {}", ex.getMessage());
- }
- }
- startTickTimer();
- }
- }, "dc-plc-init").start();
- } catch (Exception e) {
- log.error("启动主界面失败", e);
- }
- }
- });
- return true;
- }
- public static void applyConfig(MesClientProperties p) {
- if (p == null) {
- throw new IllegalStateException("MesClientProperties 为空,请检查 application.yml 中 mes 配置");
- }
- props = p;
- mes_server_ip = trim(p.getServerIp());
- mes_app_title = trim(p.getAppTitle());
- if (mes_app_title.isEmpty()) {
- mes_app_title = "MES 自动化大冲";
- }
- // 结案小票走系统打印机(可配名,空=默认)
- DcBatchPrinter.setPrinterName(p.getPrinterName());
- log.info("打印机配置 name={}", trim(p.getPrinterName()).isEmpty() ? "(系统默认)" : trim(p.getPrinterName()));
- List<MesClientProperties.StationConfig> list = p.getStations();
- if (list == null || list.isEmpty()) {
- log.warn("mes.stations 为空");
- } else {
- log.info("加载工位数={}", list.size());
- for (MesClientProperties.StationConfig s : list) {
- log.info("工位 sn={} plc={}:{} allow={}",
- s.getStationSn(), s.getPlcIp(), Integer.valueOf(s.getPlcPort()),
- s.getMesAllowAddr());
- }
- }
- }
- /** 状态栏文案:level<0 偏红提示 */
- public static void setMenuStatus(final String text, final int level) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- if (menuStatus == null) {
- return;
- }
- menuStatus.setText(text == null ? "" : text);
- if (level < 0) {
- menuStatus.setForeground(new Color(220, 80, 80));
- } else {
- menuStatus.setForeground(new Color(180, 220, 230));
- }
- }
- });
- }
- public MainFrame() {
- setIconImage(Toolkit.getDefaultToolkit().getImage(UiImages.icon("logo.png")));
- setTitle(mes_app_title);
- // 设计 1920×1020,按 150% 等比缩小(不做系统 DPI 自适应)→ 1280×680
- setBounds(40, 40, 1280, 680);
- setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
- addWindowListener(new WindowAdapter() {
- @Override
- public void windowClosing(WindowEvent e) {
- if (exiting.get()) {
- return;
- }
- int choice = JOptionPane.showConfirmDialog(MainFrame.this, "确定退出?", "关闭确认",
- JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
- if (choice != JOptionPane.YES_OPTION) {
- return;
- }
- if (!exiting.compareAndSet(false, true)) {
- return;
- }
- setVisible(false);
- dispose();
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- stopTickTimer();
- } catch (Throwable t) {
- log.warn("退出清理异常: {}", t.getMessage());
- } finally {
- System.exit(0);
- }
- }
- }, "mes-client-exit").start();
- }
- });
- // 去掉系统菜单栏(Nimbus 渐变很丑);退出走窗口关闭;状态条隐藏仅给 setMenuStatus 用
- menuStatus = new JLabel("就绪");
- menuStatus.setVisible(false);
- JPanel content = new JPanel(new BorderLayout(0, 0));
- content.setBorder(new EmptyBorder(0, 0, 0, 0));
- content.setBackground(UiTheme.BG);
- setContentPane(content);
- stationPanels.clear();
- List<MesClientProperties.StationConfig> stations =
- props == null ? null : props.getStations();
- if (stations == null || stations.isEmpty()) {
- JLabel empty = new JLabel("请在 application.yml 配置 mes.stations", SwingConstants.CENTER);
- empty.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- content.add(empty, BorderLayout.CENTER);
- return;
- }
- StationPanel workPanel = null;
- for (MesClientProperties.StationConfig sc : stations) {
- workPanel = new StationPanel(sc, mes_server_ip);
- stationPanels.add(workPanel);
- break;
- }
- workRecordPanel = new WorkRecordPanel();
- // CardLayout 切换页面;顶栏自绘导航,避免 Nimbus 页签 + 外挂按钮错位难看
- final JPanel cards = new JPanel(new CardLayout());
- cards.setOpaque(false);
- cards.add(workPanel, "work");
- cards.add(workRecordPanel, "record");
- final JButton btnSettings = navBtn("设置", false);
- final JButton btnWork = navBtn("工作面板", true);
- final JButton btnRecord = navBtn("工作记录", false);
- final JPopupMenu settingsMenu = new JPopupMenu();
- JMenuItem miManual = new JMenuItem("手动提交");
- miManual.setFont(new Font("微软雅黑", Font.PLAIN, 14));
- miManual.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (!stationPanels.isEmpty()) {
- stationPanels.get(0).onManualSubmitClick();
- }
- }
- });
- settingsMenu.add(miManual);
- btnSettings.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- settingsMenu.show(btnSettings, 0, btnSettings.getHeight());
- }
- });
- btnWork.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- ((CardLayout) cards.getLayout()).show(cards, "work");
- styleNavSelected(btnWork, true);
- styleNavSelected(btnRecord, false);
- }
- });
- btnRecord.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- ((CardLayout) cards.getLayout()).show(cards, "record");
- styleNavSelected(btnWork, false);
- styleNavSelected(btnRecord, true);
- }
- });
- JPanel nav = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
- nav.setOpaque(true);
- nav.setBackground(UiTheme.BG);
- nav.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, UiTheme.LINE));
- nav.add(btnSettings);
- nav.add(btnWork);
- nav.add(btnRecord);
- JPanel page = new JPanel(new BorderLayout(0, 0));
- page.setOpaque(false);
- page.setBorder(new EmptyBorder(10, 12, 12, 12));
- page.add(nav, BorderLayout.NORTH);
- page.add(cards, BorderLayout.CENTER);
- content.add(page, BorderLayout.CENTER);
- }
- /** 顶栏导航按钮(设置 / 工作面板 / 工作记录 统一样式) */
- private static JButton navBtn(String text, boolean selected) {
- JButton b = new JButton(text);
- b.setFont(new Font("微软雅黑", Font.PLAIN, 14));
- b.setFocusPainted(false);
- b.setMargin(new Insets(8, 18, 8, 18));
- b.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
- b.setOpaque(true);
- styleNavSelected(b, selected);
- return b;
- }
- /** 选中:白底蓝底边;未选:浅灰 */
- private static void styleNavSelected(JButton b, boolean selected) {
- if (selected) {
- b.setBackground(Color.WHITE);
- b.setForeground(UiTheme.ACCENT);
- b.setBorder(BorderFactory.createCompoundBorder(
- BorderFactory.createMatteBorder(0, 0, 2, 0, UiTheme.ACCENT),
- new EmptyBorder(8, 18, 6, 18)));
- } else {
- b.setBackground(UiTheme.BG);
- b.setForeground(UiTheme.TEXT);
- b.setBorder(BorderFactory.createCompoundBorder(
- BorderFactory.createMatteBorder(0, 0, 2, 0, UiTheme.BG),
- new EmptyBorder(8, 18, 6, 18)));
- }
- }
- /** 结案后刷新工作记录表 */
- public static void refreshWorkRecords() {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- if (workRecordPanel != null) {
- workRecordPanel.reload();
- }
- }
- });
- }
- private static void startTickTimer() {
- stopTickTimer();
- int period = 500;
- if (props != null && props.getTickMs() > 0) {
- period = props.getTickMs();
- }
- tickTimer = new Timer("dc-station-tick", true);
- tickTimer.scheduleAtFixedRate(new TimerTask() {
- @Override
- public void run() {
- for (StationPanel p : stationPanels) {
- try {
- p.tick();
- } catch (Exception ex) {
- log.debug("tick skip: {}", ex.getMessage());
- }
- }
- }
- }, 800L, period);
- }
- private static void stopTickTimer() {
- if (tickTimer != null) {
- tickTimer.cancel();
- tickTimer = null;
- }
- }
- private static String trim(String s) {
- return s == null ? "" : s.trim();
- }
- }
|