package gradertool.gradebook; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GradedItemDisplay extends JFrame { /** * Construct this, per the design explained in the class comment. */ public GradedItemDisplay(String title) { maxComponentHeight = 1.9; maxComponentWidth = 3000; panel = new JPanel(); add(panel); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); composeRows(); setTitle(title + " Graded Item"); if (title == "Modify"){ prefill(); } pack(); } protected void composeRows() { panel.add(Box.createVerticalStrut(15)); panel.add(composeTopPart()); panel.add(Box.createVerticalStrut(15)); panel.add(composeMiddlePart()); panel.add(Box.createVerticalStrut(15)); panel.add(composeBottomPart()); panel.add(Box.createVerticalStrut(15)); } protected Box composeTopPart() { Box vbox = Box.createVerticalBox(); vbox.add(composeCategoriesRow()); vbox.add(Box.createVerticalStrut(15)); vbox.add(composeNameRow()); vbox.add(Box.createVerticalStrut(15)); vbox.add(composeTypeAndPointsRow()); vbox.add(Box.createVerticalStrut(15)); vbox.add(composeHiddenRow()); vbox.add(Box.createVerticalStrut(15)); return vbox; } protected Box composeMiddlePart(){ Box vbox = Box.createVerticalBox(); vbox.add(composeAllowExtraRow()); vbox.add(Box.createVerticalStrut(15)); vbox.add(composeTotalExtraRow()); vbox.add(Box.createVerticalStrut(15)); return vbox; } protected Box composeBottomPart(){ Box vbox = Box.createVerticalBox(); vbox.add(composeAcceptLateRow()); vbox.add(Box.createVerticalStrut(15)); vbox.add(Box.createVerticalStrut(15)); vbox.add(composeGracePeriodRow()); vbox.add(Box.createVerticalStrut(15)); vbox.add(composeGracePeriodTextRow1()); vbox.add(composeGracePeriodTextRow2()); vbox.add(Box.createVerticalStrut(15)); vbox.add(Box.createVerticalStrut(15)); vbox.add(composeDecaySettingsTextRow()); vbox.add(Box.createVerticalStrut(15)); vbox.add(composeDecaySettingsRow()); vbox.add(Box.createVerticalStrut(15)); vbox.add(Box.createVerticalStrut(15)); vbox.add(composeButtonRow()); return vbox; } /** * Compose the title row as an hbox with label and text field. */ protected Box composeCategoriesRow() { Box hbox = Box.createHorizontalBox(); /* * Construct the label and text field. */ JLabel label = new JLabel("Category "); label.setForeground(Color.black); categoryComboBox = new JComboBox(); categoryComboBox.addItem("select a category"); categoryComboBox.addItem(""); categoryComboBox.addItem("Tests"); categoryComboBox.addItem("Quizzes"); categoryComboBox.addItem("Midterms"); categoryComboBox.addItem("Programs"); categoryComboBox.addItem("Labs"); categoryComboBox.setMaximumSize( new Dimension(maxComponentWidth, (int)(maxComponentHeight * categoryComboBox.getFont().getSize()))); JButton manageCategoriesButton = new JButton("Manage Categories"); manageCategoriesButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Manage Categories button clicked"); } } ); hbox.add(Box.createHorizontalStrut(15)); hbox.add(label); hbox.add(categoryComboBox); hbox.add(Box.createHorizontalStrut(15)); hbox.add(manageCategoriesButton); hbox.add(Box.createHorizontalStrut(15)); return hbox; } protected Box composeNameRow(){ Box hbox = Box.createHorizontalBox(); JLabel label = new JLabel("Graded Item Name "); nameField = new JTextField(); label.setForeground(Color.black); nameField.setMaximumSize( new Dimension(maxComponentWidth, (int)(maxComponentHeight * nameField.getFont().getSize()))); /* * Add the label and text field to the hbox and return it. Use * horizontal struts for spacing. */ hbox.add(Box.createHorizontalStrut(15)); hbox.add(label); hbox.add(nameField); hbox.add(Box.createHorizontalStrut(15)); return hbox; } protected Box composeTypeAndPointsRow(){ Box hbox = Box.createHorizontalBox(); /* * Construct the label and text field. */ JLabel label1 = new JLabel("Type "); label1.setForeground(Color.black); typeComboBox = new JComboBox(); typeComboBox.addItem("select a type"); typeComboBox.addItem(""); typeComboBox.addItem("Percentage"); typeComboBox.addItem("Points"); typeComboBox.addItem("Plus/Check/Minus"); typeComboBox.setMaximumSize( new Dimension(maxComponentWidth, (int)(maxComponentHeight * typeComboBox.getFont().getSize()))); JLabel label2 = new JLabel("Total Points Possible "); pointsField = new JTextField(5); label2.setForeground(Color.black); pointsField.setMaximumSize( new Dimension(maxComponentWidth, (int)(maxComponentHeight * pointsField.getFont().getSize()))); hbox.add(Box.createHorizontalStrut(15)); hbox.add(label1); hbox.add(typeComboBox); hbox.add(Box.createHorizontalStrut(15)); hbox.add(label2); hbox.add(pointsField); hbox.add(Box.createHorizontalStrut(15)); return hbox; } protected Box composeHiddenRow(){ Box hbox = Box.createHorizontalBox(); JLabel label = new JLabel("Hidden "); label.setForeground(Color.black); isHiddenBox = new JCheckBox(); hbox.add(Box.createHorizontalStrut(15)); hbox.add(label); hbox.add(isHiddenBox); hbox.add(Box.createHorizontalStrut(15)); return hbox; } protected Box composeAllowExtraRow(){ Box hbox = Box.createHorizontalBox(); JLabel label = new JLabel("Allow Extra Credit "); label.setForeground(Color.black); allowExtraBox = new JCheckBox(); hbox.add(Box.createHorizontalStrut(15)); hbox.add(label); hbox.add(allowExtraBox); hbox.add(Box.createHorizontalStrut(15)); return hbox; } protected Box composeTotalExtraRow(){ Box hbox = Box.createHorizontalBox(); JLabel label = new JLabel("Total Points or % possible (after extra credit) "); extraPointsField = new JTextField(); label.setForeground(Color.black); extraPointsField.setMaximumSize( new Dimension(maxComponentWidth, (int)(maxComponentHeight * extraPointsField.getFont().getSize()))); /* * Add the label and text field to the hbox and return it. Use * horizontal struts for spacing. */ hbox.add(Box.createHorizontalStrut(15)); hbox.add(label); hbox.add(extraPointsField); hbox.add(Box.createHorizontalStrut(15)); return hbox; } protected Box composeAcceptLateRow(){ Box hbox = Box.createHorizontalBox(); JLabel label = new JLabel("Accept Late Assignments "); label.setForeground(Color.black); acceptLateBox = new JCheckBox(); hbox.add(Box.createHorizontalStrut(15)); hbox.add(label); hbox.add(acceptLateBox); hbox.add(Box.createHorizontalStrut(15)); return hbox; } protected Box composeGracePeriodRow() { Box hbox = Box.createHorizontalBox(); /* * Construct the label and text field. */ JLabel label = new JLabel("Grace Period Settings "); label.setForeground(Color.black); JButton modifyButton = new JButton("Modify"); modifyButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Modify Grace Period button clicked"); } } ); hbox.add(Box.createHorizontalStrut(15)); hbox.add(label); hbox.add(modifyButton); hbox.add(Box.createHorizontalStrut(15)); return hbox; } protected Box composeGracePeriodTextRow1(){ Box hbox = Box.createHorizontalBox(); JLabel label = new JLabel("You have not set any grace period settings."); label.setForeground(Color.black); hbox.add(Box.createHorizontalStrut(15)); hbox.add(label); hbox.add(Box.createHorizontalStrut(15)); return hbox; } protected Box composeGracePeriodTextRow2(){ Box hbox = Box.createHorizontalBox(); JLabel label = new JLabel("Click modify above to set this gradebook's grace period settings"); label.setForeground(Color.black); hbox.add(Box.createHorizontalStrut(15)); hbox.add(label); hbox.add(Box.createHorizontalStrut(15)); return hbox; } protected Box composeDecaySettingsTextRow(){ Box hbox = Box.createHorizontalBox(); JLabel label = new JLabel("Decay Settings"); label.setForeground(Color.black); hbox.add(Box.createHorizontalStrut(15)); hbox.add(label); hbox.add(Box.createHorizontalStrut(15)); return hbox; } protected Box composeDecaySettingsRow(){ Box hbox = Box.createHorizontalBox(); JLabel label1 = new JLabel("Deduct "); label1.setForeground(Color.black); decayPointsField = new JTextField(5); decayPointsField.setMaximumSize( new Dimension(maxComponentWidth, (int)(maxComponentHeight * decayPointsField.getFont().getSize()))); decayTypeComboBox = new JComboBox(); decayTypeComboBox.addItem("select a type"); decayTypeComboBox.addItem(""); decayTypeComboBox.addItem("Percentage"); decayTypeComboBox.addItem("Points"); decayTypeComboBox.setMaximumSize( new Dimension(maxComponentWidth, (int)(maxComponentHeight * decayTypeComboBox.getFont().getSize()))); JLabel label2 = new JLabel("for each"); label2.setForeground(Color.black); decayUnitComboBox = new JComboBox(); decayUnitComboBox.addItem("select a unit"); decayUnitComboBox.addItem(""); decayUnitComboBox.addItem("hours"); decayUnitComboBox.addItem("days"); decayUnitComboBox.setMaximumSize( new Dimension(maxComponentWidth, (int)(maxComponentHeight * decayUnitComboBox.getFont().getSize()))); JLabel label3 = new JLabel("late"); label3.setForeground(Color.black); hbox.add(Box.createHorizontalStrut(15)); hbox.add(label1); hbox.add(decayPointsField); hbox.add(Box.createHorizontalStrut(15)); hbox.add(decayTypeComboBox); hbox.add(Box.createHorizontalStrut(15)); hbox.add(label2); hbox.add(Box.createHorizontalStrut(15)); hbox.add(decayUnitComboBox); hbox.add(Box.createHorizontalStrut(15)); hbox.add(label3); hbox.add(Box.createHorizontalStrut(15)); return hbox; } protected Box composeButtonRow() { Box hbox = Box.createHorizontalBox(); /* * Construct the three buttons. */ JButton okButton = new JButton("Save"); JButton clearButton = new JButton("Reset"); JButton cancelButton = new JButton("Cancel"); /* * Attach the appropriate action listeners to each button. */ okButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Add Graded Item Save pressed."); } } ); clearButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { clear(); } } ); cancelButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); } } ); /* * Add them to the hbox and return it. */ hbox.add(clearButton); hbox.add(Box.createHorizontalStrut(30)); hbox.add(cancelButton); hbox.add(Box.createHorizontalStrut(30)); hbox.add(okButton); return hbox; } protected void clear() { categoryComboBox.setSelectedItem("select a category"); nameField.setText(""); typeComboBox.setSelectedItem("select a type"); pointsField.setText(""); isHiddenBox.setSelected(false); allowExtraBox.setSelected(false); decayPointsField.setText(""); decayTypeComboBox.setSelectedItem("select a type"); decayUnitComboBox.setSelectedItem("select a unit"); acceptLateBox.setSelected(false); extraPointsField.setText(""); } protected void prefill(){ categoryComboBox.setSelectedItem("Tests"); nameField.setText("M2"); typeComboBox.setSelectedItem("Points"); pointsField.setText("50"); isHiddenBox.setSelected(false); allowExtraBox.setSelected(true); decayPointsField.setText(""); decayTypeComboBox.setSelectedItem("select a type"); decayUnitComboBox.setSelectedItem("select a unit"); acceptLateBox.setSelected(false); extraPointsField.setText("55"); } protected JPanel panel; protected JComboBox categoryComboBox; protected JTextField nameField; protected JComboBox typeComboBox; protected JTextField pointsField; protected JCheckBox isHiddenBox; protected JCheckBox allowExtraBox; protected JTextField extraPointsField; protected JCheckBox acceptLateBox; protected JTextField decayPointsField; protected JComboBox decayTypeComboBox; protected JComboBox decayUnitComboBox; protected final double maxComponentHeight; protected final int maxComponentWidth; }