张世豪
昨天 43bd281a47eeac52e649ef79ea25c0dd4d61af7d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package xitongshezhi;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
 
import chushihua.Chushihua;
import home.MachineConfig;
 
@SuppressWarnings("serial")
public class canshushezhi extends JDialog {
    // 屏幕尺寸常量 - 适配7寸竖屏
    private static final int SCREEN_WIDTH = 600;
    private static final int SCREEN_HEIGHT = 1024;
    
    // 颜色常量 - 使用不透明颜色
    private static final Color PRIMARY_COLOR = new Color(52, 152, 219);
    private static final Color SECONDARY_COLOR = new Color(46, 204, 113);
    private static final Color DARK_COLOR = new Color(15, 28, 48);
    private static final Color DARK_LIGHT_COLOR = new Color(26, 43, 68);
    private static final Color TEXT_COLOR = new Color(224, 224, 224);
    
    // UI组件
    private JPanel mainPanel;
    private JTextField deviceIdField;
    private JTextField serverAddressField;
    private JTextField serverPortField;
    private JTextField slotCountField;
    private JComboBox<String> readModeComboBox;
    
    // 配置管理
    private Chushihua configManager;
    
    public canshushezhi(JFrame parent) {
        super(parent, "参数设置", true);
        
        // 获取配置管理器实例
        configManager = Chushihua.getInstance();
        
        initializeUI();
        initializeData();
    }
    
    private void initializeUI() {
        setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        
        // 设置深色主题
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        // 创建主面板 - 不透明背景
        mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBackground(DARK_COLOR);
        mainPanel.setOpaque(true);
        mainPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
        
        // 添加各个区域
        mainPanel.add(createHeaderPanel(), BorderLayout.NORTH);
        mainPanel.add(createSettingsPanel(), BorderLayout.CENTER);
        
        getContentPane().add(mainPanel);
    }
    
    private JPanel createHeaderPanel() {
        JPanel headerPanel = new JPanel(new BorderLayout());
        headerPanel.setOpaque(false);
        headerPanel.setBorder(new EmptyBorder(0, 0, 12, 0));
        
        // 标题
        JLabel titleLabel = new JLabel("参数设置");
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 22));
        titleLabel.setForeground(TEXT_COLOR);
        
        // 关闭按钮
        JButton backButton = new JButton("关闭");
        backButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
        backButton.setBackground(PRIMARY_COLOR);
        backButton.setForeground(Color.WHITE);
        backButton.setOpaque(true);
        backButton.setFocusPainted(false);
        backButton.setBorder(BorderFactory.createEmptyBorder(8, 16, 8, 16));
        backButton.addActionListener(e -> dispose());
        
        // 关闭按钮悬停效果
        backButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                backButton.setBackground(brighterColor(PRIMARY_COLOR));
            }
            
            public void mouseExited(java.awt.event.MouseEvent evt) {
                backButton.setBackground(PRIMARY_COLOR);
            }
        });
        
        JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        titlePanel.setOpaque(false);
        titlePanel.add(titleLabel);
        
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.setOpaque(false);
        buttonPanel.add(backButton);
        
        headerPanel.add(titlePanel, BorderLayout.WEST);
        headerPanel.add(buttonPanel, BorderLayout.EAST);
        
        return headerPanel;
    }
    
    private JPanel createSettingsPanel() {
        JPanel settingsPanel = new JPanel();
        settingsPanel.setLayout(new BoxLayout(settingsPanel, BoxLayout.Y_AXIS));
        settingsPanel.setOpaque(false);
        settingsPanel.setBorder(new EmptyBorder(0, 0, 12, 0));
        
        // 添加滚动支持
        JScrollPane scrollPane = new JScrollPane(settingsPanel);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        scrollPane.getVerticalScrollBar().setUnitIncrement(16);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        
        // 基本参数设置卡片
        settingsPanel.add(createBasicSettingsCard());
        settingsPanel.add(Box.createRigidArea(new Dimension(0, 12)));
        
        // 设备参数设置卡片
        settingsPanel.add(createDeviceSettingsCard());
        settingsPanel.add(Box.createRigidArea(new Dimension(0, 12)));
        
        // 保存按钮
        settingsPanel.add(createSaveButton());
        
        JPanel container = new JPanel(new BorderLayout());
        container.setOpaque(false);
        container.add(scrollPane, BorderLayout.CENTER);
        
        return container;
    }
    
    private JPanel createBasicSettingsCard() {
        JPanel card = new JPanel();
        card.setLayout(new BorderLayout());
        card.setBackground(DARK_LIGHT_COLOR);
        card.setOpaque(true);
        card.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(52, 152, 219, 128)),
            BorderFactory.createEmptyBorder(15, 15, 15, 15)
        ));
        
        // 卡头
        JPanel cardHeader = new JPanel(new BorderLayout());
        cardHeader.setOpaque(false);
        
        JLabel titleLabel = new JLabel("基本参数设置");
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
        titleLabel.setForeground(TEXT_COLOR);
        
        cardHeader.add(titleLabel, BorderLayout.WEST);
        card.add(cardHeader, BorderLayout.NORTH);
        
        // 表单内容 - 使用网格布局实现标签在左,文本框在右
        // 调整行高为80像素,为60像素高的组件提供足够的间距
        JPanel formPanel = new JPanel(new GridLayout(3, 2, 10, 20));
        formPanel.setOpaque(false);
        formPanel.setBorder(new EmptyBorder(15, 0, 0, 0));
        
        // 发卡机编号
        JLabel deviceIdLabel = new JLabel("发卡机编号:");
        deviceIdLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13));
        deviceIdLabel.setForeground(TEXT_COLOR);
        formPanel.add(deviceIdLabel);
        
        deviceIdField = new JTextField();
        styleFormField(deviceIdField);
        formPanel.add(deviceIdField);
        
        // 服务器地址
        JLabel serverAddressLabel = new JLabel("服务器地址:");
        serverAddressLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13));
        serverAddressLabel.setForeground(TEXT_COLOR);
        formPanel.add(serverAddressLabel);
        
        serverAddressField = new JTextField();
        styleFormField(serverAddressField);
        formPanel.add(serverAddressField);
        
        // 服务器端口
        JLabel serverPortLabel = new JLabel("服务器端口:");
        serverPortLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13));
        serverPortLabel.setForeground(TEXT_COLOR);
        formPanel.add(serverPortLabel);
        
        serverPortField = new JTextField();
        styleFormField(serverPortField);
        formPanel.add(serverPortField);
        
        card.add(formPanel, BorderLayout.CENTER);
        
        return card;
    }
    
    private JPanel createDeviceSettingsCard() {
        JPanel card = new JPanel();
        card.setLayout(new BorderLayout());
        card.setBackground(DARK_LIGHT_COLOR);
        card.setOpaque(true);
        card.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(52, 152, 219, 128)),
            BorderFactory.createEmptyBorder(15, 15, 15, 15)
        ));
        
        // 卡头
        JPanel cardHeader = new JPanel(new BorderLayout());
        cardHeader.setOpaque(false);
        
        JLabel titleLabel = new JLabel("设备参数设置");
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
        titleLabel.setForeground(TEXT_COLOR);
        
        cardHeader.add(titleLabel, BorderLayout.WEST);
        card.add(cardHeader, BorderLayout.NORTH);
        
        // 表单内容 - 使用网格布局实现标签在左,输入组件在右
        // 调整行高为80像素,为60像素高的组件提供足够的间距
        JPanel formPanel = new JPanel(new GridLayout(2, 2, 10, 20));
        formPanel.setOpaque(false);
        formPanel.setBorder(new EmptyBorder(15, 0, 0, 0));
        
        // 卡槽总数
        JLabel slotCountLabel = new JLabel("卡槽总数:");
        slotCountLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13));
        slotCountLabel.setForeground(TEXT_COLOR);
        formPanel.add(slotCountLabel);
        
        slotCountField = new JTextField();
        styleFormField(slotCountField);
        formPanel.add(slotCountField);
        
        // 读卡号模式
        JLabel readModeLabel = new JLabel("读卡号模式:");
        readModeLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13));
        readModeLabel.setForeground(TEXT_COLOR);
        formPanel.add(readModeLabel);
        
        readModeComboBox = new JComboBox<>(new String[]{"主动模式", "被动模式"});
        styleComboBox(readModeComboBox);
        formPanel.add(readModeComboBox);
        
        card.add(formPanel, BorderLayout.CENTER);
        
        return card;
    }
    
    private JPanel createSaveButton() {
        JPanel buttonPanel = new JPanel(new BorderLayout());
        buttonPanel.setOpaque(false);
        
        JButton saveButton = new JButton("保存所有设置");
        saveButton.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
        saveButton.setBackground(SECONDARY_COLOR);
        saveButton.setForeground(Color.WHITE);
        saveButton.setOpaque(true);
        saveButton.setFocusPainted(false);
        saveButton.setPreferredSize(new Dimension(0, 60)); // 高度改为60像素
        saveButton.setBorder(BorderFactory.createEmptyBorder(12, 20, 12, 20));
        saveButton.addActionListener(e -> saveSettings());
        
        // 按钮悬停效果
        saveButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                saveButton.setBackground(brighterColor(SECONDARY_COLOR));
            }
            
            public void mouseExited(java.awt.event.MouseEvent evt) {
                saveButton.setBackground(SECONDARY_COLOR);
            }
        });
        
        buttonPanel.add(saveButton, BorderLayout.CENTER);
        return buttonPanel;
    }
    
    private void styleFormField(JTextField field) {
        field.setPreferredSize(new Dimension(200, 60)); // 高度改为60像素
        field.setBackground(Color.WHITE); // 不透明白色背景
        field.setForeground(Color.BLACK); // 黑色文字
        field.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(200, 200, 200)),
            BorderFactory.createEmptyBorder(8, 12, 8, 12)
        ));
        field.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13));
        field.setCaretColor(Color.BLACK);
        field.setOpaque(true); // 确保不透明
    }
    
    private void styleComboBox(JComboBox<String> comboBox) {
        comboBox.setPreferredSize(new Dimension(200, 60)); // 高度改为60像素
        comboBox.setBackground(Color.WHITE); // 不透明白色背景
        comboBox.setForeground(Color.BLACK); // 黑色文字
        comboBox.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(200, 200, 200)),
            BorderFactory.createEmptyBorder(8, 12, 8, 12)
        ));
        comboBox.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13));
        comboBox.setOpaque(true); // 确保不透明
    }
    
    private void initializeData() {
        try {
            if (configManager.isInitialized()) {
                MachineConfig machineConfig = configManager.getMachineConfig();
                
                // 设置表单数据
                deviceIdField.setText(machineConfig.getMachineId());
                serverAddressField.setText(machineConfig.getServerAddress());
                serverPortField.setText(String.valueOf(machineConfig.getServerPort()));
                slotCountField.setText(String.valueOf(machineConfig.getTotalSlots()));
                
                // 设置读卡模式
                String readMode = machineConfig.getReadCardMode();
                if ("AUTO".equalsIgnoreCase(readMode) || "0".equals(readMode)) {
                    readModeComboBox.setSelectedIndex(0); // 主动模式
                } else {
                    readModeComboBox.setSelectedIndex(1); // 被动模式
                }
            } else {
                // 如果配置未初始化,使用默认值
                deviceIdField.setText("M001");
                serverAddressField.setText("192.168.1.100");
                serverPortField.setText("8080");
                slotCountField.setText("60");
                readModeComboBox.setSelectedIndex(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            showMessage("错误", "加载配置数据时发生错误: " + e.getMessage(), JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void saveSettings() {
        // 验证表单数据
        if (!validateForm()) {
            return;
        }
        
        try {
            // 收集表单数据
            String deviceId = deviceIdField.getText().trim();
            String serverAddress = serverAddressField.getText().trim();
            int serverPort = Integer.parseInt(serverPortField.getText().trim());
            int slotCount = Integer.parseInt(slotCountField.getText().trim());
            String readMode = readModeComboBox.getSelectedIndex() == 0 ? "AUTO" : "MANUAL";
            
            // 更新配置文件
            updateConfigFile(deviceId, serverAddress, serverPort, slotCount, readMode);
            
            // 重新加载配置
            configManager.reload();
            
            showMessage("成功", "参数设置已成功保存并生效", JOptionPane.INFORMATION_MESSAGE);
            
        } catch (NumberFormatException e) {
            showMessage("错误", "请输入有效的数字格式", JOptionPane.ERROR_MESSAGE);
        } catch (Exception e) {
            e.printStackTrace();
            showMessage("错误", "保存设置时发生错误: " + e.getMessage(), JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private boolean validateForm() {
        // 验证发卡机编号
        String deviceId = deviceIdField.getText().trim();
        if (deviceId.isEmpty()) {
            showMessage("验证失败", "请输入发卡机编号", JOptionPane.WARNING_MESSAGE);
            deviceIdField.requestFocus();
            return false;
        }
        
        // 验证服务器地址
        String serverAddress = serverAddressField.getText().trim();
        if (serverAddress.isEmpty()) {
            showMessage("验证失败", "请输入服务器IP地址", JOptionPane.WARNING_MESSAGE);
            serverAddressField.requestFocus();
            return false;
        }
        
        // 验证服务器端口
        try {
            int port = Integer.parseInt(serverPortField.getText().trim());
            if (port < 1 || port > 65535) {
                showMessage("验证失败", "请输入有效的服务器端口 (1-65535)", JOptionPane.WARNING_MESSAGE);
                serverPortField.requestFocus();
                return false;
            }
        } catch (NumberFormatException e) {
            showMessage("验证失败", "请输入有效的服务器端口号", JOptionPane.WARNING_MESSAGE);
            serverPortField.requestFocus();
            return false;
        }
        
        // 验证卡槽总数
        try {
            int slots = Integer.parseInt(slotCountField.getText().trim());
            if (slots < 1 || slots > 120) {
                showMessage("验证失败", "请输入有效的卡槽数量 (1-120)", JOptionPane.WARNING_MESSAGE);
                slotCountField.requestFocus();
                return false;
            }
        } catch (NumberFormatException e) {
            showMessage("验证失败", "请输入有效的卡槽数量", JOptionPane.WARNING_MESSAGE);
            slotCountField.requestFocus();
            return false;
        }
        
        return true;
    }
    
    private void updateConfigFile(String deviceId, String serverAddress, int serverPort, 
                                 int slotCount, String readMode) throws IOException {
        Properties props = new Properties();
        File configFile = new File("config.properties");
        
        // 如果配置文件存在,先加载现有配置
        if (configFile.exists()) {
            try (FileInputStream in = new FileInputStream(configFile)) {
                props.load(in);
            }
        }
        
        // 更新配置值 - 使用Chushihua中定义的属性键名
        props.setProperty("machine.id", deviceId);
        props.setProperty("server.address", serverAddress);
        props.setProperty("server.port", String.valueOf(serverPort));
        props.setProperty("total.slots", String.valueOf(slotCount));
        props.setProperty("read.card.mode", readMode);
        
        // 保存配置到文件
        try (FileOutputStream out = new FileOutputStream(configFile)) {
            props.store(out, "发卡机系统配置");
        }
    }
    
    private void showMessage(String title, String message, int messageType) {
        JOptionPane.showMessageDialog(this, message, title, messageType);
    }
    
    // 辅助方法:使颜色更亮
    private Color brighterColor(Color color) {
        int r = Math.min(255, color.getRed() + 30);
        int g = Math.min(255, color.getGreen() + 30);
        int b = Math.min(255, color.getBlue() + 30);
        return new Color(r, g, b);
    }
    
    @Override
    public void dispose() {
        super.dispose();
    }
    
    // 静态方法:从其他页面调用
    public static void showSettingsDialog(JFrame parent) {
        SwingUtilities.invokeLater(() -> {
            canshushezhi dialog = new canshushezhi(parent);
            dialog.setVisible(true);
        });
    }
      
}