CheckRecordPanel.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package com.mes.ui;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import javax.swing.*;
  5. import javax.swing.table.DefaultTableCellRenderer;
  6. import javax.swing.table.DefaultTableModel;
  7. import javax.swing.table.TableColumn;
  8. import java.awt.*;
  9. import java.awt.event.ComponentAdapter;
  10. import java.awt.event.ComponentEvent;
  11. import java.util.List;
  12. /**
  13. * 点检记录面板组件
  14. * 支持分页查询和展示点检记录数据
  15. */
  16. public class CheckRecordPanel extends JPanel {
  17. private static final Logger log = LoggerFactory.getLogger(CheckRecordPanel.class);
  18. // 表格列名
  19. private static final String[] COLUMN_NAMES = {"检查项", "检查结果", "作业人员", "日期"};
  20. // 分页配置
  21. private static final int PAGE_SIZE = 20;
  22. // 查询条件
  23. private String oprno; // 工位号
  24. private String lineSn; // 产线编号
  25. // 分页状态
  26. private int pageNo = 1;
  27. private long totalCount = 0;
  28. private int totalPages = 0;
  29. // UI组件
  30. private JTable table;
  31. private DefaultTableModel tableModel;
  32. private JLabel pageLabel;
  33. private JButton btnPrevious;
  34. private JButton btnNext;
  35. private JButton btnRefresh;
  36. private JLabel statusLabel;
  37. /**
  38. * 构造函数
  39. * @param oprno 工位号
  40. * @param lineSn 产线编号
  41. */
  42. public CheckRecordPanel(String oprno, String lineSn) {
  43. this.oprno = oprno;
  44. this.lineSn = lineSn;
  45. initUI();
  46. }
  47. /**
  48. * 初始化UI
  49. */
  50. private void initUI() {
  51. setLayout(null);
  52. setBackground(Color.WHITE);
  53. // 顶部控制区
  54. JPanel topPanel = new JPanel();
  55. topPanel.setLayout(null);
  56. topPanel.setBounds(0, 0, 1200, 70);
  57. topPanel.setBackground(new Color(248, 248, 248));
  58. add(topPanel);
  59. int xPos = 10;
  60. // 状态标签
  61. statusLabel = new JLabel("工位:" + oprno);
  62. statusLabel.setFont(new Font("微软雅黑", Font.PLAIN, 25));
  63. statusLabel.setBounds(xPos, 10, 250, 30);
  64. topPanel.add(statusLabel);
  65. xPos += 260;
  66. // 刷新按钮
  67. btnRefresh = new JButton("刷新");
  68. btnRefresh.setFont(new Font("微软雅黑", Font.PLAIN, 25));
  69. btnRefresh.setBounds(xPos, 10, 120, 40);
  70. btnRefresh.addActionListener(e -> {
  71. refreshData();
  72. });
  73. topPanel.add(btnRefresh);
  74. xPos += 130;
  75. // 上一页按钮
  76. btnPrevious = new JButton("上一页");
  77. btnPrevious.setFont(new Font("微软雅黑", Font.PLAIN, 25));
  78. btnPrevious.setBounds(xPos, 10, 120, 40);
  79. btnPrevious.addActionListener(e -> {
  80. if (pageNo > 1) {
  81. pageNo--;
  82. loadData();
  83. }
  84. });
  85. topPanel.add(btnPrevious);
  86. xPos += 130;
  87. // 页码显示
  88. pageLabel = new JLabel("0/0");
  89. pageLabel.setFont(new Font("微软雅黑", Font.PLAIN, 25));
  90. pageLabel.setHorizontalAlignment(SwingConstants.CENTER);
  91. pageLabel.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
  92. pageLabel.setBounds(xPos, 10, 120, 40);
  93. topPanel.add(pageLabel);
  94. xPos += 130;
  95. // 下一页按钮
  96. btnNext = new JButton("下一页");
  97. btnNext.setFont(new Font("微软雅黑", Font.PLAIN, 25));
  98. btnNext.setBounds(xPos, 10, 120, 40);
  99. btnNext.addActionListener(e -> {
  100. if (pageNo < totalPages) {
  101. pageNo++;
  102. loadData();
  103. }
  104. });
  105. topPanel.add(btnNext);
  106. // 表格面板
  107. JPanel tablePanel = new JPanel();
  108. tablePanel.setBounds(0, 50, 1200, 518);
  109. tablePanel.setLayout(new GridLayout(1, 1, 0, 0));
  110. add(tablePanel);
  111. // 创建表格模型
  112. tableModel = new DefaultTableModel(COLUMN_NAMES, 0) {
  113. @Override
  114. public boolean isCellEditable(int row, int column) {
  115. return false;
  116. }
  117. };
  118. // 创建表格
  119. table = new JTable(tableModel);
  120. table.setRowHeight(32);
  121. table.setFont(new Font("微软雅黑", Font.PLAIN, 25));
  122. table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 30));
  123. table.getTableHeader().setReorderingAllowed(false);
  124. // 设置列宽
  125. setColumnWidths();
  126. // 设置居中渲染器
  127. DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
  128. centerRenderer.setHorizontalAlignment(JLabel.CENTER);
  129. for (int i = 0; i < table.getColumnCount(); i++) {
  130. table.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
  131. }
  132. // 添加滚动面板
  133. JScrollPane scrollPane = new JScrollPane(table);
  134. tablePanel.add(scrollPane);
  135. // 添加组件大小变化监听器
  136. addComponentListener(new ComponentAdapter() {
  137. @Override
  138. public void componentResized(ComponentEvent e) {
  139. int w = getWidth();
  140. int h = getHeight();
  141. // 调整顶部面板宽度
  142. topPanel.setBounds(0, 0, w, 60);
  143. // 调整表格面板
  144. if (h > 50) {
  145. tablePanel.setBounds(0, 60, w, h - 60);
  146. }
  147. topPanel.revalidate();
  148. tablePanel.revalidate();
  149. }
  150. });
  151. }
  152. /**
  153. * 设置列宽
  154. */
  155. private void setColumnWidths() {
  156. TableColumn column;
  157. // 检查项
  158. column = table.getColumnModel().getColumn(0);
  159. column.setPreferredWidth(300);
  160. // 检查结果
  161. column = table.getColumnModel().getColumn(1);
  162. column.setPreferredWidth(100);
  163. // 作业人员
  164. column = table.getColumnModel().getColumn(2);
  165. column.setPreferredWidth(100);
  166. // 日期
  167. column = table.getColumnModel().getColumn(3);
  168. column.setPreferredWidth(150);
  169. }
  170. /**
  171. * 刷新数据(重置到第一页)
  172. */
  173. public void refreshData() {
  174. pageNo = 1;
  175. loadData();
  176. }
  177. /**
  178. * 加载数据
  179. */
  180. public void loadData() {
  181. // 在后台线程中执行数据加载
  182. SwingWorker<CheckRecordResp, Void> worker = new SwingWorker<CheckRecordResp, Void>() {
  183. @Override
  184. protected CheckRecordResp doInBackground() throws Exception {
  185. // 更新状态
  186. SwingUtilities.invokeLater(() -> {
  187. statusLabel.setText("正在加载...");
  188. setButtonsEnabled(false);
  189. });
  190. // 调用接口查询数据
  191. return DataUtil.getCheckRecordList(oprno, pageNo, PAGE_SIZE);
  192. }
  193. @Override
  194. protected void done() {
  195. try {
  196. CheckRecordResp resp = get();
  197. updateUI(resp);
  198. } catch (Exception e) {
  199. log.error("加载点检记录失败: {}", e.getMessage());
  200. updateUIError("加载失败: " + e.getMessage());
  201. }
  202. }
  203. };
  204. worker.execute();
  205. }
  206. /**
  207. * 设置按钮启用状态
  208. */
  209. private void setButtonsEnabled(boolean enabled) {
  210. btnRefresh.setEnabled(enabled);
  211. btnPrevious.setEnabled(enabled && pageNo > 1);
  212. btnNext.setEnabled(enabled && pageNo < totalPages);
  213. }
  214. /**
  215. * 更新UI显示数据
  216. */
  217. private void updateUI(CheckRecordResp resp) {
  218. // 清空表格
  219. tableModel.setRowCount(0);
  220. if (resp == null || !resp.isResult()) {
  221. String msg = resp != null ? resp.getMessage() : "请求失败";
  222. updateUIError(msg);
  223. return;
  224. }
  225. // 更新分页信息
  226. totalCount = resp.getCount();
  227. totalPages = (int) Math.ceil((double) totalCount / PAGE_SIZE);
  228. if (totalPages == 0) totalPages = 1;
  229. // 更新页码显示
  230. pageLabel.setText(pageNo + "/" + totalPages);
  231. // 更新按钮状态
  232. setButtonsEnabled(true);
  233. // 更新状态标签
  234. statusLabel.setText("工位:" + oprno + " 共" + totalCount + "条");
  235. // 填充表格数据
  236. List<CheckRecordData> list = resp.getList();
  237. if (list != null && !list.isEmpty()) {
  238. for (CheckRecordData data : list) {
  239. Object[] row = {
  240. data.getCheckItem(),
  241. data.getCheckResult(),
  242. data.getUpdateBy(),
  243. data.getUpdateDate()
  244. };
  245. tableModel.addRow(row);
  246. }
  247. }
  248. // 刷新表格
  249. table.repaint();
  250. }
  251. /**
  252. * 更新UI显示错误信息
  253. */
  254. private void updateUIError(String message) {
  255. statusLabel.setText("错误: " + message);
  256. pageLabel.setText("0/0");
  257. setButtonsEnabled(true);
  258. btnPrevious.setEnabled(false);
  259. btnNext.setEnabled(false);
  260. tableModel.setRowCount(0);
  261. }
  262. /**
  263. * 获取当前工位号
  264. */
  265. public String getOprno() {
  266. return oprno;
  267. }
  268. }