package dell_anchor; import databases.DBConnector; import dell_targets.Dell_BaseStation; import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.table.*; import java.awt.*; import java.awt.event.*; import java.sql.SQLException; import java.util.*; import java.util.List; import java.util.ResourceBundle; import java.util.regex.Pattern; import targets.LocationBaseStation; public class BaseStationManagementPanel extends JPanel { private static final long serialVersionUID = 1L; private JTable baseStationTable; private DefaultTableModel tableModel; private JTextField searchField; private List allBaseStations; private ResourceBundle messages; public BaseStationManagementPanel(ResourceBundle messages) { this.messages = messages; setLayout(new BorderLayout()); setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // ´´½¨ËÑË÷Ãæ°å JPanel searchPanel = new JPanel(new BorderLayout(5, 5)); searchPanel.setBorder(BorderFactory.createTitledBorder(getMessage("SEARCH"))); // ´´½¨ËÑË÷×é¼þ searchField = new JTextField(15); JButton searchButton = new JButton(getMessage("SEARCH")); JButton refreshButton = new JButton(getMessage("REFRESH")); JButton addButton = new JButton(getMessage("ADD")); JButton editButton = new JButton(getMessage("EDIT")); JButton deleteButton = new JButton(getMessage("DELETE")); refreshButton.addActionListener(e -> loadBaseStationData()); JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5)); buttonPanel.add(searchButton); buttonPanel.add(refreshButton); buttonPanel.add(addButton); buttonPanel.add(editButton); buttonPanel.add(deleteButton); searchPanel.add(searchField, BorderLayout.CENTER); searchPanel.add(buttonPanel, BorderLayout.EAST); // Ìí¼Ó°´Å¥Ê¼þ¼àÌýÆ÷ addButton.addActionListener(e -> addNewBaseStation()); editButton.addActionListener(e -> editSelectedBaseStation()); deleteButton.addActionListener(e -> deleteSelectedBaseStations()); searchButton.addActionListener(new SearchAction()); // ´´½¨±í¸ñÁÐÃû String[] columnNames = { getMessage("INDEX"), getMessage("BASE_STATION_ID"), getMessage("BASE_STATION_STATUS"), getMessage("X_COORD"), getMessage("Y_COORD"), getMessage("Z_COORD"), getMessage("LAYER"), getMessage("GROUP"), getMessage("PORT"), getMessage("FIRMWARE_VERSION"), getMessage("ONLINE_TIME"), getMessage("COMPANY") }; // ´´½¨±í¸ñÄ£ÐÍ tableModel = new DefaultTableModel(columnNames, 0) { @Override public boolean isCellEditable(int row, int column) { return false; } }; // ´´½¨±í¸ñ baseStationTable = new JTable(tableModel); baseStationTable.setAutoCreateRowSorter(true); baseStationTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); // ÉèÖÃÁпí TableColumnModel columnModel = baseStationTable.getColumnModel(); columnModel.getColumn(0).setPreferredWidth(30); // ÐòºÅ columnModel.getColumn(1).setPreferredWidth(100); // »ùÕ¾±àºÅ columnModel.getColumn(2).setPreferredWidth(70); // ״̬ columnModel.getColumn(3).setPreferredWidth(70); // X×ø±ê columnModel.getColumn(4).setPreferredWidth(70); // Y×ø±ê columnModel.getColumn(5).setPreferredWidth(70); // Z×ø±ê columnModel.getColumn(6).setPreferredWidth(70); // ËùÔÚ²ã columnModel.getColumn(7).setPreferredWidth(70); // ËùÔÚ×é columnModel.getColumn(8).setPreferredWidth(70); // ¶Ë¿Ú columnModel.getColumn(9).setPreferredWidth(100); // ¹Ì¼þ°æ±¾ columnModel.getColumn(10).setPreferredWidth(120); // ÉÏÏßʱ¼ä columnModel.getColumn(11).setPreferredWidth(100); // ËùÊô¹«Ë¾ // ÉèÖñíÍ·Ñùʽ JTableHeader header = baseStationTable.getTableHeader(); // ÉèÖñíÍ·×ó¶ÔÆë header.setDefaultRenderer(new DefaultTableCellRenderer() { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); setHorizontalAlignment(SwingConstants.LEFT); setBackground(Color.GRAY); setForeground(Color.WHITE); setFont(getFont().deriveFont(Font.BOLD)); return this; } }); JScrollPane scrollPane = new JScrollPane(baseStationTable); scrollPane.setPreferredSize(new Dimension(1200, 500)); // Ìí¼Ó×é¼þ add(searchPanel, BorderLayout.NORTH); add(scrollPane, BorderLayout.CENTER); // ¼ÓÔØÊý¾Ý loadBaseStationData(); } private void loadBaseStationData() { try { allBaseStations = Dell_BaseStation.getBaseStations(); updateTable(allBaseStations); } catch (SQLException ex) { JOptionPane.showMessageDialog(this, getMessage("DATA_LOAD_ERROR") + ": " + ex.getMessage(), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); } } private void updateTable(List baseStations) { tableModel.setRowCount(0); // Çå¿Õ±í¸ñ int index = 1; for (LocationBaseStation bs : baseStations) { // ת»»ÔÚÏß״̬ String status = "1".equals(bs.getStatus()) ? getMessage("ONLINE") : getMessage("OFFLINE"); // Ìí¼ÓÐÐÊý¾Ý tableModel.addRow(new Object[]{ index++, bs.getCode(), status, bs.getXCoordinate(), bs.getYCoordinate(), bs.getZCoordinate(), bs.getFloor(), bs.getGroup(), bs.getPort(), bs.getFirmwareVersion(), bs.getOnlineTime(), bs.getCompany() }); } } // Ìí¼ÓлùÕ¾ private void addNewBaseStation() { BaseStationEditDialog dialog = new BaseStationEditDialog( (Frame) SwingUtilities.getWindowAncestor(this), messages, true, null ); dialog.setVisible(true); if (dialog.isConfirmed()) { LocationBaseStation newBaseStation = dialog.getBaseStation(); // ÑéÖ¤ÊäÈë if (validateInput(newBaseStation)) { // ¼ì²éÉ豸IDÊÇ·ñΨһ if (isDeviceIdExists(newBaseStation.getCode())) { JOptionPane.showMessageDialog(this, getMessage("DEVICE_ID_EXISTS"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return; } // ÉèÖÃĬÈÏÖµ newBaseStation.setStatus("1"); // ĬÈÏÔÚÏß newBaseStation.setOnlineTime(new Date().toString()); // µ±Ç°Ê±¼ä×÷ΪÉÏÏßʱ¼ä // ±£´æµ½Êý¾Ý¿â if (saveBaseStationToDatabase(newBaseStation)) { // ¸üÐÂÄÚ´æÊý¾Ý allBaseStations.add(newBaseStation); updateTable(allBaseStations); JOptionPane.showMessageDialog(this, getMessage("SAVE_SUCCESS"), getMessage("SUCCESS"), JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(this, getMessage("SAVE_FAILED"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); } } } } // ±à¼­Ñ¡ÖеĻùÕ¾ private void editSelectedBaseStation() { int selectedRow = baseStationTable.getSelectedRow(); if (selectedRow == -1) { JOptionPane.showMessageDialog(this, getMessage("SELECT_BASE_STATION_TO_EDIT"), getMessage("WARNING"), JOptionPane.WARNING_MESSAGE); return; } // »ñȡѡÖеĻùÕ¾ID int modelRow = baseStationTable.convertRowIndexToModel(selectedRow); String deviceId = (String) tableModel.getValueAt(modelRow, 1); LocationBaseStation bs = findBaseStationById(deviceId); if (bs == null) { JOptionPane.showMessageDialog(this, getMessage("BASE_STATION_NOT_FOUND"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return; } BaseStationEditDialog dialog = new BaseStationEditDialog( (Frame) SwingUtilities.getWindowAncestor(this), messages, false, bs ); dialog.setVisible(true); if (dialog.isConfirmed()) { LocationBaseStation updatedBs = dialog.getBaseStation(); // ÑéÖ¤ÊäÈë if (validateInput(updatedBs)) { // ¸üлùÕ¾ÐÅÏ¢ bs.setXCoordinate(updatedBs.getXCoordinate()); bs.setYCoordinate(updatedBs.getYCoordinate()); bs.setZCoordinate(updatedBs.getZCoordinate()); bs.setFloor(updatedBs.getFloor()); bs.setGroup(updatedBs.getGroup()); bs.setPort(updatedBs.getPort()); bs.setFirmwareVersion(updatedBs.getFirmwareVersion()); bs.setCompany(updatedBs.getCompany()); // ¸üÐÂÊý¾Ý¿â if (updateBaseStationInDatabase(bs)) { // ¸üбí¸ñ updateTable(allBaseStations); JOptionPane.showMessageDialog(this, getMessage("UPDATE_SUCCESS"), getMessage("SUCCESS"), JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(this, getMessage("UPDATE_FAILED"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); } } } } // ÑéÖ¤ÊäÈë private boolean validateInput(LocationBaseStation bs) { // É豸ID²»ÄÜΪ¿ÕÇÒΪHEX¸ñʽ if (bs.getCode() == null || bs.getCode().trim().isEmpty()) { JOptionPane.showMessageDialog(this, getMessage("DEVICE_ID_REQUIRED"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } // HEX¸ñʽÑéÖ¤£¨0-9, A-F£© if (!Pattern.matches("[0-9A-Fa-f]+", bs.getCode())) { JOptionPane.showMessageDialog(this, getMessage("INVALID_HEX_FORMAT"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } // É豸ID³¤¶È²»³¬¹ý10 if (bs.getCode().length() > 10) { JOptionPane.showMessageDialog(this, getMessage("DEVICE_ID_TOO_LONG"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } // ÑéÖ¤Êý×Ö×Ö¶Î String[] numberFields = { bs.getXCoordinate(), bs.getYCoordinate(), bs.getZCoordinate(), bs.getFloor(), bs.getGroup(), bs.getPort() }; for (String field : numberFields) { if (field != null && !field.isEmpty() && !isValidInteger(field)) { JOptionPane.showMessageDialog(this, getMessage("INVALID_NUMBER"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } } // ¹«Ë¾Ãû³Æ³¤¶ÈÑéÖ¤ if (bs.getCompany() != null && bs.getCompany().length() > 20) { JOptionPane.showMessageDialog(this, getMessage("FIELD_TOO_LONG").replace("{0}", "20"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } return true; } // ¼ì²éÊÇ·ñΪÓÐЧÕûÊý private boolean isValidInteger(String value) { try { Integer.parseInt(value); return true; } catch (NumberFormatException e) { return false; } } // ɾ³ýÑ¡ÖеĻùÕ¾ private void deleteSelectedBaseStations() { int[] selectedRows = baseStationTable.getSelectedRows(); if (selectedRows.length == 0) { JOptionPane.showMessageDialog(this, getMessage("SELECT_BASE_STATION_TO_DELETE"), getMessage("WARNING"), JOptionPane.WARNING_MESSAGE); return; } // È·ÈÏɾ³ý int confirm = JOptionPane.showConfirmDialog(this, getMessage("CONFIRM_DELETE") + " " + selectedRows.length + " " + getMessage("BASE_STATIONS") + "?", getMessage("CONFIRM"), JOptionPane.YES_NO_OPTION); if (confirm == JOptionPane.YES_OPTION) { int successCount = 0; int failCount = 0; // µ¹Ðòɾ³ý±ÜÃâË÷Òý±ä»¯ Arrays.sort(selectedRows); for (int i = selectedRows.length - 1; i >= 0; i--) { int viewRow = selectedRows[i]; int modelRow = baseStationTable.convertRowIndexToModel(viewRow); String deviceId = (String) tableModel.getValueAt(modelRow, 1); LocationBaseStation bs = findBaseStationById(deviceId); if (bs != null) { // ´ÓÊý¾Ý¿âɾ³ý if (deleteBaseStationFromDatabase(bs)) { // ´ÓÄÚ´æÊý¾Ýɾ³ý allBaseStations.remove(bs); successCount++; } else { failCount++; } } } // ¸üбí¸ñ updateTable(allBaseStations); // ÏÔʾ½á¹û String message = String.format(getMessage("DELETE_RESULT"), successCount, failCount); JOptionPane.showMessageDialog(this, message, getMessage("INFO"), JOptionPane.INFORMATION_MESSAGE); } } // ¼ì²éÉ豸IDÊÇ·ñ´æÔÚ private boolean isDeviceIdExists(String deviceId) { for (LocationBaseStation bs : allBaseStations) { if (bs.getCode().equals(deviceId)) { return true; } } return false; } // ¸ù¾ÝID²éÕÒ»ùÕ¾ private LocationBaseStation findBaseStationById(String deviceId) { for (LocationBaseStation bs : allBaseStations) { if (bs.getCode().equals(deviceId)) { return bs; } } return null; } // ±£´æ»ùÕ¾µ½Êý¾Ý¿â private boolean saveBaseStationToDatabase(LocationBaseStation bs) { Map fieldValues = new HashMap<>(); fieldValues.put("base_station_id", bs.getCode()); fieldValues.put("x_coordinate", bs.getXCoordinate()); fieldValues.put("y_coordinate", bs.getYCoordinate()); fieldValues.put("z_coordinate", bs.getZCoordinate()); fieldValues.put("located_layer", bs.getFloor()); fieldValues.put("located_group", bs.getGroup()); fieldValues.put("ports", bs.getPort()); fieldValues.put("firmware_version", bs.getFirmwareVersion()); fieldValues.put("online_time", bs.getOnlineTime()); fieldValues.put("statuss", bs.getStatus()); fieldValues.put("company", bs.getCompany()); int result = DBConnector.insertData("location_base_station", fieldValues); return result > 0; } // ¸üÐÂÊý¾Ý¿âÖеĻùÕ¾ private boolean updateBaseStationInDatabase(LocationBaseStation bs) { Map fieldValues = new HashMap<>(); fieldValues.put("x_coordinate", bs.getXCoordinate()); fieldValues.put("y_coordinate", bs.getYCoordinate()); fieldValues.put("z_coordinate", bs.getZCoordinate()); fieldValues.put("located_layer", bs.getFloor()); fieldValues.put("located_group", bs.getGroup()); fieldValues.put("ports", bs.getPort()); fieldValues.put("firmware_version", bs.getFirmwareVersion()); fieldValues.put("company", bs.getCompany()); // ÕÒµ½Êý¾Ý¿âID int dbId = -1; for (LocationBaseStation b : allBaseStations) { if (b.getCode().equals(bs.getCode())) { dbId = (int) b.getId(); break; } } if (dbId == -1) { return false; } int result = DBConnector.updateData("location_base_station", fieldValues, dbId); return result > 0; } // ´ÓÊý¾Ý¿âɾ³ý»ùÕ¾ private boolean deleteBaseStationFromDatabase(LocationBaseStation bs) { // ÕÒµ½Êý¾Ý¿âID int dbId = (int) bs.getId(); int result = DBConnector.deleteDataById("location_base_station", dbId); return result > 0; } // ËÑË÷¹¦ÄÜ private class SearchAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String keyword = searchField.getText().trim(); if (keyword.isEmpty()) { updateTable(allBaseStations); return; } List filteredList = new ArrayList<>(); for (LocationBaseStation bs : allBaseStations) { if (matchesKeyword(bs, keyword)) { filteredList.add(bs); } } updateTable(filteredList); } private boolean matchesKeyword(LocationBaseStation bs, String keyword) { return (bs.getCode() != null && bs.getCode().contains(keyword)) || (bs.getCompany() != null && bs.getCompany().contains(keyword)); } } private String getMessage(String key) { try { return messages.getString(key); } catch (Exception e) { return "[" + key + "]"; } } } // »ùÕ¾±à¼­¶Ô»°¿ò class BaseStationEditDialog extends JDialog { private ResourceBundle messages; private JTextField idField; private JTextField xField; private JTextField yField; private JTextField zField; private JTextField floorField; private JTextField groupField; private JTextField portField; private JTextField firmwareField; private JTextField companyField; private boolean confirmed = false; private LocationBaseStation baseStation; public BaseStationEditDialog(Frame parent, ResourceBundle messages, boolean isAddDialog, LocationBaseStation existingBs) { super(parent, isAddDialog ? messages.getString("ADD_BASE_STATION") : messages.getString("EDIT_BASE_STATION"), true); this.messages = messages; this.baseStation = existingBs; setLayout(new BorderLayout()); setSize(500, 400); setLocationRelativeTo(parent); JPanel formPanel = new JPanel(new GridLayout(9, 2, 5, 5)); formPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); // »ùÕ¾±àºÅ JLabel idLabel = new JLabel(messages.getString("BASE_STATION_ID") + ": *"); idField = new JTextField(); formPanel.add(idLabel); formPanel.add(idField); // X×ø±ê JLabel xLabel = new JLabel(messages.getString("X_COORD") + ":"); xField = new JTextField(); formPanel.add(xLabel); formPanel.add(xField); // Y×ø±ê JLabel yLabel = new JLabel(messages.getString("Y_COORD") + ":"); yField = new JTextField(); formPanel.add(yLabel); formPanel.add(yField); // Z×ø±ê JLabel zLabel = new JLabel(messages.getString("Z_COORD") + ":"); zField = new JTextField(); formPanel.add(zLabel); formPanel.add(zField); // ËùÔÚ²ã JLabel floorLabel = new JLabel(messages.getString("LAYER") + ":"); floorField = new JTextField(); formPanel.add(floorLabel); formPanel.add(floorField); // ËùÔÚ×é JLabel groupLabel = new JLabel(messages.getString("GROUP") + ":"); groupField = new JTextField(); formPanel.add(groupLabel); formPanel.add(groupField); // ¶Ë¿Ú JLabel portLabel = new JLabel(messages.getString("PORT") + ":"); portField = new JTextField(); formPanel.add(portLabel); formPanel.add(portField); // ¹Ì¼þ°æ±¾ JLabel firmwareLabel = new JLabel(messages.getString("FIRMWARE_VERSION") + ":"); firmwareField = new JTextField(); formPanel.add(firmwareLabel); formPanel.add(firmwareField); // ËùÊô¹«Ë¾ JLabel companyLabel = new JLabel(messages.getString("COMPANY") + ":"); companyField = new JTextField(); formPanel.add(companyLabel); formPanel.add(companyField); add(formPanel, BorderLayout.CENTER); // Èç¹ûÊDZ༭ģʽ£¬Ìî³äÏÖÓÐÊý¾Ý if (!isAddDialog && existingBs != null) { idField.setText(existingBs.getCode()); idField.setEditable(false); // ±à¼­Ê±²»¿ÉÐÞ¸ÄID xField.setText(existingBs.getXCoordinate()); yField.setText(existingBs.getYCoordinate()); zField.setText(existingBs.getZCoordinate()); floorField.setText(existingBs.getFloor()); groupField.setText(existingBs.getGroup()); portField.setText(existingBs.getPort()); firmwareField.setText(existingBs.getFirmwareVersion()); companyField.setText(existingBs.getCompany()); } // °´Å¥Ãæ°å JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); JButton okButton = new JButton(messages.getString("OK")); JButton cancelButton = new JButton(messages.getString("CANCEL")); okButton.addActionListener(e -> { confirmed = true; dispose(); }); cancelButton.addActionListener(e -> dispose()); buttonPanel.add(okButton); buttonPanel.add(cancelButton); add(buttonPanel, BorderLayout.SOUTH); } public LocationBaseStation getBaseStation() { if (!confirmed) { return null; } LocationBaseStation result = new LocationBaseStation(); result.setCode(idField.getText()); result.setXCoordinate(xField.getText()); result.setYCoordinate(yField.getText()); result.setZCoordinate(zField.getText()); result.setFloor(floorField.getText()); result.setGroup(groupField.getText()); result.setPort(portField.getText()); result.setFirmwareVersion(firmwareField.getText()); result.setCompany(companyField.getText()); return result; } public boolean isConfirmed() { return confirmed; } }