张世豪
4 小时以前 d22349714c8d199c02f336f90fba841ef8f5cd39
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
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;
    
    // 颜色定义 - 使用mimaguanli.java中的颜色方案
    private static final Color BACKGROUND_COLOR = new Color(15, 28, 48);
    private static final Color CARD_COLOR = new Color(26, 43, 68);
    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 TEXT_COLOR = new Color(224, 224, 224);
    private static final Color TEXT_LIGHT_COLOR = new Color(160, 200, 255);
    private static final Color FIELD_BACKGROUND = new Color(240, 240, 240);
    
    // 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();
        }
        
        // 创建主面板 - 使用mimaguanli的背景色
        mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBackground(BACKGROUND_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);
        
        // 关闭按钮 - 使用mimaguanli的按钮样式
        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.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(40, 120, 180), 1),
            BorderFactory.createEmptyBorder(8, 16, 8, 16)
        ));
        backButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        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);
        
        // 自定义滚动条
        JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar();
        verticalScrollBar.setBackground(CARD_COLOR);
        verticalScrollBar.setForeground(PRIMARY_COLOR);
        verticalScrollBar.setUnitIncrement(20);
        
        // 合并的参数设置卡片
        settingsPanel.add(createMergedSettingsCard());
        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 createMergedSettingsCard() {
        JPanel card = new JPanel();
        card.setLayout(new BorderLayout());
        card.setBackground(CARD_COLOR);
        card.setOpaque(true);
        card.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(52, 152, 219, 100), 1),
            BorderFactory.createEmptyBorder(20, 20, 20, 20)
        ));
        
        // 表单内容 - 使用水平布局实现标签在左,文本框在右
        JPanel formPanel = new JPanel();
        formPanel.setLayout(new BoxLayout(formPanel, BoxLayout.Y_AXIS));
        formPanel.setOpaque(false);
        
        // 发卡机编号
        formPanel.add(createHorizontalField("发卡机编号:", deviceIdField = new JTextField()));
        formPanel.add(Box.createRigidArea(new Dimension(0, 18)));
        
        // 服务器地址
        formPanel.add(createHorizontalField("服务器地址:", serverAddressField = new JTextField()));
        formPanel.add(Box.createRigidArea(new Dimension(0, 18)));
        
        // 服务器端口
        formPanel.add(createHorizontalField("服务器端口:", serverPortField = new JTextField()));
        formPanel.add(Box.createRigidArea(new Dimension(0, 18)));
        
        // 卡槽总数
        formPanel.add(createHorizontalField("卡槽总数:", slotCountField = new JTextField()));
        formPanel.add(Box.createRigidArea(new Dimension(0, 18)));
        
        // 读卡号模式
        formPanel.add(createHorizontalComboBoxField("读卡号模式:", readModeComboBox = new JComboBox<>(new String[]{"主动模式", "被动模式"})));
        
        card.add(formPanel, BorderLayout.CENTER);
        
        return card;
    }
    
    private JPanel createHorizontalField(String labelText, JTextField textField) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(CARD_COLOR);
        panel.setMaximumSize(new Dimension(SCREEN_WIDTH - 100, 50));
        
        // 标签
        JLabel label = new JLabel(labelText);
        label.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        label.setForeground(TEXT_COLOR);
        label.setPreferredSize(new Dimension(100, 30));
        
        // 文本框样式 - 使用mimaguanli的样式
        textField.setBackground(FIELD_BACKGROUND);
        textField.setForeground(Color.BLACK);
        textField.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(200, 200, 200), 1),
            BorderFactory.createEmptyBorder(10, 12, 10, 12)
        ));
        textField.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
        textField.setOpaque(true);
        textField.setPreferredSize(new Dimension(280, 42));
        
        // 创建水平布局容器
        JPanel horizontalPanel = new JPanel(new BorderLayout());
        horizontalPanel.setBackground(CARD_COLOR);
        horizontalPanel.setMaximumSize(new Dimension(SCREEN_WIDTH - 100, 50));
        
        horizontalPanel.add(label, BorderLayout.WEST);
        horizontalPanel.add(textField, BorderLayout.CENTER);
        
        // 添加间距
        horizontalPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
        
        return horizontalPanel;
    }
    
    private JPanel createHorizontalComboBoxField(String labelText, JComboBox<String> comboBox) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(CARD_COLOR);
        panel.setMaximumSize(new Dimension(SCREEN_WIDTH - 100, 50));
        
        // 标签
        JLabel label = new JLabel(labelText);
        label.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        label.setForeground(TEXT_COLOR);
        label.setPreferredSize(new Dimension(100, 30));
        
        // 组合框样式 - 使用mimaguanli的样式
        comboBox.setBackground(FIELD_BACKGROUND);
        comboBox.setForeground(Color.BLACK);
        comboBox.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(200, 200, 200), 1),
            BorderFactory.createEmptyBorder(10, 12, 10, 12)
        ));
        comboBox.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
        comboBox.setOpaque(true);
        comboBox.setPreferredSize(new Dimension(280, 42));
        
        // 创建水平布局容器
        JPanel horizontalPanel = new JPanel(new BorderLayout());
        horizontalPanel.setBackground(CARD_COLOR);
        horizontalPanel.setMaximumSize(new Dimension(SCREEN_WIDTH - 100, 50));
        
        horizontalPanel.add(label, BorderLayout.WEST);
        horizontalPanel.add(comboBox, BorderLayout.CENTER);
        
        // 添加间距
        horizontalPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
        
        return horizontalPanel;
    }
    
    private JPanel createSaveButton() {
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.setOpaque(false);
        buttonPanel.setBorder(new EmptyBorder(10, 0, 0, 0));
        
        JButton saveButton = new JButton("保存所有设置");
        saveButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
        saveButton.setBackground(PRIMARY_COLOR);
        saveButton.setForeground(Color.WHITE);
        saveButton.setOpaque(true);
        saveButton.setFocusPainted(false);
        saveButton.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(40, 120, 180), 1),
            BorderFactory.createEmptyBorder(8, 16, 8, 16)
        ));
        saveButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        saveButton.addActionListener(e -> saveSettings());
        
        // 按钮悬停效果
        saveButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                saveButton.setBackground(brighterColor(PRIMARY_COLOR));
            }
            
            public void mouseExited(java.awt.event.MouseEvent evt) {
                saveButton.setBackground(PRIMARY_COLOR);
            }
        });
        
        buttonPanel.add(saveButton);
        return buttonPanel;
    }
    
    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);
        });
    }
      
}