|
|
@@ -0,0 +1,333 @@
|
|
|
+package com.mes.ui;
|
|
|
+
|
|
|
+import com.mes.util.JdbcUtils;
|
|
|
+
|
|
|
+import javax.swing.BorderFactory;
|
|
|
+import javax.swing.JButton;
|
|
|
+import javax.swing.JCheckBox;
|
|
|
+import javax.swing.JLabel;
|
|
|
+import javax.swing.JOptionPane;
|
|
|
+import javax.swing.JPanel;
|
|
|
+import javax.swing.JTextField;
|
|
|
+import javax.swing.SwingConstants;
|
|
|
+import java.awt.Color;
|
|
|
+import java.awt.Font;
|
|
|
+import java.awt.GridBagConstraints;
|
|
|
+import java.awt.GridBagLayout;
|
|
|
+import java.awt.Insets;
|
|
|
+import java.awt.event.ActionEvent;
|
|
|
+import java.awt.event.ActionListener;
|
|
|
+import java.awt.event.FocusAdapter;
|
|
|
+import java.awt.event.FocusEvent;
|
|
|
+
|
|
|
+public class ConfigPanel extends JPanel {
|
|
|
+
|
|
|
+ private static final Font LABEL_FONT = new Font("微软雅黑", Font.PLAIN, 16);
|
|
|
+ private static final Font TITLE_FONT = new Font("微软雅黑", Font.BOLD, 18);
|
|
|
+
|
|
|
+ private final JTextField gwField = new JTextField();
|
|
|
+ private final JTextField gwDesField = new JTextField();
|
|
|
+ private final JTextField lineSnField = new JTextField();
|
|
|
+ private final JTextField serverIpField = new JTextField();
|
|
|
+
|
|
|
+ private final GunFields gunA = new GunFields();
|
|
|
+ private final GunFields gunB = new GunFields();
|
|
|
+
|
|
|
+ public ConfigPanel() {
|
|
|
+ setLayout(new GridBagLayout());
|
|
|
+ GridBagConstraints c = new GridBagConstraints();
|
|
|
+ c.gridx = 0;
|
|
|
+ c.fill = GridBagConstraints.HORIZONTAL;
|
|
|
+ c.weightx = 1;
|
|
|
+ c.insets = new Insets(8, 12, 8, 12);
|
|
|
+
|
|
|
+ c.gridy = 0;
|
|
|
+ add(buildBasicPanel(), c);
|
|
|
+ c.gridy = 1;
|
|
|
+ add(buildGunPanel("A枪", gunA), c);
|
|
|
+ c.gridy = 2;
|
|
|
+ add(buildGunPanel("B枪", gunB), c);
|
|
|
+
|
|
|
+ JButton saveBtn = new JButton("保存配置");
|
|
|
+ saveBtn.setFont(new Font("微软雅黑", Font.PLAIN, 20));
|
|
|
+ saveBtn.addActionListener(new ActionListener() {
|
|
|
+ public void actionPerformed(ActionEvent e) {
|
|
|
+ saveConfig();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ c.gridy = 3;
|
|
|
+ c.fill = GridBagConstraints.NONE;
|
|
|
+ c.anchor = GridBagConstraints.CENTER;
|
|
|
+ c.insets = new Insets(4, 12, 16, 12);
|
|
|
+ add(saveBtn, c);
|
|
|
+
|
|
|
+ JLabel hint = new JLabel("保存后请重启客户端生效。配置保存在本地数据库 mes_db.db。", SwingConstants.CENTER);
|
|
|
+ hint.setFont(new Font("微软雅黑", Font.PLAIN, 13));
|
|
|
+ hint.setForeground(Color.DARK_GRAY);
|
|
|
+ c.gridy = 4;
|
|
|
+ c.fill = GridBagConstraints.HORIZONTAL;
|
|
|
+ add(hint, c);
|
|
|
+
|
|
|
+ gwField.addFocusListener(new FocusAdapter() {
|
|
|
+ public void focusLost(FocusEvent e) {
|
|
|
+ autoFillGwDes();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ lineSnField.addFocusListener(new FocusAdapter() {
|
|
|
+ public void focusLost(FocusEvent e) {
|
|
|
+ autoFillGwDes();
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ reloadFromMemory();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void reloadFromMemory() {
|
|
|
+ ConfigUtil.loadFromDb();
|
|
|
+ gwField.setText(nvl(MesClient.mes_gw));
|
|
|
+ gwDesField.setText(nvl(MesClient.mes_gw_des));
|
|
|
+ lineSnField.setText(nvl(MesClient.mes_line_sn));
|
|
|
+ serverIpField.setText(nvl(MesClient.mes_server_ip));
|
|
|
+
|
|
|
+ gunA.ip.setText(nvl(MesClient.plcAIp));
|
|
|
+ gunA.setNum.setText(String.valueOf(MesClient.aSetNum));
|
|
|
+ gunA.sMin.setText(String.valueOf(MesClient.aSMin));
|
|
|
+ gunA.sMax.setText(String.valueOf(MesClient.aSMax));
|
|
|
+ gunA.fMin.setText(String.valueOf(MesClient.aFMin));
|
|
|
+ gunA.fMax.setText(String.valueOf(MesClient.aFMax));
|
|
|
+ gunA.switchEnable.setSelected(MesClient.aSwitchEnable);
|
|
|
+ gunA.switchFrom.setText(String.valueOf(MesClient.aSwitchFrom));
|
|
|
+ gunA.swSMin.setText(String.valueOf(MesClient.aSwitchSMin));
|
|
|
+ gunA.swSMax.setText(String.valueOf(MesClient.aSwitchSMax));
|
|
|
+ gunA.swFMin.setText(String.valueOf(MesClient.aSwitchFMin));
|
|
|
+ gunA.swFMax.setText(String.valueOf(MesClient.aSwitchFMax));
|
|
|
+ gunA.setSwitchEnabled(MesClient.aSwitchEnable);
|
|
|
+
|
|
|
+ gunB.ip.setText(nvl(MesClient.plcBIp));
|
|
|
+ gunB.setNum.setText(String.valueOf(MesClient.bSetNum));
|
|
|
+ gunB.sMin.setText(String.valueOf(MesClient.bSMin));
|
|
|
+ gunB.sMax.setText(String.valueOf(MesClient.bSMax));
|
|
|
+ gunB.fMin.setText(String.valueOf(MesClient.bFMin));
|
|
|
+ gunB.fMax.setText(String.valueOf(MesClient.bFMax));
|
|
|
+ gunB.switchEnable.setSelected(MesClient.bSwitchEnable);
|
|
|
+ gunB.switchFrom.setText(String.valueOf(MesClient.bSwitchFrom));
|
|
|
+ gunB.swSMin.setText(String.valueOf(MesClient.bSwitchSMin));
|
|
|
+ gunB.swSMax.setText(String.valueOf(MesClient.bSwitchSMax));
|
|
|
+ gunB.swFMin.setText(String.valueOf(MesClient.bSwitchFMin));
|
|
|
+ gunB.swFMax.setText(String.valueOf(MesClient.bSwitchFMax));
|
|
|
+ gunB.setSwitchEnabled(MesClient.bSwitchEnable);
|
|
|
+ }
|
|
|
+
|
|
|
+ private JPanel buildBasicPanel() {
|
|
|
+ JPanel panel = new JPanel(new GridBagLayout());
|
|
|
+ panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "基本参数", 0, 0, TITLE_FONT));
|
|
|
+ addRow(panel, 0, "工位号", gwField);
|
|
|
+ addRow(panel, 1, "工位名称", gwDesField);
|
|
|
+ addRow(panel, 2, "产线编号", lineSnField);
|
|
|
+ addRow(panel, 3, "服务器IP", serverIpField);
|
|
|
+ return panel;
|
|
|
+ }
|
|
|
+
|
|
|
+ private JPanel buildGunPanel(String title, final GunFields g) {
|
|
|
+ JPanel panel = new JPanel(new GridBagLayout());
|
|
|
+ panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), title, 0, 0, TITLE_FONT));
|
|
|
+ addRow(panel, 0, "设备地址", g.ip);
|
|
|
+ addRow(panel, 1, "拉铆颗数", g.setNum);
|
|
|
+ addRow(panel, 2, "初始行程下限(1070)", g.sMin);
|
|
|
+ addRow(panel, 3, "初始行程上限(1071)", g.sMax);
|
|
|
+ addRow(panel, 4, "初始拉力下限(1068)", g.fMin);
|
|
|
+ addRow(panel, 5, "初始拉力上限(1069)", g.fMax);
|
|
|
+
|
|
|
+ GridBagConstraints c = new GridBagConstraints();
|
|
|
+ c.gridx = 0;
|
|
|
+ c.gridy = 6;
|
|
|
+ c.gridwidth = 2;
|
|
|
+ c.anchor = GridBagConstraints.WEST;
|
|
|
+ c.insets = new Insets(6, 8, 4, 8);
|
|
|
+ g.switchEnable.setFont(LABEL_FONT);
|
|
|
+ g.switchEnable.setText("需要切换配方");
|
|
|
+ panel.add(g.switchEnable, c);
|
|
|
+
|
|
|
+ g.switchPanel.setLayout(new GridBagLayout());
|
|
|
+ addRow(g.switchPanel, 0, "第几颗开始切换", g.switchFrom);
|
|
|
+ addRow(g.switchPanel, 1, "切换后行程下限(1070)", g.swSMin);
|
|
|
+ addRow(g.switchPanel, 2, "切换后行程上限(1071)", g.swSMax);
|
|
|
+ addRow(g.switchPanel, 3, "切换后拉力下限(1068)", g.swFMin);
|
|
|
+ addRow(g.switchPanel, 4, "切换后拉力上限(1069)", g.swFMax);
|
|
|
+ c.gridy = 7;
|
|
|
+ c.fill = GridBagConstraints.HORIZONTAL;
|
|
|
+ c.weightx = 1;
|
|
|
+ c.insets = new Insets(0, 16, 8, 8);
|
|
|
+ panel.add(g.switchPanel, c);
|
|
|
+
|
|
|
+ g.switchEnable.addActionListener(new ActionListener() {
|
|
|
+ public void actionPerformed(ActionEvent e) {
|
|
|
+ g.setSwitchEnabled(g.switchEnable.isSelected());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return panel;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addRow(JPanel panel, int row, String label, JTextField field) {
|
|
|
+ GridBagConstraints c = new GridBagConstraints();
|
|
|
+ c.insets = new Insets(4, 8, 4, 8);
|
|
|
+ c.gridy = row;
|
|
|
+ c.anchor = GridBagConstraints.WEST;
|
|
|
+
|
|
|
+ JLabel lb = new JLabel(label);
|
|
|
+ lb.setFont(LABEL_FONT);
|
|
|
+ c.gridx = 0;
|
|
|
+ c.weightx = 0;
|
|
|
+ c.fill = GridBagConstraints.NONE;
|
|
|
+ panel.add(lb, c);
|
|
|
+
|
|
|
+ field.setFont(LABEL_FONT);
|
|
|
+ field.setColumns(18);
|
|
|
+ c.gridx = 1;
|
|
|
+ c.weightx = 1;
|
|
|
+ c.fill = GridBagConstraints.HORIZONTAL;
|
|
|
+ panel.add(field, c);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void autoFillGwDes() {
|
|
|
+ String gw = gwField.getText().trim();
|
|
|
+ String lineSn = lineSnField.getText().trim();
|
|
|
+ if (gw.isEmpty() || lineSn.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String des = OprnoUtil.lookupGwDes(lineSn, gw);
|
|
|
+ if (des != null && !des.isEmpty()) {
|
|
|
+ String cur = gwDesField.getText().trim();
|
|
|
+ if (cur.isEmpty()) {
|
|
|
+ gwDesField.setText(des);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveConfig() {
|
|
|
+ try {
|
|
|
+ LamaoStationConfig station = new LamaoStationConfig();
|
|
|
+ station.setMesGw(required(gwField, "工位号"));
|
|
|
+ station.setMesGwDes(required(gwDesField, "工位名称"));
|
|
|
+ station.setMesLineSn(required(lineSnField, "产线编号"));
|
|
|
+ station.setMesServerIp(required(serverIpField, "服务器IP"));
|
|
|
+
|
|
|
+ LamaoGunConfig gunAConfig = buildGunConfig("A", gunA, "A枪");
|
|
|
+ LamaoGunConfig gunBConfig = buildGunConfig("B", gunB, "B枪");
|
|
|
+
|
|
|
+ ConfigUtil.saveToDb(station, gunAConfig, gunBConfig);
|
|
|
+ JOptionPane.showMessageDialog(MesClient.mesClientFrame,
|
|
|
+ "配置已保存到本地数据库\n请重启客户端后生效",
|
|
|
+ "提示窗口", JOptionPane.INFORMATION_MESSAGE);
|
|
|
+ } catch (IllegalArgumentException ex) {
|
|
|
+ JOptionPane.showMessageDialog(MesClient.mesClientFrame, ex.getMessage(), "提示窗口", JOptionPane.WARNING_MESSAGE);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ JOptionPane.showMessageDialog(MesClient.mesClientFrame, "保存失败:" + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private LamaoGunConfig buildGunConfig(String gunCode, GunFields g, String title) {
|
|
|
+ LamaoGunConfig config = JdbcUtils.getGunConfig(gunCode);
|
|
|
+ if (config == null) {
|
|
|
+ config = new LamaoGunConfig();
|
|
|
+ config.setGunCode(gunCode);
|
|
|
+ }
|
|
|
+
|
|
|
+ String ip = required(g.ip, title + "设备地址");
|
|
|
+ short setNum = parseShort(g.setNum, title + "拉铆颗数");
|
|
|
+ if (setNum <= 0) {
|
|
|
+ throw new IllegalArgumentException(title + "拉铆颗数必须大于0");
|
|
|
+ }
|
|
|
+ short sMin = parseShort(g.sMin, title + "初始行程下限");
|
|
|
+ short sMax = parseShort(g.sMax, title + "初始行程上限");
|
|
|
+ short fMin = parseShort(g.fMin, title + "初始拉力下限");
|
|
|
+ short fMax = parseShort(g.fMax, title + "初始拉力上限");
|
|
|
+
|
|
|
+ boolean switchEnable = g.switchEnable.isSelected();
|
|
|
+ short switchFrom = 24;
|
|
|
+ short swSMin = sMin;
|
|
|
+ short swSMax = sMax;
|
|
|
+ short swFMin = fMin;
|
|
|
+ short swFMax = fMax;
|
|
|
+ if (switchEnable) {
|
|
|
+ switchFrom = parseShort(g.switchFrom, title + "第几颗开始切换");
|
|
|
+ if (switchFrom < 2) {
|
|
|
+ throw new IllegalArgumentException(title + "第几颗开始切换必须大于等于2(第1颗使用初始配方)");
|
|
|
+ }
|
|
|
+ if (switchFrom > setNum) {
|
|
|
+ throw new IllegalArgumentException(title + "第几颗开始切换不能大于拉铆颗数");
|
|
|
+ }
|
|
|
+ swSMin = parseShort(g.swSMin, title + "切换后行程下限");
|
|
|
+ swSMax = parseShort(g.swSMax, title + "切换后行程上限");
|
|
|
+ swFMin = parseShort(g.swFMin, title + "切换后拉力下限");
|
|
|
+ swFMax = parseShort(g.swFMax, title + "切换后拉力上限");
|
|
|
+ }
|
|
|
+
|
|
|
+ config.setIpAddress(ip);
|
|
|
+ config.setSetNum(setNum);
|
|
|
+ config.setSMin(sMin);
|
|
|
+ config.setSMax(sMax);
|
|
|
+ config.setFMin(fMin);
|
|
|
+ config.setFMax(fMax);
|
|
|
+ config.setSwitchEnable(switchEnable);
|
|
|
+ config.setSwitchFrom(switchFrom);
|
|
|
+ config.setSwitchSMin(swSMin);
|
|
|
+ config.setSwitchSMax(swSMax);
|
|
|
+ config.setSwitchFMin(swFMin);
|
|
|
+ config.setSwitchFMax(swFMax);
|
|
|
+ return config;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String required(JTextField field, String name) {
|
|
|
+ String v = field.getText() == null ? "" : field.getText().trim();
|
|
|
+ if (v.isEmpty()) {
|
|
|
+ throw new IllegalArgumentException(name + "不能为空");
|
|
|
+ }
|
|
|
+ return v;
|
|
|
+ }
|
|
|
+
|
|
|
+ private short parseShort(JTextField field, String name) {
|
|
|
+ String v = required(field, name);
|
|
|
+ try {
|
|
|
+ int n = Integer.parseInt(v);
|
|
|
+ if (n < Short.MIN_VALUE || n > Short.MAX_VALUE) {
|
|
|
+ throw new NumberFormatException();
|
|
|
+ }
|
|
|
+ return (short) n;
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ throw new IllegalArgumentException(name + "必须是整数");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String nvl(String s) {
|
|
|
+ return s == null ? "" : s;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class GunFields {
|
|
|
+ final JTextField ip = new JTextField();
|
|
|
+ final JTextField setNum = new JTextField();
|
|
|
+ final JTextField sMin = new JTextField();
|
|
|
+ final JTextField sMax = new JTextField();
|
|
|
+ final JTextField fMin = new JTextField();
|
|
|
+ final JTextField fMax = new JTextField();
|
|
|
+ final JCheckBox switchEnable = new JCheckBox();
|
|
|
+ final JTextField switchFrom = new JTextField();
|
|
|
+ final JTextField swSMin = new JTextField();
|
|
|
+ final JTextField swSMax = new JTextField();
|
|
|
+ final JTextField swFMin = new JTextField();
|
|
|
+ final JTextField swFMax = new JTextField();
|
|
|
+ final JPanel switchPanel = new JPanel();
|
|
|
+
|
|
|
+ void setSwitchEnabled(boolean enabled) {
|
|
|
+ switchPanel.setVisible(enabled);
|
|
|
+ switchFrom.setEnabled(enabled);
|
|
|
+ swSMin.setEnabled(enabled);
|
|
|
+ swSMax.setEnabled(enabled);
|
|
|
+ swFMin.setEnabled(enabled);
|
|
|
+ swFMax.setEnabled(enabled);
|
|
|
+ switchPanel.revalidate();
|
|
|
+ switchPanel.repaint();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|