826220679@qq.com
2025-08-07 4d6cd980c5c69e4d9d150669c89734642297e0cd
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
package dell_system;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.util.*;
import java.util.regex.*;
import java.lang.reflect.Method;
import dell_targets.Dell_SystemConfiguration;
import targets.SystemConfiguration;
import databases.DBConnector;
import java.util.List;
 
@SuppressWarnings("serial")
public class SystemSettingsPanel extends JPanel {
    private ResourceBundle messages; // 多语言资源包
    private SettingsPanelContent contentPanel; // 设置内容面板
    private Map<String, JComponent> settingComponents = new HashMap<>(); // 设置组件映射
    private SystemConfiguration currentConfig; // 当前系统配置
 
    public SystemSettingsPanel(ResourceBundle messages) {
        this.messages = messages;
        loadConfiguration(); // 加载配置
        initUI(); // 初始化UI
    }
 
    // 从数据库加载配置
    private void loadConfiguration() {
        // 获取系统配置列表
        List<SystemConfiguration> configs = Dell_SystemConfiguration.getSystemConfigurations();
        if (configs != null && !configs.isEmpty()) {
            // 查找ID为1的配置
            for (SystemConfiguration config : configs) {
                if (config.getId() == 1) {
                    currentConfig = config;
                    break;
                }
            }
        }
    }
 
    // 初始化用户界面
    private void initUI() {
        setLayout(new BorderLayout()); // 使用边界布局
 
        // 创建设置内容面板
        contentPanel = new SettingsPanelContent(messages, settingComponents, currentConfig);
        contentPanel.setParentPanel(this); // 设置父面板引用
 
        // 创建滚动面板
        JScrollPane scrollPane = new JScrollPane(contentPanel);
        scrollPane.setBorder(BorderFactory.createEmptyBorder()); // 设置空边框
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); // 始终显示垂直滚动条
 
        add(scrollPane, BorderLayout.CENTER); // 将滚动面板添加到中心
    }
 
    // 使用反射设置配置对象的值
    private void setConfigField(String fieldName, Object value) {
        try {
            // 构建setter方法名
            String setterName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
            // 获取setter方法
            Method setter = SystemConfiguration.class.getMethod(setterName, String.class);
            // 调用setter方法设置值
            setter.invoke(currentConfig, value.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    // 使用反射获取配置对象的值
    @SuppressWarnings("unused")
    private String getConfigField(String fieldName) {
        try {
            // 构建getter方法名
            String getterName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
            // 获取getter方法
            Method getter = SystemConfiguration.class.getMethod(getterName);
            // 调用getter方法获取值
            return (String) getter.invoke(currentConfig);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
 
    // 保存单个设置
    void saveSetting(String key, String value) {
        // 更新配置对象
        setConfigField(key, value);
        
        // 更新数据库
        int result = DBConnector.updateSystemConfiguration(key, value);
        
        if (result > 0) {
            System.out.println("保存设置成功: " + key + " = " + value);
        } else {
            System.out.println("保存设置失败: " + key + " = " + value);
        }
    }
 
    // 保存所有设置
    void saveAllSettings() {
        Map<String, String> fieldValues = new HashMap<>();
        
        // 遍历所有设置组件
        for (Map.Entry<String, JComponent> entry : settingComponents.entrySet()) {
            String key = entry.getKey();
            JComponent comp = entry.getValue();
            Object value = null;
            
            // 根据组件类型获取值
            if (comp instanceof JTextField) {
                JTextField textField = (JTextField) comp;
                String regex = (String) textField.getClientProperty("regex");
                String textValue = textField.getText().trim();
                // 验证字段是否为空
                if (textValue.isEmpty()) {
                    JOptionPane.showMessageDialog(this, messages.getString("FIELD_REQUIRED"), 
                                                 messages.getString("VALIDATION_ERROR"), JOptionPane.ERROR_MESSAGE);
                    return;
                }
                // 验证字段格式
                if (regex != null && !Pattern.matches(regex, textValue)) {
                    JOptionPane.showMessageDialog(this, messages.getString("FIELD_VALIDATION_FAIL") + key, 
                                                 messages.getString("VALIDATION_ERROR"), JOptionPane.ERROR_MESSAGE);
                    return;
                }
                value = textValue;
            } else if (comp instanceof JPasswordField) {
                value = new String(((JPasswordField) comp).getPassword());
            } else if (comp instanceof JCheckBox) {
                value = ((JCheckBox) comp).isSelected() ? "1" : "0";
            } else if (comp instanceof JComboBox) {
                JComboBox<?> comboBox = (JComboBox<?>) comp;
                int selectedIndex = comboBox.getSelectedIndex();
                Integer[] values = (Integer[]) comboBox.getClientProperty("values");
                if (values != null && selectedIndex >= 0 && selectedIndex < values.length) {
                    value = values[selectedIndex].toString();
                }
            } else if (comp instanceof JPanel) {
                for (Component c : ((JPanel) comp).getComponents()) {
                    if (c instanceof JRadioButton && ((JRadioButton) c).isSelected()) {
                        value = ((JRadioButton) c).getClientProperty("value").toString();
                        break;
                    }
                }
            }
            
            // 将值添加到映射中
            if (value != null) {
                // 转换字段名为数据库列名
                String dbColumn = DBConnector.camelToUnderline(key);
                fieldValues.put(dbColumn, value.toString());
                setConfigField(key, value.toString());
            }
        }
        
        // 批量更新数据库
        int result = DBConnector.updateData("system_configuration", fieldValues, 1);
        if (result > 0) {
            // 弹窗居中显示成功消息
            JOptionPane optionPane = new JOptionPane(messages.getString("SAVE_ALL_SUCCESS"), 
                                                    JOptionPane.INFORMATION_MESSAGE);
            JDialog dialog = optionPane.createDialog(this, messages.getString("SUCCESS"));
            dialog.setLocationRelativeTo(null); // 居中显示
            dialog.setVisible(true);
        } else {
            // 弹窗居中显示失败消息
            JOptionPane optionPane = new JOptionPane(messages.getString("SAVE_ALL_FAILED"), 
                                                    JOptionPane.ERROR_MESSAGE);
            JDialog dialog = optionPane.createDialog(this, messages.getString("ERROR"));
            dialog.setLocationRelativeTo(null); // 居中显示
            dialog.setVisible(true);
        }
    }
 
    // 限制长度的文档类
    static class LimitedLengthDocument extends PlainDocument {
        private final int maxLength; // 最大长度
 
        public LimitedLengthDocument(int maxLength) {
            this.maxLength = maxLength;
        }
 
        @Override
        public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
            if (str == null) return;
            // 检查长度是否超过限制
            if ((getLength() + str.length()) <= maxLength) {
                super.insertString(offset, str, attr);
            }
        }
    }
 
    // 数字验证器类
    static class NumericVerifier extends InputVerifier {
        @Override
        public boolean verify(JComponent input) {
            JTextField textField = (JTextField) input;
            // 验证输入是否为数字
            return textField.getText().matches("\\d*");
        }
    }    
}