UIFactory.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package com.mes.ui;
  2. import com.mes.core.StationConfig;
  3. import com.mes.ui.component.WorkstationPanel;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import javax.swing.*;
  7. import java.awt.*;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. /**
  11. * UI工厂 - 根据配置创建工位UI
  12. * 支持单工位/双工位布局自动切换
  13. */
  14. public class UIFactory {
  15. private static final Logger log = LoggerFactory.getLogger(UIFactory.class);
  16. /**
  17. * 创建主面板
  18. * @param config 工位配置
  19. * @return 主面板和工位面板列表
  20. */
  21. public static UIBuildResult createMainPanel(StationConfig config) {
  22. List<WorkstationPanel> panels = new ArrayList<>();
  23. JPanel mainPanel;
  24. if (config.isSingleMode()) {
  25. mainPanel = createSingleStationLayout(config, panels);
  26. } else {
  27. mainPanel = createDualStationLayout(config, panels);
  28. }
  29. return new UIBuildResult(mainPanel, panels);
  30. }
  31. /**
  32. * 创建单工位布局
  33. */
  34. private static JPanel createSingleStationLayout(StationConfig config, List<WorkstationPanel> panels) {
  35. JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
  36. mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  37. // 获取工位信息
  38. StationConfig.StationInfo stationInfo = config.getStation(0);
  39. if (stationInfo != null) {
  40. WorkstationPanel panel = WorkstationPanel.fromConfig(stationInfo, config);
  41. panels.add(panel);
  42. mainPanel.add(panel, BorderLayout.CENTER);
  43. }
  44. log.info("创建单工位布局");
  45. return mainPanel;
  46. }
  47. /**
  48. * 创建双工位布局
  49. */
  50. private static JPanel createDualStationLayout(StationConfig config, List<WorkstationPanel> panels) {
  51. JPanel mainPanel = new JPanel(new GridLayout(1, 2, 20, 0));
  52. mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  53. // 工位1
  54. StationConfig.StationInfo station1 = config.getStation(0);
  55. if (station1 != null) {
  56. WorkstationPanel panel1 = WorkstationPanel.fromConfig(station1, config);
  57. panels.add(panel1);
  58. mainPanel.add(panel1);
  59. }
  60. // 工位2
  61. StationConfig.StationInfo station2 = config.getStation(1);
  62. if (station2 != null) {
  63. WorkstationPanel panel2 = WorkstationPanel.fromConfig(station2, config);
  64. panels.add(panel2);
  65. mainPanel.add(panel2);
  66. }
  67. log.info("创建双工位布局");
  68. return mainPanel;
  69. }
  70. /**
  71. * 创建顶部工具栏
  72. */
  73. public static JPanel createToolbar(StationConfig config) {
  74. JPanel toolbar = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
  75. toolbar.setBorder(BorderFactory.createEtchedBorder());
  76. // 用户信息
  77. JLabel userLabel = new JLabel("用户: ");
  78. toolbar.add(userLabel);
  79. // 心跳指示器
  80. JButton heartbeatIndicator = new JButton("●");
  81. heartbeatIndicator.setForeground(Color.GRAY);
  82. heartbeatIndicator.setBorderPainted(false);
  83. heartbeatIndicator.setContentAreaFilled(false);
  84. heartbeatIndicator.setFocusable(false);
  85. heartbeatIndicator.setToolTipText("TCP连接状态");
  86. toolbar.add(heartbeatIndicator);
  87. // 工位信息
  88. JLabel stationLabel = new JLabel("工位: " + getStationCodesString(config));
  89. toolbar.add(stationLabel);
  90. return toolbar;
  91. }
  92. /**
  93. * 获取工位号字符串
  94. */
  95. private static String getStationCodesString(StationConfig config) {
  96. StringBuilder sb = new StringBuilder();
  97. for (int i = 0; i < config.getStationCount(); i++) {
  98. if (i > 0) {
  99. sb.append(" / ");
  100. }
  101. StationConfig.StationInfo station = config.getStation(i);
  102. if (station != null) {
  103. sb.append(station.getCode());
  104. }
  105. }
  106. return sb.toString();
  107. }
  108. /**
  109. * UI构建结果
  110. */
  111. public static class UIBuildResult {
  112. private final JPanel mainPanel;
  113. private final List<WorkstationPanel> workstationPanels;
  114. public UIBuildResult(JPanel mainPanel, List<WorkstationPanel> workstationPanels) {
  115. this.mainPanel = mainPanel;
  116. this.workstationPanels = workstationPanels;
  117. }
  118. public JPanel getMainPanel() {
  119. return mainPanel;
  120. }
  121. public List<WorkstationPanel> getWorkstationPanels() {
  122. return workstationPanels;
  123. }
  124. public WorkstationPanel getPanel(int index) {
  125. if (index >= 0 && index < workstationPanels.size()) {
  126. return workstationPanels.get(index);
  127. }
  128. return null;
  129. }
  130. }
  131. }