Newer
Older
marking / src / ui / MarkingFrame.java
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 String id;
	private Student student;

	private final JPanel schedulePane;
	private final ResultDAO result = new ResultDAO();

	private final JLabel lblStudent;
	private final JLabel lblAssessment;
	private final SubmissionFrame parent;
	private final String assessment;
	private final JCheckBox cbExpandable;

	public MarkingFrame(SubmissionFrame parent, String assessment) {
		this.parent = parent;
//		super(parent);
//		setModal(true);

		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 (!result.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) {
				result.save(id);
				parent.refresh();
				dispose();
			}
		} else {
			result.save(id);
			parent.refresh();
			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.id = id;

		String idType = MarkingProperties.idType();
		student = idType.equals("id") ? new ResultDAO().findStudentById(id) : new ResultDAO().findStudentByUserCode(id);

		lblStudent.setText(student.getName() + " (" + student.getId() + ")");
		lblAssessment.setText(assessment);
		result.load(id);
	}

	public void display() {
		this.setPreferredSize(new Dimension(600, 980));
		this.pack();
		this.setVisible(true);
	}

	public void createReport() {
		try {
			new FeedbackReportGenerator(new ResultDAO().getResult(id), MarkingProperties.reportPath(), MarkingProperties.attachmentFilePrefix(), MarkingProperties.reportTitle(), MarkingProperties.reportFooter()).generate(student);
		} catch (Exception ex) {
			Logger.getLogger(MarkingFrame.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	public void emailReport() {

		String report = MarkingProperties.reportPath() + "/" + MarkingProperties.attachmentFilePrefix() + "-" + student.getId() + ".pdf";

		new SendReport().sendReport(student, report);
	}

	private void email() {
		save();

		new Thread(new Runnable() {
			@Override
			public void run() {
				createReport();
				emailReport();
			}
		}).start();

	}

	public boolean getExpandable() {
		return this.cbExpandable.isSelected();
	}

}