001package edu.pdx.cs410J.grader.gradebook.ui; 002 003import edu.pdx.cs410J.grader.gradebook.Assignment; 004 005import java.awt.BorderLayout; 006import java.awt.Container; 007import java.awt.event.ActionEvent; 008import java.awt.event.ActionListener; 009 010import javax.swing.Box; 011import javax.swing.BoxLayout; 012import javax.swing.JButton; 013import javax.swing.JDialog; 014import javax.swing.JFrame; 015import javax.swing.JPanel; 016 017/** 018 * This dialog is used to create a new <code>Assignment</code> 019 */ 020@SuppressWarnings("serial") 021public class NewAssignmentDialog extends JDialog { 022 023 private Assignment assignment; 024 025 // GUI components we care about 026 private AssignmentPanel assignmentPanel; 027 028 /** 029 * Creates a new <code>NewAssignmentDialog</code> and populates with 030 * GUI components. 031 */ 032 public NewAssignmentDialog(JFrame owner) { 033 super(owner, "New Assignment", true /* modal */); 034 035 Container pane = this.getContentPane(); 036 pane.setLayout(new BorderLayout()); 037 038 this.assignmentPanel = new AssignmentPanel(true); 039 pane.add(this.assignmentPanel, BorderLayout.CENTER); 040 041 JPanel buttons = new JPanel(); 042 buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS)); 043 buttons.add(Box.createHorizontalGlue()); 044 045 JButton ok = new JButton("OK"); 046 ok.addActionListener(new ActionListener() { 047 public void actionPerformed(ActionEvent e) { 048 // Create a new Assignment from the contents of assignment 049 // panel 050 assignment = assignmentPanel.createAssignment(); 051 if (assignment != null) { 052 // Assignment is okay 053 NewAssignmentDialog.this.dispose(); 054 } 055 } 056 }); 057 buttons.add(ok); 058 059 buttons.add(Box.createHorizontalGlue()); 060 061 JButton cancel = new JButton("Cancel"); 062 cancel.addActionListener(new ActionListener() { 063 public void actionPerformed(ActionEvent e) { 064 // Do not create a new assignment 065 assignment = null; 066 NewAssignmentDialog.this.dispose(); 067 } 068 }); 069 buttons.add(cancel); 070 071 buttons.add(Box.createHorizontalGlue()); 072 073 pane.add(buttons, BorderLayout.SOUTH); 074 } 075 076 /** 077 * Returns the <code>Assignment</code> created by this dialog. If 078 * no assignment was created, <code>null</code> is returned. 079 */ 080 public Assignment getAssignment() { 081 return this.assignment; 082 } 083 084}