package dell_targets; import dell_targets.Dell_differentialBaseStation; import targets.DifferentialBaseStation; 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 DifferentialBaseStationManagementPanel extends JPanel { private static final long serialVersionUID = 1L; private JTable baseStationTable; private DefaultTableModel tableModel; private List allBaseStations; private ResourceBundle messages; private JTextField searchField; @SuppressWarnings("serial") public DifferentialBaseStationManagementPanel(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("BASE_STATION_ID") + ":"); 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("BASE_STATION_ID"), getMessage("DEVICE_STATUS"), getMessage("TCP_PORT"), getMessage("IP_ADDRESS"), getMessage("SEND_PORT"), getMessage("COVERAGE_DISTANCE"), getMessage("LONGITUDE"), getMessage("LATITUDE"), getMessage("IOT_CARD_NUMBER"), getMessage("LAST_HEARTBEAT_TIME") }; // ±í¸ñÄ£ÐÍ 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.SINGLE_SELECTION); // ÉèÖÃÁпí TableColumnModel columnModel = baseStationTable.getColumnModel(); columnModel.getColumn(0).setPreferredWidth(50); // ÐòºÅ columnModel.getColumn(1).setPreferredWidth(100); // »ùÕ¾±àºÅ columnModel.getColumn(2).setPreferredWidth(80); // É豸״̬ columnModel.getColumn(3).setPreferredWidth(80); // TCP¶Ë¿Ú columnModel.getColumn(4).setPreferredWidth(120); // IPµØÖ· columnModel.getColumn(5).setPreferredWidth(80); // ·¢ËÍ¶Ë¿Ú columnModel.getColumn(6).setPreferredWidth(100); // ¸²¸Ç¾àÀë columnModel.getColumn(7).setPreferredWidth(100); // ¾­¶È columnModel.getColumn(8).setPreferredWidth(100); // γ¶È columnModel.getColumn(9).setPreferredWidth(120); // ÎïÁª¿¨ºÅ columnModel.getColumn(10).setPreferredWidth(150); // ¸üÐÂʱ¼ä // ÉèÖñíÍ·Ñùʽ JTableHeader header = baseStationTable.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 < baseStationTable.getColumnCount(); i++) { baseStationTable.getColumnModel().getColumn(i).setHeaderRenderer(headerRenderer); } JScrollPane scrollPane = new JScrollPane(baseStationTable); scrollPane.setPreferredSize(new Dimension(1200, 400)); // Ìí¼Ó×é¼þ add(topPanel, BorderLayout.NORTH); add(scrollPane, BorderLayout.CENTER); // ¼ÓÔØÊý¾Ý loadBaseStationData(); // Ìí¼Óʼþ¼àÌýÆ÷ addButton.addActionListener(e -> addNewBaseStation()); editButton.addActionListener(e -> editSelectedBaseStation()); deleteButton.addActionListener(e -> deleteSelectedBaseStations()); refreshButton.addActionListener(e -> loadBaseStationData()); searchButton.addActionListener(e -> searchBaseStations()); resetButton.addActionListener(e -> resetSearch()); } private void loadBaseStationData() { try { allBaseStations = Dell_differentialBaseStation.getAllBaseStations(); updateTable(allBaseStations); } catch (SQLException ex) { JOptionPane.showMessageDialog(this, getMessage("DATA_LOAD_ERROR") + ": " + ex.getMessage(), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); } } private void searchBaseStations() { String keyword = searchField.getText().trim(); if (keyword.isEmpty()) { updateTable(allBaseStations); return; } List filtered = new ArrayList<>(); for (DifferentialBaseStation station : allBaseStations) { if (station.getDeviceNumber().toLowerCase().contains(keyword.toLowerCase())) { filtered.add(station); } } if (filtered.isEmpty()) { JOptionPane.showMessageDialog(this, getMessage("SEARCH_NO_RESULTS"), getMessage("INFO"), JOptionPane.INFORMATION_MESSAGE); } updateTable(filtered); } private void resetSearch() { searchField.setText(""); updateTable(allBaseStations); } private void updateTable(List stations) { tableModel.setRowCount(0); int index = 1; for (DifferentialBaseStation station : stations) { // ת»»É豸״̬£º0->ÀëÏß, 1->ÔÚÏß String status = "1".equals(station.getDeviceStatus()) ? getMessage("ONLINE") : getMessage("OFFLINE"); tableModel.addRow(new Object[]{ index++, station.getDeviceNumber(), status, station.getCommunicationPort(), station.getIpAddress(), station.getSendPort(), station.getCoverageDistance() + " m", station.getLongitude(), station.getLatitude(), station.getIotCardNumber(), station.getLastHeartbeatTime() }); } } private void addNewBaseStation() { DifferentialBaseStationEditDialog dialog = new DifferentialBaseStationEditDialog( (Frame) SwingUtilities.getWindowAncestor(this), messages, true, null ); dialog.setVisible(true); if (dialog.isConfirmed()) { DifferentialBaseStation newStation = dialog.getBaseStation(); // ÑéÖ¤ÊäÈë if (validateInput(newStation)) { // ¼ì²é»ùÕ¾±àºÅÊÇ·ñÒÑ´æÔÚ if (isBaseStationIdExists(newStation.getDeviceNumber())) { JOptionPane.showMessageDialog(this, getMessage("BASE_STATION_ID_EXISTS"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return; } try { // ±£´æµ½Êý¾Ý¿â Dell_differentialBaseStation.insertBaseStation(newStation); JOptionPane.showMessageDialog(this, getMessage("SAVE_SUCCESS"), getMessage("SUCCESS"), JOptionPane.INFORMATION_MESSAGE); // ¸üÐÂÄÚ´æÊý¾Ý allBaseStations.add(newStation); updateTable(allBaseStations); } catch (SQLException ex) { JOptionPane.showMessageDialog(this, getMessage("SAVE_FAILED") + ": " + ex.getMessage(), 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; } // »ñȡѡÖеĻùÕ¾ int modelRow = baseStationTable.convertRowIndexToModel(selectedRow); String stationId = (String) tableModel.getValueAt(modelRow, 1); DifferentialBaseStation station = findBaseStationById(stationId); if (station == null) { JOptionPane.showMessageDialog(this, getMessage("BASE_STATION_NOT_FOUND"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return; } DifferentialBaseStationEditDialog dialog = new DifferentialBaseStationEditDialog( (Frame) SwingUtilities.getWindowAncestor(this), messages, false, station ); dialog.setVisible(true); if (dialog.isConfirmed()) { DifferentialBaseStation updatedStation = dialog.getBaseStation(); // ÑéÖ¤ÊäÈë if (validateInput(updatedStation)) { // ¸üлùÕ¾ÐÅÏ¢ station.setCommunicationPort(updatedStation.getCommunicationPort()); station.setSendPort(updatedStation.getSendPort()); station.setCoverageDistance(updatedStation.getCoverageDistance()); station.setLongitude(updatedStation.getLongitude()); station.setLatitude(updatedStation.getLatitude()); try { // ¸üÐÂÊý¾Ý¿â Dell_differentialBaseStation.updateBaseStation(station); JOptionPane.showMessageDialog(this, getMessage("UPDATE_SUCCESS"), getMessage("SUCCESS"), JOptionPane.INFORMATION_MESSAGE); // ¸üбí¸ñ updateTable(allBaseStations); } catch (SQLException ex) { JOptionPane.showMessageDialog(this, getMessage("UPDATE_FAILED") + ": " + ex.getMessage(), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); } } } } 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) { Arrays.sort(selectedRows); int successCount = 0; int failCount = 0; for (int i = selectedRows.length - 1; i >= 0; i--) { int viewRow = selectedRows[i]; int modelRow = baseStationTable.convertRowIndexToModel(viewRow); String stationId = (String) tableModel.getValueAt(modelRow, 1); DifferentialBaseStation station = findBaseStationById(stationId); if (station != null) { try { // ´ÓÊý¾Ý¿âɾ³ý Dell_differentialBaseStation.deleteBaseStation(station.getDeviceNumber()); // ´ÓÄÚ´æÊý¾Ýɾ³ý allBaseStations.remove(station); successCount++; } catch (SQLException ex) { failCount++; JOptionPane.showMessageDialog(this, getMessage("DELETE_FAILED") + ": " + station.getDeviceNumber() + " - " + ex.getMessage(), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); } } } // ˢбí¸ñ updateTable(allBaseStations); // ÏÔʾɾ³ý½á¹û JOptionPane.showMessageDialog(this, String.format(getMessage("DELETE_RESULT"), successCount, failCount), getMessage("INFO"), JOptionPane.INFORMATION_MESSAGE); } } private boolean validateInput(DifferentialBaseStation station) { // »ùÕ¾±àºÅÑéÖ¤ if (station.getDeviceNumber() == null || station.getDeviceNumber().trim().isEmpty()) { JOptionPane.showMessageDialog(this, getMessage("BASE_STATION_ID_REQUIRED"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } // HEX¸ñʽÑéÖ¤£¨³¤¶È²»³¬¹ý10£© if (!station.getDeviceNumber().matches("[0-9A-Fa-f]+")) { JOptionPane.showMessageDialog(this, getMessage("INVALID_HEX_FORMAT"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } if (station.getDeviceNumber().length() > 10) { JOptionPane.showMessageDialog(this, getMessage("BASE_STATION_ID_TOO_LONG"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } // TCP¶Ë¿ÚÑéÖ¤ if (station.getCommunicationPort() == null || station.getCommunicationPort().trim().isEmpty()) { JOptionPane.showMessageDialog(this, getMessage("TCP_PORT_REQUIRED"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } try { int port = Integer.parseInt(station.getCommunicationPort()); if (port < 1 || port > 65535) { throw new NumberFormatException(); } } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, getMessage("INVALID_TCP_PORT"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } if (station.getCommunicationPort().length() > 6) { JOptionPane.showMessageDialog(this, getMessage("TCP_PORT_TOO_LONG"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } // ·¢ËͶ˿ÚÑéÖ¤ if (station.getSendPort() == null || station.getSendPort().trim().isEmpty()) { JOptionPane.showMessageDialog(this, getMessage("SEND_PORT_REQUIRED"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } try { int port = Integer.parseInt(station.getSendPort()); if (port < 1 || port > 65535) { throw new NumberFormatException(); } } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, getMessage("INVALID_SEND_PORT"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } if (station.getSendPort().length() > 6) { JOptionPane.showMessageDialog(this, getMessage("SEND_PORT_TOO_LONG"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } // ¸²¸Ç¾àÀëÑéÖ¤ if (station.getCoverageDistance() == null || station.getCoverageDistance().trim().isEmpty()) { JOptionPane.showMessageDialog(this, getMessage("COVERAGE_DISTANCE_REQUIRED"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } try { int distance = Integer.parseInt(station.getCoverageDistance()); if (distance < 1) { throw new NumberFormatException(); } } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, getMessage("INVALID_COVERAGE_DISTANCE"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } if (station.getCoverageDistance().length() > 8) { JOptionPane.showMessageDialog(this, getMessage("COVERAGE_DISTANCE_TOO_LONG"), getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); return false; } return true; } private boolean isBaseStationIdExists(String stationId) { for (DifferentialBaseStation station : allBaseStations) { if (station.getDeviceNumber().equals(stationId)) { return true; } } return false; } private DifferentialBaseStation findBaseStationById(String stationId) { for (DifferentialBaseStation station : allBaseStations) { if (station.getDeviceNumber().equals(stationId)) { return station; } } return null; } private String getMessage(String key) { try { return messages.getString(key); } catch (Exception e) { return "[" + key + "]"; } } // ²î·Ö»ùÕ¾±à¼­¶Ô»°¿ò class DifferentialBaseStationEditDialog extends JDialog { private static final long serialVersionUID = 1L; private JTextField idField; private JTextField tcpPortField; private JTextField sendPortField; private JTextField coverageField; private JTextField longitudeField; private JTextField latitudeField; private JTextField dateField; private boolean confirmed = false; private boolean isAddDialog; public DifferentialBaseStationEditDialog(Frame parent, ResourceBundle messages, boolean isAddDialog, DifferentialBaseStation existingStation) { super(parent, isAddDialog ? getMessage("ADD_BASE_STATION") : getMessage("EDIT_BASE_STATION"), true); this.isAddDialog = isAddDialog; setLayout(new BorderLayout()); setSize(500, 300); setLocationRelativeTo(parent); JPanel formPanel = new JPanel(new GridLayout(7, 2, 5, 5)); formPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); // »ùÕ¾±àºÅ JLabel idLabel = new JLabel(getMessage("BASE_STATION_ID") + ": *"); idField = new JTextField(); if (!isAddDialog && existingStation != null) { idField.setEditable(false); } formPanel.add(idLabel); formPanel.add(idField); // TCP¶Ë¿Ú JLabel tcpPortLabel = new JLabel(getMessage("TCP_PORT") + ": *"); tcpPortField = new JTextField(); formPanel.add(tcpPortLabel); formPanel.add(tcpPortField); // ·¢ËÍ¶Ë¿Ú JLabel sendPortLabel = new JLabel(getMessage("SEND_PORT") + ": *"); sendPortField = new JTextField(); formPanel.add(sendPortLabel); formPanel.add(sendPortField); // ¸²¸Ç¾àÀë JLabel coverageLabel = new JLabel(getMessage("COVERAGE_DISTANCE") + ": *"); coverageField = new JTextField(); formPanel.add(coverageLabel); formPanel.add(coverageField); // ¾­¶È JLabel longitudeLabel = new JLabel(getMessage("LONGITUDE") + ":"); longitudeField = new JTextField(); formPanel.add(longitudeLabel); formPanel.add(longitudeField); // γ¶È JLabel latitudeLabel = new JLabel(getMessage("LATITUDE") + ":"); latitudeField = new JTextField(); formPanel.add(latitudeLabel); formPanel.add(latitudeField); // Ìí¼ÓÈÕÆÚ JLabel dateLabel = new JLabel(getMessage("DATE") + ":"); dateField = new JTextField(); dateField.setEditable(false); formPanel.add(dateLabel); formPanel.add(dateField); // Ìî³äÏÖÓÐÊý¾Ý£¨Èç¹ûÊDZ༭ģʽ£© if (!isAddDialog && existingStation != null) { idField.setText(existingStation.getDeviceNumber()); tcpPortField.setText(existingStation.getCommunicationPort()); sendPortField.setText(existingStation.getSendPort()); coverageField.setText(existingStation.getCoverageDistance()); longitudeField.setText(existingStation.getLongitude()); latitudeField.setText(existingStation.getLatitude()); dateField.setText(existingStation.getAddTime()); } else { // Ìí¼Óģʽ£¬ÉèÖõ±Ç°ÈÕÆÚ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dateField.setText(sdf.format(new Date())); } 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 DifferentialBaseStation getBaseStation() { if (!confirmed) { return null; } DifferentialBaseStation station = new DifferentialBaseStation(); station.setDeviceNumber(idField.getText()); station.setCommunicationPort(tcpPortField.getText()); station.setSendPort(sendPortField.getText()); station.setCoverageDistance(coverageField.getText()); station.setLongitude(longitudeField.getText()); station.setLatitude(latitudeField.getText()); station.setAddTime(dateField.getText()); // ÉèÖÃĬÈÏÖµ station.setDeviceStatus("1"); // ĬÈÏÔÚÏß station.setIpAddress("0.0.0.0"); station.setLastHeartbeatTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); station.setIotCardNumber(""); station.setCompany(""); return station; } public boolean isConfirmed() { return confirmed; } } }