-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssignmentsScreen.java
382 lines (325 loc) · 11.2 KB
/
AssignmentsScreen.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package ws.thurn.dossier;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/*
* .
*/
/**
* The AssignmentsScreen class is one of the main GUI screens that can be
* reached from the Main Menu. It allows interaction with the functions of the
* program that deal with categories and assignments.
*
* @author Derek Thurn
*/
public class AssignmentsScreen extends Screen implements ActionListener
{
JList aList;
// The linked list containing the current assignments.
JList cList;
// The linked list containing the current categories
Klass theClass;
// The class to which the assignment is being added.
String selectedAssignment;
// The assignment the user has selected.
String selectedCategory;
// The category the user has selected.
JButton newAssignment;
JButton deleteAssignment;
JButton modifyAssignment;
JButton newCategory;
JButton deleteCategory;
JButton modifyCategory;
/**
* Creates a new AssignmentsScreen object, ensuring that a class has already
* been created.
*/
public AssignmentsScreen()
{
theClass = GradeBook.getCurrentClass();
if( GradeBook.getNumClasses() < 1 )
{
GradeBook
.error( "You must create a class before adding assignments" );
new MainMenu();
}
else
{
super.init();
displayScreen();
}
}
/**
* Displays the GUI for the Assignments Screen.
*
* @pre Specific GUI objects have been declared.
* @post GUI has been displayed
*/
public void displayScreen()
{
JPanel aListP = new JPanel();
JPanel cListP = new JPanel();
JPanel buttonsP = new JPanel();
JPanel catButtons = new JPanel();
frm.setTitle( "Assignments" );
JLabel aLabel = new JLabel( "Assignments" );
JLabel cLabel = new JLabel( "Categories" );
newAssignment = new JButton( "New Assignment" );
deleteAssignment = new JButton( "Delete Assignment" );
modifyAssignment = new JButton( "Modify Assignment" );
newCategory = new JButton( "New Category" );
deleteCategory = new JButton( "Delete Category" );
modifyCategory = new JButton( "Modify Category" );
newAssignment.addActionListener( this );
deleteAssignment.addActionListener( this );
modifyAssignment.addActionListener( this );
newCategory.addActionListener( this );
deleteCategory.addActionListener( this );
modifyCategory.addActionListener( this );
if( theClass != null )
{
String assignments[] = theClass.getAssignmentsStringArray();
String categories[] = theClass.getCategoriesStringArray();
aList = new JList( assignments );
cList = new JList( categories );
}
else
{
String assignments[] = new String[ 0 ];
String categories[] = new String[ 0 ];
aList = new JList( assignments );
cList = new JList( categories );
}
ListListener listener = new ListListener();
aList.addListSelectionListener( listener );
cList.addListSelectionListener( listener );
JScrollPane scrollPane;
scrollPane = new JScrollPane( aList );
scrollPane.setPreferredSize( new Dimension( 200, 200 ) );
JScrollPane scrollPane2;
scrollPane2 = new JScrollPane( cList );
scrollPane2.setPreferredSize( new Dimension( 200, 200 ) );
aListP.add( aLabel );
aListP.add( scrollPane );
cListP.add( cLabel );
cListP.add( scrollPane2 );
buttonsP.add( newAssignment );
buttonsP.add( deleteAssignment );
buttonsP.add( modifyAssignment );
catButtons.add( newCategory );
catButtons.add( deleteCategory );
catButtons.add( modifyCategory );
aList.setEnabled( true );
cList.setEnabled( true );
content.add( aListP );
content.add( buttonsP );
content.add( cListP );
content.add( catButtons );
}
/**
* Listens for button presses.
*
* @pre listeners have been registered.
* @post none
*/
public void actionPerformed( ActionEvent evt )
{
if( evt.getSource().equals( back ) )
{
frm.dispose();
new MainMenu();
}
if( evt.getSource().equals( newAssignment ) )
{
if( GradeBook.getCurrentClass() == null )
{
GradeBook.error( "You must select a class." );
return;
}
frm.dispose();
new NewAssignmentsScreen( theClass );
}
if( evt.getSource().equals( deleteAssignment ) )
{
if( GradeBook.getCurrentClass() == null )
{
GradeBook.error( "You must select a class." );
return;
}
if( selectedAssignment == null )
{
GradeBook.error( "You must select an assignment." );
return;
}
theClass.deleteAssignment( theClass
.getAssignmentFromString( selectedAssignment ) );
frm.dispose();
new AssignmentsScreen();
}
if( evt.getSource().equals( modifyAssignment ) )
{
if( GradeBook.getCurrentClass() == null )
{
GradeBook.error( "You must select a class." );
return;
}
if( selectedAssignment == null )
{
GradeBook.error( "You must select an assignment." );
return;
}
Assignment temp;
temp = theClass.getAssignmentFromString( selectedAssignment );
if( temp == null )
{
GradeBook.error( "Could not find assignment." );
return;
}
frm.dispose();
new ModifyAssignmentScreen( theClass, temp );
}
if( evt.getSource().equals( newCategory ) )
{
if( GradeBook.getCurrentClass() == null )
{
GradeBook.error( "You must select a class." );
return;
}
frm.dispose();
new NewCategoryScreen( theClass );
}
if( evt.getSource().equals( deleteCategory ) )
{
if( GradeBook.getCurrentClass() == null )
{
GradeBook.error( "You must select a class." );
return;
}
if( selectedCategory == null )
{
GradeBook.error( "You must select a category." );
return;
}
theClass.deleteCategory( selectedCategory );
frm.dispose();
new AssignmentsScreen();
}
if( evt.getSource().equals( modifyCategory ) )
{
if( GradeBook.getCurrentClass() == null )
{
GradeBook.error( "You must select a class." );
return;
}
if( selectedCategory == null )
{
GradeBook.error( "You must select a category." );
return;
}
Category temp;
temp = theClass.getCategoryFromString( selectedCategory );
if( temp == null )
{
GradeBook.error( "Could not find category." );
return;
}
frm.dispose();
new ModifyCategoryScreen( theClass, temp );
}
if( evt.getSource().equals( currentCombo ) )
{
if( currentCombo.getSelectedItem() == null )
return;
Klass temp2 = GradeBook.getCurrentClass();
String cl = currentCombo.getSelectedItem().toString();
Klass temp;
temp = GradeBook.getClassFromString( cl );
GradeBook.setCurrentClass( temp );
if( !temp.equals( temp2 ) )
{
frm.dispose();
new AssignmentsScreen();
}
}
if( evt.getSource().equals( save ) )
{
if( GradeBook.getCurrentClass() == null )
{
GradeBook.error( "You must select a class to save." );
return;
}
File f = GradeBook.getSaveFile();
try
{
RandomAccessFile io = new RandomAccessFile( f, "rw" );
long classAt = GradeBook.fileFindClass( GradeBook
.getCurrentClass() );
if( classAt == -1 )
{
new FileSaver( io, GradeBook.getCurrentClass() );
GradeBook.info( "The class "
+ GradeBook.getCurrentClass().toString()
+ " has been sucessfully saved." );
}
else
{
new FileSaver( io, GradeBook.getCurrentClass(), classAt );
GradeBook.info( "The class "
+ GradeBook.getCurrentClass().toString()
+ " has been sucessfully saved." );
}
}
catch( IOException e )
{
GradeBook.error( "IO Exception in CLASS SCREEN!" );
}
}
if( evt.getSource().equals( load ) )
{
if( GradeBook.getSaveFile().length() < 100 )
{
GradeBook.error( "No classed saved." );
return;
}
frm.dispose();
new FileLoadUI( "assignments" );
}
if( evt.getSource().equals( knew ) )
{
frm.dispose();
new NewClassScreen();
}
}
/**
* @author Derek Thurn Listens for changes in the JList
*/
private class ListListener implements ListSelectionListener
{
public void valueChanged( ListSelectionEvent event )
{
if( !event.getValueIsAdjusting() )
{
if( event.getSource().equals( aList ) )
{
Object selection = aList.getSelectedValue();
selectedAssignment = selection.toString();
}
if( event.getSource().equals( cList ) )
{
Object selection = cList.getSelectedValue();
selectedCategory = selection.toString();
}
}
}
}
}