This repository was archived by the owner on Mar 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddRefFrame.java
More file actions
415 lines (348 loc) · 13 KB
/
AddRefFrame.java
File metadata and controls
415 lines (348 loc) · 13 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
/**
* Class to define a window in which to add a new referee
* to the referee list
* @author Adam John Campbell Murray
*
*/
@SuppressWarnings("serial")
public class AddRefFrame extends JFrame implements ActionListener
{
/* Instance variables */
// GUI instance variables
private JPanel north, south;
private JTextField firstNameField, lastNameField;
private JButton addRefButton;
private JRadioButton njbButton, ijbButton;
private JRadioButton oneButton, twoButton, threeButton, fourButton;
private JRadioButton northButton, centralButton, southButton;
private JCheckBox northCheckBox, centralCheckBox, southCheckBox;
// Object instance variables
private RefereeProgram refereeProgram;
private RefereeGUI refereeGUI;
/* Class constants */
// GUI constants
/** The width of the frame */
private final int GUI_WIDTH = 250;
/** The height of the frame */
private final int GUI_HEIGHT = 350;
/** The on-screen horizontal position of the frame */
private final int GUI_X_POSITION = 400;
/** The on-screen vertical position of the frame */
private final int GUI_Y_POSITION = 200;
/**
* Constructor for AddRefFrame.
* @param rProg - RefereeProgram class to be used
* @param rGUI - the main GUI
*/
public AddRefFrame(RefereeProgram rProg, RefereeGUI rGUI)
{
// initialise instance variabless
refereeProgram = rProg;
refereeGUI = rGUI;
// set default properties of JFrame
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setTitle("Add New Referee");
setSize(GUI_WIDTH, GUI_HEIGHT);
setLocation(GUI_X_POSITION, GUI_Y_POSITION);
// layout GUI components
layoutNorth();
layoutSouth();
}
/**
* Adds GUI components to the NORTH section of the JFrame.
*/
public void layoutNorth()
{
// create north JPanel, set layout and border
north = new JPanel();
north.setLayout(new GridLayout(2, 2));
north.setBorder(new TitledBorder(new EtchedBorder(), "New Referee's Name"));
north.setBackground(Color.LIGHT_GRAY);
// create label for first name text field, add to north
JLabel firstNameLabel = new JLabel("Enter first name:");
north.add(firstNameLabel);
// create text field for first name, add to north
firstNameField = new JTextField();
north.add(firstNameField);
// create label for last name text field, add to north
JLabel lastNameLabel = new JLabel("Enter last name:");
north.add(lastNameLabel);
// create text field for last name, add to north
lastNameField = new JTextField();
north.add(lastNameField);
// add north to JFrame
add(north, BorderLayout.NORTH);
}
/**
* Adds GUI components to the SOUTH section of the JFrame.
*/
public void layoutSouth()
{
// create south JPanel
south = new JPanel(new BorderLayout());
// create topSouth JPanel
JPanel topSouth = new JPanel();
topSouth.setLayout(new GridLayout(2, 1));
topSouth.setBorder(new TitledBorder(new EtchedBorder(), "Qualification"));
topSouth.setBackground(Color.LIGHT_GRAY);
// add topSouth to south
south.add(topSouth, BorderLayout.SOUTH);
// create radio buttons for qualification title
njbButton = new JRadioButton("NJB");
njbButton.setBackground(Color.LIGHT_GRAY);
ijbButton = new JRadioButton("IJB");
ijbButton.setBackground(Color.LIGHT_GRAY);
// set the initially selected radio button
njbButton.setSelected(true);
// create button group for qualification title buttons
// and add buttons to group
ButtonGroup jbGroup = new ButtonGroup();
jbGroup.add(njbButton);
jbGroup.add(ijbButton);
// create new JPanel to be added to the upper part of
// topSouth, and add buttons to panel
JPanel upperTopSouth = new JPanel();
upperTopSouth.setBackground(Color.LIGHT_GRAY);
upperTopSouth.add(njbButton);
upperTopSouth.add(ijbButton);
// add panel to topSouth
topSouth.add(upperTopSouth);
// create radio buttons for qualification level
oneButton = new JRadioButton("1");
oneButton.setBackground(Color.LIGHT_GRAY);
twoButton = new JRadioButton("2");
twoButton.setBackground(Color.LIGHT_GRAY);
threeButton = new JRadioButton("3");
threeButton.setBackground(Color.LIGHT_GRAY);
fourButton = new JRadioButton("4");
fourButton.setBackground(Color.LIGHT_GRAY);
// set initially selected radio button
oneButton.setSelected(true);
// create button group for qualification level buttons
// and add to buttons to button group
ButtonGroup numbersGroup = new ButtonGroup();
numbersGroup.add(oneButton);
numbersGroup.add(twoButton);
numbersGroup.add(threeButton);
numbersGroup.add(fourButton);
// create new JPanel to add to the lower part of
// topSouth, and add buttons to panel
JPanel lowerTopSouth = new JPanel();
lowerTopSouth.setBackground(Color.LIGHT_GRAY);
lowerTopSouth.add(oneButton);
lowerTopSouth.add(twoButton);
lowerTopSouth.add(threeButton);
lowerTopSouth.add(fourButton);
// add panel to topSouth
topSouth.add(lowerTopSouth);
// add topSouth to the NORTH section of south
south.add(topSouth, BorderLayout.NORTH);
// create centerSouth JPanel, add components to it
// and set border
JPanel centerSouth = new JPanel();
centerSouth.setBorder(new TitledBorder(new EtchedBorder(), "Home Locality"));
centerSouth.setBackground(Color.LIGHT_GRAY);
// create radio buttons for the home locality
northButton = new JRadioButton("North");
northButton.setBackground(Color.LIGHT_GRAY);
centralButton = new JRadioButton("Central");
centralButton.setBackground(Color.LIGHT_GRAY);
southButton = new JRadioButton("South");
southButton.setBackground(Color.LIGHT_GRAY);
// set initially selected radio button
northButton.setSelected(true);
// create button group for the home locality buttons
// and add buttons to group
ButtonGroup homeLocalityGroup = new ButtonGroup();
homeLocalityGroup.add(northButton);
homeLocalityGroup.add(centralButton);
homeLocalityGroup.add(southButton);
// add buttons to centerSouth panel
centerSouth.add(northButton);
centerSouth.add(centralButton);
centerSouth.add(southButton);
// add centerSouth panel to the CENTER part of south
south.add(centerSouth, BorderLayout.CENTER);
// create bottomSouth JPanel and add components to it
JPanel bottomSouth = new JPanel();
bottomSouth.setLayout(new GridLayout(2, 1));
bottomSouth.setBackground(Color.LIGHT_GRAY);
// create check boxes for visiting localities
northCheckBox = new JCheckBox("North");
northCheckBox.setBackground(Color.LIGHT_GRAY);
centralCheckBox = new JCheckBox("Central");
centralCheckBox.setBackground(Color.LIGHT_GRAY);
southCheckBox = new JCheckBox("South");
southCheckBox.setBackground(Color.LIGHT_GRAY);
// create new panel, set border, add check boxes
JPanel upperBottomSouth = new JPanel();
upperBottomSouth.setBorder(new TitledBorder(new EtchedBorder(), "Visiting Localities"));
upperBottomSouth.setBackground(Color.LIGHT_GRAY);
upperBottomSouth.add(northCheckBox);
upperBottomSouth.add(centralCheckBox);
upperBottomSouth.add(southCheckBox);
// add panel to bottomSouth
bottomSouth.add(upperBottomSouth);
// create add referee button, add action listener
addRefButton = new JButton("Add Referee");
addRefButton.addActionListener(this);
// create new panel to hold add referee button
JPanel lowerBottomSouth = new JPanel();
lowerBottomSouth.setBorder(new TitledBorder(new EtchedBorder(), "Finish Adding Referee"));
lowerBottomSouth.setBackground(Color.LIGHT_GRAY);
lowerBottomSouth.add(addRefButton);
// add panel to bottomSouth
bottomSouth.add(lowerBottomSouth);
// add bottomSouth to SOUTH part of south
south.add(bottomSouth, BorderLayout.SOUTH);
// add south JPanel to JFrame
add(south, BorderLayout.SOUTH);
}
/**
* Adds referee to the referee list, updates the main display,
* and handles any input errors on the part of the user.
*/
public void addReferee()
{
// declare variables for the new referee's first, last,
// and full (first + " " + last) names
String newRefFirstName = "";
String newRefLastName = "";
String newRefFullName = "";
try
{
// if either of the first/last name text fields are empty throw an exception
if (firstNameField.getText().equals("") || lastNameField.getText().equals(""))
throw new IllegalArgumentException();
// otherwise, create new RefereeClass object
else
{
// get the new referee's first and last name from text fields;
// if either of the names begin with a lower case letter
// then this it corrected to upper case
newRefFirstName = firstNameField.getText().trim();
if (newRefFirstName.charAt(0) < 'A' || newRefFirstName.charAt(0) > 'Z')
{
newRefFirstName = newRefFirstName.substring(0, 1).toUpperCase()
+ newRefFirstName.substring(1);
}
newRefLastName = lastNameField.getText().trim();
if (newRefLastName.charAt(0) < 'A' || newRefLastName.charAt(0) > 'Z')
{
newRefLastName = newRefLastName.substring(0, 1).toUpperCase()
+ newRefLastName.substring(1);
}
// instantiate the new referee's full name by the
// concatenation of the first and last names, with
// a space between them
newRefFullName = newRefFirstName + " " + newRefLastName;
// instantiate the number part of the new referee's ID
int newRefIDNumber = 1;
// search through refereeProgram for any referee's with the
// same initials as the new referee. For each match found,
// increment the ID number of the new referee by 1
for (int x = 0; x < refereeProgram.getelementsInArray(); x++)
{
RefereeClass refAtX = refereeProgram.getRefereeClassAtX(x);
if (refAtX.getRefereeID().charAt(0) == newRefFirstName.charAt(0)
&& refAtX.getRefereeID().charAt(1) == newRefLastName.charAt(0))
{
newRefIDNumber++;
}
}
// create the ID for the new referee from initials and ID number
String newRefID = String.format("%c%c%d", newRefFirstName.toUpperCase().charAt(0),
newRefLastName.toUpperCase().charAt(0), newRefIDNumber);
// declare new referee's qualification
String newRefQualification = "";
// instantiate the new referee's qualification
// from the selected radio buttons
if (njbButton.isSelected())
{
if (oneButton.isSelected())
newRefQualification = "NJB1";
else if (twoButton.isSelected())
newRefQualification = "NJB2";
else if (threeButton.isSelected())
newRefQualification = "NJB3";
else if (fourButton.isSelected())
newRefQualification = "NJB4";
}
if (ijbButton.isSelected())
{
if (oneButton.isSelected())
newRefQualification = "IJB1";
else if (twoButton.isSelected())
newRefQualification = "IJB2";
else if (threeButton.isSelected())
newRefQualification = "IJB3";
else if (fourButton.isSelected())
newRefQualification = "IJB4";
}
// declare new referee's allocated matches
int newRefAllocatedMatches = 0;
// declare new referee's home locality
String newRefHomeLocality = "";
// instantiate the new referee's home
// locality from the selected radio buttons
if (northButton.isSelected())
newRefHomeLocality = "North";
else if (centralButton.isSelected())
newRefHomeLocality = "Central";
else if (southButton.isSelected())
newRefHomeLocality = "South";
// declare new referee's visiting localities
String newRefVisitingLocalities = "";
// instantiate the new referee's visiting
// localities from the selected radio buttons
if (northCheckBox.isSelected() || northButton.isSelected())
newRefVisitingLocalities += "Y";
else
newRefVisitingLocalities += "N";
if (centralCheckBox.isSelected() || centralButton.isSelected())
newRefVisitingLocalities += "Y";
else
newRefVisitingLocalities += "N";
if (southCheckBox.isSelected() || southButton.isSelected())
newRefVisitingLocalities += "Y";
else
newRefVisitingLocalities += "N";
// create new RefereeClass object from input data
RefereeClass newRefRefereeClass = new RefereeClass(newRefID, newRefFullName, newRefQualification,
newRefAllocatedMatches, newRefHomeLocality, newRefVisitingLocalities);
// add new RefereeClass object to refereeProgram's RefereeClass list
refereeProgram.addRefereeClassToList(newRefRefereeClass);
// updates the display in the main GUI
refereeGUI.updateRefDisplay();
refereeGUI.updateComboBox();
// dispose the add referee frame
this.dispose();
}
}
catch (IllegalArgumentException iax)
{
JOptionPane.showMessageDialog(this, "Name fields must not be blank",
"Input error", JOptionPane.ERROR_MESSAGE);
}
catch (StringIndexOutOfBoundsException six)
{
JOptionPane.showMessageDialog(this, "Can't add any more items to list",
"Error", JOptionPane.ERROR_MESSAGE);
this.dispose();
}
}
/**
* Handles button presses, etc
* @param e - the action event being processed
*/
public void actionPerformed(ActionEvent e)
{
// handles the add referee button being pressed
if (e.getSource() == addRefButton)
addReferee();
}
}