StationPanel.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.mes.config.MesClientProperties;
  4. import com.mes.entity.DcRunSnapshot;
  5. import com.mes.service.DcBatchPrinter;
  6. import com.mes.service.DcPackCode;
  7. import com.mes.service.DcPlcClient;
  8. import com.mes.service.DcWorkRecordStore;
  9. import com.mes.service.MesDcApiClient;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import javax.swing.*;
  13. import javax.swing.border.EmptyBorder;
  14. import java.awt.*;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Arrays;
  17. import java.util.Date;
  18. import java.util.LinkedHashMap;
  19. import java.util.Map;
  20. import java.util.concurrent.atomic.AtomicBoolean;
  21. /**
  22. * 自动化大冲单线面板:轮询取批次 → MES允许=true → 读 DB260 盯产量 → 结案再允许=false → 打印。
  23. */
  24. public class StationPanel extends JPanel {
  25. private static final Logger log = LoggerFactory.getLogger(StationPanel.class);
  26. /** 空闲时拉取 PDA 批次的最小间隔 */
  27. private static final long POLL_BATCH_INTERVAL_MS = 2000L;
  28. /** IDLE 等 PDA;RUN 允许运行盯产量;DONE 结案过渡 */
  29. public enum Phase {
  30. IDLE, RUN, DONE
  31. }
  32. private final MesClientProperties.StationConfig cfg;
  33. private final String serverIp;
  34. private final DcPlcClient plc;
  35. private final AtomicBoolean busy = new AtomicBoolean(false);
  36. private volatile Phase phase = Phase.IDLE;
  37. private volatile String batchCode = "";
  38. private volatile int targetQty;
  39. private volatile int baseActualQty;
  40. private volatile int producedQty;
  41. private volatile String batchStartTime = "";
  42. private volatile long lastPollBatchAtMs;
  43. /** 最近一次 DB260 快照,结案时一并上传(只传一次,不采样堆) */
  44. private volatile DcRunSnapshot lastSnap = new DcRunSnapshot();
  45. private final JLabel lblStation;
  46. /** 顶栏 PLC 地址文案(不含连接状态) */
  47. private final JLabel lblPlc;
  48. private final JLabel lblStatus;
  49. private final JTextField txtBatch;
  50. private final JTextField txtTarget;
  51. private final JTextField txtCurrentPart;
  52. private final JTextField txtNextPart;
  53. private final JTextField txtActualQty;
  54. private final JTextField txtProduced;
  55. private final JTextField txtDowntime;
  56. private final JTextField txtTakt;
  57. /** PLC 连接指示灯:绿=已连,红=未连 */
  58. private final StatusLight lampPlc;
  59. /** PLC 连接状态说明 */
  60. private final JLabel lblPlcState;
  61. /** 产线状态指示灯:绿=正常,灰=待机,红=故障 */
  62. private final StatusLight lampLineState;
  63. /** 产线状态说明 */
  64. private final JLabel lblLineState;
  65. /** MES 允许运行指示灯:绿=已开,灰=关 */
  66. private final StatusLight lampMesAllow;
  67. /** MES 允许状态说明 */
  68. private final JLabel lblMesAllowState;
  69. public StationPanel(MesClientProperties.StationConfig cfg, String serverIp) {
  70. this.cfg = cfg;
  71. this.serverIp = serverIp == null ? "" : serverIp.trim();
  72. this.plc = new DcPlcClient(cfg.getStationSn(), cfg.getPlcIp(), cfg.getPlcPort(),
  73. cfg.getMesAllowAddr());
  74. setLayout(new BorderLayout(0, 0));
  75. setBackground(UiTheme.BG);
  76. setBorder(new EmptyBorder(0, 0, 0, 0));
  77. String titleText = nvl(cfg.getTitle());
  78. if (titleText.isEmpty()) {
  79. titleText = "自动化大冲";
  80. }
  81. JPanel header = UiTheme.gradientHeader();
  82. header.setBorder(new EmptyBorder(16, 20, 16, 20));
  83. lblStation = new JLabel(titleText);
  84. lblStation.setFont(UiTheme.FONT_TITLE);
  85. lblStation.setForeground(Color.WHITE);
  86. // Consolas 不支持中文,副标题只用工位编码,避免乱码方框
  87. JLabel lblCode = new JLabel(nvl(cfg.getStationSn()));
  88. lblCode.setFont(new Font("Consolas", Font.BOLD, 13));
  89. lblCode.setForeground(new Color(140, 210, 220));
  90. JPanel titleCol = new JPanel();
  91. titleCol.setOpaque(false);
  92. titleCol.setLayout(new BoxLayout(titleCol, BoxLayout.Y_AXIS));
  93. titleCol.add(lblStation);
  94. titleCol.add(Box.createVerticalStrut(3));
  95. titleCol.add(lblCode);
  96. lampPlc = new StatusLight();
  97. lblPlcState = headerStateLabel("未连接");
  98. lampLineState = new StatusLight();
  99. lblLineState = headerStateLabel("待机");
  100. lampMesAllow = new StatusLight();
  101. lblMesAllowState = headerStateLabel("关");
  102. lblPlc = new JLabel(plcAddrText());
  103. lblPlc.setFont(new Font("Consolas", Font.PLAIN, 12));
  104. lblPlc.setForeground(new Color(160, 210, 220));
  105. JPanel east = new JPanel(new FlowLayout(FlowLayout.RIGHT, 14, 0));
  106. east.setOpaque(false);
  107. east.add(headerLampItem("PLC", lampPlc, lblPlcState));
  108. east.add(headerLampItem("产线", lampLineState, lblLineState));
  109. east.add(headerLampItem("MES允许", lampMesAllow, lblMesAllowState));
  110. // 有 IP 时才跟地址
  111. if (!plcAddrText().isEmpty()) {
  112. east.add(lblPlc);
  113. }
  114. header.add(titleCol, BorderLayout.WEST);
  115. header.add(east, BorderLayout.EAST);
  116. // 状态条紧贴顶栏下方,一眼能看到当前阶段
  117. lblStatus = new JLabel("空闲:等待扫码下发批次…");
  118. lblStatus.setFont(UiTheme.FONT_BODY);
  119. lblStatus.setOpaque(true);
  120. lblStatus.setBackground(UiTheme.OK_BG);
  121. lblStatus.setForeground(UiTheme.OK_FG);
  122. lblStatus.setBorder(new EmptyBorder(9, 16, 9, 16));
  123. JPanel north = new JPanel(new BorderLayout(0, 0));
  124. north.setOpaque(false);
  125. north.add(header, BorderLayout.NORTH);
  126. north.add(lblStatus, BorderLayout.SOUTH);
  127. add(north, BorderLayout.NORTH);
  128. JPanel main = new JPanel(new BorderLayout(0, 14));
  129. main.setOpaque(false);
  130. main.setBorder(new EmptyBorder(16, 20, 16, 20));
  131. // —— 本批:上方两格,固定偏好高度 ——
  132. JPanel batchCard = UiTheme.card();
  133. batchCard.setLayout(new BorderLayout(0, 10));
  134. batchCard.add(UiTheme.sectionTitle("本批"), BorderLayout.NORTH);
  135. JPanel batchGrid = new JPanel(new GridLayout(1, 2, 14, 0));
  136. batchGrid.setOpaque(false);
  137. batchGrid.setPreferredSize(new Dimension(10, UiTheme.TILE_PREF_H));
  138. txtBatch = roField();
  139. txtTarget = roField();
  140. batchGrid.add(UiTheme.paramTile("批次码", txtBatch, UiTheme.STRIPE_SIDE));
  141. batchGrid.add(UiTheme.paramTile("本批目标数量", txtTarget, UiTheme.STRIPE_SIDE));
  142. batchCard.add(batchGrid, BorderLayout.CENTER);
  143. // —— 线体过程:占满剩余高度,大字示值填空 ——
  144. JPanel runCard = UiTheme.card();
  145. runCard.setLayout(new BorderLayout(0, 10));
  146. runCard.add(UiTheme.sectionTitle("线体过程"), BorderLayout.NORTH);
  147. JPanel grid = new JPanel(new GridLayout(2, 3, 14, 14));
  148. grid.setOpaque(false);
  149. txtCurrentPart = roField();
  150. txtNextPart = roField();
  151. txtActualQty = roField();
  152. txtProduced = roField();
  153. txtProduced.setFont(UiTheme.FONT_SN);
  154. txtDowntime = roField();
  155. txtTakt = roField();
  156. grid.add(UiTheme.paramTile("当前零件", txtCurrentPart, UiTheme.STRIPE_SIDE));
  157. grid.add(UiTheme.paramTile("下一零件", txtNextPart, UiTheme.STRIPE_SIDE));
  158. grid.add(UiTheme.paramTile("累计实际产量", txtActualQty, UiTheme.STRIPE_SIDE));
  159. grid.add(UiTheme.paramTile("本批已产", txtProduced, UiTheme.STRIPE_SIDE));
  160. grid.add(UiTheme.paramTile("停机时间", txtDowntime, UiTheme.STRIPE_SIDE));
  161. grid.add(UiTheme.paramTile("生产节拍", txtTakt, UiTheme.STRIPE_SIDE));
  162. runCard.add(grid, BorderLayout.CENTER);
  163. main.add(batchCard, BorderLayout.NORTH);
  164. main.add(runCard, BorderLayout.CENTER);
  165. add(main, BorderLayout.CENTER);
  166. }
  167. public String getStationSn() {
  168. return nvl(cfg.getStationSn());
  169. }
  170. /** 单设备客户端:页签名固定为工作面板 */
  171. public String tabTitle() {
  172. return "工作面板";
  173. }
  174. public void initPlc() {
  175. plc.connect();
  176. uiPlcLabel();
  177. refreshSnapshotUi(plc.readSnapshot());
  178. }
  179. /**
  180. * 周期:重连、刷 DB260;空闲拉 PDA 批次;RUN 算产量差达标结案。
  181. */
  182. public void tick() {
  183. if (busy.get()) {
  184. return;
  185. }
  186. try {
  187. plc.maintainConnection();
  188. uiPlcLabel();
  189. DcRunSnapshot snap = plc.readSnapshot();
  190. if (snap != null) {
  191. lastSnap = snap;
  192. }
  193. refreshSnapshotUi(snap);
  194. if (phase == Phase.IDLE) {
  195. pollPdaBatchIfDue();
  196. return;
  197. }
  198. if (phase == Phase.RUN) {
  199. int produced = snap.actualQty - baseActualQty;
  200. if (produced < 0) {
  201. produced = 0;
  202. }
  203. producedQty = produced;
  204. setText(txtProduced, String.valueOf(producedQty));
  205. setStatusUi("生产中 批次=" + batchCode + " 已产 " + producedQty + " / 目标 " + targetQty);
  206. if (producedQty >= targetQty) {
  207. finishBatch(producedQty, false);
  208. }
  209. }
  210. } catch (Exception e) {
  211. log.warn("工位[{}] tick 异常: {}", cfg.getStationSn(), e.getMessage());
  212. }
  213. }
  214. /** 空闲:向服务端要 PDA 已扫的待开批次 */
  215. private void pollPdaBatchIfDue() {
  216. long now = System.currentTimeMillis();
  217. if (now - lastPollBatchAtMs < POLL_BATCH_INTERVAL_MS) {
  218. return;
  219. }
  220. lastPollBatchAtMs = now;
  221. if (serverIp.isEmpty()) {
  222. setStatusUi("空闲:等待扫码(server-ip 未配,仅可联调模拟)");
  223. return;
  224. }
  225. JSONObject ret = MesDcApiClient.nextBatch(serverIp, cfg.getStationSn());
  226. if (!MesDcApiClient.isSuccess(ret)) {
  227. setStatusUi("空闲:等待扫码下发批次…");
  228. return;
  229. }
  230. String code = MesDcApiClient.dataStr(ret, "batchCode");
  231. if (code.isEmpty()) {
  232. code = MesDcApiClient.dataStr(ret, "sn");
  233. }
  234. int target = MesDcApiClient.dataInt(ret, "targetQty", 0);
  235. if (target <= 0) {
  236. target = MesDcApiClient.dataInt(ret, "qty", 0);
  237. }
  238. if (code.isEmpty() || target <= 0) {
  239. log.warn("工位[{}] nextBatch 成功但缺 batchCode/targetQty: {}", cfg.getStationSn(), ret);
  240. return;
  241. }
  242. startBatchFromPda(code, target);
  243. }
  244. /**
  245. * 批次到位:记起点产量 → MES允许=true → 进入 RUN;本批做完再写 false。
  246. */
  247. private void startBatchFromPda(String code, int target) {
  248. if (phase == Phase.RUN) {
  249. return;
  250. }
  251. if (!busy.compareAndSet(false, true)) {
  252. return;
  253. }
  254. try {
  255. if (!plc.isConnected()) {
  256. plc.connect();
  257. }
  258. Integer base = plc.readActualQty();
  259. if (base == null) {
  260. setStatusUi("收到批次,但读实际产量失败,请检查 PLC");
  261. MainFrame.setMenuStatus(cfg.getStationSn() + " 开批失败:读产量", -1);
  262. return;
  263. }
  264. batchCode = code;
  265. targetQty = target;
  266. baseActualQty = base.intValue();
  267. producedQty = 0;
  268. batchStartTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
  269. boolean wrote = plc.setMesAllow(true);
  270. phase = Phase.RUN;
  271. setText(txtBatch, batchCode);
  272. setText(txtTarget, String.valueOf(targetQty));
  273. setText(txtProduced, "0");
  274. setMesAllowLamp(wrote);
  275. setStatusUi((wrote ? "批次已开,MES允许已写" : "批次已开(允许点未配/写失败,本地仍盯产量)")
  276. + " 起始产量=" + baseActualQty);
  277. MainFrame.setMenuStatus(cfg.getStationSn() + " 开批 " + batchCode + " 目标" + targetQty, 0);
  278. log.info("工位[{}] 开批 batch={} target={} baseQty={}",
  279. cfg.getStationSn(), batchCode, Integer.valueOf(targetQty), Integer.valueOf(baseActualQty));
  280. } finally {
  281. busy.set(false);
  282. }
  283. }
  284. /**
  285. * 手动提交:校验密码 mes 后,按当前已产数量结案(过站结果 OK)。
  286. * 由主窗页签左侧「设置」菜单调用。
  287. */
  288. public void onManualSubmitClick() {
  289. if (phase != Phase.RUN) {
  290. setStatusUi("当前无进行中的批次,无法手动提交");
  291. return;
  292. }
  293. if (!askMesPassword()) {
  294. return;
  295. }
  296. finishBatch(producedQty, false, true);
  297. }
  298. /**
  299. * 弹出密码框;正确密码为 mes。
  300. *
  301. * @return true=校验通过
  302. */
  303. private boolean askMesPassword() {
  304. JPasswordField pf = new JPasswordField(12);
  305. JPanel box = new JPanel(new BorderLayout(8, 8));
  306. box.add(new JLabel("请输入密码"), BorderLayout.NORTH);
  307. box.add(pf, BorderLayout.CENTER);
  308. int opt = JOptionPane.showConfirmDialog(this, box, "手动提交",
  309. JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  310. if (opt != JOptionPane.OK_OPTION) {
  311. return false;
  312. }
  313. char[] input = pf.getPassword();
  314. boolean ok = Arrays.equals(input, "mes".toCharArray());
  315. Arrays.fill(input, '\0');
  316. if (!ok) {
  317. JOptionPane.showMessageDialog(this, "密码错误", "手动提交", JOptionPane.WARNING_MESSAGE);
  318. return false;
  319. }
  320. return true;
  321. }
  322. /** 产量达标结案 */
  323. private void finishBatch(final int qty, final boolean aborted) {
  324. finishBatch(qty, aborted, false);
  325. }
  326. /**
  327. * 结案。
  328. *
  329. * @param qty 本批数量
  330. * @param aborted 是否强制中断(结果 NG)
  331. * @param manual 是否手动提交
  332. */
  333. private void finishBatch(final int qty, final boolean aborted, final boolean manual) {
  334. if (!busy.compareAndSet(false, true)) {
  335. return;
  336. }
  337. phase = Phase.DONE;
  338. final String codeSnap = batchCode;
  339. final String timeSnap = batchStartTime;
  340. new Thread(new Runnable() {
  341. @Override
  342. public void run() {
  343. try {
  344. // 本批做完:MES允许改回 false
  345. plc.setMesAllow(false);
  346. String time = timeSnap;
  347. if (time == null || time.isEmpty()) {
  348. time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
  349. }
  350. // 包装码:年月日 + 随机6位,打印与上传都用它做批量追溯
  351. final String packCode = DcPackCode.next();
  352. boolean printed = DcBatchPrinter.printPack(packCode, time, qty);
  353. // 过站结果:中断 NG,否则 OK(含手动提交)
  354. String passResult = aborted ? "NG" : "OK";
  355. DcWorkRecordStore.add(packCode, codeSnap, time, qty, passResult);
  356. MainFrame.refreshWorkRecords();
  357. // 结案只上传一次:包装码 + 批次码 + 本批数量 + DB260 过程参数
  358. DcRunSnapshot snap = lastSnap == null ? new DcRunSnapshot() : lastSnap;
  359. Map<String, String> fields = new LinkedHashMap<String, String>();
  360. fields.put("stationSn", cfg.getStationSn());
  361. fields.put("batchCode", codeSnap);
  362. fields.put("packCode", packCode);
  363. fields.put("qty", String.valueOf(qty));
  364. fields.put("produceTime", time);
  365. fields.put("aborted", aborted ? "1" : "0");
  366. fields.put("manual", manual ? "1" : "0");
  367. fields.put("result", passResult);
  368. fields.put("currentPart", nvl(snap.currentPart));
  369. fields.put("nextPart", nvl(snap.nextPart));
  370. fields.put("actualQty", String.valueOf(snap.actualQty));
  371. fields.put("downtime", String.valueOf(snap.downtime));
  372. fields.put("takt", String.valueOf(snap.takt));
  373. fields.put("lineState", String.valueOf(snap.lineState));
  374. fields.put("lineStateText", nvl(snap.lineStateText));
  375. MesDcApiClient.completeBatch(serverIp, fields);
  376. String how = aborted ? "已强制结束" : (manual ? "手动提交" : "本批达标");
  377. final String tip = how + " 包装码=" + packCode + " 数量=" + qty
  378. + (printed ? " 已送打印" : " 打印失败/无打印机");
  379. SwingUtilities.invokeLater(new Runnable() {
  380. @Override
  381. public void run() {
  382. resetToIdle(tip);
  383. MainFrame.setMenuStatus(cfg.getStationSn() + " 结案 " + packCode + " x" + qty, 0);
  384. }
  385. });
  386. log.info("工位[{}] 结案 batch={} pack={} qty={} aborted={} manual={} printed={}",
  387. cfg.getStationSn(), codeSnap, packCode, Integer.valueOf(qty),
  388. Boolean.valueOf(aborted), Boolean.valueOf(manual), Boolean.valueOf(printed));
  389. } finally {
  390. busy.set(false);
  391. }
  392. }
  393. }, "dc-finish-" + cfg.getStationSn()).start();
  394. }
  395. private void resetToIdle(String msg) {
  396. phase = Phase.IDLE;
  397. batchCode = "";
  398. targetQty = 0;
  399. baseActualQty = 0;
  400. producedQty = 0;
  401. batchStartTime = "";
  402. SwingUtilities.invokeLater(new Runnable() {
  403. @Override
  404. public void run() {
  405. setText(txtBatch, "");
  406. setText(txtTarget, "");
  407. setText(txtProduced, "");
  408. setMesAllowLamp(false);
  409. setStatusUi(msg == null || msg.isEmpty()
  410. ? "空闲:等待扫码下发批次…" : msg + " → 等待下一笔");
  411. }
  412. });
  413. }
  414. private void refreshSnapshotUi(final DcRunSnapshot s) {
  415. if (s == null) {
  416. return;
  417. }
  418. SwingUtilities.invokeLater(new Runnable() {
  419. @Override
  420. public void run() {
  421. setText(txtCurrentPart, s.currentPart);
  422. setText(txtNextPart, s.nextPart);
  423. setText(txtActualQty, String.valueOf(s.actualQty));
  424. setText(txtDowntime, String.valueOf(s.downtime));
  425. setText(txtTakt, String.valueOf(s.takt));
  426. setLineStateLamp(s.lineState);
  427. setMesAllowLamp(phase == Phase.RUN);
  428. }
  429. });
  430. }
  431. /**
  432. * 产线状态灯 + 说明:2 正常;3 故障;0/1 待机。
  433. */
  434. private void setLineStateLamp(final int lineState) {
  435. SwingUtilities.invokeLater(new Runnable() {
  436. @Override
  437. public void run() {
  438. StatusLight.Level lv = StatusLight.Level.OFF;
  439. String tip = "待机";
  440. if (lineState == 2) {
  441. lv = StatusLight.Level.ON;
  442. tip = "正常";
  443. } else if (lineState == 3) {
  444. lv = StatusLight.Level.FAULT;
  445. tip = "故障";
  446. }
  447. lampLineState.setLevel(lv);
  448. lblLineState.setText(tip);
  449. }
  450. });
  451. }
  452. /** 刷新 MES 允许指示灯 + 说明 */
  453. private void setMesAllowLamp(final boolean on) {
  454. SwingUtilities.invokeLater(new Runnable() {
  455. @Override
  456. public void run() {
  457. lampMesAllow.setOn(on);
  458. lblMesAllowState.setText(on ? "开" : "关");
  459. }
  460. });
  461. }
  462. /** 顶栏刷新:连接灯 + 状态说明 + 地址 */
  463. private void uiPlcLabel() {
  464. final boolean ok = plc.isConnected();
  465. SwingUtilities.invokeLater(new Runnable() {
  466. @Override
  467. public void run() {
  468. lblPlc.setText(plcAddrText());
  469. lampPlc.setLevel(ok ? StatusLight.Level.ON : StatusLight.Level.FAULT);
  470. lblPlcState.setText(ok ? "已连接" : "未连接");
  471. }
  472. });
  473. }
  474. /** PLC 地址;IP 未配返回空 */
  475. private String plcAddrText() {
  476. String ip = nvl(cfg.getPlcIp());
  477. if (ip.isEmpty()) {
  478. return "";
  479. }
  480. return ip + ":" + cfg.getPlcPort();
  481. }
  482. /** 顶栏状态说明(灯正下方) */
  483. private static JLabel headerStateLabel(String text) {
  484. JLabel lb = new JLabel(text, SwingConstants.CENTER);
  485. lb.setFont(new Font("微软雅黑", Font.PLAIN, 11));
  486. lb.setForeground(new Color(200, 230, 235));
  487. lb.setAlignmentX(Component.CENTER_ALIGNMENT);
  488. return lb;
  489. }
  490. /**
  491. * 顶栏一组:名称在上、灯居中、状态说明在灯下方。
  492. */
  493. private static JPanel headerLampItem(String name, StatusLight lamp, JLabel stateLbl) {
  494. JPanel p = new JPanel();
  495. p.setOpaque(false);
  496. p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
  497. JLabel lb = new JLabel(name, SwingConstants.CENTER);
  498. lb.setFont(new Font("微软雅黑", Font.PLAIN, 12));
  499. lb.setForeground(new Color(160, 210, 220));
  500. lb.setAlignmentX(Component.CENTER_ALIGNMENT);
  501. lamp.setAlignmentX(Component.CENTER_ALIGNMENT);
  502. stateLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
  503. p.add(lb);
  504. p.add(Box.createVerticalStrut(2));
  505. p.add(lamp);
  506. p.add(Box.createVerticalStrut(2));
  507. p.add(stateLbl);
  508. return p;
  509. }
  510. private void setStatusUi(final String text) {
  511. SwingUtilities.invokeLater(new Runnable() {
  512. @Override
  513. public void run() {
  514. lblStatus.setText(text == null ? "" : text);
  515. String t = text == null ? "" : text;
  516. if (t.contains("失败") || t.contains("故障")) {
  517. lblStatus.setBackground(UiTheme.ERR_BG);
  518. lblStatus.setForeground(UiTheme.ERR_FG);
  519. } else if (t.contains("生产中") || t.contains("已开") || t.contains("开批")) {
  520. lblStatus.setBackground(UiTheme.RUN_BG);
  521. lblStatus.setForeground(UiTheme.RUN_FG);
  522. } else {
  523. lblStatus.setBackground(UiTheme.OK_BG);
  524. lblStatus.setForeground(UiTheme.OK_FG);
  525. }
  526. }
  527. });
  528. }
  529. private static JTextField roField() {
  530. JTextField t = new JTextField();
  531. t.setEditable(false);
  532. t.setFont(UiTheme.FONT_VALUE);
  533. return t;
  534. }
  535. private static void setText(JTextField t, String v) {
  536. if (t != null) {
  537. t.setText(v == null ? "" : v);
  538. }
  539. }
  540. private static String nvl(String s) {
  541. return s == null ? "" : s.trim();
  542. }
  543. }