-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewAssignmentsScreen.java
267 lines (244 loc) · 8.11 KB
/
NewAssignmentsScreen.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package ws.thurn.dossier;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*
* .
*/
/**
* The NewAssignmentScreen class contains the GUI and methods for adding a new
* assignment to the program.
*
* @author Derek Thurn
*/
public class NewAssignmentsScreen implements ActionListener
{
Klass theClass;
// The class you are adding assignments to.
private JPanel weightPanel;
private JLabel nameLabel;
private JLabel totalMarksLabel;
private JLabel catLabel;
private JButton cancel;
private JButton addButton;
private JTextField weightField;
private JLabel weightLabel;
private JTextField totalMarksField;
private JTextField nameField;
private JPanel buttonsPanel;
private JPanel totalMarksPanel;
private JPanel namePanel;
private JPanel catPanel;
private JComboBox catCombo;
JFrame assignFrame;
Category theCategory;
// The category that is selected.
/**
* Creates a screen for adding a new assignment.
*
* @param tc the class of the new assignment.
*/
public NewAssignmentsScreen( Klass tc )
{
theClass = tc;
displayScreen();
}
/**
* Displays the GUI for the screen
*
* @pre the correct objects have been declared
* @post the GUI is displayed.
*/
public void displayScreen()
{
assignFrame = new JFrame();
assignFrame.setTitle( "New Assignment" );
assignFrame.setSize( 400, 400 );
assignFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
BoxLayout assignFrameLayout = new BoxLayout( assignFrame
.getContentPane(), javax.swing.BoxLayout.Y_AXIS );
assignFrame.getContentPane().setLayout( assignFrameLayout );
GradeBook.center( assignFrame );
assignFrame.setVisible( true );
{
namePanel = new JPanel();
assignFrame.getContentPane().add( namePanel );
namePanel.setPreferredSize( new java.awt.Dimension( 392, 48 ) );
{
nameLabel = new JLabel();
namePanel.add( nameLabel );
nameLabel.setText( "Assignment Name:" );
}
{
nameField = new JTextField();
namePanel.add( nameField );
nameField.setPreferredSize( new java.awt.Dimension( 167, 20 ) );
}
}
{
totalMarksPanel = new JPanel();
assignFrame.getContentPane().add( totalMarksPanel );
{
totalMarksLabel = new JLabel();
totalMarksPanel.add( totalMarksLabel );
totalMarksLabel.setText( "Maximum Possible Marks:" );
}
{
totalMarksField = new JTextField();
totalMarksPanel.add( totalMarksField );
totalMarksField.setPreferredSize( new java.awt.Dimension( 40,
20 ) );
}
}
{
weightPanel = new JPanel();
assignFrame.getContentPane().add( weightPanel );
{
weightLabel = new JLabel();
weightPanel.add( weightLabel );
weightLabel.setText( "Assignment Weight (Optional):" );
}
{
weightField = new JTextField();
weightPanel.add( weightField );
weightField.setPreferredSize( new java.awt.Dimension( 48, 20 ) );
}
}
{
catPanel = new JPanel();
assignFrame.getContentPane().add( catPanel );
{
catLabel = new JLabel();
catPanel.add( catLabel );
catLabel.setText( "Category:" );
}
{
ComboBoxModel catComboModel = new DefaultComboBoxModel(
theClass.getCategoriesStringArray() );
catCombo = new JComboBox();
catPanel.add( catCombo );
catCombo.setModel( catComboModel );
catCombo.addActionListener( this );
catCombo.setSelectedIndex( -1 );
}
}
{
buttonsPanel = new JPanel();
assignFrame.getContentPane().add( buttonsPanel );
{
addButton = new JButton();
buttonsPanel.add( addButton );
addButton.setText( "Add Assignment" );
addButton.addActionListener( this );
}
{
cancel = new JButton();
buttonsPanel.add( cancel );
cancel.setText( "Cancel" );
cancel.addActionListener( this );
}
}
}
/**
* Listens for button presses
*
* @pre listeners have been added
* @post the correct action is taken after an event.
*/
public void actionPerformed( ActionEvent evt )
{
if( evt.getSource().equals( cancel ) )
{
assignFrame.dispose();
new AssignmentsScreen();
}
if( evt.getSource().equals( catCombo ) )
{
if( catCombo.getSelectedItem() == null )
return;
String cat = catCombo.getSelectedItem().toString();
theCategory = theClass.getCategoryFromString( cat );
}
if( evt.getSource().equals( addButton ) )
{
if( nameField.getText().equals( "" ) )
{
GradeBook.error( "You must specify a name for the assignment" );
return;
}
if( totalMarksField.getText().equals( "" ) )
{
GradeBook
.error( "You must specify the number of marks the assignment is out of." );
return;
}
int tm;
int w;
try
{
tm = Integer.parseInt( totalMarksField.getText() );
}
catch( Exception e )
{
GradeBook
.error( "You must specify an integer value for the total marks." );
return;
}
if( tm < 1 )
{
GradeBook
.error( "The total marks value must be greater than one" );
return;
}
if( weightField.getText().equals( "" ) )
{
w = 1;
}
else
{
try
{
w = Integer.parseInt( weightField.getText() );
}
catch( Exception e )
{
GradeBook
.error( "You must specify an integer value for the weight." );
return;
}
if( w < 1 )
{
GradeBook
.error( "The weight value must be greater than one" );
return;
}
}
Assignment temp;
if( theCategory == null )
{
temp = new Assignment( nameField.getText(), tm, w );
}
else
{
temp = new Assignment( nameField.getText(), tm, w, theCategory );
}
if( theClass.isDuplicateAssignment( temp ) )
{
GradeBook
.error( "An assignment with that name already exists." );
return;
}
theClass.addAssignment( temp );
assignFrame.dispose();
new AssignmentsScreen();
}
}
}