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 settingComponents = new HashMap<>(); // 设置组件映射 private SystemConfiguration currentConfig; // 当前系统配置 public SystemSettingsPanel(ResourceBundle messages) { this.messages = messages; loadConfiguration(); // 加载配置 initUI(); // 初始化UI } // 从数据库加载配置 private void loadConfiguration() { // 获取系统配置列表 List 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 fieldValues = new HashMap<>(); // 遍历所有设置组件 for (Map.Entry 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*"); } } }