This commit is contained in:
lds217 committed 2025-01-25 17:38:06 -05:00
1 parent 880dadcad7
commit 8d9b3ed69a
8 files changed
+295 -31

No files matched your search

@@ -1,25 +1,73 @@
package BsK.client.ui.component.CheckUpPage.AddDialog;
import javax.swing.*;
import java.awt.*;
import BsK.client.network.handler.ClientHandler;
import BsK.client.network.handler.ResponseListener;
import BsK.common.packet.req.GetRecentPatientRequest;
import BsK.common.packet.res.GetMedInfoResponse;
import BsK.common.packet.res.GetRecentPatientResponse;
import BsK.common.util.network.NetworkUtil;
import BsK.common.util.text.TextUtils;
import lombok.extern.slf4j.Slf4j;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
@Slf4j
public class AddDialog extends JDialog {
private JTextField patientNameField;
private JTextField patientYearField;
private JTextField patientIdField;
private JTextField patientPhoneField;
private JComboBox patientGenderField;
private JComboBox villageComboBox, districtComboBox, provinceComboBox;
private String[] villagesOptions, districtOptions, provinceOptions;
private DefaultTableModel patientTableModel;
private JTable patientTable;
private String[] patientColumns = {"Patient ID", "Patient Name", "Patient Year", "Patient Phone" ,"Patient Address"};
private String[][] patientData; // {"P001", "Lê Thành Đạt", "1985", "1234567890", "123 Main St, Springfield"
private final ResponseListener<GetRecentPatientResponse> getRecentPatientResponseListener = this::getRecentPatientHandler;
private void sendGetRecentPatientRequest() {
log.info("Sending GetRecentPatientRequest");
ClientHandler.addResponseListener(GetRecentPatientResponse.class, getRecentPatientResponseListener);
NetworkUtil.sendPacket(ClientHandler.ctx.channel(), new GetRecentPatientRequest());
}
void getRecentPatientHandler(GetRecentPatientResponse response) {
log.info("Received GetRecentPatientResponse");
patientData = response.getPatientData();
patientTableModel.setDataVector(patientData, patientColumns);
}
public AddDialog(Frame parent) {
super(parent, "Add Patient", true);
// Set size of the dialog
setSize(400, 300);
setSize(1000, 600);
setResizable(true);
// Send request to get the first 20 patients in the database
// Send request to get the latest 20 patients in the database
sendGetRecentPatientRequest();
// Add patent table on the right side
// Add a scroll pane to the table
patientTableModel = new DefaultTableModel(patientData, patientColumns) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
patientTable = new JTable(patientTableModel);
patientTable.setFont(new Font("Serif", Font.BOLD, 20));
patientTable.setRowHeight(30);
patientTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane(patientTable);
// Set layout of the dialog
// left side is the text fields, right side is the patient list
@@ -34,7 +82,7 @@ public class AddDialog extends JDialog {
gbc.insets = new Insets(5, 5, 5, 5); // Padding between components
gbc.weightx = 1.0; // Components stretch horizontally
gbc.weighty = 0.0; // Allow minor vertical flexibility
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST; // Align components to the left
gbc.gridx = 0;
@@ -43,22 +91,63 @@ public class AddDialog extends JDialog {
gbc.gridwidth = 1;
inputPanel.add(new JLabel("Patient name:"), gbc);
gbc.gridwidth = 2;
gbc.gridwidth = 3;
gbc.gridx = 1;
patientNameField = new JTextField(15);
patientNameField = new JTextField(20);
inputPanel.add(patientNameField, gbc);
gbc.gridx = 3;
// Add add icon
ImageIcon addIcon = new ImageIcon("src/main/java/BsK/client/ui/assets/icon/add.png");
ImageIcon addIconScaled = new ImageIcon(addIcon.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
JButton addButton = new JButton(addIconScaled);
addButton.addActionListener(e -> {
// Send request to add patient
patientNameField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
handleRowSelection(patientTable.getSelectedRow());
}
}
});
inputPanel.add(addButton, gbc);
patientNameField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
findAndSelectRow();
}
@Override
public void removeUpdate(DocumentEvent e) {
findAndSelectRow();
}
@Override
public void changedUpdate(DocumentEvent e) {
findAndSelectRow();
}
private void findAndSelectRow() {
String searchText = patientNameField.getText().trim();
if (searchText.isEmpty()) {
// Clear selection if search text is empty
patientTable.clearSelection();
return;
}
boolean found = false;
for (int row = 0; row < patientTable.getRowCount(); row++) {
// Assuming the search is targeting the name column (column index 1)
String cellValue = patientTable.getValueAt(row, 1).toString();
if (TextUtils.removeAccents(cellValue.toLowerCase()).contains(TextUtils.removeAccents(searchText.toLowerCase()))) {
patientTable.setRowSelectionInterval(row, row);
patientTable.scrollRectToVisible(patientTable.getCellRect(row, 1, true));
found = true;
break; // Stop after selecting the first match
}
}
if (!found) {
patientTable.clearSelection(); // No matches found
}
}
});
gbc.gridx = 0;
gbc.gridy++;
@@ -66,14 +155,33 @@ public class AddDialog extends JDialog {
inputPanel.add(new JLabel("Patient year:"), gbc);
gbc.gridx = 1;
patientYearField = new JTextField(15);
patientYearField = new JTextField(8);
inputPanel.add(patientYearField, gbc);
gbc.gridx = 2;
inputPanel.add(new JLabel("Patient ID:"), gbc);
inputPanel.add(new JLabel("Patient gender:"), gbc);
gbc.gridx = 3;
patientIdField = new JTextField(15);
patientGenderField = new JComboBox(new String[]{"Nam", "Nữ"});
inputPanel.add(patientGenderField, gbc);
gbc.gridx = 0;
gbc.gridy++;
inputPanel.add(new JLabel("Patient phone:"), gbc);
gbc.gridx = 1;
patientPhoneField = new JTextField(8);
inputPanel.add(patientPhoneField, gbc);
gbc.gridx = 2;
gbc.anchor = GridBagConstraints.NORTHEAST;
inputPanel.add(new JLabel("Patient ID:"), gbc);
gbc.gridx = 3;
gbc.anchor = GridBagConstraints.WEST;
patientIdField = new JTextField(4);
inputPanel.add(patientIdField, gbc);
gbc.gridx = 0;
@@ -81,9 +189,9 @@ public class AddDialog extends JDialog {
inputPanel.add(new JLabel("Address:"), gbc);
gbc.gridx = 1;
villagesOptions = new String[]{"Village 1", "Village 2", "Village 3"};
villageComboBox = new JComboBox(villagesOptions);
inputPanel.add(villageComboBox, gbc);
provinceOptions = new String[]{"Province 1", "Province 2", "Province 3"};
provinceComboBox = new JComboBox(provinceOptions);
inputPanel.add(provinceComboBox, gbc);
gbc.gridx = 2;
districtOptions = new String[]{"District 1", "District 2", "District 3"};
@@ -91,16 +199,60 @@ public class AddDialog extends JDialog {
inputPanel.add(districtComboBox, gbc);
gbc.gridx = 3;
provinceOptions = new String[]{"Province 1", "Province 2", "Province 3"};
provinceComboBox = new JComboBox(provinceOptions);
inputPanel.add(provinceComboBox, gbc);
villagesOptions = new String[]{"Village 1", "Village 2", "Village 3"};
villageComboBox = new JComboBox(villagesOptions);
inputPanel.add(villageComboBox, gbc);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, inputPanel, scrollPane);
splitPane.setResizeWeight(0.1);
add(splitPane, BorderLayout.CENTER);
JPanel ButtonPanel = new JPanel();
JButton closeButton = new JButton("Close");
closeButton.addActionListener(e -> {
setVisible(false);
});
ButtonPanel.add(closeButton);
add(inputPanel, BorderLayout.CENTER);
JButton saveButton = new JButton("Save");
saveButton.addActionListener(e -> {
// Send request to save the patient
});
ButtonPanel.add(saveButton);
add(ButtonPanel, BorderLayout.SOUTH);
}
private void handleRowSelection(int selectedRow) {
if (selectedRow < 0) {
return;
}
String patientId = patientTable.getValueAt(selectedRow, 0).toString();
String patientName = patientTable.getValueAt(selectedRow, 1).toString();
String patientYear = patientTable.getValueAt(selectedRow, 2).toString();
String patientPhone = patientTable.getValueAt(selectedRow, 3).toString();
String patientAddress = patientTable.getValueAt(selectedRow, 4).toString();
patientIdField.setText(patientId);
patientNameField.setText(patientName);
patientYearField.setText(patientYear);
patientPhoneField.setText(patientPhone);
// Extract province, district, village from the address
String[] addressParts = patientAddress.split(", ");
if (addressParts.length == 3) {
String village = addressParts[0];
String district = addressParts[1];
String province = addressParts[2];
villageComboBox.setSelectedItem(village);
districtComboBox.setSelectedItem(district);
provinceComboBox.setSelectedItem(province);
}
}
public static void main(String[] args) {
@@ -337,7 +337,6 @@ public class CheckUpPage extends JPanel {
inputPanel.add(customerHeightSpinner, gbc);
JLabel checkupInfoLabel = new JLabel("Thông tin khám bệnh");
checkupInfoLabel.setFont(checkupInfoLabel.getFont().deriveFont(Font.BOLD, 16)); // Bold, size 16
checkupInfoLabel.setHorizontalAlignment(SwingConstants.CENTER);
@@ -449,6 +448,8 @@ public class CheckUpPage extends JPanel {
}
medicinePrescription = new String[][]{};
servicePrescription = new String[][]{};
medDialog = null;
serDialog = null;
handleRowSelection(selectedRow);
}
@@ -11,6 +11,7 @@ import BsK.client.network.handler.ResponseListener;
import BsK.common.packet.req.GetMedInfoRequest;
import BsK.common.packet.res.GetMedInfoResponse;
import BsK.common.util.network.NetworkUtil;
import BsK.common.util.text.TextUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -64,7 +65,7 @@ public class MedicineDialog extends JDialog {
columnModel.getColumn(6).setPreferredWidth(20);
}
void sendGetMedInfoRequest() {
private void sendGetMedInfoRequest() {
log.info("Sending GetMedInfoRequest");
NetworkUtil.sendPacket(ClientHandler.ctx.channel(), new GetMedInfoRequest());
}
@@ -172,7 +173,8 @@ public class MedicineDialog extends JDialog {
for (int row = 0; row < medicineTable.getRowCount(); row++) {
// Assuming the search is targeting the name column (column index 1)
String cellValue = medicineTable.getValueAt(row, 1).toString();
if (cellValue.toLowerCase().contains(searchText.toLowerCase())) {
if (TextUtils.removeAccents(cellValue.toLowerCase()).contains(TextUtils.removeAccents(searchText.
toLowerCase()))) {
medicineTable.setRowSelectionInterval(row, row);
medicineTable.scrollRectToVisible(medicineTable.getCellRect(row, 1, true));
found = true;
@@ -11,6 +11,7 @@ import BsK.client.network.handler.ResponseListener;
import BsK.common.packet.req.GetSerInfoRequest;
import BsK.common.packet.res.GetSerInfoResponse;
import BsK.common.util.network.NetworkUtil;
import BsK.common.util.text.TextUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -163,7 +164,8 @@ public class ServiceDialog extends JDialog {
for (int row = 0; row < serviceTable.getRowCount(); row++) {
// Assuming the search is targeting the name column (column index 1)
String cellValue = serviceTable.getValueAt(row, 1).toString();
if (cellValue.toLowerCase().contains(searchText.toLowerCase())) {
if (TextUtils.removeAccents(cellValue.toLowerCase()).contains(TextUtils.removeAccents(searchText
.toLowerCase()))) {
serviceTable.setRowSelectionInterval(row, row);
serviceTable.scrollRectToVisible(serviceTable.getCellRect(row, 1, true));
found = true;
@@ -177,7 +179,6 @@ public class ServiceDialog extends JDialog {
}
});
gbc.gridx = 0;
gbc.gridy++;
inputPanel.add(new JLabel("Quantity:"), gbc);
@@ -0,0 +1,12 @@
package BsK.common.packet.req;
import BsK.common.packet.Packet;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Data
@AllArgsConstructor
public class GetRecentPatientRequest implements Packet {
}
@@ -0,0 +1,13 @@
package BsK.common.packet.res;
import BsK.common.packet.Packet;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@AllArgsConstructor
@Data
public class GetRecentPatientResponse implements Packet {
String [][] patientData;
}
@@ -0,0 +1,43 @@
package BsK.common.util.text;
import java.text.Normalizer;
public final class TextUtils {
// Private constructor to prevent instantiation
private TextUtils() {
throw new UnsupportedOperationException("Utility class cannot be instantiated.");
}
/**
* Removes accents from a given string.
*
* @param text The input string.
* @return A string without accents.
*/
public static String removeAccents(String text) {
if (text == null) {
return null;
}
String normalizedText = Normalizer.normalize(text, Normalizer.Form.NFD)
.replaceAll("\\p{M}", "");
normalizedText = normalizedText.replaceAll("Đ", "D")
.replaceAll("đ", "d");
return normalizedText;
}
public static void main(String[] args) {
String[] testWords = {
"Đạt", "đạt", "Ân", "Ấn", "Lê", "Hồ", "Trần", "Hoàng",
"Quốc", "Phạm", "Dũng", "Bạch", "Hạnh", "Cường", "Vỹ"
};
for (String word : testWords) {
System.out.println("Original: " + word + " → Without Accents: " + TextUtils.removeAccents(word));
}
}
}
@@ -302,6 +302,46 @@ public class ServerHandler extends SimpleChannelInboundHandler<TextWebSocketFram
throw new RuntimeException(e);
}
}
if (packet instanceof GetRecentPatientRequest getRecentPatientRequest) {
log.debug("Received GetRecentPatientRequest");
try {
ResultSet rs = statement.executeQuery(
"SELECT customer_id, customer_last_name, customer_first_name, customer_dob, customer_number, " +
"customer_address, customer_address, customer_gender\n" +
"FROM Customer\n" +
"ORDER BY customer_id DESC\n" +
"LIMIT 20;");
if (!rs.isBeforeFirst()) {
System.out.println("No data found in the customer table.");
} else {
ArrayList<String> resultList = new ArrayList<>();
while (rs.next()) {
String customerId = rs.getString("customer_id");
String customerLastName = rs.getString("customer_last_name");
String customerFirstName = rs.getString("customer_first_name");
String customerDob = rs.getString("customer_dob");
String customerNumber = rs.getString("customer_number");
String customerAddress = rs.getString("customer_address");
String customerGender = rs.getString("customer_gender");
String result = String.join("|", customerId, customerLastName + customerFirstName,
customerDob, customerNumber, customerAddress, customerGender);
resultList.add(result);
}
String[] resultString = resultList.toArray(new String[0]);
String[][] resultArray = new String[resultString.length][];
for (int i = 0; i < resultString.length; i++) {
resultArray[i] = resultString[i].split("\\|");
}
UserUtil.sendPacket(user.getSessionId(), new GetRecentPatientResponse(resultArray));
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}