Advertisement
Gaudenz

SessionForm.java

Mar 6th, 2025
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.96 KB | None | 0 0
  1. package lyfjshs.gomis.view.sessions;
  2.  
  3. import java.awt.Color;
  4. import java.awt.FlowLayout;
  5. import java.awt.Font;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.awt.print.PageFormat;
  9. import java.awt.print.Printable;
  10. import java.awt.print.PrinterException;
  11. import java.awt.print.PrinterJob;
  12. import java.sql.Connection;
  13.  
  14. import javax.swing.BorderFactory;
  15. import javax.swing.JButton;
  16. import javax.swing.JComboBox;
  17. import javax.swing.JFormattedTextField;
  18. import javax.swing.JLabel;
  19. import javax.swing.JOptionPane;
  20. import javax.swing.JPanel;
  21. import javax.swing.JScrollPane;
  22. import javax.swing.JTextArea;
  23. import javax.swing.JTextField;
  24.  
  25. import lyfjshs.gomis.Database.DAO.SessionsDAO;
  26. import lyfjshs.gomis.Database.entity.Sessions;
  27. import lyfjshs.gomis.components.FormManager.Form;
  28. import lyfjshs.gomis.view.appointment.StudentSearchUI;
  29. import net.miginfocom.swing.MigLayout;
  30. import javax.swing.JSeparator;
  31. import javax.swing.SwingConstants;
  32. import java.awt.Rectangle;
  33. import java.awt.Dimension;
  34.  
  35. public class SessionsForm extends Form implements Printable {
  36.     private JTextField dateField, violationField, recordedByField;
  37.     private JFormattedTextField startSessionTimeField, endSessionTimeField;
  38.     private JTextArea sessionSummaryArea, notesArea;
  39.     private JButton saveButton, printButton, searchStudentButton;
  40.     private JComboBox<String> participantsComboBox;
  41.     private JComboBox<String> consultationTypeComboBox;
  42.     private JComboBox<String> appointmentTypeComboBox;
  43.     private Connection connect;
  44.     private JPanel mainPanel;
  45.     private JTextField firstNameField, lastNameField, contactNumberField, emailField;
  46.     private JPanel contentPanel;
  47.     private JPanel panel;
  48.     private JSeparator separator;
  49.     private JPanel panel_1;
  50.  
  51.     public SessionsForm(Connection conn) {
  52.         this.connect = conn;
  53.         initializeComponents();
  54.         layoutComponents();
  55.     }
  56.  
  57.     private void initializeComponents() {
  58.     }
  59.  
  60.     private void toggleSearchStudentButton() {
  61.         searchStudentButton.setEnabled("Student".equals(participantsComboBox.getSelectedItem()));
  62.     }
  63.  
  64.     private void openStudentSearchUI() {
  65.         StudentSearchUI studentSearchUI = new StudentSearchUI();
  66.         studentSearchUI.createAndShowGUI();
  67.     }
  68.  
  69.     private int getAppointmentId() {
  70.         // Implement logic to retrieve the actual appointment ID
  71.         // This is a placeholder implementation and should be replaced with actual logic
  72.         // For example, you might query the database or get the ID from a selected appointment
  73.         return 1; // Replace with actual appointment ID retrieval logic
  74.     }
  75.  
  76.     private void saveSession() {
  77.         try {
  78.             String date = dateField.getText();
  79.             String participants = (String) participantsComboBox.getSelectedItem();
  80.             String violation = violationField.getText();
  81.             String recordedBy = recordedByField.getText();
  82.             String notes = notesArea.getText();
  83.             String summary = sessionSummaryArea.getText();
  84.             String appointmentType = (String) appointmentTypeComboBox.getSelectedItem();
  85.  
  86.             int appointmentId = 0; // Default value for Walk-in
  87.             if (!"Walk-in".equals(appointmentType)) {
  88.                 // Retrieve the actual appointment ID if not Walk-in
  89.                 appointmentId = getAppointmentId();
  90.             }
  91.  
  92.             // Create a new Session object using the constructor
  93.             Sessions session = new Sessions(0, appointmentId, // appointmentId
  94.                     0, // counselorsId (not retrieved in the query)
  95.                     participants.equals("Student") ? 1 : 0, // Use 1 for Student, 0 for Non-Student
  96.                     0, // violationId (not retrieved in the query)
  97.                     violation, // sessionType
  98.                     null, // sessionDateTime (not retrieved in the query)
  99.                     notes, // sessionNotes
  100.                     "Active", // sessionStatus
  101.                     new java.sql.Timestamp(System.currentTimeMillis()) // updatedAt
  102.             );
  103.  
  104.             // Use SessionsDAO to save the session
  105.             SessionsDAO sessionsDAO = new SessionsDAO(connect);
  106.             sessionsDAO.addSession(session); // Assuming you have an addSession method in SessionsDAO
  107.  
  108.             JOptionPane.showMessageDialog(this, "Session saved successfully!", "Success",
  109.                     JOptionPane.INFORMATION_MESSAGE);
  110.         } catch (Exception e) {
  111.             JOptionPane.showMessageDialog(this, "Error saving session: " + e.getMessage(), "Error",
  112.                     JOptionPane.ERROR_MESSAGE);
  113.             e.printStackTrace();
  114.         }
  115.     }
  116.  
  117.     private void layoutComponents() {
  118.         this.setLayout(new MigLayout("gap 10", "[grow]", "[grow]"));
  119.         contentPanel = new JPanel(new MigLayout("", "[grow]", "[][grow]"));
  120.         add(contentPanel, "cell 0 0,grow");
  121.         participantsComboBox = new JComboBox<>(new String[] { "Student", "Non-Student" });
  122.         participantsComboBox.addActionListener(e -> toggleSearchStudentButton());
  123.         sessionSummaryArea = new JTextArea(4, 20);
  124.  
  125.         saveButton = new JButton("SAVE");
  126.         saveButton.setBackground(new Color(70, 130, 180));
  127.         saveButton.setForeground(Color.WHITE);
  128.         saveButton.setFocusPainted(false);
  129.         saveButton.addActionListener(e -> saveSession());
  130.  
  131.         searchStudentButton = new JButton("Search Student");
  132.         searchStudentButton.setEnabled(false);
  133.         searchStudentButton.addActionListener(e -> openStudentSearchUI());
  134.  
  135.         // Header
  136.         JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
  137.         contentPanel.add(headerPanel, "cell 0 0,growx");
  138.         JLabel headerLabel = new JLabel("Session Documentation Form");
  139.         headerLabel.setFont(new Font("SansSerif", Font.BOLD, 20));
  140.         headerPanel.add(headerLabel);
  141.         headerPanel.setBackground(new Color(5, 117, 230));
  142.         headerPanel.setForeground(Color.WHITE);
  143.  
  144.         consultationTypeComboBox = new JComboBox<>(new String[] { "Academic Consultation", "Career Guidance",
  145.                 "Personal Consultation", "Behavioral Consultation", "Group Consultation" });
  146.  
  147.         appointmentTypeComboBox = new JComboBox<>(new String[] { "Walk-in", "From Appointment" });
  148.  
  149.         firstNameField = new JTextField(10);
  150.         lastNameField = new JTextField(10);
  151.         contactNumberField = new JTextField(10);
  152.         emailField = new JTextField(10);
  153.  
  154.         // Main Panel
  155.         mainPanel = new JPanel(new MigLayout("wrap, gap 10, hidemode 3", "[][grow][][10][][]", "[][][][][][][][grow][][]"));
  156.         contentPanel.add(mainPanel, "cell 0 1,grow");
  157.         mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  158.  
  159.         // Participant
  160.         JLabel participantLabel = new JLabel("Participant");
  161.         mainPanel.add(participantLabel, "flowx,cell 1 0,alignx right");
  162.         mainPanel.add(participantsComboBox, "cell 1 0,growx");
  163.         mainPanel.add(searchStudentButton, "cell 2 0");
  164.        
  165.         separator = new JSeparator();
  166.         separator.setSize(new Dimension(10, 10));
  167.         separator.setBounds(new Rectangle(0, 0, 5, 5));
  168.         separator.setBackground(Color.GRAY);
  169.         separator.setForeground(Color.DARK_GRAY);
  170.         separator.setOrientation(SwingConstants.VERTICAL);
  171.         mainPanel.add(separator, "cell 3 0 1 6");
  172.  
  173.         // Participant Panel
  174.         JPanel participantPanel = new JPanel(new MigLayout("gap 10", "[][grow][][][]", "[][][][]"));
  175.         participantPanel.setBorder(BorderFactory.createTitledBorder("Non-Student Participant"));
  176.  
  177.         participantPanel.add(new JLabel("First Name"), "cell 0 0");
  178.         participantPanel.add(firstNameField, "cell 1 0,growx");
  179.  
  180.         participantPanel.add(new JLabel("Last Name"), "cell 3 0");
  181.         participantPanel.add(lastNameField, "cell 4 0");
  182.  
  183.         participantPanel.add(new JLabel("Contact Number"), "cell 0 1");
  184.         participantPanel.add(contactNumberField, "cell 1 1 3 1,growx");
  185.  
  186.         participantPanel.add(new JLabel("Email:"), "cell 0 2");
  187.         participantPanel.add(emailField, "cell 1 2 3 1,growx");
  188.  
  189.         JButton saveParticipantButton = new JButton("Save Participant");
  190.         participantPanel.add(saveParticipantButton, "cell 1 3 4 1,alignx center");
  191.  
  192.         mainPanel.add(participantPanel, "cell 1 1 2 4,growx"); // Initially visible
  193.  
  194.         // Violation
  195.         JLabel violationLabel = new JLabel("Violation");
  196.         mainPanel.add(violationLabel, "flowx,cell 4 1");
  197.         violationField = new JTextField(10);
  198.         mainPanel.add(violationField, "cell 4 1");
  199.  
  200.         // Appointment Type
  201.         JLabel appointmentTypeLabel = new JLabel("Appointment Type");
  202.         mainPanel.add(appointmentTypeLabel, "flowx,cell 4 2,alignx left");
  203.         mainPanel.add(appointmentTypeComboBox, "cell 4 2,growx");
  204.  
  205.         // Start Time
  206.         JLabel startTimeLabel = new JLabel("Start Session Time");
  207.         mainPanel.add(startTimeLabel, "flowx,cell 4 3");
  208.         startSessionTimeField = new JFormattedTextField();
  209.         startSessionTimeField.setColumns(10);
  210.         mainPanel.add(startSessionTimeField, "cell 4 3,growx");
  211.  
  212.         // End Time
  213.         JLabel endTimeLabel = new JLabel("End Session Time");
  214.         mainPanel.add(endTimeLabel, "flowx,cell 5 3");
  215.         endSessionTimeField = new JFormattedTextField();
  216.         endSessionTimeField.setColumns(10);
  217.         mainPanel.add(endSessionTimeField, "cell 5 3,growx");
  218.  
  219.         // Consultation Type
  220.         JLabel consultationTypeLabel = new JLabel("Consultation Type");
  221.         mainPanel.add(consultationTypeLabel, "flowx,cell 4 4,aligny top");
  222.         mainPanel.add(consultationTypeComboBox, "cell 4 4,growx,aligny top");
  223.                
  224.                         // Date
  225.                         JLabel dateLabel = new JLabel("Date");
  226.                         mainPanel.add(dateLabel, "flowx,cell 5 4,alignx left,aligny top");
  227.        
  228.         panel_1 = new JPanel();
  229.         mainPanel.add(panel_1, "cell 1 5 2 1,grow");
  230.        
  231.         panel = new JPanel();
  232.         mainPanel.add(panel, "cell 4 5 2 1,grow");
  233.         panel.setLayout(new MigLayout("", "[][grow]", "[]"));
  234.        
  235.                 // Notes
  236.                 JLabel notesLabel = new JLabel("Notes");
  237.                 panel.add(notesLabel, "cell 0 0");
  238.                 notesArea = new JTextArea(4, 20);
  239.                 JScrollPane notesScrollPane = new JScrollPane(notesArea);
  240.                 panel.add(notesScrollPane, "cell 1 0,growx");
  241.  
  242.         // Session Summary
  243.         JLabel summaryLabel = new JLabel("Session Summary");
  244.         mainPanel.add(summaryLabel, "cell 1 6 5 1,alignx center,aligny bottom");
  245.         JScrollPane summaryScrollPane = new JScrollPane(sessionSummaryArea);
  246.         mainPanel.add(summaryScrollPane, "cell 1 7 5 1,grow");
  247.        
  248.                 printButton = new JButton("PRINT");
  249.                 printButton.setBackground(new Color(70, 130, 180));
  250.                 printButton.setForeground(Color.WHITE);
  251.                 printButton.setFocusPainted(false);
  252.                 printButton.addActionListener(e -> printSessionDetails());
  253.                        
  254.                                 // Recorded By
  255.                                 JLabel recordedByLabel = new JLabel("Recorded By");
  256.                                 mainPanel.add(recordedByLabel, "flowx,cell 4 8");
  257.                
  258.                         // Buttons
  259.                         mainPanel.add(printButton, "flowx,cell 5 8,growx");
  260.         mainPanel.add(saveButton, "cell 5 8,growx");
  261.         dateField = new JTextField(10);
  262.         mainPanel.add(dateField, "cell 5 4,alignx right,aligny top");
  263.         recordedByField = new JTextField(10);
  264.         mainPanel.add(recordedByField, "cell 4 8 2 1,growx");
  265.     }
  266.  
  267.     private void printSessionDetails() {
  268.         PrinterJob printerJob = PrinterJob.getPrinterJob();
  269.         printerJob.setPrintable(this);
  270.         if (printerJob.printDialog()) {
  271.             try {
  272.                 printerJob.print();
  273.             } catch (PrinterException e) {
  274.                 JOptionPane.showMessageDialog(this, "Printing Error: " + e.getMessage(), "Error",
  275.                         JOptionPane.ERROR_MESSAGE);
  276.             }
  277.         }
  278.     }
  279.  
  280.     @Override
  281.     public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
  282.         if (pageIndex > 0) {
  283.             return NO_SUCH_PAGE;
  284.         }
  285.         Graphics2D g2d = (Graphics2D) g;
  286.         g2d.translate(pf.getImageableX(), pf.getImageableY());
  287.         this.printAll(g);
  288.         return PAGE_EXISTS;
  289.     }
  290.  
  291. }
  292.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement