package dell_anchor;
|
|
import databases.DBConnector;
|
import dell_targets.Dell_BaseStation;
|
import targets.LocationBaseStation;
|
|
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;
|
|
public class BaseStationAdjacentConfigPanel extends JPanel {
|
private static final long serialVersionUID = 1L;
|
private JTable adjacentTable;
|
private DefaultTableModel tableModel;
|
private JTextField searchField;
|
private List<LocationBaseStation> allBaseStations;
|
private ResourceBundle messages;
|
|
public BaseStationAdjacentConfigPanel(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 quickConfigButton = new JButton(getMessage("QUICK_CONFIG"));
|
JButton editButton = new JButton(getMessage("EDIT"));
|
|
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
|
buttonPanel.add(quickConfigButton);
|
buttonPanel.add(editButton);
|
buttonPanel.add(searchButton);
|
buttonPanel.add(refreshButton);
|
|
searchPanel.add(searchField, BorderLayout.CENTER);
|
searchPanel.add(buttonPanel, BorderLayout.EAST);
|
|
// Ìí¼Óʼþ¼àÌýÆ÷
|
refreshButton.addActionListener(e -> loadBaseStationData());
|
editButton.addActionListener(e -> editSelectedBaseStation());
|
quickConfigButton.addActionListener(e -> quickConfigureAllAdjacent());
|
searchButton.addActionListener(new SearchAction());
|
|
// ´´½¨±í¸ñÁÐÃû
|
String[] columnNames = {
|
getMessage("INDEX"),
|
getMessage("BASE_STATION_ID"),
|
getMessage("ADJACENT1"),
|
getMessage("ADJACENT2"),
|
getMessage("ADJACENT3"),
|
getMessage("ADJACENT4"),
|
getMessage("ADJACENT5"),
|
getMessage("ADJACENT6"),
|
getMessage("ADJACENT7"),
|
getMessage("ADJACENT8"),
|
getMessage("ADJACENT9"),
|
getMessage("ADJACENT10"),
|
getMessage("COMPANY")
|
};
|
|
// ´´½¨±í¸ñÄ£ÐÍ
|
tableModel = new DefaultTableModel(columnNames, 0) {
|
@Override
|
public boolean isCellEditable(int row, int column) {
|
return false;
|
}
|
};
|
|
// ´´½¨±í¸ñ
|
adjacentTable = new JTable(tableModel);
|
adjacentTable.setAutoCreateRowSorter(true);
|
adjacentTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // Ö»Äܵ¥Ñ¡
|
|
// ÉèÖÃÁпí
|
TableColumnModel columnModel = adjacentTable.getColumnModel();
|
columnModel.getColumn(0).setPreferredWidth(30); // ÐòºÅ
|
columnModel.getColumn(1).setPreferredWidth(100); // »ùÕ¾±àºÅ
|
for (int i = 2; i <= 11; i++) {
|
columnModel.getColumn(i).setPreferredWidth(70); // ÁÙ½ü1-10
|
}
|
columnModel.getColumn(12).setPreferredWidth(100); // ËùÊô¹«Ë¾
|
|
// ÉèÖñíÍ·Ñùʽ
|
JTableHeader header = adjacentTable.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(adjacentTable);
|
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<LocationBaseStation> baseStations) {
|
tableModel.setRowCount(0); // Çå¿Õ±í¸ñ
|
|
int index = 1;
|
for (LocationBaseStation bs : baseStations) {
|
tableModel.addRow(new Object[]{
|
index++,
|
bs.getCode(),
|
bs.getAdjacent1(),
|
bs.getAdjacent2(),
|
bs.getAdjacent3(),
|
bs.getAdjacent4(),
|
bs.getAdjacent5(),
|
bs.getAdjacent6(),
|
bs.getAdjacent7(),
|
bs.getAdjacent8(),
|
bs.getAdjacent9(),
|
bs.getAdjacent10(),
|
bs.getCompany()
|
});
|
}
|
}
|
|
// ±à¼Ñ¡ÖеĻùÕ¾ÁÙ½üÅäÖÃ
|
private void editSelectedBaseStation() {
|
int selectedRow = adjacentTable.getSelectedRow();
|
if (selectedRow == -1) {
|
JOptionPane.showMessageDialog(this, getMessage("SELECT_BASE_STATION_TO_EDIT"),
|
getMessage("WARNING"), JOptionPane.WARNING_MESSAGE);
|
return;
|
}
|
|
// »ñȡѡÖеĻùÕ¾
|
int modelRow = adjacentTable.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;
|
}
|
|
// ´ò¿ª±à¼¶Ô»°¿ò
|
AdjacentEditDialog dialog = new AdjacentEditDialog(
|
(Frame) SwingUtilities.getWindowAncestor(this),
|
messages,
|
bs
|
);
|
dialog.setVisible(true);
|
|
if (dialog.isConfirmed()) {
|
LocationBaseStation updatedBs = dialog.getUpdatedBaseStation();
|
|
// ¸üÐÂÄÚ´æÊý¾Ý
|
bs.setAdjacent1(updatedBs.getAdjacent1());
|
bs.setAdjacent2(updatedBs.getAdjacent2());
|
bs.setAdjacent3(updatedBs.getAdjacent3());
|
bs.setAdjacent4(updatedBs.getAdjacent4());
|
bs.setAdjacent5(updatedBs.getAdjacent5());
|
bs.setAdjacent6(updatedBs.getAdjacent6());
|
bs.setAdjacent7(updatedBs.getAdjacent7());
|
bs.setAdjacent8(updatedBs.getAdjacent8());
|
bs.setAdjacent9(updatedBs.getAdjacent9());
|
bs.setAdjacent10(updatedBs.getAdjacent10());
|
|
// ¸üÐÂÊý¾Ý¿â
|
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 void quickConfigureAllAdjacent() {
|
// °´¹«Ë¾·Ö×é»ùÕ¾
|
Map<String, List<LocationBaseStation>> stationsByCompany = new HashMap<>();
|
for (LocationBaseStation bs : allBaseStations) {
|
String company = bs.getCompany();
|
if (company == null || company.isEmpty()) continue;
|
|
stationsByCompany.computeIfAbsent(company, k -> new ArrayList<>()).add(bs);
|
}
|
|
int successCount = 0;
|
int failCount = 0;
|
|
// ´¦Àíÿ¸ö¹«Ë¾µÄ»ùÕ¾
|
for (List<LocationBaseStation> companyStations : stationsByCompany.values()) {
|
int stationCount = companyStations.size();
|
|
// ¼ì²é»ùÕ¾ÊýÁ¿ÊÇ·ñ·ûºÏÒªÇó (2-11)
|
if (stationCount < 2 || stationCount > 11) {
|
failCount += stationCount;
|
continue;
|
}
|
|
// Ϊÿ¸ö»ùÕ¾ÅäÖÃÁÙ½ü¹ØÏµ
|
for (LocationBaseStation currentBs : companyStations) {
|
// »ñÈ¡³ý×ÔÉíÍâµÄÆäËû»ùÕ¾
|
List<LocationBaseStation> otherStations = new ArrayList<>(companyStations);
|
otherStations.remove(currentBs);
|
|
// ÉèÖÃÁÙ½ü¹ØÏµ
|
setAdjacentStations(currentBs, otherStations);
|
|
// ¸üÐÂÊý¾Ý¿â
|
if (updateBaseStationInDatabase(currentBs)) {
|
successCount++;
|
} else {
|
failCount++;
|
}
|
}
|
}
|
|
// ˢбí¸ñÏÔʾ
|
loadBaseStationData();
|
|
// ÏÔʾ½á¹û
|
String message = String.format(getMessage("QUICK_CONFIG_RESULT"), successCount, failCount);
|
JOptionPane.showMessageDialog(this, message,
|
getMessage("INFO"), JOptionPane.INFORMATION_MESSAGE);
|
}
|
|
// ÉèÖûùÕ¾µÄÁÙ½ü¹ØÏµ
|
private void setAdjacentStations(LocationBaseStation bs, List<LocationBaseStation> adjacentStations) {
|
// Çå³ýËùÓÐÁÙ½ü×Ö¶Î
|
bs.setAdjacent1("");
|
bs.setAdjacent2("");
|
bs.setAdjacent3("");
|
bs.setAdjacent4("");
|
bs.setAdjacent5("");
|
bs.setAdjacent6("");
|
bs.setAdjacent7("");
|
bs.setAdjacent8("");
|
bs.setAdjacent9("");
|
bs.setAdjacent10("");
|
|
// ÉèÖÃеÄÁÙ½ü¹ØÏµ
|
for (int i = 0; i < Math.min(adjacentStations.size(), 10); i++) {
|
String adjacentCode = adjacentStations.get(i).getCode();
|
switch (i) {
|
case 0: bs.setAdjacent1(adjacentCode); break;
|
case 1: bs.setAdjacent2(adjacentCode); break;
|
case 2: bs.setAdjacent3(adjacentCode); break;
|
case 3: bs.setAdjacent4(adjacentCode); break;
|
case 4: bs.setAdjacent5(adjacentCode); break;
|
case 5: bs.setAdjacent6(adjacentCode); break;
|
case 6: bs.setAdjacent7(adjacentCode); break;
|
case 7: bs.setAdjacent8(adjacentCode); break;
|
case 8: bs.setAdjacent9(adjacentCode); break;
|
case 9: bs.setAdjacent10(adjacentCode); break;
|
}
|
}
|
}
|
|
// ¸ù¾ÝID²éÕÒ»ùÕ¾
|
private LocationBaseStation findBaseStationById(String deviceId) {
|
for (LocationBaseStation bs : allBaseStations) {
|
if (bs.getCode().equals(deviceId)) {
|
return bs;
|
}
|
}
|
return null;
|
}
|
|
// ¸üÐÂÊý¾Ý¿âÖеĻùÕ¾
|
private boolean updateBaseStationInDatabase(LocationBaseStation bs) {
|
Map<String, String> fieldValues = new HashMap<>();
|
fieldValues.put("adjacent1", bs.getAdjacent1());
|
fieldValues.put("adjacent2", bs.getAdjacent2());
|
fieldValues.put("adjacent3", bs.getAdjacent3());
|
fieldValues.put("adjacent4", bs.getAdjacent4());
|
fieldValues.put("adjacent5", bs.getAdjacent5());
|
fieldValues.put("adjacent6", bs.getAdjacent6());
|
fieldValues.put("adjacent7", bs.getAdjacent7());
|
fieldValues.put("adjacent8", bs.getAdjacent8());
|
fieldValues.put("adjacent9", bs.getAdjacent9());
|
fieldValues.put("adjacent10", bs.getAdjacent10());
|
|
// ÕÒµ½Êý¾Ý¿âID
|
int dbId = (int) bs.getId();
|
|
int result = DBConnector.updateData("location_base_station", fieldValues, 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<LocationBaseStation> 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 AdjacentEditDialog extends JDialog {
|
private ResourceBundle messages;
|
private JTextField idField;
|
private JTextField[] adjacentFields = new JTextField[10];
|
private boolean confirmed = false;
|
private LocationBaseStation baseStation;
|
|
public AdjacentEditDialog(Frame parent, ResourceBundle messages, LocationBaseStation existingBs) {
|
super(parent, messages.getString("EDIT_ADJACENT_CONFIG"), true);
|
this.messages = messages;
|
this.baseStation = existingBs;
|
setLayout(new BorderLayout());
|
setSize(500, 500);
|
setLocationRelativeTo(parent);
|
|
JPanel formPanel = new JPanel(new GridLayout(11, 2, 5, 5));
|
formPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
|
|
// »ùÕ¾±àºÅ£¨²»¿É±à¼£©
|
JLabel idLabel = new JLabel(messages.getString("BASE_STATION_ID") + ":");
|
idField = new JTextField();
|
idField.setEditable(false);
|
formPanel.add(idLabel);
|
formPanel.add(idField);
|
|
// ´´½¨10¸öÁÙ½üÅäÖÃ×Ö¶Î
|
for (int i = 0; i < 10; i++) {
|
JLabel label = new JLabel(messages.getString("ADJACENT" + (i+1)) + ":");
|
adjacentFields[i] = new JTextField();
|
formPanel.add(label);
|
formPanel.add(adjacentFields[i]);
|
}
|
|
add(formPanel, BorderLayout.CENTER);
|
|
// Ìî³äÏÖÓÐÊý¾Ý
|
idField.setText(baseStation.getCode());
|
adjacentFields[0].setText(baseStation.getAdjacent1());
|
adjacentFields[1].setText(baseStation.getAdjacent2());
|
adjacentFields[2].setText(baseStation.getAdjacent3());
|
adjacentFields[3].setText(baseStation.getAdjacent4());
|
adjacentFields[4].setText(baseStation.getAdjacent5());
|
adjacentFields[5].setText(baseStation.getAdjacent6());
|
adjacentFields[6].setText(baseStation.getAdjacent7());
|
adjacentFields[7].setText(baseStation.getAdjacent8());
|
adjacentFields[8].setText(baseStation.getAdjacent9());
|
adjacentFields[9].setText(baseStation.getAdjacent10());
|
|
// °´Å¥Ãæ°å
|
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 -> {
|
if (validateInput()) {
|
confirmed = true;
|
dispose();
|
}
|
});
|
|
cancelButton.addActionListener(e -> dispose());
|
|
buttonPanel.add(okButton);
|
buttonPanel.add(cancelButton);
|
add(buttonPanel, BorderLayout.SOUTH);
|
}
|
|
// ÑéÖ¤ÊäÈë
|
private boolean validateInput() {
|
// ÑéÖ¤HEX¸ñʽºÍ³¤¶È
|
Pattern hexPattern = Pattern.compile("[0-9A-Fa-f]*");
|
for (int i = 0; i < 10; i++) {
|
String value = adjacentFields[i].getText().trim();
|
if (!value.isEmpty()) {
|
if (!hexPattern.matcher(value).matches()) {
|
JOptionPane.showMessageDialog(this,
|
messages.getString("INVALID_HEX_FORMAT") + ": " + messages.getString("ADJACENT" + (i+1)),
|
messages.getString("ERROR"), JOptionPane.ERROR_MESSAGE);
|
adjacentFields[i].requestFocus();
|
return false;
|
}
|
if (value.length() > 6) {
|
JOptionPane.showMessageDialog(this,
|
messages.getString("ADJACENT_TOO_LONG") + ": " + messages.getString("ADJACENT" + (i+1)),
|
messages.getString("ERROR"), JOptionPane.ERROR_MESSAGE);
|
adjacentFields[i].requestFocus();
|
return false;
|
}
|
}
|
}
|
return true;
|
}
|
|
public LocationBaseStation getUpdatedBaseStation() {
|
LocationBaseStation result = new LocationBaseStation();
|
result.setId(baseStation.getId());
|
result.setCode(baseStation.getCode());
|
result.setAdjacent1(adjacentFields[0].getText());
|
result.setAdjacent2(adjacentFields[1].getText());
|
result.setAdjacent3(adjacentFields[2].getText());
|
result.setAdjacent4(adjacentFields[3].getText());
|
result.setAdjacent5(adjacentFields[4].getText());
|
result.setAdjacent6(adjacentFields[5].getText());
|
result.setAdjacent7(adjacentFields[6].getText());
|
result.setAdjacent8(adjacentFields[7].getText());
|
result.setAdjacent9(adjacentFields[8].getText());
|
result.setAdjacent10(adjacentFields[9].getText());
|
return result;
|
}
|
|
public boolean isConfirmed() {
|
return confirmed;
|
}
|
}
|
}
|