package ui; import dao.MarkingProperties; import dao.ResultDAO; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import model.Criterion; import model.Student; import report.FeedbackReportGenerator; import report.SendReport; /** * * @author mark */ public class MarkingFrame extends JFrame { private Student student; private String submissionKey; private final JPanel schedulePane; private final ResultDAO resultDao = new ResultDAO(); private final JLabel lblStudent; private final JLabel lblAssessment; private final SubmissionFrame parent; private final String assessment; private final JCheckBox cbExpandable; @SuppressWarnings("Convert2Lambda") public MarkingFrame(SubmissionFrame parent, String assessment) { this.parent = parent; this.assessment = assessment; // add scroll pane to the root JScrollPane scrollPane = new JScrollPane(); JPanel contentPane = new JPanel(); JPanel titlePane = new JPanel(); cbExpandable = new JCheckBox("Expand Text Areas?", false); scrollPane.setViewportView(contentPane); scrollPane.getVerticalScrollBar().setUnitIncrement(16); getContentPane().add(titlePane, java.awt.BorderLayout.NORTH); getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER); titlePane.setLayout(new BorderLayout()); lblStudent = new JLabel("Student"); Font newFont = lblStudent.getFont().deriveFont(Font.BOLD, 15); lblStudent.setFont(newFont); lblAssessment = new JLabel("Assessment"); lblAssessment.setFont(newFont); titlePane.add(BorderLayout.WEST, lblStudent); titlePane.add(BorderLayout.EAST, lblAssessment); // add other panes schedulePane = new JPanel(); JPanel buttonPane = new JPanel(); contentPane.setLayout(new BorderLayout()); contentPane.add(schedulePane, BorderLayout.CENTER); contentPane.add(buttonPane, BorderLayout.SOUTH); schedulePane.setLayout(new BoxLayout(schedulePane, BoxLayout.Y_AXIS)); // add buttons JButton btnSave = new JButton("Save"); btnSave.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { save(); } }); // add buttons JButton btnCancel = new JButton("Cancel"); btnCancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { dispose(); } }); JButton btnMin = new JButton("Min"); btnMin.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { min(); } }); JButton btnMax = new JButton("Max"); btnMax.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { max(); } }); JButton btnEmail = new JButton("Save & Email"); btnEmail.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { email(); } }); buttonPane.add(btnMin); buttonPane.add(btnMax); buttonPane.add(btnCancel); if (MarkingProperties.isEmailEnabled()) { buttonPane.add(btnEmail); } buttonPane.add(btnSave); buttonPane.add(cbExpandable); } public void addCategory(String category, List<Criterion> criteria) { schedulePane.add(new CategoryPanel(category, criteria)); } private void save() { if (!resultDao.isComplete()) { int res = JOptionPane.showConfirmDialog(MarkingFrame.this, "You haven't entered values for all criteria. Are you sure you want save?", "Incomplete", JOptionPane.OK_CANCEL_OPTION); if (JOptionPane.OK_OPTION == res) { resultDao.save(student.getId()); parent.refresh(); dispose(); } } else { resultDao.save(this.submissionKey); parent.refresh(); String mark = resultDao.getResult(student.getId()).iterator().next().get("MARK").toString(); JOptionPane.showMessageDialog(this, "Mark is " + mark); dispose(); } } private void min() { Component[] components = schedulePane.getComponents(); for (Component component : components) { if (component instanceof CategoryPanel) { CategoryPanel cp = (CategoryPanel) component; Component[] categories = cp.getComponents(); for (Component criterion : categories) { if (criterion instanceof CriterionPanel) { CriterionPanel c = (CriterionPanel) criterion; c.setZero(); } } } } } private void max() { Component[] components = schedulePane.getComponents(); for (Component component : components) { if (component instanceof CategoryPanel) { CategoryPanel cp = (CategoryPanel) component; Component[] categories = cp.getComponents(); for (Component criterion : categories) { if (criterion instanceof CriterionPanel) { CriterionPanel c = (CriterionPanel) criterion; if (!c.getCriterion().isIsSpecial()) { c.setMax(); } else { c.setZero(); } } } } } } public void load(String id) { this.submissionKey = id; student = new ResultDAO().findStudent(id); lblStudent.setText(student.getFirstName() + " " + student.getLastName() + " (" + student.getId() + ")"); lblAssessment.setText(assessment); resultDao.load(this.submissionKey); } public void display() { this.setPreferredSize(new Dimension(600, 980)); this.pack(); this.setVisible(true); } public void createReport() { try { new FeedbackReportGenerator(new ResultDAO().getResult(student.getId()), MarkingProperties.reportPath(), MarkingProperties.attachmentFilePrefix(), MarkingProperties.reportTitle(), MarkingProperties.reportFooter()).generate(student); } catch (Exception ex) { throw new RuntimeException(ex); } } public void emailReport() { String report = MarkingProperties.reportPath() + "/" + MarkingProperties.attachmentFilePrefix() + "-" + student.getId() + ".pdf"; new SendReport().sendReport(student, report); } @SuppressWarnings("Convert2Lambda") private void email() { save(); new Thread(new Runnable() { @Override public void run() { createReport(); emailReport(); } }).start(); } public boolean getExpandable() { return this.cbExpandable.isSelected(); } }