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 pathMatchFrame.java
More file actions
544 lines (456 loc) · 16.1 KB
/
MatchFrame.java
File metadata and controls
544 lines (456 loc) · 16.1 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
/**
* Defines a frame for display/updating
* of match information
* @author Adam John Campbell Murray
*
*/
@SuppressWarnings("serial")
public class MatchFrame extends JFrame implements ActionListener
{
/* Instance variables */
// GUI instance variables
private JPanel north, center, south;
private JComboBox<Integer> weekNumberComboBox;
private JRadioButton northRadioButton, centralRadioButton, southRadioButton;
private JRadioButton juniorRadioButton, seniorRadioButton;
private JButton assignRefereesButton;
private JTextArea assignedRefereesTextArea;
// Object instance variables
private Match match; // match being created each time
private MatchList matchList; // match list being added to
private RefereeGUI refereeGUI; // needs to be updated with match allocation info
private RefereeProgram refereeProgram;
private RefereeClass allocatedReferee1; // first referee allocated to match
private RefereeClass allocatedReferee2; // second referee allocated to match
private String mainArea; // area match being held in
private String otherArea1; // adjacent area to match area
private String otherArea2; // other adjacent or non adjacent area
private String assignmentLevel;
private RefereeClass [] arrayFirstOrdering; // RefereeClass array after first ordering
private RefereeClass [] arraySecondOrdering; // RefereeClass array after second ordering
/* Class constants */
/** The width of the frame */
private final int GUI_WIDTH = 320;
/** The height of the frame */
private final int GUI_HEIGHT = 460;
/** 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 = 50;
// Text area constants
/** The font size of the text area */
private final int TEXT_AREA_FONT_SIZE = 12;
/** The number of rows in the text area */
private final int MAIN_TEXT_AREA_ROWS = 9;
/** The number of columns in the text area */
private final int MAIN_TEXT_AREA_COLS = 40;
/**
* Constructor for MatchFrame which takes a RefereeProgram object,
* a RefereeGUI object, and a MatchList object as parameters
* @param rProg - the RefereeProgram object being used
* @param rGUI - the main GUI to be updated
* @param mList - the MatchList object being used
*/
public MatchFrame(RefereeProgram rProg, RefereeGUI rGUI, MatchList mList)
{
// initialise instance variables
refereeProgram = rProg;
refereeGUI = rGUI;
matchList = mList;
// set default frame properties
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setTitle("Referee Allocation");
setSize(GUI_WIDTH, GUI_HEIGHT);
setLocation(GUI_X_POSITION, GUI_Y_POSITION);
// layout GUI components
layoutNorth();
layoutCenter();
layoutSouth();
updateMatchAllocationDisplay();
}
/**
* Adds the components to the GUI which
* are in the NORTH area of the JFrame.
*/
public void layoutNorth()
{
// create north, set properties
north = new JPanel();
north.setLayout(new GridLayout(1, 2));
north.setBorder(new TitledBorder(new EtchedBorder(), "Choose match week"));
north.setBackground(Color.LIGHT_GRAY);
// create label for combo box, add to north
JLabel weekNumberComboBoxLabel = new JLabel("Select week number:");
north.add(weekNumberComboBoxLabel);
// create combo box for match weeks
weekNumberComboBox = new JComboBox<Integer>();
// populate it with week numbers
for (int i = 1; i < 53; i++)
weekNumberComboBox.addItem(i);
// add combo box to north
north.add(weekNumberComboBox);
// add north to the frame
add(north, BorderLayout.NORTH);
}
/**
* Adds the components to the GUI which
* are in the CENTER area of the JFrame.
*/
public void layoutCenter()
{
// create center panel, set layout, set background colour
center = new JPanel();
center.setLayout(new GridLayout(3, 1));
center.setBackground(Color.LIGHT_GRAY);
// create topCenter panel, set border, set background colour
JPanel topCenter = new JPanel();
topCenter.setBorder(new TitledBorder(new EtchedBorder(), "Select Area"));
topCenter.setBackground(Color.LIGHT_GRAY);
// create radio buttons for selecting match area
// and set button background colours
northRadioButton = new JRadioButton("North");
northRadioButton.setBackground(Color.LIGHT_GRAY);
centralRadioButton = new JRadioButton("Central");
centralRadioButton.setBackground(Color.LIGHT_GRAY);
southRadioButton = new JRadioButton("South");
southRadioButton.setBackground(Color.LIGHT_GRAY);
// set initially selected radio button
northRadioButton.setSelected(true);
// create button groups and add buttons to it
ButtonGroup areaGroup = new ButtonGroup();
areaGroup.add(northRadioButton);
areaGroup.add(centralRadioButton);
areaGroup.add(southRadioButton);
// add buttons to topCenter panel
topCenter.add(northRadioButton);
topCenter.add(centralRadioButton);
topCenter.add(southRadioButton);
// add topCenter panel to center panel
center.add(topCenter);
// create midCenter panal, set border, set background colour
JPanel midCenter = new JPanel();
midCenter.setBorder(new TitledBorder(new EtchedBorder(), "Select Level"));
midCenter.setBackground(Color.LIGHT_GRAY);
// create radio buttons for selecting match level
// and set button background colours
juniorRadioButton = new JRadioButton("Junior");
juniorRadioButton.setBackground(Color.LIGHT_GRAY);
seniorRadioButton = new JRadioButton("Senior");
seniorRadioButton.setBackground(Color.LIGHT_GRAY);
// set initially selected radio button
juniorRadioButton.setSelected(true);
// create button group to hold level buttons
ButtonGroup levelGroup = new ButtonGroup();
levelGroup.add(juniorRadioButton);
levelGroup.add(seniorRadioButton);
// add level buttons to midCenter panel
midCenter.add(juniorRadioButton);
midCenter.add(seniorRadioButton);
// add midCenter to center panel
center.add(midCenter);
// create bottomCenter panel, set border, set background
JPanel bottomCenter = new JPanel();
bottomCenter.setBorder(new TitledBorder(new EtchedBorder(), "Assign Referees"));
bottomCenter.setBackground(Color.LIGHT_GRAY);
// create button initiate referee assignment,
// and add actionListener
assignRefereesButton = new JButton("Assign Referees");
assignRefereesButton.addActionListener(this);
// add button to bottomCenter
bottomCenter.add(assignRefereesButton);
// add bottomCenter to center panel
center.add(bottomCenter);
// add center panel to the CENTER of the JFrame
add(center, BorderLayout.CENTER);
}
/**
* Adds the components to the GUI which
* are in the CENTER area of the JFrame.
*/
public void layoutSouth()
{
// create south panel, set border, set background colour
south = new JPanel();
south.setBorder(new TitledBorder(new EtchedBorder(), "Referee Suitability List"));
south.setBackground(Color.LIGHT_GRAY);
// create text area, set border, set to non-editable,
// set font, set foreground and background colours
assignedRefereesTextArea = new JTextArea(MAIN_TEXT_AREA_ROWS, MAIN_TEXT_AREA_COLS);
assignedRefereesTextArea.setBorder(new EtchedBorder());
assignedRefereesTextArea.setEditable(false);
assignedRefereesTextArea.setFont(new Font("Courier", Font.PLAIN, TEXT_AREA_FONT_SIZE));
assignedRefereesTextArea.setForeground(Color.WHITE);
assignedRefereesTextArea.setBackground(Color.BLUE);
// add text area to south panel
south.add(assignedRefereesTextArea);
// add south panel to the SOUTH of the JFrame
add(south, BorderLayout.SOUTH);
}
/**
* Updates the text area of the frame with
* a list of referees and their number of
* allocated matches in order of suitability
* of refereeing the match being allocated.
*/
public void updateMatchAllocationDisplay()
{
// clear the text area
assignedRefereesTextArea.setText("");
// create and add headings to the text area
String headings = String.format("%-20s%-20s", "Referee Name", "Allocated Matches");
assignedRefereesTextArea.append(headings + "\n");
// create and add a break line to the text area
String breakLine = "";
for (int i = 0; i < 40; i++)
breakLine += "*";
assignedRefereesTextArea.append(breakLine + "\n");
}
/**
* Handles the assignment of two referees to a certain match.
* If no two suitable referees are available an error is displayed
* to the user. If two suitable referees are found, their names
* are displayed to the user.
*/
public void assignReferees()
{
try
{
// get the week from combo box, then parse to an integer
int assignmentWeek = (Integer) weekNumberComboBox.getSelectedItem();
// get the match area from selected radio button
if (northRadioButton.isSelected())
{
mainArea = "North";
otherArea1 = "Central";
otherArea2 = "South";
}
else if (centralRadioButton.isSelected())
{
mainArea = "Central";
otherArea1 = "North";
otherArea2 = "South";
}
else if (southRadioButton.isSelected())
{
mainArea = "South";
otherArea1 = "Central";
otherArea2 = "North";
}
// get the match level from selected radio button
if (juniorRadioButton.isSelected())
assignmentLevel = "Junior";
else
assignmentLevel = "Senior";
// order the arrays
firstArrayOrdering();
secondArrayOrdering();
// reset allocated referees to null (so checks can be carried out)
allocatedReferee1 = null;
allocatedReferee2 = null;
finalCheck();
// update the match allocations of the allocated referees
allocatedReferee1.setAllocatedMatches(allocatedReferee1.getAllocatedMatches() + 1);
allocatedReferee2.setAllocatedMatches(allocatedReferee2.getAllocatedMatches() + 1);
// create match object
match = new Match(assignmentWeek, mainArea, assignmentLevel,
allocatedReferee1.getRefereeName(), allocatedReferee2.getRefereeName());
// update the text area
updateMatchAllocationDisplay();
// list of referees in order of suitability added to text area
for (int x = 0; x < arraySecondOrdering.length; x++)
{
assignedRefereesTextArea.append(
arraySecondOrdering[x].matchAllocationsReportString() + "\n");
}
// display allocated referees to the user
JOptionPane.showMessageDialog(this, "Allocated Referee 1: " + allocatedReferee1.getRefereeName(),
"Match Allocation", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(this, "Allocated Referee 2: " + allocatedReferee2.getRefereeName(),
"Match Allocation", JOptionPane.INFORMATION_MESSAGE);
// add match to match list
matchList.addMatchToList(match);
// update main GUI
refereeGUI.updateRefDisplay();
// remove selected week from the combo box
weekNumberComboBox.removeItem(weekNumberComboBox.getSelectedItem());
}
catch (IllegalArgumentException iax)
{
updateMatchAllocationDisplay();
JOptionPane.showMessageDialog(this, "No Two Suitable Referees",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
public void levelCheck()
{
boolean found1 = false;
int i = 0;
while(!found1 && i < arraySecondOrdering.length)
{
if (arraySecondOrdering[i].getQualification().charAt(3) > '1')
{
allocatedReferee1 = arraySecondOrdering[i];
found1 = true;
}
i++;
}
boolean found2 = false;
int j = 0;
while(!found2 && j < arraySecondOrdering.length)
{
if (arraySecondOrdering[j] != allocatedReferee1
&& arraySecondOrdering[j].getQualification().charAt(3) > '1')
{
allocatedReferee2 = arraySecondOrdering[j];
found2 = true;
}
j++;
}
if (allocatedReferee1 == null || allocatedReferee2 == null)
throw new IllegalArgumentException();
}
public void finalCheck()
{
int visLocalIndex = 0;
if (mainArea.equals("North"))
visLocalIndex = 0;
else if (mainArea.equals("Central"))
visLocalIndex = 1;
else if (mainArea.equals("South"))
visLocalIndex = 2;
char levelIndex = '0';
if (assignmentLevel.equals("Junior"))
levelIndex = '0';
else if (assignmentLevel.equals("Senior"))
levelIndex = '1';
boolean found1 = false;
int i = 0;
while(!found1 && i < arraySecondOrdering.length)
{
if (arraySecondOrdering[i].getVisitingLocalities().charAt(visLocalIndex) == 'Y'
&& arraySecondOrdering[i].getQualification().charAt(3) > levelIndex)
{
allocatedReferee1 = arraySecondOrdering[i];
found1 = true;
}
i++;
}
boolean found2 = false;
int j = 0;
while(!found2 && j < arraySecondOrdering.length)
{
if (arraySecondOrdering[j] != allocatedReferee1
&& arraySecondOrdering[j].getVisitingLocalities().charAt(visLocalIndex) == 'Y'
&& arraySecondOrdering[j].getQualification().charAt(3) > levelIndex)
{
allocatedReferee2 = arraySecondOrdering[j];
found2 = true;
}
j++;
}
if (allocatedReferee1 == null || allocatedReferee2 == null)
throw new IllegalArgumentException();
}
public void firstArrayOrdering()
{
// set whichOrder in order to sort the RefereeClass array
// by match allocations
RefereeClass.setWhichOrder("matches");
Arrays.sort(refereeProgram.getrefereeClassArray());
// create new array
arrayFirstOrdering = new RefereeClass[refereeProgram.getelementsInArray()];
// count to hold the amount of array entries already added
int newArrayElementsAdded = 0;
// add RefereeClass objects which are in the main area
for (int x = 0; x < refereeProgram.getelementsInArray(); x++)
{
RefereeClass refAtX = refereeProgram.getRefereeClassAtX(x);
if (refAtX.getHomeLocality().equals(mainArea))
{
arrayFirstOrdering[newArrayElementsAdded] = refAtX;
newArrayElementsAdded++;
}
}
// add RefereeClass objects which are in the adjacent area
for (int x = 0; x < refereeProgram.getelementsInArray(); x++)
{
RefereeClass refAtX = refereeProgram.getRefereeClassAtX(x);
if (refAtX.getHomeLocality().equals(otherArea1))
{
arrayFirstOrdering[newArrayElementsAdded] = refAtX;
newArrayElementsAdded++;
}
}
// add RefereeClass objects which are in the non-adjacent or
// second adjacent area
for (int x = 0; x < refereeProgram.getelementsInArray(); x++)
{
RefereeClass refAtX = refereeProgram.getRefereeClassAtX(x);
if (refAtX.getHomeLocality().equals(otherArea2))
{
arrayFirstOrdering[newArrayElementsAdded] = refAtX;
newArrayElementsAdded++;
}
}
}
public void secondArrayOrdering()
{
// create the new array
arraySecondOrdering = new RefereeClass[refereeProgram.getelementsInArray()];
// count to hold the number of array entries already added
int newArrayElementsAdded = 0;
int visitingLocalitiesCheck = 0;
if (mainArea.equals("North"))
visitingLocalitiesCheck = 0;
else if (mainArea.equals("Central"))
visitingLocalitiesCheck = 1;
else if (mainArea.equals("South"))
visitingLocalitiesCheck = 2;
// add elements which have home locality in main area
for (int x = 0; x < arrayFirstOrdering.length; x++)
{
if (arrayFirstOrdering[x].getHomeLocality().equals(mainArea))
{
arraySecondOrdering[newArrayElementsAdded] = arrayFirstOrdering[x];
newArrayElementsAdded++;
}
}
for (int x = 0; x < arrayFirstOrdering.length; x++)
{
if (arrayFirstOrdering[x].getHomeLocality().equals(otherArea1)
&& arrayFirstOrdering[x].getVisitingLocalities().charAt(visitingLocalitiesCheck) == 'Y')
{
arraySecondOrdering[newArrayElementsAdded] = arrayFirstOrdering[x];
newArrayElementsAdded++;
}
}
for (int x = 0; x < arrayFirstOrdering.length; x++)
{
if (arrayFirstOrdering[x].getHomeLocality().equals(otherArea2)
&& arrayFirstOrdering[x].getVisitingLocalities().charAt(visitingLocalitiesCheck) == 'Y')
{
arraySecondOrdering[newArrayElementsAdded] = arrayFirstOrdering[x];
newArrayElementsAdded++;
}
}
for (int x = 0; x < arrayFirstOrdering.length; x++)
{
if (arrayFirstOrdering[x].getVisitingLocalities().charAt(visitingLocalitiesCheck) == 'N')
{
arraySecondOrdering[newArrayElementsAdded] = arrayFirstOrdering[x];
newArrayElementsAdded++;
}
}
}
public void actionPerformed(ActionEvent e)
{
// handles assign referees button being pressed
if (e.getSource() == assignRefereesButton)
assignReferees();
}
}