/
ModifyAssignmentScreen.java
292 lines (267 loc) · 8.75 KB
/
ModifyAssignmentScreen.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*
* .
*/
/**
* The ModifyAssignmentsScreen class implements the GUI for saving modifications
* to an assignment.
*
* @author Derek Thurn
*/
public class ModifyAssignmentScreen implements ActionListener
{
Klass theClass;
// the class which the assignment being modified is in
Assignment theAssignment;
// the assignment being modified.
String originalName;
// the original name of the assignment being modified
Category theCategory;
// the category which the assignment is in.
JFrame aFrame;
private JPanel marksPanel;
private JLabel nameL;
private JTextField nameF;
private JComboBox catCombo;
private JLabel catLabel;
private JPanel catPanel;
private JButton cancel;
private JTextField weightF;
private JButton save;
private JLabel weightL;
private JTextField marksF;
private JLabel marksL;
private JPanel buttonPanel;
private JPanel weightPanel;
private JPanel namePanel;
/**
* @param k
* @param a
*/
public ModifyAssignmentScreen( Klass k, Assignment a )
{
theClass = k;
theAssignment = a;
originalName = theAssignment.getName();
displayScreen();
}
/**
* Displays the GUI for the screen
*
* @pre the correct objects have been declared
* @post the GUI is displayed.
*/
public void displayScreen()
{
aFrame = new JFrame();
aFrame.setTitle( "Modify Assignment" );
aFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
aFrame.setSize( 400, 400 );
GradeBook.center( aFrame );
BoxLayout aFrameLayout = new BoxLayout( aFrame.getContentPane(),
javax.swing.BoxLayout.Y_AXIS );
aFrame.getContentPane().setLayout( aFrameLayout );
aFrame.setVisible( true );
{
namePanel = new JPanel();
aFrame.getContentPane().add( namePanel );
{
nameL = new JLabel();
namePanel.add( nameL );
nameL.setText( "Assignment Name:" );
}
{
nameF = new JTextField();
namePanel.add( nameF );
nameF.setPreferredSize( new java.awt.Dimension( 180, 20 ) );
}
}
{
marksPanel = new JPanel();
aFrame.getContentPane().add( marksPanel );
{
marksL = new JLabel();
marksPanel.add( marksL );
marksL.setText( "Total Marks:" );
}
{
marksF = new JTextField();
marksPanel.add( marksF );
marksF.setPreferredSize( new java.awt.Dimension( 57, 20 ) );
}
}
{
weightPanel = new JPanel();
aFrame.getContentPane().add( weightPanel );
{
weightL = new JLabel();
weightPanel.add( weightL );
weightL.setText( "Weight:" );
}
{
weightF = new JTextField();
weightPanel.add( weightF );
weightF.setPreferredSize( new java.awt.Dimension( 50, 20 ) );
}
}
{
catPanel = new JPanel();
aFrame.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 );
}
}
{
buttonPanel = new JPanel();
aFrame.getContentPane().add( buttonPanel );
{
save = new JButton();
buttonPanel.add( save );
save.setText( "Save Changes" );
save.addActionListener( this );
}
{
cancel = new JButton();
buttonPanel.add( cancel );
cancel.setText( "Cancel" );
cancel.addActionListener( this );
}
}
nameF.setText( theAssignment.getName() );
marksF.setText( "" + theAssignment.getTotalMarks() );
weightF.setText( "" + theAssignment.getWeight() );
try
{
catCombo.setSelectedItem( theAssignment.getCategory().toString() );
}
catch( Exception e )
{
catCombo.setSelectedIndex( -1 );
}
}
/**
* Listens for button presses
*
* @pre the correct listeners have been added
* @post the buttons function correctly.
*/
public void actionPerformed( ActionEvent evt )
{
if( evt.getSource().equals( cancel ) )
{
aFrame.dispose();
new AssignmentsScreen();
return;
}
if( evt.getSource().equals( catCombo ) )
{
if( catCombo.getSelectedItem() == null )
return;
String cat = catCombo.getSelectedItem().toString();
theCategory = theClass.getCategoryFromString( cat );
}
if( evt.getSource().equals( save ) )
{
if( nameF.getText().equals( "" ) )
{
GradeBook.error( "You must specify a name for the assignment" );
return;
}
if( marksF.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( marksF.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( weightF.getText().equals( "" ) )
{
w = 1;
}
else
{
try
{
w = Integer.parseInt( weightF.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 = new Assignment( nameF.getText(), tm, w,
theCategory );
int q = -1;
if( theClass.isDuplicateAssignment( temp )
&& !( nameF.getText().equals( originalName ) ) )
{
q = JOptionPane.showConfirmDialog( null,
"Do you want to overwrite the assignment "
+ nameF.getText() + "?", "Confirm",
JOptionPane.YES_NO_OPTION );
if( q == 0 )
{
theClass.deleteAssignment( theClass
.getAssignmentFromString( nameF.getText() ) );
}
if( q == 1 )
{
return;
}
}
theAssignment.setName( nameF.getText() );
theAssignment.setTotalMarks( Integer.parseInt( marksF.getText() ) );
theAssignment.setWeight( Integer.parseInt( weightF.getText() ) );
theAssignment.setCategory( theCategory );
aFrame.dispose();
new AssignmentsScreen();
}
}
}