-
Notifications
You must be signed in to change notification settings - Fork 2
/
part4.java
804 lines (663 loc) · 34.5 KB
/
part4.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
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
public class part4 extends JFrame {
private JPanel cardHolder;
private CardLayout cards;
private static final String cardA = "A";
private static final String cardB = "B";
private static final String cardC = "C";
private static final String cardD = "D";
private static Dimension d = new Dimension(820, 600);
private static String STOPS_PATH = "inputs/stops.txt";
private static String STOP_TIMES_PATH = "inputs/stop_times.txt";
private static String TRANSFERS_PATH = "inputs/transfers.txt";
// private static part1 part1 = new part1(STOPS_PATH, STOP_TIMES_PATH,
// TRANSFERS_PATH);
// private static part2 part2 = new part2(STOPS_PATH);
private static part1 part1;
private static part2 part2;
private static part3 part3;
public static void initParts() throws IOException {
try {
File stops = new File(STOPS_PATH);
File stop_times = new File(STOP_TIMES_PATH);
File transfers = new File(TRANSFERS_PATH);
part1 = new part1(STOPS_PATH, STOP_TIMES_PATH, TRANSFERS_PATH);
part2 = new part2(STOPS_PATH);
part3 = new part3(STOP_TIMES_PATH);
} catch (FileNotFoundException fe) {
System.out.println("Please make sure that your input file path is correct.");
}
}
private class Switcher implements ActionListener {
String card;
Switcher(String card) {
this.card = card;
}
@Override
public void actionPerformed(ActionEvent e) {
cards.show(cardHolder, card);
}
}
private JPanel makePart1Panel() {
try {
// part1 part1 = new part1();
final JLabel label2 = new JLabel();
label2.setHorizontalAlignment(JLabel.CENTER);
label2.setVerticalAlignment(JLabel.CENTER);
label2.setSize(55, 10);
label2.setText("Page 2");
JPanel part1Panel = new JPanel();
JButton buttonNextB = new JButton("Next");
buttonNextB.setBounds(10, 100, 90, 20);
JButton buttonBackB = new JButton("Back");
buttonBackB.setBounds(600, 100, 90, 20);
// String[] columnLabels = {
// "stop_id","stop_code","stop_name","stop_desc","stop_lat","stop_lon","zone_id","stop_url","location_type","parent_station"};
// String[] stopColumnLabels = getColumnNames(stops);
// String[] columnLabels = { "order", "a", "b", "c", "d" };
part1.setupGraphFiles();
String[] stopColumnLabels = part1.getColumnNamesFromStops();
String[] columnLabels = { "order", stopColumnLabels[0], stopColumnLabels[1], stopColumnLabels[2],
stopColumnLabels[6] };
String[][] tableData = new String[20][5];
DefaultTableModel dtm = new DefaultTableModel(tableData, columnLabels);
JTable table = new JTable(dtm);
JTableHeader header = table.getTableHeader();
String fg_color = "#ffffff";
String bg_color = "#000000";
header.setBackground(Color.decode(bg_color));
header.setForeground(Color.decode(fg_color));
table.setShowHorizontalLines(false);
table.setShowVerticalLines(true);
table.setGridColor(Color.decode(bg_color));
JScrollPane scrollPane = new JScrollPane(table);
// JScrollBar vScroll = scrollPane.getVerticalScrollBar();
table.setLayout(new BorderLayout());
int N_ROWS = tableData.length;
Dimension d = new Dimension(800, N_ROWS * table.getRowHeight());
table.setPreferredScrollableViewportSize(d);
TableColumn column = null;
// column = table.getColumnModel().getColumn(0);
// column.setPreferredWidth(columnLabels[0].length() * 10);
for (int i = 0; i < columnLabels.length; i++) {
column = table.getColumnModel().getColumn(i);
column.setPreferredWidth(columnLabels[i].length() * 10);
}
for (int i = 0; i < N_ROWS; i++) {
dtm.addRow(tableData[i]);
}
part1Panel.add(scrollPane, BorderLayout.CENTER);
final JLabel cost_label = new JLabel();
// count_label.setBounds(250, -15, 100, 20);
cost_label.setHorizontalAlignment(JLabel.CENTER);
cost_label.setVerticalAlignment(JLabel.CENTER);
cost_label.setSize(table.getWidth(), table.getHeight() * 160 / 100);
final JLabel path_label = new JLabel();
path_label.setHorizontalAlignment(JLabel.CENTER);
path_label.setVerticalAlignment(JLabel.CENTER);
path_label.setSize(table.getWidth(), table.getHeight() * 160 / 100);
JLabel error_label = new JLabel();
error_label.setSize(table.getWidth(), table.getHeight() * 160 / 100);
error_label.setForeground(Color.RED);
error_label.setHorizontalAlignment(JLabel.CENTER);
error_label.setVerticalAlignment(JLabel.CENTER);
JTextField tf1 = new JTextField(7);
JTextField tf2 = new JTextField(7);
tf1.setBounds(225, 300, 150, 20);
tf2.setBounds(425, 300, 150, 20);
JLabel fromLabel = new JLabel();
fromLabel.setText("From Stop ID");
fromLabel.setBounds(225, 400, 500, 20);
fromLabel.setHorizontalAlignment(JLabel.CENTER);
fromLabel.setVerticalAlignment(JLabel.CENTER);
JLabel toLabel = new JLabel();
toLabel.setText("Dest. Stop ID");
toLabel.setBounds(225, 400, 500, 20);
toLabel.setHorizontalAlignment(JLabel.CENTER);
toLabel.setVerticalAlignment(JLabel.CENTER);
JButton inputButton = new JButton("Shortest Path");
inputButton.setBackground(Color.BLACK);
inputButton.setForeground(Color.RED);
buttonNextB.addActionListener(new Switcher(cardC));
buttonBackB.addActionListener(new Switcher(cardA));
part1Panel.add(buttonNextB);
part1Panel.add(fromLabel);
part1Panel.add(tf1);
part1Panel.add(toLabel);
part1Panel.add(tf2);
part1Panel.add(inputButton);
part1Panel.add(buttonBackB);
part1Panel.add(cost_label);
part1Panel.add(path_label);
part1Panel.add(error_label);
inputButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int fromStopID = Integer.parseInt(tf1.getText());
int toStopID = Integer.parseInt(tf2.getText());
if (part1.routes.isValidStopId(fromStopID) && part1.routes.isValidStopId(toStopID)) {
System.out
.println("Inputs start stop - " + tf1.getText() + " dest stop - " + tf2.getText());
// int fromStopID = 71;
// int toStopID = 646;
// printShortestPathInfo(fromStopID, toStopID);
ArrayList<Integer> shortestPath = part1.routes.getShortestPath(fromStopID, toStopID);
ArrayList<Stop> enrouteDetails = part1.routes.getEnrouteStops(shortestPath);
String[][] tableData = new String[enrouteDetails.size()][5];
double shortestCost = part1.routes.getShortestPathCost();
if (shortestCost == Double.POSITIVE_INFINITY) {
path_label.setText("No route from from " + fromStopID + " to " + toStopID);
} else if (shortestCost == Double.NEGATIVE_INFINITY) {
path_label.setText("both from and to have the same stop IDs");
} else if (shortestCost == -1.0) {
// System.out.println("Invalid input");
throw new IllegalArgumentException();
} else {
for (int i = 0; i < enrouteDetails.size(); i++) {
Stop stop = enrouteDetails.get(i);
tableData[i][0] = String.valueOf(i + 1);
tableData[i][1] = (stop.stop_id == -1) ? "" : String.valueOf(stop.stop_id);
tableData[i][2] = (stop.stop_code == -1) ? "" : String.valueOf(stop.stop_code);
tableData[i][3] = stop.stop_name;
tableData[i][4] = stop.zone_id;
}
String[][] tempStopsData = new String[20][5];
if (tableData.length < 20) {
for (int i = 0; i < tableData.length; i++) {
tempStopsData[i] = tableData[i];
}
tableData = tempStopsData;
}
dtm.setDataVector(tableData, columnLabels);
String costLabelStr = "The cost associated with moving from " + fromStopID + " to "
+ toStopID + " is " + shortestCost;
String pathStr = "";
for (int i = 0; i < shortestPath.size(); i++) {
pathStr.concat(String.valueOf(shortestPath.get(i)));
// System.out.print(shortestPath.get(i));
if (i != shortestPath.size() - 1) {
pathStr.concat(" -> ");
// System.out.print(" -> ");
}
}
error_label.setText("");
cost_label.setText(costLabelStr);
// path_label.setText(pathStr);
// System.out.println();
}
} else
throw new IllegalArgumentException();
} catch (NumberFormatException nfe) {
String errorMsg = "Invalid input, please check the stop IDs you have entered.";
// System.out.println(errorMsg);
cost_label.setText("");
path_label.setText("");
error_label.setText(errorMsg);
String[][] emptyData = new String[20][columnLabels.length];
dtm.setDataVector(emptyData, columnLabels);
} catch (IllegalArgumentException iae) {
String errorMsg = "Invalid input, please check the stop IDs you have entered.";
// System.out.println(errorMsg);
cost_label.setText("");
path_label.setText("");
error_label.setText(errorMsg);
String[][] emptyData = new String[20][columnLabels.length];
dtm.setDataVector(emptyData, columnLabels);
}
}
});
return part1Panel;
} catch (IOException ib) {
return null;
}
}
private JPanel makePart2Panel() {
try {
// partpart2 = new part2(STOPS_PATH);
JPanel part2Panel = new JPanel();
part2Panel.setPreferredSize(d);
part2Panel.setMaximumSize(part2Panel.getMaximumSize());
JButton buttonNextC = new JButton("Next");
buttonNextC.setBounds(10, 100, 90, 20);
JButton buttonBackC = new JButton("Back");
buttonBackC.setBounds(600, 100, 90, 20);
buttonNextC.addActionListener(new Switcher(cardD));
buttonBackC.addActionListener(new Switcher(cardB));
JButton inputButton = new JButton("Show");
inputButton.setBackground(Color.BLACK);
inputButton.setForeground(Color.RED);
inputButton.setBounds(625, 300, 90, 20);
String[] columnLabels = part2.getColumnNamesFromStopTimes();
String[][] tableData = new String[20][10];
DefaultTableModel dtm = new DefaultTableModel(tableData, columnLabels);
JTable table = new JTable(dtm);
// to get the label name when hovered on
ToolTipHeader tooltipHeader = new ToolTipHeader(table.getColumnModel());
tooltipHeader.setToolTipStrings(columnLabels);
table.setTableHeader(tooltipHeader);
// table.getTableHeader().setOpaque(false);
// table.getTableHeader().setBackground(Color.blue);
JTableHeader header = table.getTableHeader();
String fg_color = "#ffffff";
String bg_color = "#000000";
header.setBackground(Color.decode(bg_color));
header.setForeground(Color.decode(fg_color));
table.setShowHorizontalLines(false);
table.setShowVerticalLines(true);
table.setGridColor(Color.decode(bg_color));
JScrollPane scrollPane = new JScrollPane(table);
// JScrollBar vScroll = scrollPane.getVerticalScrollBar();
table.setLayout(new BorderLayout());
int N_ROWS = tableData.length;
Dimension d = new Dimension(800, N_ROWS * table.getRowHeight());
table.setPreferredScrollableViewportSize(d);
TableColumn column = null;
// column = table.getColumnModel().getColumn(0);
// column.setPreferredWidth(columnLabels[0].length() * 10);
for (int i = 0; i < columnLabels.length; i++) {
column = table.getColumnModel().getColumn(i);
column.setPreferredWidth(columnLabels[i].length() * 10);
}
for (int i = 0; i < N_ROWS; i++) {
dtm.addRow(tableData[i]);
}
part2Panel.add(scrollPane, BorderLayout.CENTER);
final JLabel label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);
label.setText("Search stop name");
// label.crossVerticalAlignment(JLabel.CENTER);s
// label.setBounds(300, -35, 100, 20);
label.setSize(table.getWidth(), table.getHeight() * 135 / 100);
// f.add(label,BorderLayout.CENTER);
final JLabel count_label = new JLabel();
// count_label.setBounds(250, -15, 100, 20);
count_label.setHorizontalAlignment(JLabel.CENTER);
count_label.setVerticalAlignment(JLabel.CENTER);
count_label.setSize(table.getWidth(), table.getHeight() * 160 / 100);
JLabel error_label = new JLabel();
error_label.setSize(table.getWidth(), table.getHeight() * 160 / 100);
error_label.setForeground(Color.RED);
error_label.setHorizontalAlignment(JLabel.CENTER);
error_label.setVerticalAlignment(JLabel.CENTER);
JTextField tf1 = new JTextField(20);
tf1.setBounds(425, 300, 150, 20);
tf1.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent fe) {
if (tf1.getText().equals("Search Stop Name")) {
tf1.setText("");
}
}
@Override
public void focusLost(FocusEvent fe) {
if (tf1.getText().isEmpty()) {
tf1.setText("Search Stop Name");
}
}
});
Map<String, ArrayList<String[]>> stopDetails = part2.createNameDetailsMapFromStops();
inputButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String input = tf1.getText();
Pattern p = Pattern.compile("[^a-z0-9\\- ]", Pattern.CASE_INSENSITIVE); // in order to check for
// an empty string input
Matcher m = p.matcher(input);
boolean b = m.find();
if ((!input.equals("") && !input.equals(" ")) && !b) {
if (input.length() > 1) {
if (input.substring(0, 2).equals(" "))
throw new IllegalArgumentException();
;
}
String displayData = "Input String - " + input;
System.out.println(displayData);
String searchInput = part2.removeSpacesAndCapitlize(input);
String[] searchResults = part2.STOP_NAMES_TST.search(searchInput);
if (searchResults == null || searchResults.length == 0) {
String errorMessage = "Sorry, There doesn't seem to be any buses at the time you have selected";
count_label.setText("");
error_label.setHorizontalAlignment(JLabel.CENTER);
error_label.setText(errorMessage);
System.out.println(errorMessage);
// dtm.setDataVector(null, columnLabels);
String[][] emptyData = new String[20][columnLabels.length];
dtm.setDataVector(emptyData, columnLabels);
}
ArrayList<String[]> lines = new ArrayList<>();
for (String res : searchResults) {
ArrayList<String[]> details = stopDetails.get(res);
for (String[] d : details) {
// String[] temp = new String[columnLabels.length];
// for(int i = 0; i < temp.length; i++) {
// temp[i] = (d[i]=="" || d[i]==null || d[i]==" ") ? " " : d[i];
// }
lines.add(d);
}
}
if (lines.size() == 0)
throw new NullPointerException();
String[][] stopsData;
stopsData = new String[lines.size()][columnLabels.length];
for (int i = 0; i < stopsData.length; i++) {
stopsData[i] = lines.get(i);
}
String[][] tempStopsData = new String[20][columnLabels.length];
if (stopsData.length < 20) {
for (int i = 0; i < stopsData.length; i++) {
tempStopsData[i] = stopsData[i];
}
stopsData = tempStopsData;
}
int stopsCount = searchResults.length;
error_label.setText("");
count_label.setText("\nThere seem to be " + stopsCount
+ " matching bus stops with the your input text");
dtm.setDataVector(stopsData, columnLabels);
} else
throw new IllegalArgumentException();
} catch (NullPointerException np) {
String errorMessage = "Sorry, There doesn't seem to be any bus stops that matches your input.";
error_label.setHorizontalAlignment(JLabel.CENTER);
// count_label.alignCenter();
error_label.setText(errorMessage);
count_label.setText("");
// System.out.println(errorMessage);
// dtm.setDataVector(null, columnLabels);
String[][] emptyData = new String[20][columnLabels.length];
dtm.setDataVector(emptyData, columnLabels);
} catch (IllegalArgumentException ie) {
String errorMsg = "Please enter something valid";
// System.out.println("print something valid");
error_label.setText(errorMsg);
count_label.setText("");
String[][] emptyData = new String[20][columnLabels.length];
dtm.setDataVector(emptyData, columnLabels);
} catch (StringIndexOutOfBoundsException s) {
System.out.println("please enter some more characters");
}
}
});
part2Panel.add(buttonNextC);
part2Panel.add(label);
part2Panel.add(tf1);
part2Panel.add(inputButton);
part2Panel.add(buttonBackC);
part2Panel.add(count_label);
part2Panel.add(error_label);
return part2Panel;
} catch (IOException ib) {
return null;
}
}
private JPanel makePart3Panel() {
try {
Map<String, ArrayList<String>> arrivalTimes_String = part3.createSortedArrivalTimeMapFromStopTimes();
JPanel part3Panel = new JPanel();
part3Panel.setPreferredSize(d);
part3Panel.setMaximumSize(part3Panel.getMaximumSize());
JButton buttonNextD = new JButton("Home");
buttonNextD.setBounds(10, 100, 90, 20);
JButton buttonBackD = new JButton("Back");
buttonBackD.setBounds(600, 100, 90, 20);
buttonNextD.addActionListener(new Switcher(cardA));
buttonBackD.addActionListener(new Switcher(cardC));
JButton timeInputButton = new JButton("Show");
timeInputButton.setBackground(Color.BLACK);
timeInputButton.setForeground(Color.RED);
timeInputButton.setBounds(625, 300, 90, 20);
String hours[] = { " 0", " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", " 10", " 11",
" 12", " 13", " 14", " 15", " 16", " 17", " 18", " 19", " 20", " 21", " 22", " 23" };
String minutes_seconds[] = { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "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" };
final JLabel hoursLabel = new JLabel();
hoursLabel.setBounds(45, 300, 90, 20);
hoursLabel.setText("Hours");
final JComboBox hoursInput = new JComboBox(hours);
hoursInput.setBounds(85, 300, 90, 20);
final JLabel minutesLabel = new JLabel();
minutesLabel.setBounds(235, 300, 90, 20);
minutesLabel.setText("Minutes");
final JComboBox minutesInput = new JComboBox(minutes_seconds);
minutesInput.setBounds(285, 300, 90, 20);
final JLabel secondsLabel = new JLabel();
secondsLabel.setBounds(435, 300, 90, 20);
secondsLabel.setText("Seconds");
final JComboBox secondsInput = new JComboBox(minutes_seconds);
secondsInput.setBounds(485, 300, 90, 20);
String[] columnLabels = part3.getColumnNamesFromStopTimes();
// String[] columnLabels = new String[10];
String[][] tableData = new String[20][10];
DefaultTableModel dtm = new DefaultTableModel(tableData, columnLabels);
JTable table = new JTable(dtm);
// taken from
// https://www.tutorialspoint.com/how-to-set-a-tooltip-to-each-column-of-a-jtableheader-in-java
// to get the label name when hovered on
ToolTipHeader tooltipHeader = new ToolTipHeader(table.getColumnModel());
tooltipHeader.setToolTipStrings(columnLabels);
table.setTableHeader(tooltipHeader);
// table.getTableHeader().setOpaque(false);
// table.getTableHeader().setBackground(Color.blue);
JTableHeader header = table.getTableHeader();
String fg_color = "#ffffff";
String bg_color = "#000000";
header.setBackground(Color.decode(bg_color));
header.setForeground(Color.decode(fg_color));
table.setShowHorizontalLines(false);
table.setShowVerticalLines(true);
table.setGridColor(Color.decode(bg_color));
JScrollPane scrollPane = new JScrollPane(table);
table.setLayout(new BorderLayout());
int N_ROWS = tableData.length;
Dimension td = new Dimension(800, N_ROWS * table.getRowHeight());
table.setPreferredScrollableViewportSize(td);
TableColumn column = null;
for (int i = 0; i < columnLabels.length; i++) {
column = table.getColumnModel().getColumn(i);
column.setPreferredWidth(columnLabels[i].length() * 10);
}
for (int i = 0; i < N_ROWS; i++) {
dtm.addRow(tableData[i]);
}
final JLabel label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);
label.setSize(table.getWidth(), table.getHeight() * 135 / 100);
final JLabel count_label = new JLabel();
count_label.setHorizontalAlignment(JLabel.CENTER);
count_label.setVerticalAlignment(JLabel.CENTER);
count_label.setSize(table.getWidth(), table.getHeight() * 160 / 100);
part3Panel.add(scrollPane, BorderLayout.CENTER);
timeInputButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String time = hoursInput.getItemAt(hoursInput.getSelectedIndex()) + ":"
+ minutesInput.getItemAt(minutesInput.getSelectedIndex()) + ":"
+ secondsInput.getItemAt(secondsInput.getSelectedIndex());
String displayData = "Time you've input is " + time;
count_label.setText("");
label.setText(displayData);
System.out.println(displayData);
ArrayList<String> lines = new ArrayList<String>();
lines = arrivalTimes_String.get(time);
// printTripDetailsFromList(lines);
String[][] tripsData;
try {
tripsData = new String[lines.size()][columnLabels.length];
for (int i = 0; i < tripsData.length; i++) {
String line = lines.get(i);
String lineWithoutBraces = line.substring(1, line.length() - 1);
String[] values = lineWithoutBraces.split(",");
for (int j = 0; j < tripsData[i].length; j++) {
try {
tripsData[i][j] = values[j];
} catch (ArrayIndexOutOfBoundsException aio) {
tripsData[i][j] = " ";
// for some lines in the file where the last elements is missing
}
}
}
int busesCount = tripsData.length;
count_label.setText("There seem to be " + busesCount + " buses arriving at " + time);
tripsData = part3.sortTripsBasedOnID(tripsData); // sorting trips based on their id using bubble
// sort
// for
// this
dtm.setDataVector(tripsData, columnLabels);
} catch (NullPointerException np) {
String errorMessage = "Sorry, There doesn't seem to be any buses at the time you have selected";
count_label.setHorizontalAlignment(JLabel.CENTER);
// count_label.alignCenter();
count_label.setText(errorMessage);
System.out.println(errorMessage);
// dtm.setDataVector(null, columnLabels);
String[][] emptyData = new String[20][columnLabels.length];
dtm.setDataVector(emptyData, columnLabels);
}
}
});
part3Panel.add(buttonNextD);
part3Panel.add(hoursLabel);
part3Panel.add(hoursInput);
part3Panel.add(minutesLabel);
part3Panel.add(minutesInput);
part3Panel.add(secondsLabel);
part3Panel.add(secondsInput);
part3Panel.add(timeInputButton);
part3Panel.add(buttonBackD);
part3Panel.add(count_label);
// pd.add(label);
// pd.add(label4);
return part3Panel;
} catch (IOException ig) {
return null;
}
}
private JPanel makeIntroPanel() {
try {
final JLabel label1 = new JLabel();
label1.setHorizontalAlignment(JLabel.CENTER);
label1.setVerticalAlignment(JLabel.CENTER);
// label1.setSize(55, 10);
// label1.setPrefferedSize(200, 40);
Dimension d1 = new Dimension(820, 150);
label1.setPreferredSize(d1);
label1.setText("CSU22012: Data Structures and Algorithms Group Project");
label1.setFont(new Font("Myriad Pro", Font.PLAIN, 25));
JPanel pa = new JPanel();
JButton buttonPartA = new JButton("Part 1");
buttonPartA.setFont(new Font("Myriad Pro", Font.PLAIN, 20));
JButton buttonPartB = new JButton("Part 2");
buttonPartB.setFont(new Font("Myriad Pro", Font.PLAIN, 20));
JButton buttonPartC = new JButton("Part 3");
buttonPartC.setFont(new Font("Myriad Pro", Font.PLAIN, 20));
buttonPartA.addActionListener(new Switcher(cardB));
buttonPartB.addActionListener(new Switcher(cardC));
buttonPartC.addActionListener(new Switcher(cardD));
BufferedImage myPicture = ImageIO.read(
new File("Demo Pictures and Videos/TranslinkLogo.png"));
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
Dimension d2 = new Dimension(820, 250);
picLabel.setPreferredSize(d2);
final JLabel paddingLabel1 = new JLabel();
paddingLabel1.setHorizontalAlignment(JLabel.CENTER);
paddingLabel1.setVerticalAlignment(JLabel.CENTER);
paddingLabel1.setText(" ");
final JLabel paddingLabel2 = new JLabel();
paddingLabel2.setHorizontalAlignment(JLabel.CENTER);
paddingLabel2.setVerticalAlignment(JLabel.CENTER);
paddingLabel2.setText(" ");
pa.add(label1);
pa.add(picLabel);
pa.add(buttonPartA);
pa.add(paddingLabel1);
pa.add(buttonPartB);
pa.add(paddingLabel2);
pa.add(buttonPartC);
// pa.setBackground(Color.CYAN);
return pa;
} catch (IOException ioe) {
return null;
}
}
private void run() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
// pa.setLayout(null);
// pa.setSize(820, 600);
JPanel pa = makeIntroPanel();
JPanel pb = makePart1Panel();
JPanel pc = makePart2Panel();
JPanel pd = makePart3Panel();
cardHolder = new JPanel();
cards = new CardLayout();
cardHolder.setLayout(cards);
cardHolder.add(pa, cardA);
cardHolder.add(pb, cardB);
cardHolder.add(pc, cardC);
cardHolder.add(pd, cardD);
cardHolder.setPreferredSize(d);
cardHolder.setMaximumSize(cardHolder.getPreferredSize());
// cardHolder.setSize(820, 600);
add(cardHolder);
pack();
setVisible(true);
}
public static void main(String[] args) {
try {
initParts();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
new part4().run();
}
});
} catch (Exception ex) {
System.out.println("Unable to open GUI");
}
}
}
// taken from
// https://www.tutorialspoint.com/how-to-set-a-tooltip-to-each-column-of-a-jtableheader-in-java
// implementation code to set a tooltip text to each column of JTableHeader
class ToolTipHeader extends JTableHeader {
String[] toolTips;
public ToolTipHeader(TableColumnModel model) {
super(model);
}
public String getToolTipText(MouseEvent e) {
int col = columnAtPoint(e.getPoint());
int modelCol = getTable().convertColumnIndexToModel(col);
String retStr;
try {
retStr = toolTips[modelCol];
} catch (NullPointerException ex) {
retStr = "";
} catch (ArrayIndexOutOfBoundsException ex) {
retStr = "";
}
if (retStr.length() < 1) {
retStr = super.getToolTipText(e);
}
return retStr;
}
public void setToolTipStrings(String[] toolTips) {
this.toolTips = toolTips;
}
}