-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisual.java
996 lines (981 loc) · 43.6 KB
/
Visual.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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
import com.googlecode.lanterna.screen.Screen;
import com.googlecode.lanterna.TextColor;
import com.googlecode.lanterna.TextColor.ANSI;
import com.googlecode.lanterna.gui2.table.Table;
import com.googlecode.lanterna.gui2.CheckBoxList;
import com.googlecode.lanterna.gui2.Button;
import com.googlecode.lanterna.gui2.GridLayout;
import com.googlecode.lanterna.gui2.WindowShadowRenderer;
import com.googlecode.lanterna.gui2.Separator;
import com.googlecode.lanterna.gui2.GridLayout;
import com.googlecode.lanterna.gui2.Label;
import com.googlecode.lanterna.gui2.EmptySpace;
import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.BasicWindow;
import com.googlecode.lanterna.gui2.ComboBox;
import com.googlecode.lanterna.gui2.TextBox;
import com.googlecode.lanterna.gui2.Window;
import com.googlecode.lanterna.gui2.MultiWindowTextGUI;
import com.googlecode.lanterna.gui2.ProgressBar;
import com.googlecode.lanterna.gui2.CheckBox;
import com.googlecode.lanterna.gui2.Component;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import com.googlecode.lanterna.gui2.RadioBoxList;
import com.googlecode.lanterna.gui2.Direction;
import com.googlecode.lanterna.gui2.LinearLayout;
import com.googlecode.lanterna.gui2.dialogs.MessageDialog;
import com.googlecode.lanterna.gui2.dialogs.MessageDialogBuilder;
import com.googlecode.lanterna.gui2.dialogs.MessageDialogButton;
import com.googlecode.lanterna.SGR;
import com.googlecode.lanterna.TerminalSize;
import com.googlecode.lanterna.TerminalPosition;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.EOFException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Arrays;
import java.net.MalformedURLException;
/**
* Die Klasse Visual verarbeitet alle Vorgänge zur Darstellung der Informationen
* auf der Konsole.
* @author Thomas Davies
* @version 1.0
*/
public class Visual{
private DefaultTerminalFactory terminalFactory = null;
private Screen screen = null;
private WindowBasedTextGUI textGUI = null;
private ComboBox.Listener changeScoreEventListener;
/**
* Der Konstruktor initialisiert alle notwendigen Variablen um die grafische
* Oberfläche des Terminals zu erzeugen.
* <br> Um das Programm zu starten muss die Methode run() aufgerufen werden.
*/
public Visual(){
try{
terminalFactory = new DefaultTerminalFactory();
screen = terminalFactory.createScreen();
textGUI = new MultiWindowTextGUI(screen);
}catch(IOException e){
e.printStackTrace();
}
}
/**
* Erzeugt Menü- und Durchschnittsfenster.
*/
private void createWelcomeWindows(){
String surname = DataHandler.getSurname();
if(surname.length() != 0){
surname = " " + surname;
}
final Window menueWindow = new BasicWindow("Willkommen"+surname+"!");
final Window averageWindow = new BasicWindow("Durchschnitte");
Panel averagePanel = new Panel(new LinearLayout(Direction.VERTICAL));
Panel welcomePanel = new Panel(new LinearLayout(Direction.VERTICAL));
welcomePanel.addComponent(new Label("Was möchtest du gerne tun?"));
welcomePanel.addComponent(new Button("Noten anzeigen", new Runnable(){
@Override
public void run(){
if(!DataHandler.testfileExists()){
}
createScoreScreen();
}
}));
welcomePanel.addComponent(new Button("Note testen", new Runnable(){
@Override
public void run(){
createTestScreen(0);
}
}));
welcomePanel.addComponent(new Button("Notentester Zurücksetzen", new Runnable(){
@Override
public void run(){
createAreYouSureScreen(textGUI);
}
}));
welcomePanel.addComponent(new Button("Noten updaten", new Runnable(){
@Override
public void run(){
updateData(menueWindow, averageWindow);
}
}));
welcomePanel.addComponent(new Button("Exit", new Runnable(){
@Override
public void run(){
close();
}
}));
Label average = new Label("Durchschnitt: " + DataHandler.getUser().getAverage());
Label bestAverage = new Label("Beste Endnote: " + DataHandler.getUser().getBestAverage());
Label worstAverage = new Label("Schlechteste Endnote: " + DataHandler.getUser().getWorstAverage());
averagePanel.addComponent(average);
averagePanel.addComponent(bestAverage);
averagePanel.addComponent(worstAverage);
averageWindow.setComponent(averagePanel);
averageWindow.setHints(Arrays.asList(Window.Hint.FIXED_POSITION, Window.Hint.NO_FOCUS, Window.Hint.FIXED_SIZE));
menueWindow.setComponent(welcomePanel);
textGUI.addWindow(menueWindow);
TerminalSize menueSize = menueWindow.getDecoratedSize();
TerminalPosition averagePos = new TerminalPosition(1, menueSize.getRows()+2);
TerminalSize averageSize = new TerminalSize(menueSize.getColumns()-2, 3);
averageWindow.setPosition(averagePos);
averageWindow.setSize(averageSize);
textGUI.addWindowAndWait(averageWindow);
}
/**
* Diese Methode wird über das Menü aufgerufen. Sie erzeugt ein Fenster, in dem alle Noten dargestellt werden.
*/
private void createScoreScreen(){
final Window window = new BasicWindow("Notenliste");
window.setHints(Arrays.asList(Window.Hint.FIXED_POSITION));
window.setPosition(new TerminalPosition(0,0));
Panel notenPanel = new Panel(new GridLayout(3));
Label head1 = new Label("Fach");
Label head2 = new Label("Versuch");
Label head3 = new Label("Note");
head1.addStyle(SGR.BOLD);
head2.addStyle(SGR.BOLD);
head3.addStyle(SGR.BOLD);
notenPanel.addComponent(head1);
notenPanel.addComponent(head2);
notenPanel.addComponent(head3);
HashMap<String, Score> syllabusMap = DataHandler.getSyllabus();
User user = DataHandler.getUser();
HashMap<Integer, ArrayList<Score>> sortedMap = sortSemester(syllabusMap);
drawSemesters(sortedMap, notenPanel, 1);
drawAverage(user, notenPanel);
Button exit = new Button("Exit", new Runnable(){
@Override
public void run(){
window.close();
}
});
notenPanel.addComponent(exit);
window.setComponent(notenPanel);
textGUI.addWindowAndWait(window);
}
/**
* Diese Methode wird von der createScoreScreen() Methode aufgerufen. Hiermit werden alle Module dem Notenpanel hinzugefügt.
* <br> Mit jeder Iteration wird eine Map mit den Wahlpflichtfächern erstellt.
* <br> Falls ein Modul Untermodule hat, werden diese mit höherer Identation ausgegeben.
*
* @param sortedMap Ist die Sortierte Map der Module. Das Fachsemester ist als Key gesetzt und der Score als Value.
* @param notenPanel Ist das Panel was für das neue Fenster erzeugt wurde. Alle Einträge müssen hierin gesetzt werden.
* @param indent Gibt an wie weit ein Eintrag eingerück werden muss.
*/
private void drawSemesters(HashMap<Integer, ArrayList<Score>> sortedMap, Panel notenPanel, int indent){
for(int i = 0; i<=sortedMap.size(); i++){
HashMap<String,ArrayList<Score>> wpfMap = new HashMap<String, ArrayList<Score>>();
Label heading = null;
if(i == 0 && sortedMap.get(i) != null){
heading = new Label("Unbekanntes Semester");
}else if(sortedMap.get(i) != null){
heading = new Label(i + ". Semester");
}
if(heading != null){
heading.setLayoutData(GridLayout.createLayoutData(
GridLayout.Alignment.BEGINNING, // Horizontal alignment in the grid cell if the cell is larger than the component's preferred size
GridLayout.Alignment.BEGINNING, // Vertical alignment in the grid cell if the cell is larger than the component's preferred size
true, // Give the component extra horizontal space if available
false, // Give the component extra vertical space if available
3, // Horizontal span
1)); // Vertical span
heading.addStyle(SGR.BOLD);
notenPanel.addComponent(heading);
for(Score scoreSet: sortedMap.get(i)){
if(scoreSet.isWpf()){
if(wpfMap.get(scoreSet.getWpfTopic()) == null){
ArrayList<Score> wpfScore = new ArrayList<Score>();
wpfScore.add(scoreSet);
wpfMap.put(scoreSet.getWpfTopic(), wpfScore);
}else{
wpfMap.get(scoreSet.getWpfTopic()).add(scoreSet);
}
continue;
}
String scoreString;
if(scoreSet.getScore()==0.0){
scoreString = "-";
}else{
scoreString = "" + scoreSet.getScore();
}
String attemptsString;
if(scoreSet.hasSubScore()){
attemptsString = "";
}else if(scoreSet.getAttempts()==0){
attemptsString = "-";
}else{
attemptsString = "" + scoreSet.getAttempts();
}
String tabs = "";
for(int j = 0; j<indent; j++){
tabs += "\t";
}
Label subject = new Label(tabs + scoreSet.getSubject());
Label score = new Label(scoreString);
Label attempts = new Label(attemptsString);
notenPanel.addComponent(subject);
notenPanel.addComponent(attempts);
notenPanel.addComponent(score);
if(scoreSet.hasSubScore()){
drawScores(scoreSet.getSubScore(), notenPanel, indent+1);
}
}
drawWpf(wpfMap, notenPanel);
}
}
}
/**
* Diese Methode wird von drawSemesters() aufgerufen. Sie fügt alle WahlPflichtfächer dem Notenpanel zu.
* @param sortedMap Ist die Map, die alle WahlPflichtfächer des entsprechenden Semesters enthält.
* @param notenPanel Ist das Panel was für das neue Fenster erzeugt wurde. Alle Einträge müssen hierin gesetzt werden.
*/
private void drawWpf(HashMap<String, ArrayList<Score>> sortedMap, Panel notenPanel){
Iterator<Map.Entry<String,ArrayList<Score>>> it = sortedMap.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String, ArrayList<Score>> pair = it.next();
String wpfTopic = pair.getKey();
ArrayList<Score> wpfList = pair.getValue();
Label heading = new Label("\t" + wpfTopic + "("+wpfList.get(0).getWpfWeight()[0]+" aus "+wpfList.get(0).getWpfWeight()[1]+")");
heading.setLayoutData(GridLayout.createLayoutData(
GridLayout.Alignment.BEGINNING, // Horizontal alignment in the grid cell if the cell is larger than the component's preferred size
GridLayout.Alignment.BEGINNING, // Vertical alignment in the grid cell if the cell is larger than the component's preferred size
true, // Give the component extra horizontal space if available
false, // Give the component extra vertical space if available
3, // Horizontal span
1)); // Vertical span
heading.addStyle(SGR.BOLD);
notenPanel.addComponent(heading);
for(Score scoreSet: wpfList){
String scoreString;
if(scoreSet.getScore()==0.0){
scoreString = "-";
}else{
scoreString = "" + scoreSet.getScore();
}
String attemptsString;
if(scoreSet.getAttempts()==0){
attemptsString = "-";
}else{
attemptsString = "" + scoreSet.getAttempts();
}
String tabs = "\t\t";
Label subject = new Label(tabs + scoreSet.getSubject());
Label score = new Label(scoreString);
Label attempts = new Label(attemptsString);
notenPanel.addComponent(subject);
notenPanel.addComponent(attempts);
notenPanel.addComponent(score);
if(scoreSet.hasSubScore()){
drawScores(scoreSet.getSubScore(), notenPanel, 3);
}
}
it.remove();
}
}
/**
* Diese Methode fügt alle nötigen Element dem Notenpanel hinzu. Von hier aus werden keine weiteren Funktionen aufgerufen.
* @param scoreSet Enthält alle not abzubildenden Untermodule.
* @param notenPanel Ist das Panel was für das neue Fenster erzeugt wurde. Alle Einträge müssen hierin gesetzt werden.
* @param indent Gibt an um wieviel ein Eintrag eingerückt werden muss.
*/
private void drawScores(HashMap<String, Score> scoreSet, Panel notenPanel, int indent){
for(Map.Entry<String,Score> subMap: scoreSet.entrySet()){
Score subSet = subMap.getValue();
String scoreSubString;
if(subSet.getScore()==0.0){
scoreSubString = "-";
}else{
scoreSubString = "" + subSet.getScore();
}
String attemptsSubString;
if(subSet.getAttempts()==0){
attemptsSubString = "-";
}else{
attemptsSubString = "" + subSet.getAttempts();
}
String tabs = "";
for(int j = 0; j<indent; j++){
tabs += "\t";
}
Label subSubject = new Label(tabs + subSet.getSubject());
Label subScore = new Label(scoreSubString);
Label subAttempts = new Label(attemptsSubString);
notenPanel.addComponent(subSubject);
notenPanel.addComponent(subAttempts);
notenPanel.addComponent(subScore);
}
}
/**
* Diese Methode ist dafür zuständig die Informationen des aktuellen Durchschnitts in das Notenpanel einzufügen
* @param user enthält die Daten des Users inklusive seiner Durchschnitte
* @param notenPanel Ist das Panel was für das neue Fenster erzeugt wurde. Alle Einträge müssen hierin gesetzt werden.
*/
private void drawAverage(User user, Panel notenPanel){
Label durchschnitt = new Label("Durchschnitt: ");
durchschnitt.addStyle(SGR.BOLD);
durchschnitt.setLayoutData(GridLayout.createLayoutData(
GridLayout.Alignment.BEGINNING, // Horizontal alignment in the grid cell if the cell is larger than the component's preferred size
GridLayout.Alignment.BEGINNING, // Vertical alignment in the grid cell if the cell is larger than the component's preferred size
true, // Give the component extra horizontal space if available
false, // Give the component extra vertical space if available
2, // Horizontal span
1));
Label note = new Label("" + user.getAverage());
note.addStyle(SGR.BOLD);
notenPanel.addComponent(durchschnitt);
notenPanel.addComponent(note);
}
/**
* Diese Methode zeichnet das Notenfenster mit Testfunktion.
* @param focus Gibt an welche Box im Fokus steht, wenn der Testscreen angelegt wird.
*/
private void createTestScreen(int focus){
if(!DataHandler.testfileExists()){
DataHandler.createTestfile();
DataHandler.createTestWpfCounter();
}
final BasicWindow window = new BasicWindow("Notenliste");
window.setHints(Arrays.asList(Window.Hint.FIXED_POSITION));
window.setPosition(new TerminalPosition(0,0));
Panel notenPanel = new Panel(new GridLayout(3));
Label head1 = new Label("Fach");
Label head2 = new Label("Versuch");
Label head3 = new Label("Note");
head1.addStyle(SGR.BOLD);
head2.addStyle(SGR.BOLD);
head3.addStyle(SGR.BOLD);
notenPanel.addComponent(head1);
notenPanel.addComponent(head2);
notenPanel.addComponent(head3);
HashMap<String, Score> testMap = DataHandler.getTestMap();
User user = DataHandler.getUser();
HashMap<Integer, ArrayList<Score>> sortedMap = sortSemester(testMap);
drawTestSemesters(window, sortedMap, notenPanel, 1, testMap, 0);
drawTestAverage(notenPanel);
Button exit = new Button("Exit", new Runnable(){
@Override
public void run(){
window.close();
}
});
notenPanel.addComponent(exit);
window.setComponent(notenPanel);
textGUI.addWindow(window);
window.setFocusedInteractable(setFocus(notenPanel, focus));
}
/**
* Diese Methode legt alle Informationen der einzelnen Semester an.
* @param window Ist das Fenster indem alle Module dargestellt werden.
* @param sortedMap Hier sind alle Semester nach ihrem Semester sortiert. Die Semester sind der Key und die Scores der entsprechende Value.
* @param notenPanel Ist das Panel was für das neue Fenster erzeugt wurde. Alle Einträge müssen hierin gesetzt werden.
* @param indent Gibt an wie weit ein Modul eingerückt werden muss.
* @param testMap Hier sind alle Testnoten gespeichert.
* @param boxCount Fügt jeder Box einen Index zu, damit sie beim Neuzeichnen des Fensters wiedergefunden werden kann.
* @return Gibt den aktuellen Index der Boxen zurück
*/
private int drawTestSemesters(Window window,
HashMap<Integer, ArrayList<Score>> sortedMap,
Panel notenPanel,
int indent,
HashMap<String,Score> testMap,
int boxCount){
for(int i = 0; i<=sortedMap.size(); i++){
HashMap<String,ArrayList<Score>> wpfMap = new HashMap<String, ArrayList<Score>>();
Label heading = null;
if(i == 0 && sortedMap.get(i) != null){
heading = new Label("Unbekanntes Semester");
}else if(sortedMap.get(i) != null){
heading = new Label(i + ". Semester");
}
if(heading != null){
heading.setLayoutData(GridLayout.createLayoutData(
GridLayout.Alignment.BEGINNING, // Horizontal alignment in the grid cell if the cell is larger than the component's preferred size
GridLayout.Alignment.BEGINNING, // Vertical alignment in the grid cell if the cell is larger than the component's preferred size
true, // Give the component extra horizontal space if available
false, // Give the component extra vertical space if available+
3, // Horizontal span
1)); // Vertical span
heading.addStyle(SGR.BOLD);
notenPanel.addComponent(heading);
for(final Score scoreSet: sortedMap.get(i)){
if(scoreSet.isWpf()){
if(wpfMap.get(scoreSet.getWpfTopic()) == null){
ArrayList<Score> wpfScore = new ArrayList<Score>();
wpfScore.add(scoreSet);
wpfMap.put(scoreSet.getWpfTopic(), wpfScore);
}else{
wpfMap.get(scoreSet.getWpfTopic()).add(scoreSet);
}
continue;
}
String scoreString;
if(scoreSet.getScore()==0.0){
scoreString = "-";
}else{
scoreString = "" + scoreSet.getScore();
}
String attemptsString;
if(scoreSet.getAttempts()==0){
attemptsString = "-";
}else{
attemptsString = "" + scoreSet.getAttempts();
}
String tabs = "";
for(int j = 0; j<indent; j++){
tabs += "\t";
}
Label subject = new Label(tabs + scoreSet.getSubject());
ComboBox<String> scoreBox = createScoreComboBox(scoreSet.hasSubScore());
Label attempts = new Label(attemptsString);
scoreBox.setReadOnly(true)
.addListener(changeScoreEvent(window, scoreSet, testMap, scoreBox, boxCount));
notenPanel.addComponent(subject);
notenPanel.addComponent(attempts);
if(!scoreString.contains("-")){
scoreBox.removeListener(changeScoreEventListener);
scoreBox.setSelectedItem(scoreString);
scoreBox.addListener(changeScoreEventListener);
}
notenPanel.addComponent(scoreBox);
boxCount++;
if(scoreSet.hasSubScore()){
boxCount = drawTestScores(window, scoreSet.getSubScore(), notenPanel, indent+1, testMap, boxCount);
}
}
boxCount = drawTestWpf(window, wpfMap, notenPanel, testMap, boxCount);
}
}
return boxCount;
}
/**
* Diese Methode fügt alle Untermodule hinzu.
* @param window Ist das Fenster indem alle Module dargestellt werden.
* @param scoreSet Hier sind alle Untermodule drin.
* @param notenPanel Ist das Panel was für das neue Fenster erzeugt wurde. Alle Einträge müssen hierin gesetzt werden.
* @param indent Gibt an wie weit ein Modul eingerückt werden muss.
* @param testMap Hier sind alle Testnoten gespeichert.
* @param boxCount Fügt jeder Box einen Index zu, damit sie beim Neuzeichnen des Fensters wiedergefunden werden kann.
* @return Gibt den aktuellen Index der Boxen zurück
*/
private int drawTestScores(Window window,
HashMap<String, Score> scoreSet,
Panel notenPanel,
int indent,
HashMap<String, Score> testMap,
int boxCount){
for(Map.Entry<String,Score> subMap: scoreSet.entrySet()){
final Score subSet = subMap.getValue();
String scoreSubString;
if(subSet.getScore()==0.0){
scoreSubString = "-";
}else{
scoreSubString = "" + subSet.getScore();
}
String attemptsSubString;
if(subSet.getAttempts()==0){
attemptsSubString = "-";
}else{
attemptsSubString = "" + subSet.getAttempts();
}
String tabs = "";
for(int j = 0; j<indent; j++){
tabs += "\t";
}
Label subSubject = new Label(tabs + subSet.getSubject());
ComboBox<String> scoreBox = createScoreComboBox(subSet.hasSubScore());
Label subAttempts = new Label(attemptsSubString);
scoreBox.setReadOnly(true)
.addListener(changeScoreEvent(window, subSet, testMap, scoreBox,boxCount));
notenPanel.addComponent(subSubject);
notenPanel.addComponent(subAttempts);
if(!scoreSubString.contains("-")){
scoreBox.removeListener(changeScoreEventListener);
scoreBox.setSelectedItem(scoreSubString);
scoreBox.addListener(changeScoreEventListener);
}
notenPanel.addComponent(scoreBox);
boxCount++;
}
return boxCount;
}
/**
* Diese Methode fügt alle Wahlpflichtfächer hinzu.
* @param window Ist das Fenster indem alle Module dargestellt werden.
* @param sortedMap Hier sind alle Module nach dem Semester geordnet. Semester sind der Key und Scores der Value.
* @param notenPanel Ist das Panel was für das neue Fenster erzeugt wurde. Alle Einträge müssen hierin gesetzt werden.
* @param indent Gibt an wie weit ein Modul eingerückt werden muss.
* @param testMap Hier sind alle Testnoten gespeichert.
* @param boxCount Fügt jeder Box einen Index zu, damit sie beim Neuzeichnen des Fensters wiedergefunden werden kann.
* @return Gibt den aktuellen Index der Boxen zurück
*/
private int drawTestWpf(Window window,
HashMap<String,
ArrayList<Score>> sortedMap,
Panel notenPanel,
HashMap<String, Score> testMap,
int boxCount){
Iterator<Map.Entry<String,ArrayList<Score>>> it = sortedMap.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String, ArrayList<Score>> pair = it.next();
String wpfTopic = pair.getKey();
ArrayList<Score> wpfList = pair.getValue();
Label heading = new Label("\t" + wpfTopic + "("+wpfList.get(0).getWpfWeight()[0]+" aus "+wpfList.get(0).getWpfWeight()[1]+")");
heading.setLayoutData(GridLayout.createLayoutData(
GridLayout.Alignment.BEGINNING, // Horizontal alignment in the grid cell if the cell is larger than the component's preferred size
GridLayout.Alignment.BEGINNING, // Vertical alignment in the grid cell if the cell is larger than the component's preferred size
true, // Give the component extra horizontal space if available
false, // Give the component extra vertical space if available
3, // Horizontal span
1)); // Vertical span
heading.addStyle(SGR.BOLD);
notenPanel.addComponent(heading);
for(Score scoreSet: wpfList){
String scoreString;
if(scoreSet.getScore()==0.0){
scoreString = "-";
}else{
scoreString = "" + scoreSet.getScore();
}
String attemptsString;
if(scoreSet.getAttempts()==0){
attemptsString = "-";
}else{
attemptsString = "" + scoreSet.getAttempts();
}
String tabs = "\t\t";
Label subject = new Label(tabs + scoreSet.getSubject());
ComboBox<String> scoreBox = createScoreComboBox(scoreSet.hasSubScore());
scoreBox.setReadOnly(true)
.addListener(changeScoreEvent(window, scoreSet, testMap, scoreBox, boxCount));
Label attempts = new Label(attemptsString);
notenPanel.addComponent(subject);
notenPanel.addComponent(attempts);
if(!scoreString.contains("-")){
scoreBox.removeListener(changeScoreEventListener);
scoreBox.setSelectedItem(scoreString);
scoreBox.addListener(changeScoreEventListener);
}
notenPanel.addComponent(scoreBox);
if(scoreSet.hasSubScore()){
drawScores(scoreSet.getSubScore(), notenPanel, 3);
}
boxCount++;
}
it.remove();
}
return boxCount;
}
/**
* Diese Methode stellt den Durchschnitt im Notentester dar
* @param notenPanel Ist das Panel in dem die Elemente für das Fenster hinzugefügt werden
*/
private void drawTestAverage(Panel notenPanel){
Label durchschnitt = new Label("Durchschnitt: ");
durchschnitt.addStyle(SGR.BOLD);
durchschnitt.setLayoutData(GridLayout.createLayoutData(
GridLayout.Alignment.BEGINNING, // Horizontal alignment in the grid cell if the cell is larger than the component's preferred size
GridLayout.Alignment.BEGINNING, // Vertical alignment in the grid cell if the cell is larger than the component's preferred size
true, // Give the component extra horizontal space if available
false, // Give the component extra vertical space if available
2, // Horizontal span
1));
Label note = new Label("" + DataHandler.getUser().getTestAverage());
note.addStyle(SGR.BOLD);
notenPanel.addComponent(durchschnitt);
notenPanel.addComponent(note);
}
/**
* Erstellt eine Liste mit möglichen Noten für Hauptmodule ohne Untermodule.
* @return Gibt eine Liste an Noten als String in Form einer ArrayList zurück.
*/
private ArrayList<String> createNonSubCandidates(){
ArrayList<String> collection = new ArrayList<String>();
collection.add("-");
String candidate = "";
for(int i = 1; i<6; i++){
candidate = i + ".";
if(i == 4 || i == 5){
candidate += "0";
collection.add(candidate);
}else{
for(int j = 0; j<3; j++){
if(j == 2){
candidate += ((j*3)+1);
collection.add(candidate);
candidate = i + ".";
}else{
candidate += j*3;
collection.add(candidate);
candidate = i + ".";
}
}
}
}
return collection;
}
/**
* Erstellt eine Liste mit möglichen Noten für Hauptmodule mit Untermodulen.
* @return Gibt eine Liste an Noten als String in Form einer ArrayList zurück.
*/
private ArrayList<String> createSubCandidates(){
ArrayList<String> collection = new ArrayList<String>();
collection.add("-");
String candidate = "";
for(int i = 1; i<6; i++){
candidate = i + ".";
if(i == 4 || i == 5){
candidate += "0";
collection.add(candidate);
}else{
for(int j = 0; j<10; j++){
candidate += j;
collection.add(candidate);
candidate = i + ".";
}
}
}
return collection;
}
/**
* Methode um das Programm zu starten. Es wird geprüft ob bereits Daten vorliegen.
* Wenn keine Daten vorliegen, wird ein Login Screen eingeblendet, damit die Daten erstellt werden können
*/
public void run(){
try{
screen.startScreen();
if(DataHandler.mainfileExists()){
createWelcomeWindows();
}else{
updateData(null, null);
createWelcomeWindows();
}
}catch(IOException e){
e.printStackTrace();
}
}
/**
* Beendet das Programm.
*/
public void close(){
if(screen != null){
try{
screen.stopScreen();
System.exit(0);
}catch(IOException e){
e.printStackTrace();
}
}
}
/**
* Sortiert alle Elemente der übergebenen Map nach den Semestern im Score Objekt.
* @param syllabusMap Ist eine Map, die alle Kurse mit allen dazugehörigen Informationen enthält.
* @return Es wird eine Map zurückgegeben, die als Key die Semester in Form von Integern hat und als Value eine ArrayList mit den dementsprechenden Modulen hat.
*/
private HashMap<Integer, ArrayList<Score>> sortSemester(HashMap<String, Score> syllabusMap){
HashMap <Integer, ArrayList<Score>> sorted = new HashMap<Integer, ArrayList<Score>>();
for(Map.Entry<String,Score> score: syllabusMap.entrySet()){
Score value = score.getValue();
ArrayList<Score> scoreList = null;
if(sorted.get(value.getSemester()) == null){
scoreList = new ArrayList<Score>();
}else{
scoreList = sorted.get(value.getSemester());
}
scoreList.add(value);
sorted.put(value.getSemester(), scoreList);
}
return sorted;
}
/**
* Erzeugt eine Box(ComboBox), die für den Notentester mögliche Noten zur Auswahl hat.
* @param hasSubScore gibt an ob eine Liste mit createNonSubCandidates oder mit createSubCandidates erstellt werden soll.
* @return gibt eine ComboBox mit den Noten als Auswahloption zurück.
*/
private ComboBox<String> createScoreComboBox(boolean hasSubScore){
ComboBox<String> scoreBox;
if(hasSubScore == false){
scoreBox = new ComboBox<String>(createNonSubCandidates());
}else{
scoreBox = new ComboBox<String>(createSubCandidates());
}
return scoreBox;
}
/**
* Wenn eine Note im Notentester geändert wird, dann wird diese in der testMap in Form einer Datei gespeichert, sofern sich die Note geändert hat.
* <br> Es wird ebenfalls überprüft, dass der User nicht mehr Wahlpflichtmodule testet, als es ihm vom Modulplan her möglich ist. Das wird mit updateTestWpf(...) realisiert.
* <br> Zum Schluss wird das Fenster komplett geschlossen und alles neu gezeichnet. Dabei wird der boxCount übergeben, um den Fokus wieder auf die zuletzt aufgerufene Box zu setzen.
* @param window Ist das TestScreen Fenster, was wieder geschlossen und neugezeichnet wird
* @param score Das Score Objekt enthält die Daten um zu entscheiden ob es sich um ein Wahlpflichtmodul handelt oder nicht.
* @param testMap Ist die Map, die alle Daten für den Notentester enthält
* @param scoreBox Ist die Box, die gerade selectiert ist.
* @param boxCount Gibt an welche Box selektiert wurde, damit diese dann wieder fokussiert werden kann.
* @return Gibt einen ComboBox.Listener zurück, der alle Werte nach dem ändern aktualisiert hat.
*/
private ComboBox.Listener changeScoreEvent(Window window, Score score, HashMap<String, Score> testMap, ComboBox<String> scoreBox, int boxCount){
changeScoreEventListener = new ComboBox.Listener(){
@Override public void onSelectionChanged(int selectedIndex, int previousSelection){
if(!score.isWpf()){
updateTestScore(score, testMap, scoreBox, selectedIndex, previousSelection);
DataHandler.updateTestMap(testMap);
DataHandler.updateTestAverage(Syllabus.updateAverage(testMap));
}else{
if(updateTestWpf(selectedIndex, previousSelection, score.getWpfTopic(), score)){
updateTestScore(score, testMap, scoreBox, selectedIndex, previousSelection);
DataHandler.updateTestMap(testMap);
DataHandler.updateTestAverage(Syllabus.updateAverage(testMap));
}else{
scoreBox.removeListener(this);
selectedIndex = previousSelection;
scoreBox.setSelectedIndex(previousSelection);
String module = "";
if(score.getWpfWeight()[0] > 1){
module = "Module";
}else{
module = "Modul";
}
MessageDialog.showMessageDialog(textGUI,
"Achtung", "Du kannst maximal " + score.getWpfWeight()[0]
+ " " + module +" in " + score.getWpfTopic() + "auswählen!"
, MessageDialogButton.OK);
scoreBox.addListener(changeScoreEvent(window, score, testMap, scoreBox, boxCount));
}
}
window.close();
createTestScreen(boxCount);
}
};
return changeScoreEventListener;
}
/**
* Erzeugt einen Auswahlbildschirm, bei dem abgefragt, ob der Nutzer die Interaktion wirklich durchführen möchte.
*/
private void createAreYouSureScreen(WindowBasedTextGUI textGUI){
MessageDialog message = new MessageDialogBuilder()
.setTitle("Tester Zurücksetzen")
.setText("Sind Sie sicher, dass Sie den Tester zurücksetzen wollen?")
.addButton(MessageDialogButton.Cancel)
.addButton(MessageDialogButton.OK)
.build();
Panel basePanel = (Panel) message.getComponent();
Panel buttonPanel = null;
for(Object obj: basePanel.getChildren()){
if(obj instanceof Panel){
buttonPanel = (Panel) obj;
}
}
Button okayButton = null;
for(Object obj: buttonPanel.getChildren()){
Button btn = (Button) obj;
if(btn.getLabel().equals("Ok")){
okayButton = btn;
}
}
okayButton.addListener(new Button.Listener(){
@Override
public void onTriggered(Button button){
DataHandler.removeTestFile();
}
});
message.showDialog(textGUI);
}
/**
* Überprüft ob die maximale Anzahl von Wahlpflichmodulen schon erreicht ist.
* @param selectedIndex Gibt an, welcher der neue gesetzte Index der Combobox is.
* @param previousSelection Gibt an, welcher der zuletzt gesetzte Index der Combobox war.
* @param wpfTopic Gibt an, unter welchem Thema das entsprechende Wahlpflichtmodul gezält wird.
* @param score Gibt an, welcher der entsprechende Score/Modul ist, über den selektiert wurde.
* @return Gibt "true" zurück, wenn ein Update durchgeführt wurde, ansonsten "false".
*/
private boolean updateTestWpf(int selectedIndex, int previousSelection, String wpfTopic, Score score){
boolean isUpdated = false;
User user = DataHandler.getUser();
if(user.getTestWpfCounter() == null){
user.createTestWpfCounter();
}
if(user.getTestWpfCounter().get(wpfTopic) == null){
user.getTestWpfCounter().put(wpfTopic, 0);
}
if(previousSelection != 0 && selectedIndex !=0){
isUpdated = true;
}else if((user.getTestWpfCounter().get(wpfTopic)<score.getWpfWeight()[0]) && previousSelection == 0 && previousSelection != selectedIndex){
user.increaseTestWpfCounter(wpfTopic);
isUpdated = true;
}else if(selectedIndex == 0 && previousSelection != selectedIndex){
user.decreaseTestWpfCounter(wpfTopic);
isUpdated = true;
}
DataHandler.writeUser(user);
return isUpdated;
}
/**
* Updated die Combobox entsprechend der Auswahl, sofern eine neue Auswahl getroffen wurde.
* <br> Wenn das Modul bisher ungetestet war, wird es als "gestestet" gesetzt und die Werte je nach dem geupdated.
* <br> Wenn das Modul ein Übermodul von Untermodulen ist, werden die Untermodule zurückgesetzt.
* <br> Wenn das Modul ein Untermodul ist, wird der Durchschnitt vom Übermodul angepasst, sofern alle Untermodule ihre Noten gesetzt haben.
* @param score Ist das entsprechende Modul mit all seinen Werten.
* @param testMap Ist die Map, die alle Module für den Notentester gespeichert hält.
* @param scoreBox Ist die Box, die das Event auslöst.
* @param selectedIndex Gibt an, welcher der neue gesetzte Index der Combobox is.
* @param previousSelection Gibt an, welcher der zuletzt gesetzte Index der Combobox war.
*/
private void updateTestScore(Score score, HashMap<String, Score> testMap, ComboBox<String> scoreBox, int selectedIndex, int previousSelection){
if(!score.isTested() && selectedIndex != previousSelection){
float testScore = 0.0f;
if(!scoreBox.getSelectedItem().equals("-")){
testScore = Float.parseFloat(scoreBox.getSelectedItem());
}
if(score.hasParentScore()){
testMap.get(score.getParentStudienElement()).getSubScore().get(score.getStudienElement()).setIsTested();
testMap.get(score.getParentStudienElement()).setIsTested();
testMap.get(score.getParentStudienElement()).getSubScore().get(score.getStudienElement()).setScore(testScore);
testMap.get(score.getParentStudienElement()).setScore(Syllabus.updateParentScore(testMap, score));
}else{
testMap.get(score.getStudienElement()).setIsTested();
testMap.get(score.getStudienElement()).setScore(testScore);
}
if(score.hasSubScore()){
Syllabus.resetSubScores(testMap, score);
}
}else if(selectedIndex != previousSelection){
float testScore = 0.0f;
if(!scoreBox.getSelectedItem().equals("-")){
testScore = Float.parseFloat(scoreBox.getSelectedItem());
}
if(score.hasParentScore()){
testMap.get(score.getParentStudienElement()).getSubScore().get(score.getStudienElement()).setScore(testScore);
testMap.get(score.getParentStudienElement()).setScore(Syllabus.updateParentScore(testMap, score));
}else{
testMap.get(score.getStudienElement()).setScore(testScore);
}
if(score.hasSubScore()){
Syllabus.resetSubScores(testMap, score);
}
}
}
/**
* Diese Funktion öffnet ein Eingabefenster, um die Logindaten für die Hochschule einzugeben.
* Dabei werden alle Fenster im Hintergrund geschlossen. Es wird nach Eingabe eine Verbindung zur Hochschule aufgebaut und versucht einen Modulplan(SyllabusMap) zu erstellen.
* Je nach möglichen Fehler, wird eine Vermutung für die Ursache des Fehlers ausgegeben.
* @param menueWindow Ist das Fenster, was das Menü enthält.
* @param averageWindow Ist das Fenster, was die Durchschnitte enthält.
*/
private void updateData(Window menueWindow, Window averageWindow){
if(menueWindow != null && averageWindow != null){
menueWindow.close();
averageWindow.close();
}
final Window updateWindow = new BasicWindow("Login");
Panel panel = new Panel(new GridLayout(2));
Label userLabel = new Label("HSMW-Username:");
Label passwordLabel = new Label("HSMW-Passwort:");
TextBox userText = new TextBox();
TextBox passwordText = new TextBox();
GridLayout gridLayout = (GridLayout) panel.getLayoutManager();
updateWindow.setPosition(new TerminalPosition(0,0));
gridLayout.setHorizontalSpacing(4);
passwordText.setMask('*');
userText.setPreferredSize(new TerminalSize(25,1));
passwordText.setPreferredSize(new TerminalSize(25,1));
panel.addComponent(new EmptySpace().setLayoutData(
GridLayout.createHorizontallyFilledLayoutData(2)
));
panel.addComponent(userLabel);
panel.addComponent(userText);
panel.addComponent(passwordLabel);
panel.addComponent(passwordText);
panel.addComponent(new Button("Abbruch", new Runnable(){
@Override
public void run(){
updateWindow.close();
if(menueWindow != null && averageWindow != null){
createWelcomeWindows();
}
}
}));
panel.addComponent(new Button("Okay", new Runnable(){
@Override
public void run(){
String username = userText.getText();
String password = passwordText.getText();
try{
Hsmw.createDataFromHSMW(username, password);
}catch(MalformedURLException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
MessageDialog.showMessageDialog(textGUI, "Ups", "Konnte die Internetseite nicht finden");
}catch(IOException e){
MessageDialog.showMessageDialog(textGUI, "Ups", "Das Lesen oder Schreiben ist schief gegangen");
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}catch(StringIndexOutOfBoundsException e){
MessageDialog.showMessageDialog(textGUI, "Ups", "Passwort vielleicht falsch?");
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}catch(Exception e){
MessageDialog.showMessageDialog(textGUI, "Ups", "Etwas ist schief gegangen");
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}finally{
updateWindow.close();
if(menueWindow != null && averageWindow != null){
createWelcomeWindows();
}
}
}
}));
updateWindow.setHints(Arrays.asList(Window.Hint.CENTERED));
updateWindow.setComponent(panel);
textGUI.addWindowAndWait(updateWindow);
}
/**
* Diese Funktion setzt den Fokus auf die zuletzt ausgewählte ComboBox, nachdem das Fenster neugezeichnet wurde.
* @param panel Enthält alle Elemente, die im Fenster gezeichnet wurden.
* @param focus Enthält die Nummer, welche Combobox zuletzt ausgewählt wurde.
* @return Gibt die Combobox zurück, die fokussiert werden soll.
*/
private ComboBox<String> setFocus(Panel panel, int focus){
int i = 0;
Collection<Component> components= panel.getChildren();
ComboBox<String> focusBox = null;
for(Component component: components){
if(component instanceof ComboBox<?> && i == focus){
@SuppressWarnings("unchecked")
ComboBox<String> box = (ComboBox<String>)component;
focusBox = box;
break;
}else if(component instanceof ComboBox<?>){
i++;
}
}
return focusBox;
}
}