· 8 years ago · Apr 24, 2018, 02:24 AM
1import javax.swing.*;
2import java.awt.*;
3import java.io.*;
4import java.awt.event.*;
5import java.util.*;
6import javax.swing.table.*;
7
8/**
9 * This class acts as the main GUI frame for the application.
10 *
11 * @author Mads Sønderstrup
12 * @version 4
13 */
14public class MainFrameGUI extends JFrame {
15 private JTable table;
16 private DefaultTableModel model;
17 private JTextField input;
18 private JTextArea memberInfo;
19
20 private SearchAction lSearch = new SearchAction();
21 private AddAction lAdd = new AddAction();
22 private RemoveAction lRemove = new RemoveAction();
23 private AddEliteAction lAddElite = new AddEliteAction();
24 private RemoveEliteAction lRemoveElite = new RemoveEliteAction();
25 private SaveAction lSave = new SaveAction();
26 private LoadAction lLoad = new LoadAction();
27 private ExitAction lExit = new ExitAction();
28
29 private GenerateAction lGen = new GenerateAction();
30
31 private SortJuniorAction lSortJunior = new SortJuniorAction();
32 private SortSeniorAction lSortSenior = new SortSeniorAction();
33 private SortJuniorEliteAction lSortJuniorElite = new SortJuniorEliteAction();
34 private SortSeniorEliteAction lSortSeniorElite = new SortSeniorEliteAction();
35 private ShowAllMembersAction lShowAllMembers = new ShowAllMembersAction();
36 private ShowAllElitesAction lShowAllElites = new ShowAllElitesAction();
37
38 private TableSelection lTable = new TableSelection();
39 /**
40 * Constructor for objects of class MainFrameGUI
41 */
42
43 public MainFrameGUI() {
44 makeFrame();
45 checkFiles();
46 }
47
48 /**
49 * This method is run only on startup.
50 * It will check whether the database file exists, and if they do
51 * fill out the main table with the data for convenience as well as
52 * load the information into the Database.
53 */
54
55 private void checkFiles() {
56 File file = new File("members.dat");
57 File file2 = new File("elites.dat");
58 File file3 = new File("competitions.dat");
59
60 if(!file.exists()) {
61 // File doesn't exist
62 } else {
63 // Try and load the Member File
64 try {
65 FileInputStream f = new FileInputStream(file);
66 ObjectInputStream s = new ObjectInputStream(f);
67 StartProgram.getDB().setMemberList((HashMap<String, Member>) s.readObject());
68 s.close();
69 Collection<Member> c = StartProgram.getDB().getMemberList().values();
70 Iterator<Member> itr = c.iterator();
71 while(itr.hasNext()) {
72 Member temp = itr.next();
73 if(model.getRowCount() == 0) {
74 model.insertRow(0, new Object[] {temp.getCPR(), temp.getName()});
75 } else {
76 model.insertRow(model.getRowCount(), new Object[] {temp.getCPR(), temp.getName()});
77 }
78 }
79 } catch(IOException ioe) {
80 JFrame temp = new JFrame();
81 JOptionPane.showMessageDialog(temp, "Problem loading the Database: \n" + ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
82 } catch(ClassNotFoundException cnfe) {
83 JFrame temp = new JFrame();
84 JOptionPane.showMessageDialog(temp, "Problem loading the Database: \n" + cnfe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
85 }
86
87 // Try and load the Elite File
88 try {
89 FileInputStream f = new FileInputStream(file2);
90 ObjectInputStream s = new ObjectInputStream(f);
91 StartProgram.getDB().setEliteList((HashMap<String, Member>) s.readObject());
92 s.close();
93 } catch(IOException ioe) {
94 JFrame temp = new JFrame();
95 JOptionPane.showMessageDialog(temp, "Problem loading the Database: \n" + ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
96 } catch(ClassNotFoundException cnfe) {
97 JFrame temp = new JFrame();
98 JOptionPane.showMessageDialog(temp, "Problem loading the Database: \n" + cnfe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
99 }
100 }
101
102 if(!file3.exists()) {
103 // File doesn't exist
104 } else {
105 // Try and load the Competition File
106 try {
107 FileInputStream f = new FileInputStream(file3);
108 ObjectInputStream s = new ObjectInputStream(f);
109 StartProgram.getDB().setCompetitionList((HashMap<String, Competition>) s.readObject());
110 s.close();
111 } catch(IOException ioe) {
112 JFrame temp = new JFrame();
113 JOptionPane.showMessageDialog(temp, "Problem loading the Database: \n" + ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
114 } catch(ClassNotFoundException cnfe) {
115 JFrame temp = new JFrame();
116 JOptionPane.showMessageDialog(temp, "Problem loading the Database: \n" + cnfe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
117 }
118 }
119 }
120
121 public void makeFrame() {
122 setTitle("Dolphin Manager");
123 setMinimumSize(new Dimension(1024,600));
124 setDefaultCloseOperation(this.EXIT_ON_CLOSE);
125 //setResizable(false);
126 JPanel contentPane = (JPanel) getContentPane();
127
128 // Make the Menu Bar
129 JMenuBar mBar = new JMenuBar();
130
131 // Add Menus, submenus and functionality to the Menu Bar
132 // File Menu
133 JMenu file = new JMenu("File");
134 JMenuItem iNewMemb = new JMenuItem("New Member", new ImageIcon("images/plus_16.gif"));
135 iNewMemb.addActionListener(lAdd);
136 JMenuItem iRemoveMemb = new JMenuItem("Remove Member", new ImageIcon("images/minus.png"));
137 iRemoveMemb.addActionListener(lRemove);
138 JMenuItem iMakeElite = new JMenuItem("Make Elite", new ImageIcon("images/plus_16_elite.gif"));
139 iMakeElite.addActionListener(lAddElite);
140 JMenuItem iRemoveElite = new JMenuItem("Remove Elite", new ImageIcon("images/minus.png"));
141 iRemoveElite.addActionListener(lRemoveElite);
142 JMenuItem iUpdateTimes = new JMenuItem("Update Training Times", new ImageIcon("images/stopwatch.png"));
143 JMenuItem iListLateDue = new JMenuItem("List all Late Dues", new ImageIcon("images/ico-money.png"));
144 JMenuItem iNewCompetition = new JMenuItem("New Competition Results", new ImageIcon("images/trophy.png"));
145 JMenuItem iSave = new JMenuItem("Save...", new ImageIcon("images/save.png"));
146 iSave.addActionListener(lSave);
147 JMenuItem iLoad = new JMenuItem("Load...", new ImageIcon("images/load.png"));
148 iLoad.addActionListener(lLoad);
149 JMenuItem iExit = new JMenuItem("Exit", new ImageIcon("images/exit.gif"));
150 iExit.addActionListener(lExit);
151 mBar.add(file);
152 file.add(iNewMemb);
153 file.add(iRemoveMemb);
154 file.addSeparator();
155 file.add(iMakeElite);
156 file.add(iRemoveElite);
157 file.addSeparator();
158 file.add(iUpdateTimes);
159 file.addSeparator();
160 file.add(iListLateDue);
161 file.addSeparator();
162 file.add(iNewCompetition);
163 file.addSeparator();
164 file.add(iSave);
165 file.add(iLoad);
166 file.add(iExit);
167
168 // Options Menu
169 JMenu options = new JMenu("Options");
170 JMenuItem iGen = new JMenuItem("Generate Test Members");
171 iGen.addActionListener(lGen);
172 JMenuItem iAbout = new JMenuItem("About", new ImageIcon("images/about.png"));
173 mBar.add(options);
174
175 options.add(iGen);
176 options.add(iAbout);
177
178 // West Panel
179 JPanel westPane = new JPanel(new BorderLayout(1,1));
180 JPanel westPaneBtn = new JPanel(new GridLayout(6,0));
181 JPanel westPaneLogo = new JPanel(new BorderLayout(1,1));
182
183 // Buttons for the west part of the frame
184 JButton sJunior = new JButton("Show Junior Members");
185 sJunior.addActionListener(lSortJunior);
186 JButton sSenior = new JButton("Show Senior Members");
187 sSenior.addActionListener(lSortSenior);
188 JButton sEliteJunior = new JButton("Show Junior Elite Members");
189 sEliteJunior.addActionListener(lSortJuniorElite);
190 JButton sEliteSenior = new JButton("Show Senior Elite Members");
191 sEliteSenior.addActionListener(lSortSeniorElite);
192 JButton sAllMembers = new JButton("Show All Members");
193 sAllMembers.addActionListener(lShowAllMembers);
194 JButton sAllElite = new JButton("Show All Elite Members");
195 sAllElite.addActionListener(lShowAllElites);
196
197 JLabel logoLabel = new JLabel(new ImageIcon("images/DolphinLogo_Small.png"));
198 JPanel logoPane = new JPanel(new BorderLayout(1,1));
199 logoPane.add(logoLabel, BorderLayout.CENTER);
200
201 westPaneBtn.add(sJunior);
202 westPaneBtn.add(sSenior);
203 westPaneBtn.add(sEliteJunior);
204 westPaneBtn.add(sEliteSenior);
205 westPaneBtn.add(sAllMembers);
206 westPaneBtn.add(sAllElite);
207
208 westPaneLogo.add(logoPane, BorderLayout.CENTER);
209
210 westPane.add(westPaneBtn, BorderLayout.NORTH);
211 westPane.add(westPaneLogo, BorderLayout.CENTER);
212
213 // Center Panel
214 JPanel centerPane = new JPanel(new BorderLayout(1,1));
215 JPanel centerPaneSearch = new JPanel(new BorderLayout(1,1));
216
217 // Search button for the center part of the frame
218 JButton search = new JButton("Search", new ImageIcon("images/search.gif"));
219 search.addActionListener(lSearch);
220 input = new JTextField();
221 centerPaneSearch.add(search, BorderLayout.WEST);
222 centerPaneSearch.add(input, BorderLayout.CENTER);
223
224 // Table with information
225 model = new DefaultTableModel();
226 model.addColumn("CPR");
227 model.addColumn("Name");
228
229 table = new JTable(model);
230 table.setEnabled(false);
231 table.addMouseListener(lTable);
232 JScrollPane tScroll = new JScrollPane(table);
233
234 centerPane.add(centerPaneSearch, BorderLayout.NORTH);
235 centerPane.add(tScroll, BorderLayout.CENTER);
236
237 // Member Information for the east part of the frame
238 Font font = new Font("default", 1, 20);
239 memberInfo = new JTextArea();
240 memberInfo.setFont(font);
241 memberInfo.setEditable(false);
242
243 JScrollPane mScroll = new JScrollPane(memberInfo);
244 mScroll.setPreferredSize(new Dimension(395,300));
245 // Competition Information for the east part of the frame
246 ImageIcon trophy = new ImageIcon("images/trophy.png");
247
248 JTabbedPane tPane = new JTabbedPane();
249 tPane.setPreferredSize(new Dimension(300,300));
250 JButton updateCrawl = new JButton("Update Results");
251 JButton updateBreast = new JButton("Update Results");
252 JButton updateButterfly = new JButton("Update Results");
253 JButton updateBack = new JButton("Update Results");
254
255 // Crawl
256 JTable tCrawl = new JTable(StartProgram.getCompTables().getCrawlModel());
257 JScrollPane crawlScroll = new JScrollPane(tCrawl);
258 JPanel crawlPane = new JPanel(new BorderLayout(1,1));
259 crawlPane.add(crawlScroll, BorderLayout.CENTER);
260 crawlPane.add(updateCrawl, BorderLayout.NORTH);
261
262 // Breaststroke
263 JTable tBreast = new JTable(StartProgram.getCompTables().getBreastModel());
264 JScrollPane breastScroll = new JScrollPane(tBreast);
265 JPanel breastPane = new JPanel(new BorderLayout(1,1));
266 breastPane.add(breastScroll, BorderLayout.CENTER);
267 breastPane.add(updateBreast, BorderLayout.NORTH);
268
269 // Butterfly
270 JTable tButterfly = new JTable(StartProgram.getCompTables().getButterflyModel());
271 JScrollPane butterflyScroll = new JScrollPane(tButterfly);
272 JPanel butterflyPane = new JPanel(new BorderLayout(1,1));
273 butterflyPane.add(butterflyScroll, BorderLayout.CENTER);
274 butterflyPane.add(updateButterfly, BorderLayout.NORTH);
275
276 // Backstroke
277 JTable tBack = new JTable(StartProgram.getCompTables().getBackStrokeModel());
278 JScrollPane backScroll = new JScrollPane(tBack);
279 JPanel backPane = new JPanel(new BorderLayout(1,1));
280 backPane.add(backScroll, BorderLayout.CENTER);
281 backPane.add(updateBack, BorderLayout.NORTH);
282
283 tPane.addTab("Crawl", trophy, crawlPane, "Shows the newest Crawl Competition Results");
284 tPane.addTab("Breaststroke", trophy, breastPane, "Shows the newest Breaststroke Competition Results");
285 tPane.addTab("Butterfly", trophy, butterflyPane, "Shows the newest Butterfly Competition Results");
286 tPane.addTab("Backstroke", trophy, backPane, "Shows the newest Backstroke Competition Results");
287
288 // Panel for the text area and Competition menu
289 JPanel eastPane = new JPanel(new GridLayout(2,0));
290
291 eastPane.add(mScroll);
292 eastPane.add(tPane);
293
294 // Set the Menu Bar for the frame
295 this.setJMenuBar(mBar);
296
297 // Add panels to the Content Pane
298 contentPane.add(westPane, BorderLayout.WEST);
299 contentPane.add(centerPane, BorderLayout.CENTER);
300 contentPane.add(eastPane, BorderLayout.EAST);
301
302 this.setVisible(true);
303 pack();
304 }
305
306 /**
307 * This method will clear the table.
308 */
309
310 private void clear() {
311 while(model.getRowCount() > 0) {
312 model.removeRow(0);
313 }
314 }
315
316 /**
317 * This class holds the functionality to show one specific Member.
318 */
319
320 class SearchAction implements ActionListener {
321 /**
322 * Will search through the general Member list, as all members
323 * can be found here, regardless of their status. Then it will
324 * clear the table if it finds something, and show the member
325 * asked for instead.
326 */
327 public void actionPerformed(ActionEvent e) {
328 Collection<Member> c = StartProgram.getDB().getMemberList().values();
329 Iterator<Member> itr = c.iterator();
330 // Do we have any members?
331 if(!itr.hasNext()) {
332 JFrame temp = new JFrame();
333 JOptionPane.showMessageDialog(temp, "There are no Members in the Database!", "Error", JOptionPane.ERROR_MESSAGE);
334 } else {
335 // We do have members!
336 if(input.getText().isEmpty()) {
337 // The user didn't provide a CPR number
338 JFrame temp = new JFrame();
339 JOptionPane.showMessageDialog(temp, "Please specify a CPR number to search for first!", "Error", JOptionPane.ERROR_MESSAGE);
340 return;
341 } else {
342 // We got input, now to look for the result.
343 while(itr.hasNext()) {
344 Member temp = itr.next();
345 if(temp.getCPR().equals(input.getText())) {
346 // We found the member we were looking for
347 clear();
348 model.insertRow(0, new Object[] {temp.getCPR(), temp.getName()});
349 return;
350 }
351 }
352 // We just exited the while, meaning we went through the entire hashmap.
353 // While doing so, we didn't find the member we were looking for.
354 JFrame tempf = new JFrame();
355 JOptionPane.showMessageDialog(tempf, "No Member with that CPR was found.", "Error", JOptionPane.ERROR_MESSAGE);
356 }
357 }
358 }
359 }
360
361 /**
362 * This class holds the functionality to show all Junior Members.
363 */
364
365 class SortJuniorAction implements ActionListener {
366 /**
367 * Will go through the Members HashMap and list all
368 * members who are not elite and under the age of 18.
369 */
370 public void actionPerformed(ActionEvent e) {
371 clear();
372 Collection<Member> c = StartProgram.getDB().getMemberList().values();
373 Iterator<Member> itr = c.iterator();
374 while(itr.hasNext()) {
375 Member temp = itr.next();
376 if(temp.getAge() < 18 && temp.isElite() == false) {
377 if(model.getRowCount() == 0) {
378 model.insertRow(0, new Object[] {temp.getCPR(), temp.getName()});
379 } else {
380 model.insertRow(model.getRowCount(), new Object[] {temp.getCPR(), temp.getName()});
381 }
382 }
383 }
384 }
385 }
386
387 /**
388 * This class holds the functionality to show all Senior Members.
389 */
390 class SortSeniorAction implements ActionListener {
391 /**
392 * Will go through the Members HashMap and list all
393 * members who are not elite and are over the age of 18.
394 */
395 public void actionPerformed(ActionEvent e) {
396 clear();
397 memberInfo.setText("");
398 Collection<Member> c = StartProgram.getDB().getMemberList().values();
399 Iterator<Member> itr = c.iterator();
400 while(itr.hasNext()) {
401 Member temp = itr.next();
402 if(temp.getAge() > 18 && temp.isElite() == false) {
403 if(model.getRowCount() == 0) {
404 model.insertRow(0, new Object[] {temp.getCPR(), temp.getName()});
405 } else {
406 model.insertRow(model.getRowCount(), new Object[] {temp.getCPR(), temp.getName()});
407 }
408 }
409 }
410 }
411 }
412
413 /**
414 * This class holds the functionality to show all Elite Junior Members.
415 */
416 class SortJuniorEliteAction implements ActionListener {
417 /**
418 * Will go through the Elite HashMap and list all
419 * members who are elite and under the age of 18.
420 */
421 public void actionPerformed(ActionEvent e) {
422 clear();
423 Collection<Member> c = StartProgram.getDB().getEliteList().values();
424 Iterator<Member> itr = c.iterator();
425 while(itr.hasNext()) {
426 Member temp = itr.next();
427 if(temp.getAge() < 18) {
428 if(model.getRowCount() == 0) {
429 model.insertRow(0, new Object[] {temp.getCPR(), temp.getName()});
430 } else {
431 model.insertRow(model.getRowCount(), new Object[] {temp.getCPR(), temp.getName()});
432 }
433 }
434 }
435 }
436 }
437
438 /**
439 * This class holds the functionality to show all Elite Senior Members.
440 */
441 class SortSeniorEliteAction implements ActionListener {
442 /**
443 * Will go through the Members HashMap and list all
444 * members who are not elite and over the age of 18.
445 */
446 public void actionPerformed(ActionEvent e) {
447 clear();
448 Collection<Member> c = StartProgram.getDB().getEliteList().values();
449 Iterator<Member> itr = c.iterator();
450 while(itr.hasNext()) {
451 Member temp = itr.next();
452 if(temp.getAge() > 18) {
453 if(model.getRowCount() == 0) {
454 model.insertRow(0, new Object[] {temp.getCPR(), temp.getName()});
455 } else {
456 model.insertRow(model.getRowCount(), new Object[] {temp.getCPR(), temp.getName()});
457 }
458 }
459 }
460 }
461 }
462
463 /**
464 * This class holds the functionality to show all Members.
465 */
466 class ShowAllMembersAction implements ActionListener {
467 /**
468 * Will go through the Members HashMap and list all members.
469 */
470 public void actionPerformed(ActionEvent e) {
471 clear();
472 Collection<Member> c = StartProgram.getDB().getMemberList().values();
473 Iterator<Member> itr = c.iterator();
474 while(itr.hasNext()) {
475 Member temp = itr.next();
476 if(model.getRowCount() == 0) {
477 model.insertRow(0, new Object[] {temp.getCPR(), temp.getName()});
478 } else {
479 model.insertRow(model.getRowCount(), new Object[] {temp.getCPR(), temp.getName()});
480 }
481 }
482 }
483 }
484
485 /**
486 * This class holds the functionality to show all Elites.
487 */
488 class ShowAllElitesAction implements ActionListener {
489 /**
490 * Will go through the Members HashMap and list all elites.
491 */
492 public void actionPerformed(ActionEvent e) {
493 clear();
494 Collection<Member> c = StartProgram.getDB().getEliteList().values();
495 Iterator<Member> itr = c.iterator();
496 while(itr.hasNext()) {
497 Member temp = itr.next();
498 if(model.getRowCount() == 0) {
499 model.insertRow(0, new Object[] {temp.getCPR(), temp.getName()});
500 } else {
501 model.insertRow(model.getRowCount(), new Object[] {temp.getCPR(), temp.getName()});
502 }
503 }
504 }
505 }
506
507 /**
508 * This class will start the MemberGUI.
509 */
510
511 class AddAction implements ActionListener {
512 /**
513 * Create a Member
514 */
515 public void actionPerformed(ActionEvent e) {
516 MemberGUI mg = new MemberGUI();
517 }
518 }
519
520 /**
521 * This class will start the RemoveMemberGUI.
522 */
523
524 class RemoveAction implements ActionListener {
525 /**
526 * Remove a Member
527 */
528 public void actionPerformed(ActionEvent e) {
529 if(StartProgram.getDB().getMemberList().size() == 0) {
530 JFrame temp = new JFrame();
531 JOptionPane.showMessageDialog(temp, "There are no Members to Remove.", "Error", JOptionPane.ERROR_MESSAGE);
532 } else {
533 RemoveMemberGUI rmg = new RemoveMemberGUI();
534 }
535 }
536 }
537
538 /**
539 * This class will start the MakeEliteGUI.
540 */
541
542 class AddEliteAction implements ActionListener {
543 /**
544 * Make a Member Elite
545 */
546 public void actionPerformed(ActionEvent e) {
547 if(StartProgram.getDB().getMemberList().size() == 0) {
548 JFrame temp = new JFrame();
549 JOptionPane.showMessageDialog(temp, "There are no Members to make Elite.", "Error", JOptionPane.ERROR_MESSAGE);
550 } else {
551 MakeEliteGUI meg = new MakeEliteGUI();
552 }
553 }
554 }
555
556 /**
557 * This class will start the RemoveEliteGUI.
558 */
559
560 class RemoveEliteAction implements ActionListener {
561 /**
562 * Remove Elite Status from Member
563 */
564 public void actionPerformed(ActionEvent e) {
565 if(StartProgram.getDB().getEliteList().size() == 0) {
566 JFrame temp = new JFrame();
567 JOptionPane.showMessageDialog(temp, "There are no Elites.", "Error", JOptionPane.ERROR_MESSAGE);
568 } else {
569 RemoveEliteGUI reg = new RemoveEliteGUI();
570 }
571 }
572 }
573
574 /**
575 * This will save the Database.
576 */
577
578 class SaveAction implements ActionListener {
579 public void actionPerformed(ActionEvent e) {
580 File file = new File("members.dat");
581 File file2 = new File("elites.dat");
582 File file3 = new File("competitions.dat");
583 boolean anyMembers = true;
584
585 if(StartProgram.getDB().getMemberList().size() == 0) {
586 anyMembers = false;
587 } else {
588 // First we save the Member List
589 try {
590 FileOutputStream f = new FileOutputStream(file);
591 ObjectOutputStream s = new ObjectOutputStream(f);
592 s.writeObject(StartProgram.getDB().getMemberList());
593 s.close();
594 } catch(IOException ioe) {
595 JFrame temp = new JFrame();
596 JOptionPane.showMessageDialog(temp, "An error occured while saving the Database: \n" + ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
597 }
598 }
599
600 if(anyMembers == false) {
601 // No members, so no elites
602 } else {
603 // Then we try the Elite List
604 try {
605 FileOutputStream f = new FileOutputStream(file2);
606 ObjectOutputStream s = new ObjectOutputStream(f);
607 s.writeObject(StartProgram.getDB().getEliteList());
608 s.close();
609 } catch(IOException ioe) {
610 JFrame temp = new JFrame();
611 JOptionPane.showMessageDialog(temp, "An error occured while saving the Database: \n" + ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
612 }
613 }
614
615 if(StartProgram.getDB().getCompetitionList().size() == 0) {
616
617 } else {
618 // Then we try the Competitions List
619 try {
620 FileOutputStream f = new FileOutputStream(file3);
621 ObjectOutputStream s = new ObjectOutputStream(f);
622 s.writeObject(StartProgram.getDB().getCompetitionList());
623 s.close();
624 } catch(IOException ioe) {
625 JFrame temp = new JFrame();
626 JOptionPane.showMessageDialog(temp, "An error occured while saving the Database: \n" + ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
627 }
628 }
629 }
630 }
631
632 /**
633 * This will load the Database.
634 */
635
636 class LoadAction implements ActionListener {
637 public void actionPerformed(ActionEvent e) {
638 File file = new File("members.dat");
639 File file2 = new File("elites.dat");
640 File file3 = new File("competitions.dat");
641
642 if(!file.exists()) {
643 JFrame temp = new JFrame();
644 JOptionPane.showMessageDialog(temp, "There is no Database to load.", "Error", JOptionPane.ERROR_MESSAGE);
645 } else {
646 // Try and load the Member File
647 try {
648 FileInputStream f = new FileInputStream(file);
649 ObjectInputStream s = new ObjectInputStream(f);
650 StartProgram.getDB().setMemberList((HashMap<String, Member>) s.readObject());
651 s.close();
652 Collection<Member> c = StartProgram.getDB().getMemberList().values();
653 Iterator<Member> itr = c.iterator();
654 while(itr.hasNext()) {
655 Member temp = itr.next();
656 if(model.getRowCount() == 0) {
657 model.insertRow(0, new Object[] {temp.getCPR(), temp.getName()});
658 } else {
659 model.insertRow(model.getRowCount(), new Object[] {temp.getCPR(), temp.getName()});
660 }
661 }
662 } catch(IOException ioe) {
663 JFrame temp = new JFrame();
664 JOptionPane.showMessageDialog(temp, "An error occured while loading the Database: \n" + ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
665 } catch(ClassNotFoundException cnfe) {
666 JFrame temp = new JFrame();
667 JOptionPane.showMessageDialog(temp, "An error occured while loading the Database: \n" + cnfe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
668 }
669
670 // Try and load the Elite File
671 try {
672 FileInputStream f = new FileInputStream(file2);
673 ObjectInputStream s = new ObjectInputStream(f);
674 StartProgram.getDB().setEliteList((HashMap<String, Member>) s.readObject());
675 s.close();
676 } catch(IOException ioe) {
677 JFrame temp = new JFrame();
678 JOptionPane.showMessageDialog(temp, "An error occured while loading the Database: \n" + ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
679 } catch(ClassNotFoundException cnfe) {
680 JFrame temp = new JFrame();
681 JOptionPane.showMessageDialog(temp, "An error occured while loading the Database: \n" + cnfe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
682 }
683 }
684
685 if(file3.exists() == false) {
686
687 } else {
688 // Try and load the Competition File
689 try {
690 FileInputStream f = new FileInputStream(file3);
691 ObjectInputStream s = new ObjectInputStream(f);
692 StartProgram.getDB().setCompetitionList((HashMap<String, Competition>) s.readObject());
693 s.close();
694 } catch(IOException ioe) {
695 JFrame temp = new JFrame();
696 JOptionPane.showMessageDialog(temp, "An error occured while loading the Database: \n" + ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
697 } catch(ClassNotFoundException cnfe) {
698 JFrame temp = new JFrame();
699 JOptionPane.showMessageDialog(temp, "An error occured while loading the Database: \n" + cnfe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
700 }
701 }
702 }
703 }
704
705 /**
706 * This class will close the application.
707 */
708
709 class ExitAction implements ActionListener {
710 /**
711 * Close the Application
712 */
713 public void actionPerformed(ActionEvent e) {
714 System.exit(0);
715 }
716 }
717
718 /**
719 * This class will make test members for the application.
720 * It is only made for the application in the test phase
721 * and should be removed from the application entirely,
722 * when it is handed over to the client.
723 */
724
725 class GenerateAction implements ActionListener {
726 /**
727 * Since the application will be low on members
728 * this little generator will make a lot of random
729 * members. All the information is extracted from txt
730 * files provided by the user (in this case, us), and put into ArrayLists.
731 */
732 public void actionPerformed(ActionEvent e) {
733 JFrame temp = new JFrame();
734 String s = (String) JOptionPane.showInputDialog(temp, "How many Test Members should be created?", "Member Generator",
735 JOptionPane.PLAIN_MESSAGE, null, null, 1);
736 int i = Integer.parseInt(s);
737 if((i != 0 ) && (i > 0)) {
738 MemberGenerator.generateMembers(i);
739 }
740 }
741 }
742
743 /**
744 * This class will act as a mouse listener for the DefaultTableModel.
745 */
746
747 class TableSelection implements MouseListener {
748 /**
749 * When a row in the CPR column is chosen, it will search the DataBase
750 * for a matching Member Object, and call it's toString() method. Then
751 * show the text in the memberInfo JTextArea.
752 */
753 public void mouseClicked(MouseEvent event) {
754 if(table.getRowCount() == 0) {
755 // Do nothing;
756 } else {
757 int row = table.rowAtPoint(event.getPoint());
758 int col = table.columnAtPoint(event.getPoint());
759 if(col != 0) {
760 // Do nothing
761 } else {
762 String info = StartProgram.getDB().search((String)model.getValueAt(row,0)).toString();
763 memberInfo.setText(info);
764 if(StartProgram.getDB().search((String) model.getValueAt(row,0)).isElite()) {
765 memberInfo.append("\n" + StartProgram.getDB().search((String) model.getValueAt(row,0)).getTraining().toString());
766 } else {
767
768 }
769 }
770 }
771 }
772
773 // Methods implemented by MouseListener, but not used.
774 public void mousePressed(MouseEvent event) {
775 }
776
777 public void mouseReleased(MouseEvent event) {
778 }
779
780 public void mouseExited(MouseEvent event) {
781 }
782
783 public void mouseEntered(MouseEvent event) {
784 }
785 }
786}