| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- package com.mes.ui;
- import com.mes.core.StationConfig;
- import com.mes.ui.component.WorkstationPanel;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import javax.swing.*;
- import java.awt.*;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * UI工厂 - 根据配置创建工位UI
- * 支持单工位/双工位布局自动切换
- */
- public class UIFactory {
- private static final Logger log = LoggerFactory.getLogger(UIFactory.class);
- /**
- * 创建主面板
- * @param config 工位配置
- * @return 主面板和工位面板列表
- */
- public static UIBuildResult createMainPanel(StationConfig config) {
- List<WorkstationPanel> panels = new ArrayList<>();
- JPanel mainPanel;
- if (config.isSingleMode()) {
- mainPanel = createSingleStationLayout(config, panels);
- } else {
- mainPanel = createDualStationLayout(config, panels);
- }
- return new UIBuildResult(mainPanel, panels);
- }
- /**
- * 创建单工位布局
- */
- private static JPanel createSingleStationLayout(StationConfig config, List<WorkstationPanel> panels) {
- JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
- mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
- // 获取工位信息
- StationConfig.StationInfo stationInfo = config.getStation(0);
- if (stationInfo != null) {
- WorkstationPanel panel = WorkstationPanel.fromConfig(stationInfo, config);
- panels.add(panel);
- mainPanel.add(panel, BorderLayout.CENTER);
- }
- log.info("创建单工位布局");
- return mainPanel;
- }
- /**
- * 创建双工位布局
- */
- private static JPanel createDualStationLayout(StationConfig config, List<WorkstationPanel> panels) {
- JPanel mainPanel = new JPanel(new GridLayout(1, 2, 20, 0));
- mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
- // 工位1
- StationConfig.StationInfo station1 = config.getStation(0);
- if (station1 != null) {
- WorkstationPanel panel1 = WorkstationPanel.fromConfig(station1, config);
- panels.add(panel1);
- mainPanel.add(panel1);
- }
- // 工位2
- StationConfig.StationInfo station2 = config.getStation(1);
- if (station2 != null) {
- WorkstationPanel panel2 = WorkstationPanel.fromConfig(station2, config);
- panels.add(panel2);
- mainPanel.add(panel2);
- }
- log.info("创建双工位布局");
- return mainPanel;
- }
- /**
- * 创建顶部工具栏
- */
- public static JPanel createToolbar(StationConfig config) {
- JPanel toolbar = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
- toolbar.setBorder(BorderFactory.createEtchedBorder());
- // 用户信息
- JLabel userLabel = new JLabel("用户: ");
- toolbar.add(userLabel);
- // 心跳指示器
- JButton heartbeatIndicator = new JButton("●");
- heartbeatIndicator.setForeground(Color.GRAY);
- heartbeatIndicator.setBorderPainted(false);
- heartbeatIndicator.setContentAreaFilled(false);
- heartbeatIndicator.setFocusable(false);
- heartbeatIndicator.setToolTipText("TCP连接状态");
- toolbar.add(heartbeatIndicator);
- // 工位信息
- JLabel stationLabel = new JLabel("工位: " + getStationCodesString(config));
- toolbar.add(stationLabel);
- return toolbar;
- }
- /**
- * 获取工位号字符串
- */
- private static String getStationCodesString(StationConfig config) {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < config.getStationCount(); i++) {
- if (i > 0) {
- sb.append(" / ");
- }
- StationConfig.StationInfo station = config.getStation(i);
- if (station != null) {
- sb.append(station.getCode());
- }
- }
- return sb.toString();
- }
- /**
- * UI构建结果
- */
- public static class UIBuildResult {
- private final JPanel mainPanel;
- private final List<WorkstationPanel> workstationPanels;
- public UIBuildResult(JPanel mainPanel, List<WorkstationPanel> workstationPanels) {
- this.mainPanel = mainPanel;
- this.workstationPanels = workstationPanels;
- }
- public JPanel getMainPanel() {
- return mainPanel;
- }
- public List<WorkstationPanel> getWorkstationPanels() {
- return workstationPanels;
- }
- public WorkstationPanel getPanel(int index) {
- if (index >= 0 && index < workstationPanels.size()) {
- return workstationPanels.get(index);
- }
- return null;
- }
- }
- }
|