mirror of
https://github.com/tiennm99/BSK-All-in-One-Clinic-Management-System.git
synced 2026-10-14 06:17:18 +00:00
[add] medicine list request
This commit is contained in:
1 parent
5dffb67160
commit
00f054e817
6 files changed
+267
-105
No files matched your search
@@ -36,7 +36,6 @@ public class CheckUpPage extends JPanel {
|
||||
|
||||
private String[][] queue;
|
||||
private String[][] history;
|
||||
private String[][] medicine;
|
||||
private DefaultTableModel model, historyModel;
|
||||
private JTable table1, historyTable;
|
||||
private final ResponseListener<GetDoctorGeneralInfoResponse> doctorGeneralInfoListener = this::handleGetDoctorGeneralInfoResponse;
|
||||
@@ -336,6 +335,11 @@ public class CheckUpPage extends JPanel {
|
||||
break;
|
||||
}
|
||||
case "medicine":
|
||||
// If there is no user selected, show a warning message
|
||||
if (checkupIdField.getText().isEmpty()) {
|
||||
JOptionPane.showMessageDialog(null, "Please select a patient from the queue");
|
||||
return;
|
||||
}
|
||||
// Open medicine dialog
|
||||
MedicineDialog dialog = new MedicineDialog(mainFrame);
|
||||
dialog.setVisible(true);
|
||||
|
||||
+199
-102
@@ -1,103 +1,200 @@
|
||||
package BsK.client.ui.component.CheckUpPage.MedicineWindow;//package BsK.client.ui.component.CheckUpPage.MedicineWindow;
|
||||
//
|
||||
//import javax.swing.*;
|
||||
//import javax.swing.table.DefaultTableModel;
|
||||
//import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
|
||||
//import java.awt.*;
|
||||
//import java.util.List;
|
||||
//
|
||||
//public class MedicineDialog extends JDialog {
|
||||
// private JComboBox<String> medicineNameComboBox;
|
||||
// private JTextField dosageField;
|
||||
// private JTextField frequencyField;
|
||||
// private JTextField quantityField;
|
||||
// private JTextField priceField;
|
||||
// private DefaultTableModel tableModel;
|
||||
//
|
||||
// // Example list of medicine names for autocomplete
|
||||
// private List<String> medicineList = List.of(
|
||||
// "Paracetamol|20|4", "Ibuprofen|20|4", "Aspirin|400|2", "Amoxicillin|32|4",
|
||||
// "Ciprofloxacin|323|4", "Doxycycline|43|43", "Metformin|332|23", "Atorvastatin|232|32"
|
||||
// );
|
||||
//
|
||||
// public MedicineDialog(Frame parent) {
|
||||
// super(parent, "Enter Medicine Details", true);
|
||||
// setLayout(new BorderLayout());
|
||||
//
|
||||
// // Create a panel to hold the input fields
|
||||
// JPanel inputPanel = new JPanel(new GridLayout(0, 2, 5, 5));
|
||||
// inputPanel.add(new JLabel("Medicine Name:"));
|
||||
// medicineNameComboBox = new JComboBox<>(medicineList.toArray(new String[0]));
|
||||
// medicineNameComboBox.setEditable(true);
|
||||
// AutoCompleteDecorator.decorate(medicineNameComboBox);
|
||||
// inputPanel.add(medicineNameComboBox);
|
||||
//
|
||||
// inputPanel.add(new JLabel("Dosage:"));
|
||||
// dosageField = new JTextField();
|
||||
// inputPanel.add(dosageField);
|
||||
// inputPanel.add(new JLabel("Frequency:"));
|
||||
// frequencyField = new JTextField();
|
||||
// inputPanel.add(frequencyField);
|
||||
// inputPanel.add(new JLabel("Quantity:"));
|
||||
// quantityField = new JTextField();
|
||||
// inputPanel.add(quantityField);
|
||||
// inputPanel.add(new JLabel("Price:"));
|
||||
// priceField = new JTextField();
|
||||
// inputPanel.add(priceField);
|
||||
//
|
||||
// // Create a table to display selected medicines
|
||||
// String[] columnNames = {"Medicine Name", "Dosage", "Frequency", "Quantity", "Price"};
|
||||
// tableModel = new DefaultTableModel(columnNames, 0);
|
||||
// JTable table = new JTable(tableModel);
|
||||
// JScrollPane tableScrollPane = new JScrollPane(table);
|
||||
//
|
||||
// // Create a panel for add and remove buttons
|
||||
// JPanel buttonPanel = new JPanel();
|
||||
// JButton addButton = new JButton("Add");
|
||||
// JButton removeButton = new JButton("Remove");
|
||||
// buttonPanel.add(addButton);
|
||||
// buttonPanel.add(removeButton);
|
||||
//
|
||||
// // Add action listeners for buttons
|
||||
// addButton.addActionListener(e -> {
|
||||
// String medicineName = (String) medicineNameComboBox.getSelectedItem();
|
||||
// String dosage = dosageField.getText();
|
||||
// String frequency = frequencyField.getText();
|
||||
// String quantity = quantityField.getText();
|
||||
// String price = priceField.getText();
|
||||
// tableModel.addRow(new Object[]{medicineName, dosage, frequency, quantity, price});
|
||||
// });
|
||||
//
|
||||
// removeButton.addActionListener(e -> {
|
||||
// int selectedRow = table.getSelectedRow();
|
||||
// if (selectedRow != -1) {
|
||||
// tableModel.removeRow(selectedRow);
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// // Add components to the dialog
|
||||
// add(inputPanel, BorderLayout.NORTH);
|
||||
// add(tableScrollPane, BorderLayout.CENTER);
|
||||
// add(buttonPanel, BorderLayout.SOUTH);
|
||||
//
|
||||
// // Set dialog properties
|
||||
// pack();
|
||||
// setLocationRelativeTo(parent);
|
||||
// }
|
||||
//
|
||||
// public static void main(String[] args) {
|
||||
// JFrame frame = new JFrame();
|
||||
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
// frame.setSize(400, 300);
|
||||
// frame.setVisible(true);
|
||||
//
|
||||
// JButton openDialogButton = new JButton("Open Medicine Dialog");
|
||||
// openDialogButton.addActionListener(e -> {
|
||||
// MedicineDialog dialog = new MedicineDialog(frame);
|
||||
// dialog.setVisible(true);
|
||||
// });
|
||||
//
|
||||
// frame.add(openDialogButton, BorderLayout.CENTER);
|
||||
// }
|
||||
//}
|
||||
package BsK.client.ui.component.CheckUpPage.MedicineWindow;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.DocumentEvent;
|
||||
import javax.swing.event.DocumentListener;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import javax.swing.table.TableRowSorter;
|
||||
|
||||
import BsK.client.network.handler.ClientHandler;
|
||||
import BsK.client.network.handler.ResponseListener;
|
||||
import BsK.common.packet.req.GetDoctorGeneralInfo;
|
||||
import BsK.common.packet.req.GetMedInfoRequest;
|
||||
import BsK.common.packet.res.GetMedInfoResponse;
|
||||
import BsK.common.util.network.NetworkUtil;
|
||||
import BsK.server.network.util.UserUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class MedicineDialog extends JDialog {
|
||||
private JTextField medicineNameField;
|
||||
private JTextField dosageField;
|
||||
private JTextField frequencyField;
|
||||
private JTextField quantityField;
|
||||
private JTextField priceField;
|
||||
private DefaultTableModel tableModel;
|
||||
private String[] medcineColumns = {"Medicine Name", "Medicine Company", "Description"};
|
||||
private String[][] medicineData;
|
||||
private final ResponseListener<GetMedInfoResponse> getMedInfoResponseListener = this::getMedInfoHandler;
|
||||
|
||||
void getMedInfoHandler(GetMedInfoResponse response) {
|
||||
log.info("Received medicine data");
|
||||
medicineData = response.getMedInfo();
|
||||
tableModel.setDataVector(medicineData, medcineColumns);
|
||||
}
|
||||
|
||||
void sendGetMedInfoRequest() {
|
||||
log.info("Sending GetMedInfoRequest");
|
||||
NetworkUtil.sendPacket(ClientHandler.ctx.channel(), new GetMedInfoRequest());
|
||||
}
|
||||
|
||||
public MedicineDialog(Frame parent) {
|
||||
super(parent, "Enter Medicine Details", true);
|
||||
|
||||
// Set the size of the dialog
|
||||
setSize(800, 300);
|
||||
|
||||
ClientHandler.addResponseListener(GetMedInfoResponse.class, getMedInfoResponseListener);
|
||||
sendGetMedInfoRequest();
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
|
||||
JPanel inputPanel = new JPanel();
|
||||
inputPanel.setLayout(new GridBagLayout());
|
||||
GridBagConstraints gbc = new GridBagConstraints();
|
||||
|
||||
gbc.insets = new Insets(5, 5, 5, 5); // Padding between components
|
||||
gbc.fill = GridBagConstraints.HORIZONTAL; // Stretch horizontally only
|
||||
gbc.gridx = 0;
|
||||
gbc.gridy = 0;
|
||||
|
||||
inputPanel.add(new JLabel("Medicine Name:"), gbc);
|
||||
gbc.gridx = 1;
|
||||
medicineNameField = new JTextField(15);
|
||||
inputPanel.add(medicineNameField, gbc);
|
||||
|
||||
// Listen for changes in the text event
|
||||
tableModel = new DefaultTableModel(medicineData, medcineColumns);
|
||||
JTable medicineTable = new JTable(tableModel);
|
||||
TableRowSorter<DefaultTableModel> rowSorter = new TableRowSorter<>(tableModel);
|
||||
medicineTable.setRowSorter(rowSorter);
|
||||
medicineTable.setPreferredSize(new Dimension(400, 100));
|
||||
JScrollPane scrollPane = new JScrollPane(medicineTable);
|
||||
|
||||
medicineNameField.getDocument().addDocumentListener(new DocumentListener() {
|
||||
@Override
|
||||
public void insertUpdate(DocumentEvent e) {
|
||||
filterRows();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeUpdate(DocumentEvent e) {
|
||||
filterRows();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changedUpdate(DocumentEvent e) {
|
||||
filterRows();
|
||||
}
|
||||
|
||||
private void filterRows() {
|
||||
String searchText = medicineNameField.getText().trim();
|
||||
if (searchText.isEmpty()) {
|
||||
rowSorter.setRowFilter(null);
|
||||
} else {
|
||||
rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + searchText, 0));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
gbc.gridx = 0;
|
||||
gbc.gridy++;
|
||||
inputPanel.add(new JLabel("Dosage:"), gbc);
|
||||
gbc.gridx = 1;
|
||||
dosageField = new JTextField(15);
|
||||
inputPanel.add(dosageField, gbc);
|
||||
|
||||
gbc.gridx = 0;
|
||||
gbc.gridy++;
|
||||
inputPanel.add(new JLabel("Frequency:"), gbc);
|
||||
gbc.gridx = 1;
|
||||
frequencyField = new JTextField(15);
|
||||
inputPanel.add(frequencyField, gbc);
|
||||
|
||||
gbc.gridx = 0;
|
||||
gbc.gridy++;
|
||||
inputPanel.add(new JLabel("Quantity:"), gbc);
|
||||
gbc.gridx = 1;
|
||||
quantityField = new JTextField(15);
|
||||
inputPanel.add(quantityField, gbc);
|
||||
|
||||
gbc.gridx = 0;
|
||||
gbc.gridy++;
|
||||
inputPanel.add(new JLabel("Price:"), gbc);
|
||||
gbc.gridx = 1;
|
||||
priceField = new JTextField(15);
|
||||
inputPanel.add(priceField, gbc);
|
||||
|
||||
|
||||
scrollPane.setPreferredSize(new Dimension(400, 200)); // 200px height
|
||||
|
||||
// Set the split pane
|
||||
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, inputPanel, scrollPane);
|
||||
splitPane.setResizeWeight(0.45); // Allocate more space to the inputPanel
|
||||
// Wrap the splitPane in a JPanel to control height
|
||||
JPanel topPanel = new JPanel(new BorderLayout());
|
||||
topPanel.add(splitPane, BorderLayout.CENTER);
|
||||
topPanel.setPreferredSize(new Dimension(600, 250));
|
||||
|
||||
|
||||
// Create a table to display selected medicines
|
||||
String[] columnNames = {"Medicine Name", "Dosage", "Frequency", "Quantity", "Price"};
|
||||
DefaultTableModel selectedTableModel = new DefaultTableModel(columnNames, 0);
|
||||
JTable selectedTable = new JTable(selectedTableModel);
|
||||
JScrollPane tableScrollPane = new JScrollPane(selectedTable);
|
||||
|
||||
// Create a panel for add and remove buttons
|
||||
JPanel buttonPanel = new JPanel();
|
||||
JButton addButton = new JButton("Add");
|
||||
JButton removeButton = new JButton("Remove");
|
||||
buttonPanel.add(addButton);
|
||||
buttonPanel.add(removeButton);
|
||||
|
||||
addButton.addActionListener(e -> {
|
||||
int selectedRow = medicineTable.getSelectedRow();
|
||||
if (selectedRow != -1) {
|
||||
int modelRow = medicineTable.convertRowIndexToModel(selectedRow);
|
||||
Object[] rowData = new Object[tableModel.getColumnCount()];
|
||||
for (int i = 0; i < tableModel.getColumnCount(); i++) {
|
||||
rowData[i] = tableModel.getValueAt(modelRow, i);
|
||||
}
|
||||
selectedTableModel.addRow(rowData);
|
||||
}
|
||||
});
|
||||
|
||||
removeButton.addActionListener(e -> {
|
||||
int selectedRow = selectedTable.getSelectedRow();
|
||||
if (selectedRow != -1) {
|
||||
selectedTableModel.removeRow(selectedRow);
|
||||
}
|
||||
});
|
||||
|
||||
// Add components to the dialog
|
||||
add(topPanel, BorderLayout.NORTH);
|
||||
add(tableScrollPane, BorderLayout.CENTER);
|
||||
add(buttonPanel, BorderLayout.SOUTH);
|
||||
|
||||
// Set dialog properties
|
||||
pack();
|
||||
setLocationRelativeTo(parent);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
JFrame frame = new JFrame();
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setSize(400, 300);
|
||||
frame.setVisible(true);
|
||||
|
||||
JButton openDialogButton = new JButton("Open Medicine Dialog");
|
||||
openDialogButton.addActionListener(e -> {
|
||||
MedicineDialog dialog = new MedicineDialog(frame);
|
||||
dialog.setVisible(true);
|
||||
});
|
||||
|
||||
frame.add(openDialogButton, BorderLayout.CENTER);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class GetMedInfoRequest 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
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class GetMedInfoResponse implements Packet {
|
||||
private String[][] medInfo;
|
||||
}
|
||||
@@ -105,7 +105,7 @@ public class ServerHandler extends SimpleChannelInboundHandler<TextWebSocketFram
|
||||
|
||||
resultList.add(result);
|
||||
|
||||
log.info(result);
|
||||
// log.info(result);
|
||||
}
|
||||
|
||||
String[] resultString = resultList.toArray(new String[0]);
|
||||
@@ -180,7 +180,7 @@ public class ServerHandler extends SimpleChannelInboundHandler<TextWebSocketFram
|
||||
String notes = rs.getString("notes");
|
||||
String result = String.join("|", checkupDate, checkupId, symptoms, diagnosis, prescriptionId, notes);
|
||||
resultList.add(result);
|
||||
log.info(result);
|
||||
// log.info(result);
|
||||
}
|
||||
|
||||
String[] resultString = resultList.toArray(new String[0]);
|
||||
@@ -196,6 +196,42 @@ public class ServerHandler extends SimpleChannelInboundHandler<TextWebSocketFram
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
if (packet instanceof GetMedInfoRequest getMedInfoRequest) {
|
||||
log.debug("Received GetMedInfoRequest");
|
||||
try {
|
||||
ResultSet rs = statement.executeQuery(
|
||||
"SELECT med_name, med_company, med_description\n" +
|
||||
"FROM Medicine\n" +
|
||||
"GROUP BY med_name, med_company, med_description"
|
||||
);
|
||||
|
||||
if (!rs.isBeforeFirst()) {
|
||||
System.out.println("No data found in the medicine table.");
|
||||
} else {
|
||||
ArrayList<String> resultList = new ArrayList<>();
|
||||
while (rs.next()) {
|
||||
String medName = rs.getString("med_name");
|
||||
String medCompany = rs.getString("med_company");
|
||||
String medDescription = rs.getString("med_description");
|
||||
|
||||
String result = String.join("|", medName, medCompany, medDescription);
|
||||
resultList.add(result);
|
||||
// log.info(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 GetMedInfoResponse(resultArray));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
Reference in new issue
Block a user