package dell_system; import targets.Company; import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.table.*; import java.awt.*; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.*; import java.util.List; import java.util.ResourceBundle; public class CompanyManagementPanel extends JPanel { private static final long serialVersionUID = 1L; private JTable companyTable; private DefaultTableModel tableModel; private List allCompanies; private ResourceBundle messages; private JTextField searchField; @SuppressWarnings("serial") public CompanyManagementPanel(ResourceBundle messages) { this.messages = messages; setLayout(new BorderLayout()); setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // ¶¥²¿Ãæ°å JPanel topPanel = new JPanel(new BorderLayout(5, 5)); topPanel.setBorder(BorderFactory.createTitledBorder(getMessage("QKOP"))); // ×ó²àËÑË÷ÊäÈë×é¼þ JPanel searchInputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0)); JLabel searchLabel = new JLabel(getMessage("COMPANY_NAME") + ":"); searchField = new JTextField(20); JButton searchButton = new JButton(getMessage("SEARCH")); JButton resetButton = new JButton(getMessage("RESET")); searchInputPanel.add(searchLabel); searchInputPanel.add(searchField); searchInputPanel.add(searchButton); searchInputPanel.add(resetButton); // ÓÒ²à²Ù×÷°´Å¥ JPanel actionPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0)); JButton addButton = new JButton(getMessage("ADD")); JButton editButton = new JButton(getMessage("EDIT")); JButton deleteButton = new JButton(getMessage("DELETE")); JButton refreshButton = new JButton(getMessage("REFRESH")); actionPanel.add(addButton); actionPanel.add(editButton); actionPanel.add(deleteButton); actionPanel.add(refreshButton); // ½«×ó²àËÑË÷ºÍÓÒ²à²Ù×÷Ìí¼Óµ½ËÑË÷Ãæ°å topPanel.add(searchInputPanel, BorderLayout.CENTER); topPanel.add(actionPanel, BorderLayout.EAST); // ´´½¨±í¸ñÁÐÃû String[] columnNames = { getMessage("INDEX"), getMessage("NAME"), getMessage("LOGO"), getMessage("DATE") }; // ´´½¨±í¸ñÄ£ÐÍ tableModel = new DefaultTableModel(columnNames, 0) { @Override public boolean isCellEditable(int row, int column) { return false; } }; // ´´½¨±í¸ñ companyTable = new JTable(tableModel); companyTable.setAutoCreateRowSorter(true); companyTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // ÉèÖÃÁпí companyTable.getColumnModel().getColumn(0).setPreferredWidth(50); companyTable.getColumnModel().getColumn(1).setPreferredWidth(200); companyTable.getColumnModel().getColumn(2).setPreferredWidth(100); companyTable.getColumnModel().getColumn(3).setPreferredWidth(200); // ÉèÖñíÍ·Ñùʽ JTableHeader header = companyTable.getTableHeader(); header.setBackground(Color.GRAY); header.setForeground(Color.WHITE); header.setFont(header.getFont().deriveFont(Font.BOLD)); // ´´½¨±íÍ·äÖȾÆ÷ DefaultTableCellRenderer headerRenderer = new DefaultTableCellRenderer() { { setHorizontalAlignment(SwingConstants.LEFT); setBackground(Color.GRAY); setForeground(Color.WHITE); setFont(getFont().deriveFont(Font.BOLD)); } }; // Ó¦ÓñíÍ·äÖȾÆ÷ for (int i = 0; i < companyTable.getColumnCount(); i++) { companyTable.getColumnModel().getColumn(i).setHeaderRenderer(headerRenderer); } JScrollPane scrollPane = new JScrollPane(companyTable); scrollPane.setPreferredSize(new Dimension(800, 400)); // Ìí¼Ó×é¼þ add(topPanel, BorderLayout.NORTH); add(scrollPane, BorderLayout.CENTER); // ¼ÓÔØÊý¾Ý loadCompanyData(); // Ìí¼Óʼþ¼àÌýÆ÷ addButton.addActionListener(e -> addNewCompany()); editButton.addActionListener(e -> editSelectedCompany()); deleteButton.addActionListener(e -> deleteSelectedCompanies()); refreshButton.addActionListener(e -> loadCompanyData()); searchButton.addActionListener(e -> searchCompanies()); resetButton.addActionListener(e -> resetSearch()); } private void loadCompanyData() { try { allCompanies = Dell_company.getAllCompanies(); updateTable(allCompanies); } catch (SQLException ex) { JOptionPane.showMessageDialog(this, getMessage("DATA_LOAD_ERROR") + ": " + ex.getMessage(), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); } } // ËÑË÷¹«Ë¾ private void searchCompanies() { String keyword = searchField.getText().trim(); if (keyword.isEmpty()) { updateTable(allCompanies); return; } List filtered = new ArrayList<>(); for (Company company : allCompanies) { if (company.getCompanyName().toLowerCase().contains(keyword.toLowerCase())) { filtered.add(company); } } if (filtered.isEmpty()) { JOptionPane.showMessageDialog(this, getMessage("SEARCH_NO_RESULTS"), getMessage("INFO"), JOptionPane.INFORMATION_MESSAGE); } updateTable(filtered); } // ÖØÖÃËÑË÷ private void resetSearch() { searchField.setText(""); updateTable(allCompanies); } private void updateTable(List companies) { tableModel.setRowCount(0); int index = 1; for (Company company : companies) { tableModel.addRow(new Object[]{ index++, company.getCompanyName(), company.getCompanyLogoUrl() != null ? company.getCompanyLogoUrl() : "", company.getAddedDate() }); } } private void addNewCompany() { CompanyEditDialog dialog = new CompanyEditDialog( (Frame) SwingUtilities.getWindowAncestor(this), messages, true, null ); dialog.setVisible(true); if (dialog.isConfirmed()) { Company newCompany = dialog.getCompany(); // ÑéÖ¤ÊäÈë if (validateInput(newCompany)) { // ¼ì²é¹«Ë¾Ãû³ÆÊÇ·ñΨһ if (isCompanyNameExists(newCompany.getCompanyName())) { JOptionPane.showMessageDialog(this, getMessage("COMPANY_NAME_EXISTS"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return; } // ±£´æµ½Êý¾Ý¿â if (saveCompanyToDatabase(newCompany)) { // ¸üÐÂÄÚ´æÊý¾Ý allCompanies.add(newCompany); updateTable(allCompanies); } } } } private void editSelectedCompany() { int selectedRow = companyTable.getSelectedRow(); if (selectedRow == -1) { JOptionPane.showMessageDialog(this, getMessage("SELECT_COMPANY_TO_EDIT"), getMessage("WARNING"), JOptionPane.WARNING_MESSAGE); return; } // »ñȡѡÖеĹ«Ë¾ int modelRow = companyTable.convertRowIndexToModel(selectedRow); String companyName = (String) tableModel.getValueAt(modelRow, 1); Company company = findCompanyByName(companyName); if (company == null) { JOptionPane.showMessageDialog(this, getMessage("COMPANY_NOT_FOUND"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return; } CompanyEditDialog dialog = new CompanyEditDialog( (Frame) SwingUtilities.getWindowAncestor(this), messages, false, company ); dialog.setVisible(true); if (dialog.isConfirmed()) { Company updatedCompany = dialog.getCompany(); // ÑéÖ¤ÊäÈë if (validateInput(updatedCompany)) { // ¸üй«Ë¾ÐÅÏ¢ company.setCompanyName(updatedCompany.getCompanyName()); // ¸üÐÂÊý¾Ý¿â if (updateCompanyInDatabase(company)) { // ¸üбí¸ñ updateTable(allCompanies); } } } } private void deleteSelectedCompanies() { int[] selectedRows = companyTable.getSelectedRows(); if (selectedRows.length == 0) { JOptionPane.showMessageDialog(this, getMessage("SELECT_COMPANY_TO_DELETE"), getMessage("WARNING"), JOptionPane.WARNING_MESSAGE); return; } int confirm = JOptionPane.showConfirmDialog(this, getMessage("CONFIRM_DELETE") + " " + selectedRows.length + " " + getMessage("COMPANIES") + "?", getMessage("CONFIRM"), JOptionPane.YES_NO_OPTION); if (confirm == JOptionPane.YES_OPTION) { Arrays.sort(selectedRows); for (int i = selectedRows.length - 1; i >= 0; i--) { int viewRow = selectedRows[i]; int modelRow = companyTable.convertRowIndexToModel(viewRow); String companyName = (String) tableModel.getValueAt(modelRow, 1); Company company = findCompanyByName(companyName); if (company != null) { // ´ÓÊý¾Ý¿âɾ³ý if (deleteCompanyFromDatabase(company)) { // ´ÓÄÚ´æÊý¾Ýɾ³ý allCompanies.remove(company); } } } // ˢбí¸ñ updateTable(allCompanies); } } private boolean validateInput(Company company) { // ¹«Ë¾Ãû³Æ²»ÄÜΪ¿Õ if (company.getCompanyName() == null || company.getCompanyName().trim().isEmpty()) { JOptionPane.showMessageDialog(this, getMessage("COMPANY_NAME_REQUIRED"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } // ¹«Ë¾Ãû³Æ³¤¶ÈÑéÖ¤ (5-50×Ö·û) String name = company.getCompanyName().trim(); if (name.length() < 5 || name.length() > 50) { JOptionPane.showMessageDialog(this, getMessage("COMPANY_NAME_LENGTH"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } return true; } private boolean isCompanyNameExists(String companyName) { for (Company company : allCompanies) { if (company.getCompanyName().equals(companyName)) { return true; } } return false; } private Company findCompanyByName(String companyName) { for (Company company : allCompanies) { if (company.getCompanyName().equals(companyName)) { return company; } } return null; } private boolean saveCompanyToDatabase(Company company) { try { Dell_company.insertCompany(company); JOptionPane.showMessageDialog(this, getMessage("SAVE_SUCCESS"), getMessage("SUCCESS"), JOptionPane.INFORMATION_MESSAGE); return true; } catch (SQLException ex) { JOptionPane.showMessageDialog(this, getMessage("SAVE_FAILED") + ": " + ex.getMessage(), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } } private boolean updateCompanyInDatabase(Company company) { try { Dell_company.updateCompany(company); JOptionPane.showMessageDialog(this, getMessage("UPDATE_SUCCESS"), getMessage("SUCCESS"), JOptionPane.INFORMATION_MESSAGE); return true; } catch (SQLException ex) { JOptionPane.showMessageDialog(this, getMessage("UPDATE_FAILED") + ": " + ex.getMessage(), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } } private boolean deleteCompanyFromDatabase(Company company) { try { Dell_company.deleteCompany(company.getId()); JOptionPane.showMessageDialog(this, getMessage("DELETE_SUCCESS"), getMessage("SUCCESS"), JOptionPane.INFORMATION_MESSAGE); return true; } catch (SQLException ex) { JOptionPane.showMessageDialog(this, getMessage("DELETE_FAILED") + ": " + ex.getMessage(), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } } private String getMessage(String key) { try { return messages.getString(key); } catch (Exception e) { return "[" + key + "]"; } } // ¹«Ë¾±à¼­¶Ô»°¿ò class CompanyEditDialog extends JDialog { private static final long serialVersionUID = 1L; private JTextField nameField; private JTextField dateField; private boolean confirmed = false; public CompanyEditDialog(Frame parent, ResourceBundle messages, boolean isAddDialog, Company existingCompany) { super(parent, isAddDialog ? getMessage("ADD_COMPANY") : getMessage("EDIT_COMPANY"), true); setLayout(new BorderLayout()); setSize(400, 150); setLocationRelativeTo(parent); JPanel formPanel = new JPanel(new GridLayout(2, 2, 5, 5)); formPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); // ¹«Ë¾Ãû³Æ JLabel nameLabel = new JLabel(getMessage("NAME") + ": *"); nameField = new JTextField(); formPanel.add(nameLabel); formPanel.add(nameField); // Ìí¼ÓÈÕÆÚ JLabel dateLabel = new JLabel(getMessage("DATE") + ":"); dateField = new JTextField(); dateField.setEditable(false); // ÉèÖõ±Ç°ÈÕÆÚ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dateField.setText(sdf.format(new Date())); formPanel.add(dateLabel); formPanel.add(dateField); // Èç¹ûÊDZ༭¶Ô»°¿ò£¬Ìî³äÏÖÓÐÊý¾Ý if (!isAddDialog && existingCompany != null) { nameField.setText(existingCompany.getCompanyName()); dateField.setText(existingCompany.getAddedDate()); } add(formPanel, BorderLayout.CENTER); // °´Å¥Ãæ°å JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); JButton okButton = new JButton(getMessage("OK")); JButton cancelButton = new JButton(getMessage("CANCEL")); okButton.addActionListener(e -> { confirmed = true; dispose(); }); cancelButton.addActionListener(e -> dispose()); buttonPanel.add(okButton); buttonPanel.add(cancelButton); add(buttonPanel, BorderLayout.SOUTH); } public Company getCompany() { if (!confirmed) { return null; } Company company = new Company(); company.setCompanyName(nameField.getText()); company.setAddedDate(dateField.getText()); return company; } public boolean isConfirmed() { return confirmed; } } }