Advertisement
Gaudenz

AppointmentDialog.java

Mar 5th, 2025
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.07 KB | None | 0 0
  1. package lyfjshs.gomis.view.appointment;
  2.  
  3. import java.awt.Frame;
  4. import java.time.LocalDate;
  5. import java.time.LocalDateTime;
  6. import java.time.LocalTime;
  7.  
  8. import javax.swing.JButton;
  9. import javax.swing.JComboBox;
  10. import javax.swing.JDialog;
  11. import javax.swing.JFormattedTextField;
  12. import javax.swing.JLabel;
  13. import javax.swing.JOptionPane;
  14. import javax.swing.JTextField;
  15. import javax.swing.SwingUtilities;
  16.  
  17. import lyfjshs.gomis.Database.entity.Appointment;
  18. import net.miginfocom.swing.MigLayout;
  19. import raven.datetime.DatePicker;
  20. import raven.datetime.TimePicker;
  21.  
  22.  
  23. public class AppointmentDialog extends JDialog {
  24.     private JComboBox<String> participantTypeCombo;
  25.     private JTextField participantIdField;
  26.     private JTextField counselorIdField;
  27.     private JTextField titleField;
  28.     private JComboBox<String> appointmentTypeCombo;
  29.     private DatePicker datePicker;
  30.     private TimePicker timePicker;
  31.     private JTextField notesField;
  32.     private JComboBox<String> statusCombo;
  33.     private boolean confirmed;
  34.     private Appointment appointment;
  35.     private JButton studentSearchButton;
  36.  
  37.     /**
  38.      * @wbp.parser.constructor
  39.      */
  40.     public AppointmentDialog(Frame parent) {
  41.         this(parent, new Appointment());
  42.     }
  43.  
  44.     public AppointmentDialog(Frame parent, Appointment existingAppt) {
  45.         super(parent, existingAppt.getAppointmentId() == 0 ? "Create Appointment" : "Edit Appointment", true);
  46.         this.appointment = existingAppt;
  47.         initComponents();
  48.         if (existingAppt.getAppointmentId() != 0) {
  49.             populateFields();
  50.         }
  51.         setSize(450, 450);
  52.         setLocationRelativeTo(parent);
  53.     }
  54.  
  55.     private void initComponents() {
  56.         getContentPane().setLayout(new MigLayout("wrap 2", "[][grow]", "[]10[]"));
  57.  
  58.         // Participant Type Selection
  59.         participantTypeCombo = new JComboBox<>(new String[]{"Student", "Non-Student"});
  60.         participantTypeCombo.addActionListener(e -> {
  61.             boolean isStudent = participantTypeCombo.getSelectedItem().equals("Student");
  62.             participantIdField.setEnabled(isStudent);
  63.             studentSearchButton.setEnabled(isStudent);
  64.         });
  65.  
  66.         // Participant ID field
  67.         participantIdField = new JTextField(10);
  68.  
  69.         // Student Search Button
  70.         studentSearchButton = new JButton("Search Student");
  71.         studentSearchButton.setEnabled(false);
  72.         studentSearchButton.addActionListener(e -> {
  73.             SwingUtilities.invokeLater(() -> new StudentSearchUI().createAndShowGUI());
  74.         });
  75.  
  76.         // Counselor ID field
  77.         counselorIdField = new JTextField(10);
  78.  
  79.         // Title field
  80.         titleField = new JTextField(20);
  81.  
  82.         // Appointment Type field
  83.         appointmentTypeCombo = new JComboBox<>(new String[]{
  84.             "Academic Consultation",
  85.             "Career Guidance",
  86.             "Personal Consultation",
  87.             "Behavioral Consultation",
  88.             "Group Consultation"
  89.         });
  90.  
  91.         // Date Picker
  92.         datePicker = new DatePicker();
  93.         datePicker.now();
  94.         JFormattedTextField dateEditor = new JFormattedTextField();
  95.         datePicker.setEditor(dateEditor);
  96.  
  97.         // Time Picker
  98.         timePicker = new TimePicker();
  99.         timePicker.now();
  100.         JFormattedTextField timeEditor = new JFormattedTextField();
  101.         timePicker.setEditor(timeEditor);
  102.  
  103.         // Notes field
  104.         notesField = new JTextField(20);
  105.  
  106.         // Status field
  107.         statusCombo = new JComboBox<>(new String[]{"Scheduled", "Completed", "Cancelled"});
  108.  
  109.         // Add components
  110.         getContentPane().add(new JLabel("Participant Type:"));
  111.         getContentPane().add(participantTypeCombo, "growx");
  112.  
  113.         getContentPane().add(new JLabel("Participant ID:"));
  114.         getContentPane().add(participantIdField, "growx");
  115.  
  116.         getContentPane().add(new JLabel(""));
  117.         getContentPane().add(studentSearchButton, "growx");
  118.  
  119.         getContentPane().add(new JLabel("Counselor ID:"));
  120.         getContentPane().add(counselorIdField, "growx");
  121.  
  122.         getContentPane().add(new JLabel("Title:"));
  123.         getContentPane().add(titleField, "growx");
  124.  
  125.         getContentPane().add(new JLabel("Appointment Type:"));
  126.         getContentPane().add(appointmentTypeCombo, "growx");
  127.  
  128.         getContentPane().add(new JLabel("Date:"));
  129.         getContentPane().add(dateEditor, "growx");
  130.  
  131.         getContentPane().add(new JLabel("Time:"));
  132.         getContentPane().add(timeEditor, "growx");
  133.  
  134.         getContentPane().add(new JLabel("Notes:"));
  135.         getContentPane().add(notesField, "growx");
  136.  
  137.         getContentPane().add(new JLabel("Status:"));
  138.         getContentPane().add(statusCombo, "growx");
  139.  
  140.         JButton confirmButton = new JButton("Confirm");
  141.         confirmButton.addActionListener(e -> {
  142.             if (validateInputs()) {
  143.                 confirmed = true;
  144.                 updateAppointment();
  145.                 setVisible(false);
  146.             }
  147.         });
  148.  
  149.         JButton cancelButton = new JButton("Cancel");
  150.         cancelButton.addActionListener(e -> setVisible(false));
  151.  
  152.         getContentPane().add(confirmButton, "span 2, split 2, align center");
  153.         getContentPane().add(cancelButton);
  154.     }
  155.  
  156.     private void populateFields() {
  157.         participantTypeCombo.setSelectedItem(
  158.             appointment.getParticipantId() > 0 ? "Student" : "Non-Student");
  159.         participantIdField.setText(String.valueOf(appointment.getParticipantId()));
  160.         counselorIdField.setText(appointment.getGuidanceCounselorId() != null ?
  161.             String.valueOf(appointment.getGuidanceCounselorId()) : "");
  162.         titleField.setText(appointment.getAppointmentTitle());
  163.         appointmentTypeCombo.setSelectedItem(appointment.getAppointmentType());
  164.         datePicker.setSelectedDate(appointment.getAppointmentDateTime().toLocalDateTime().toLocalDate());
  165.         timePicker.setSelectedTime(appointment.getAppointmentDateTime().toLocalDateTime().toLocalTime());
  166.         notesField.setText(appointment.getAppointmentNotes());
  167.         statusCombo.setSelectedItem(appointment.getAppointmentStatus());
  168.     }
  169.  
  170.     private boolean validateInputs() {
  171.         // Validate participant ID
  172.         String participantIdText = participantIdField.getText().trim();
  173.         if (participantIdText.isEmpty()) {
  174.             JOptionPane.showMessageDialog(this, "Please enter Participant ID");
  175.             return false;
  176.         }
  177.         try {
  178.             Integer.parseInt(participantIdText);
  179.         } catch (NumberFormatException e) {
  180.             JOptionPane.showMessageDialog(this, "Participant ID must be a valid number");
  181.             return false;
  182.         }
  183.  
  184.         // Validate counselor ID if provided
  185.         String counselorIdText = counselorIdField.getText().trim();
  186.         if (!counselorIdText.isEmpty()) {
  187.             try {
  188.                 Integer.parseInt(counselorIdText);
  189.             } catch (NumberFormatException e) {
  190.                 JOptionPane.showMessageDialog(this, "Counselor ID must be a valid number");
  191.                 return false;
  192.             }
  193.         }
  194.  
  195.         // Validate title
  196.         if (titleField.getText().trim().isEmpty()) {
  197.             JOptionPane.showMessageDialog(this, "Please enter appointment title");
  198.             return false;
  199.         }
  200.  
  201.         // Validate date and time
  202.         if (!datePicker.isDateSelected() || !timePicker.isTimeSelected()) {
  203.             JOptionPane.showMessageDialog(this, "Please select both date and time");
  204.             return false;
  205.         }
  206.  
  207.         return true;
  208.     }
  209.  
  210.     private void updateAppointment() {
  211.         appointment.setParticipantId(Integer.parseInt(participantIdField.getText().trim()));
  212.        
  213.         String counselorIdText = counselorIdField.getText().trim();
  214.         appointment.setGuidanceCounselorId(counselorIdText.isEmpty() ? null : Integer.parseInt(counselorIdText));
  215.        
  216.         appointment.setAppointmentTitle(titleField.getText().trim());
  217.         appointment.setAppointmentType((String)appointmentTypeCombo.getSelectedItem());
  218.        
  219.         LocalDate date = datePicker.getSelectedDate();
  220.         LocalTime time = timePicker.getSelectedTime();
  221.         appointment.setAppointmentDateTime(java.sql.Timestamp.valueOf(LocalDateTime.of(date, time)));
  222.        
  223.         appointment.setAppointmentNotes(notesField.getText().trim());
  224.         appointment.setAppointmentStatus((String)statusCombo.getSelectedItem());
  225.     }
  226.  
  227.     public boolean isConfirmed() {
  228.         return confirmed;
  229.     }
  230.  
  231.     public Appointment getAppointment() {
  232.         return appointment;
  233.     }
  234.  
  235.     // Legacy methods for backward compatibility
  236.     public Integer getStudentUid() {
  237.         return participantTypeCombo.getSelectedItem().equals("Student") ?
  238.             Integer.parseInt(participantIdField.getText().trim()) : null;
  239.     }
  240.  
  241.     public String getAppointmentType() {
  242.         return (String)appointmentTypeCombo.getSelectedItem();
  243.     }
  244.  
  245.     public LocalDateTime getAppointmentDateTime() {
  246.         return LocalDateTime.of(datePicker.getSelectedDate(), timePicker.getSelectedTime());
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement