-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logbook.java
988 lines (971 loc) · 32.6 KB
/
Logbook.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
package HAS_Tools;
// Kindlet imports
import com.amazon.kindle.kindlet.AbstractKindlet;
import com.amazon.kindle.kindlet.KindletContext;
import com.amazon.kindle.kindlet.ui.KindletUIResources.*;
import com.amazon.kindle.kindlet.ui.KMenu;
import com.amazon.kindle.kindlet.ui.KMenuItem;
import com.amazon.kindle.kindlet.ui.KRepaintManager;
import com.amazon.kindle.kindlet.input.Gestures;
import com.amazon.kindle.kindlet.ui.KOptionPane;
import com.amazon.kindle.kindlet.input.keyboard.*;
import com.amazon.kindle.kindlet.net.NetworkDisabledException;
// Utilities
import java.util.Calendar;
import java.util.ArrayList;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
// Ui Imports
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.ActionMap;
import javax.swing.Action;
import javax.swing.AbstractAction;
import javax.swing.SwingUtilities;
// awt imports
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.CardLayout;
//import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
//import java.awt.event.TextListener;
//import java.awt.event.TextEvent;
import java.awt.Point;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.Color;
import java.awt.Dimension;
// Process
import java.lang.Process;
import java.lang.Runtime;
public class Logbook extends AbstractKindlet {
public KindletContext ctx;
public Container root;
// Data
public String date;
public String save_date;
public static String dir = "/mnt/us/developer/APP Logbook/work/";
public String filename;
public FlightModel fmodel;
public Flight flight; // Flight being edited or Added
public String flight_backup;
public char[] def_crew = {'S',' ','A'};
// UI Components
public KRepaintManager screenManager;
public CardLayout card;
public JLabel logs_day_info;
public JTable logs_cells;
public JTextField name_input;
public JPanel name_buttons;
public JPanel alt_buttons;
public CardLayout card_notes;
public JPanel notes;
public JPanel notes_tandem;
public JPanel notes_solo;
public JPanel work_tug;
public JButton work_tug_but;
public JPanel work_crew;
public JButton work_crew_but;
public JPanel work_tandem;
public JButton work_tandem_but;
public void reCreate() {
// Remove all from root of ctx
//root.
this.create(ctx);
}
public void create(KindletContext context) {
this.ctx = context;
this.root = ctx.getRootContainer();
screenManager = KRepaintManager.getInstance();
get_date();
String[] crew_list = load_list(dir.concat("config/crew.csv"));
Flight.init(crew_list);
fmodel = new FlightModel(filename, save_date);
try {
// Set Fonts to Use
Font menu_font = ctx.getUIResources().getFont(KFontFamilyName.MONOSPACE,14);
Font table_font = ctx.getUIResources().getFont(KFontFamilyName.MONOSPACE,11);
Font edit_font = ctx.getUIResources().getFont(KFontFamilyName.MONOSPACE,12);
// Setup menu Once
KMenu menu = new KMenu();
menu.add("Save",new ActionListener() {
public void actionPerformed(ActionEvent e) {
fmodel.save();
KOptionPane.showMessageDialog(root,"Data Saved");
}
});
menu.add("Sync",new ActionListener() {
public void actionPerformed(ActionEvent e) {
fmodel.save();
if(python("sync.py")) {
KOptionPane.showMessageDialog(root,"Sync Completed");
} else KOptionPane.showMessageDialog(root,"Sync Failed");
}
});
menu.add("Update",new ActionListener() {
public void actionPerformed(ActionEvent e) {
fmodel.save();
if(python("update.py")) {
KOptionPane.showMessageDialog(root,"Update Completed");
reCreate();
} else KOptionPane.showMessageDialog(root,"Update Failed");
}
});
ctx.setMenu(menu);
// Setup Log UI
logs_day_info = new JLabel(date.concat(" Flights: ") // Info
.concat(Integer.toString(fmodel.getFlightCount())));
logs_day_info.setFont(menu_font);
logs_day_info.setBorder(new LineBorder(Color.black));
JPanel log = new JPanel(new BorderLayout()); // Log
logs_cells = new JTable(fmodel);
logs_cells.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
logs_cells.setRowSelectionAllowed(true);
logs_cells.setFont(table_font);
logs_cells.setRowHeight(36);
logs_cells.getColumnModel().getColumn(0).setPreferredWidth(54); // #
logs_cells.getColumnModel().getColumn(1).setPreferredWidth(308); // Name
logs_cells.getColumnModel().getColumn(2).setPreferredWidth(130); // Notes
logs_cells.getColumnModel().getColumn(3).setPreferredWidth(54); // Alt
logs_cells.getColumnModel().getColumn(4).setPreferredWidth(54); // Work
logs_cells.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
switch(e.getButton()) {
case Gestures.BUTTON_FLICK_NORTH :
case Gestures.BUTTON_FLICK_WEST :
fmodel.next_page();
break;
case Gestures.BUTTON_FLICK_SOUTH :
case Gestures.BUTTON_FLICK_EAST :
fmodel.prev_page();
break;
case Gestures.BUTTON_HOLD : // Enter Edit Mode
Point p = e.getPoint();
int row = logs_cells.rowAtPoint(p);
int col = logs_cells.columnAtPoint(p);
int flight_num = Integer.parseInt((String)logs_cells.getValueAt(row,0));
flight = (Flight)fmodel.fdata.get(flight_num-1); // Set Flight
flight_backup = flight.toString();
switch(col) {
case 0 : // # -> Delete Dialog
StringBuffer to_del = new StringBuffer();
to_del.append(Integer.toString(flight_num)).append(" ?\n");
to_del.append(' ').append(flight.name).append('\n');
to_del.append(' ').append(flight.notes).append('\n');
to_del.append(' ').append(flight.getAltstr()).append('\n');
to_del.append(' ').append(new String(flight.work));
int result = KOptionPane.showConfirmDialog(root,
"Delete flight #".concat(to_del.toString()),
"Deletion Confirmation",
KOptionPane.NO_YES_OPTIONS);
if(result==KOptionPane.YES_OPTION) {
fmodel.remove(flight_num);
update_log();
}
break;
case 1 : // Name
get_name(false);
break;
case 2 : // Notes
case 3 : // Altitude
case 4 : // Work Logs
get_other(false);
break;
default :
}
break;
case Gestures.BUTTON_GROW :
case Gestures.BUTTON_SHRINK :
case Gestures.BUTTON_TAP :
}
}
public void mouseReleased(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
});
logs_cells.getTableHeader().setBorder(new LineBorder(Color.black));
log.add(logs_cells.getTableHeader(),BorderLayout.NORTH);
log.add(logs_cells,BorderLayout.CENTER);
JButton tandem = new JButton("Add Tandem"); // Tandem
tandem.setFont(menu_font);
tandem.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
int row = fmodel.fdata.indexOf(flight);
switch(e.getButton()) {
case Gestures.BUTTON_HOLD : // Duplicate Flight
int pos = fmodel.fdata.size()-1;
while(pos>=0) { // Get next similar flight
Flight cur = (Flight)fmodel.fdata.get(pos--);
if(cur.solo==false) {
fmodel.addFlight(new Flight(cur.toString()));
screenManager.repaint(root,false);
return;
}
}
case Gestures.BUTTON_TAP : // Add Flight
flight = fmodel.createFlight(false,def_crew);
get_name(true);
break;
}
}
public void mouseReleased(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
});
JButton duplicate = new JButton("Copy"); // Duplicate
duplicate.setFont(menu_font);
duplicate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int[] sel_rows = logs_cells.getSelectedRows();
for(int i=0;i<sel_rows.length;i++) {
Flight sel = (Flight) fmodel.fdata.get(sel_rows[i]+fmodel.page*fmodel.page_size);
fmodel.addFlight(new Flight(sel.toString()));
screenManager.repaint(logs_cells,false);
}
if(sel_rows.length==0) KOptionPane.showMessageDialog(root,"No flight Selected");
}
});
JButton solo = new JButton("Add Solo"); // Solo
solo.setFont(menu_font);
solo.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
int row = fmodel.fdata.indexOf(flight);
switch(e.getButton()) {
case Gestures.BUTTON_HOLD : // Duplicate Flight
int pos = fmodel.fdata.size()-1;
while(pos>=0) { // Get next similar flight
Flight cur = (Flight)fmodel.fdata.get(pos--);
if(cur.solo==true) {
fmodel.addFlight(new Flight(cur.toString()));
screenManager.repaint(root,false);
return;
}
}
case Gestures.BUTTON_TAP : // Add Flight
flight = fmodel.createFlight(true,def_crew);
get_name(true);
break;
}
}
public void mouseReleased(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
});
JPanel log_buttons = new JPanel(new BorderLayout()); // Log Buttons
log_buttons.add(tandem, BorderLayout.WEST);
log_buttons.add(duplicate, BorderLayout.CENTER);
log_buttons.add(solo, BorderLayout.EAST);
// Name Card
ActionListener name_listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = e.getSource() instanceof JTextField ?
name_input.getText() : ((JButton)e.getSource()).getText();
if( name.equals("") ) return;
flight.name = name;
if(flight_backup==null) get_other(true); // Add Flight
else { // Edit Flight
card.show(root, "logs");
}
}
};
name_input = new JTextField();
name_input.setFont(edit_font);
name_input.addActionListener(name_listener);
name_input.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) { textValueChanged(e); }
public void insertUpdate(DocumentEvent e) { textValueChanged(e); }
public void removeUpdate(DocumentEvent e) { textValueChanged(e); }
public void textValueChanged(DocumentEvent e) {
Process match = null;
BufferedReader matches = null;
if(name_input.getText().equals("")) {
mod_recent_names();
return;
}
String[] cmd = {"/bin/sh", dir.concat(".dropbox/match_")
.concat(flight.solo?"solo":"tandem"),name_input.getText()};
try {
match = Runtime.getRuntime().exec(cmd);
matches = new BufferedReader(new InputStreamReader(match.getInputStream()));
String line; int i=0; Component[] buts = name_buttons.getComponents();
while((line=matches.readLine())!=null) {
buts[i].setEnabled(true);
((JButton)buts[i]).setText(line);
i++;
}
matches.close();
while(i<buts.length) {
buts[i++].setEnabled(false);
}
screenManager.repaint(name_buttons,false);
}
catch(IOException x) { x.printStackTrace();}
//catch(InterruptedException x) { match.destroy(); matches.close(); }
}
});
OnscreenKeyboardUtil.configure(name_input, OnscreenKeyboardUtil.KEYBOARD_MODE_INIT_CAP_ONCE);
JButton name_cancel = new JButton("Cancel");
name_cancel.setFont(edit_font);
name_cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
card.show(root, "logs");
}
});
JPanel name_head = new JPanel(new BorderLayout());
name_head.add(name_input, BorderLayout.CENTER);
name_head.add(name_cancel, BorderLayout.EAST);
name_buttons = new JPanel(new GridLayout(10,1));
for(int i=0;i<10;i++) {
JButton but = new JButton();
but.setFont(edit_font);
but.addActionListener(name_listener);
name_buttons.add(but);
}
JLabel name_keyboard_space = new JLabel("Keyboard Space",JLabel.CENTER);
name_keyboard_space.setPreferredSize(ctx.getOnscreenKeyboardManager().getSize());
// Other Card
String[] alt_button_text = {"X","Pat","2500","3500","Mile","10k"}; // Alt
alt_buttons = new JPanel();
for(int i=0;i<alt_button_text.length;i++){
JButton but = new JButton(alt_button_text[i]);
but.setFont(edit_font);
but.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int alt = flight.alt;
alt_buttons.getComponent(alt).setEnabled(true);
JButton but = (JButton)e.getSource();
mod_alt_button(but.getText().charAt(0),false);
}
});
alt_buttons.add(but);
}
String[] tandem_list = load_list(dir.concat("config/notes.tandem.csv"));
String[] solo_list = load_list(dir.concat("config/notes.solo.csv"));
notes_tandem = new JPanel(); // Notes
notes_solo = new JPanel();
for(int i=0;i<tandem_list.length;i++) {
JCheckBox but = new JCheckBox(tandem_list[i],false);
but.setFont(edit_font);
but.setPreferredSize(new Dimension(200,50));
notes_tandem.add(but);
}
for(int i=0;i<solo_list.length;i++) {
JCheckBox but = new JCheckBox(solo_list[i],false);
but.setFont(edit_font);
but.setPreferredSize(new Dimension(200,50));
notes_solo.add(but);
}
card_notes = new CardLayout();
notes = new JPanel();
notes.setLayout(card_notes);
notes.add(notes_tandem, "tandem");
notes.add(notes_solo, "solo");
//String[] crew_list = load_list(dir.concat("config/crew.csv"));
work_tug = new JPanel(new GridLayout(crew_list.length,1));// Work Logs
work_tug.add(new JLabel("Tug"));
work_tug.add(new JLabel());
work_crew = new JPanel(new GridLayout(crew_list.length,1));
work_crew.add(new JLabel("Crew"));
work_tandem = new JPanel(new GridLayout(crew_list.length,1));
work_tandem.add(new JLabel("Tan"));
work_tandem.add(new JLabel());
for(int i=1;i<crew_list.length;i++) {
JButton worker;
String initial = crew_list[i].substring(0,1);
char def_char = crew_list[i].charAt(2);
if(def_char=='@') { // Default Tandem Pilot
def_crew[2]=initial.charAt(0);
}
else if(def_char=='*') { // Default Tug Pilot
def_crew[0]=initial.charAt(0);
}
int index = crew_list[i].lastIndexOf(",");
if(crew_list[i].charAt(index+1)=='1') {
worker = new JButton(initial); worker.setFont(edit_font); work_crew.add(worker);
worker.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
work_crew_but.setEnabled(true);
work_crew_but = (JButton)e.getSource();
flight.work[1] = work_crew_but.getText().charAt(0);
work_crew_but.setEnabled(false);
}
});
}
if(crew_list[i].charAt(index-3)=='1') {
worker = new JButton(initial); worker.setFont(edit_font); work_tug.add(worker);
worker.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
work_tug_but.setEnabled(true);
work_tug_but = (JButton)e.getSource();
flight.work[0] = work_tug_but.getText().charAt(0);
work_tug_but.setEnabled(false);
}
});
}
if(crew_list[i].charAt(index-1)=='1') {
worker = new JButton(initial); worker.setFont(edit_font); work_tandem.add(worker);
worker.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
work_tandem_but.setEnabled(true);
work_tandem_but = (JButton)e.getSource();
flight.work[2] = work_tandem_but.getText().charAt(0);
work_tandem_but.setEnabled(false);
}
});
}
}
JPanel work_logs = new JPanel(new FlowLayout(FlowLayout.CENTER,30,0));
work_logs.add(work_tug);
work_logs.add(work_crew);
work_logs.add(work_tandem);
JPanel other_center = new JPanel(new GridLayout(1,2)); // Other Center
other_center.add(notes);
other_center.add(work_logs);
JButton other_save = new JButton("Save (Hold to Cancel)"); // Other Buttons
other_save.setFont(menu_font);
other_save.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
int row = fmodel.fdata.indexOf(flight);
switch(e.getButton()) {
case Gestures.BUTTON_HOLD : // Cancel Add/Eddit
if(flight_backup!=null)
fmodel.fdata.set(row,new Flight(flight_backup));
break;
case Gestures.BUTTON_TAP : // Save Changes
StringBuffer new_notes = new StringBuffer();
Component[] buts = flight.solo ?
notes_solo.getComponents():
notes_tandem.getComponents();
for(int i=0;i<buts.length;i++) {
JCheckBox but = (JCheckBox)buts[i];
if(but.isSelected())
new_notes.append(but.getText()).append("/");
}
if(new_notes.length() > 0)
flight.notes = new_notes.deleteCharAt(new_notes.length()-1).toString();
else flight.notes = "";
if(flight_backup==null) {
fmodel.addFlight(flight);
update_log();
} else fmodel.update_row(row);
break;
}
card.show(root,"logs");
alt_buttons.getComponent(flight.alt).setEnabled(true);
work_tug_but.setEnabled(true);
work_crew_but.setEnabled(true);
if(!flight.solo) work_tandem_but.setEnabled(true);
}
public void mouseReleased(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
});
JPanel log_card = new JPanel(new BorderLayout());
log_card.add(logs_day_info, BorderLayout.NORTH);
log_card.add(log, BorderLayout.CENTER);
log_card.add(log_buttons, BorderLayout.SOUTH);
JPanel name_card = new JPanel(new BorderLayout());
name_card.add(name_head, BorderLayout.NORTH);
name_card.add(name_buttons, BorderLayout.CENTER);
name_card.add(name_keyboard_space, BorderLayout.SOUTH);
JPanel other_card = new JPanel(new BorderLayout());
other_card.add(alt_buttons, BorderLayout.NORTH);
other_card.add(other_center, BorderLayout.CENTER);
other_card.add(other_save, BorderLayout.SOUTH);
card = new CardLayout();
root.setLayout(card);
root.add(log_card,"logs");
root.add(name_card,"name");
root.add(other_card,"other");
} catch (Throwable t) {
t.printStackTrace();
}
}
public void start() {
}
public void stop() { // On Sleep, USB, Exit
fmodel.save(); // Write Info to file
}
public void destroy() {
}
private String week_toString(int day) {
switch(day){
case Calendar.MONDAY : return "Mon";
case Calendar.TUESDAY : return "Tue";
case Calendar.WEDNESDAY : return "Wed";
case Calendar.THURSDAY : return "Thu";
case Calendar.FRIDAY : return "Fri";
case Calendar.SATURDAY : return "Sat";
case Calendar.SUNDAY : return "Sun";
default : return "";
}
}
private String month_toString(int month) {
switch(month) {
case Calendar.JANUARY : return "Jan";
case Calendar.FEBRUARY : return "Feb";
case Calendar.MARCH : return "Mar";
case Calendar.APRIL : return "Apr";
case Calendar.MAY : return "May";
case Calendar.JUNE : return "Jun";
case Calendar.JULY : return "Jul";
case Calendar.AUGUST : return "Aug";
case Calendar.SEPTEMBER : return "Sep";
case Calendar.OCTOBER : return "Oct";
case Calendar.NOVEMBER : return "Nov";
case Calendar.DECEMBER : return "Dec";
default : return "";
}
}
private void get_date() {
Calendar today = Calendar.getInstance();
date = new StringBuffer()
.append(" ")
.append( week_toString(today.get(Calendar.DAY_OF_WEEK)))
.append(", ")
.append(month_toString(today.get(Calendar.MONTH)))
.append(" ")
.append(Integer.toString(today.get(Calendar.DAY_OF_MONTH)))
.toString();
save_date = new StringBuffer()
.append(Integer.toString(today.get(Calendar.DAY_OF_MONTH)))
.append('-')
.append(month_toString(today.get(Calendar.MONTH)))
.toString();
filename = new StringBuffer(dir)
.append(Integer.toString(today.get(Calendar.YEAR)))
.append( (today.get(Calendar.MONTH)+1)<10 ?"-0":"-")
.append(Integer.toString(today.get(Calendar.MONTH)+1))
.append("-")
.append(Integer.toString(today.get(Calendar.DAY_OF_MONTH)))
.append(".csv")
.toString();
}
private void update_log() {
logs_day_info.setText(date.concat(" Flights: ")
.concat(Integer.toString(fmodel.getFlightCount())));
}
private void get_name(boolean add) {
if(add) {
flight_backup = null;
mod_recent_names();
} else {
Component[] buts = name_buttons.getComponents();
int pos = fmodel.fdata.size()-1;
for(int i=0;i<buts.length;i++) {
JButton but = (JButton) buts[i];
if(pos<0) {
but.setText("");
but.setEnabled(false);
}
}
name_input.setText(flight.name);
}
// initiate grep
card.show(root, "name");
name_input.requestFocus();
}
private void mod_recent_names() {
Component[] buts = name_buttons.getComponents();
int pos = fmodel.fdata.size()-1;
name_input.setText("");
for(int i=0;i<buts.length;i++) {
JButton but = (JButton) buts[i];
next_similar:
while(pos>=0) { // Get next similar flight
Flight cur = (Flight)fmodel.fdata.get(pos--);
if(flight.solo!=cur.solo) continue;
for(int j=0;j<i;j++) {
if(cur.name.equals(((JButton)buts[j]).getText()))
continue next_similar;
}
but.setEnabled(true);
but.setText(cur.name);
break;
}
if(pos<0) {
but.setText("");
but.setEnabled(false);
}
}
}
private void get_other(boolean add) {
mod_alt_button(' ',false);
mod_notes_list();
mod_work_log();
if(flight.solo) work_tandem.setVisible(false);
else work_tandem.setVisible(true);
card.show(root, "other");
}
private void mod_alt_button(char alt, boolean enable) {
switch(alt) {
case 'X' : flight.alt = 0; break;
case 'P' : flight.alt = 1; break;
case '2' : flight.alt = 2; break;
case '3' : flight.alt = 3; break;
case 'M' : flight.alt = 4; break;
case '1' : flight.alt = 5; break;
case ' ' : default : break;
}
alt_buttons.getComponent(flight.alt).setEnabled(enable);
}
private void mod_notes_list() {
Component[] buts = flight.solo ?
notes_solo.getComponents():
notes_tandem.getComponents();
for(int i=0;i<buts.length;i++) {
JCheckBox but = (JCheckBox)buts[i];
but.setSelected( flight.notes.indexOf(but.getText())!=-1 );
}
card_notes.show(notes,flight.solo ? "solo" : "tandem");
}
private void mod_work_log() {
Component[] buts = work_tug.getComponents();
for(int i=2;i<buts.length;i++) {
if(((JButton)buts[i]).getText().charAt(0)==flight.work[0]) {
work_tug_but = (JButton)buts[i]; work_tug_but.setEnabled(false); break;
}
}
buts = work_crew.getComponents();
for(int i=1;i<buts.length;i++) {
if(((JButton)buts[i]).getText().charAt(0)==flight.work[1]) {
work_crew_but = (JButton)buts[i]; work_crew_but.setEnabled(false); break;
}
}
if(flight.solo) return;
buts = work_tandem.getComponents();
for(int i=2;i<buts.length;i++) {
if(((JButton)buts[i]).getText().charAt(0)==flight.work[2]) {
work_tandem_but = (JButton)buts[i]; work_tandem_but.setEnabled(false); break;
}
}
}
private String[] load_list(String filename) {
try {
BufferedReader in = new BufferedReader(new FileReader(filename));
String line;
ArrayList list = new ArrayList(10);
while((line=in.readLine())!=null) {
list.add(line);
}
in.close();
return (String[]) list.toArray(new String[0]);
} catch(IOException e) {
String[] failed = {"Failed","To","Get","File",filename.substring(filename.lastIndexOf('/'))};
return failed;
}
}
private boolean python(String script) {
String[] cmd = {"/mnt/us/python/usr/bin/python2.7",dir.concat(".dropbox/").concat(script)};
Process python=null;
try {
ctx.getConnectivity().requestConnectivity(true);
ctx.setSubTitle("Running... ".concat(script));
python = Runtime.getRuntime().exec(cmd);
python.waitFor();
}
catch(NetworkDisabledException e) {
ctx.setSubTitle("No Network");
return false;
} catch(InterruptedException e) {
if(python!=null)python.destroy();
ctx.setSubTitle("");
return false;
} catch(IOException e) {
ctx.setSubTitle(script.concat(" Failed"));
return false;
}
ctx.setSubTitle(script.concat(" Succeded"));
return true;
}
}
class FlightModel extends AbstractTableModel {
private String[] col_names = {"#","Pilot","Notes","Alt","Wrk"};
public ArrayList fdata;
public String filename;
public String save_date;
public int page;
public int page_size = 16;
public FlightModel(String filename, String save_date) {
this.page = 0;
this.filename = filename;
this.save_date = save_date;
this.fdata = new ArrayList(30);
try{
BufferedReader in = new BufferedReader(new FileReader(filename));
String line;
while((line = in.readLine()) != null) {
this.fdata.add(new Flight(line));
}
in.close();
} catch(IOException e) {
System.err.println(e);
}
}
public void save() { this.save(this.filename); }
public void save(String filename) {
// Create String to Record
StringBuffer save = new StringBuffer();
int index = 0;
while(index < fdata.size()) {
save.append(save_date).append(this.fdata.get(index++).toString()).append('\n');
}
// Write to file
try {
BufferedWriter out = new BufferedWriter(new FileWriter(filename));
out.write(save.toString());
out.close();
} catch(IOException e) {
System.err.println(e);
}
}
public int getColumnCount() { return col_names.length; }
public int getRowCount() {
int count = fdata.size()-this.page*this.page_size;
return count < this.page_size ? count : this.page_size;
}
public int getFlightCount() { return fdata.size(); }
public String getColumnName(int col) { return col_names[col]; }
public Object getValueAt(int row, int col) {
switch(col) {
case 0 : return Integer.toString(row+1+(this.page*this.page_size));
case 1 : return ((Flight)fdata.get(row+this.page*this.page_size)).name;
case 2 : return ((Flight)fdata.get(row+this.page*this.page_size)).notes;
case 3 : return ((Flight)fdata.get(row+this.page*this.page_size)).getAltstr();
case 4 : return new String(((Flight)fdata.get(row+this.page*this.page_size)).work);
default: return "E";
}
}
public Class getColumnClass(int col) { return "".getClass(); }
public void prev_page() {
if(this.page>0) {
this.page--;
fireTableDataChanged();
}
}
public void next_page() {
if(this.fdata.size() > (this.page+1) * this.page_size) {
this.page++;
fireTableDataChanged();
}
}
public void setValueAt(Object value,int row,int col) {
((Flight)this.fdata.get(row+this.page*this.page_size)).setCol(col,(String)value);
fireTableCellUpdated(row-(this.page*this.page_size),col);
}
public void update_row(int urow) {
fireTableRowsUpdated(urow,urow);
}
public void remove(int flight_num) {
fdata.remove(flight_num-1);
int page_index = flight_num%this.page_size - 1;
if(flight_num - this.page*this.page_size > 0) { // Last Page
if(flight_num-1==this.fdata.size()) // Last Row
if(page_index == 0) prev_page();
else fireTableRowsDeleted(page_index,page_index);
else {
fireTableRowsDeleted(page_index,page_index);
fireTableRowsUpdated(page_index,this.fdata.size()-1-this.page*this.page_size);
}
} else fireTableRowsUpdated(page_index,19); // Current Page
}
public void addFlight(Flight newf) {
this.fdata.add(newf);
int size = this.fdata.size();
this.page = size!=0 ? (size-1)/this.page_size : 0;
if(size%this.page_size==1) {
fireTableDataChanged();
} else fireTableRowsInserted(this.fdata.size(),this.fdata.size());
}
public Flight createFlight(boolean solo,char[] def_crew) {
Flight newf = null;
for(int i=fdata.size()-1;i>=0;i--) { // Search for matching
newf = (Flight)fdata.get(i);
if(newf.solo==solo) break;
else newf = null;
}
if(newf!=null) {
newf = new Flight("","",newf.alt,newf.work);
} else {
if(solo) {
def_crew[2]=' ';
newf = new Flight("","",2,def_crew);
}
else newf = new Flight("","",2,def_crew);
}
return newf;
}
}
class Flight {
public String name;
public String notes;
public int alt;
public char[] work = new char[3];
public boolean solo;
private static char[] initials;
private static String[] names;
public static void init(String[] crew_list) {
initials = new char[crew_list.length];
names = new String[crew_list.length];
crew_list[1] = crew_list[1].substring(0,2)
.concat(crew_list[1].substring(crew_list[1].indexOf(',',2)));
for(int i=1;i<crew_list.length;i++) {
String line = crew_list[i];
initials[i] = line.charAt(0);
char first = line.charAt(2);
int s_i = first=='@'||first=='*' ? 3 : 2;
names[i] = line.substring(s_i,line.indexOf(',',s_i));
}
}
private static String i_name(char initial) {
for(int i=0;i<initials.length;i++)
if(initial==initials[i]) return names[i];
return "";
}
private static char name_i(String name) {
for(int i=0;i<names.length;i++)
if(name.equals(names[i])) return initials[i];
return ' ';
}
public Flight(String name, String notes, int alt, char[] wrk) {
this.name = name;
this.notes = notes;
this.alt = alt; // 0,1,2,3,4
this.work[0] = wrk[0];
this.work[1] = wrk[1];
this.work[2] = wrk[2];
this.solo = this.work[2]==' ';
}
public Flight( String data ) {
int prev_i = data.indexOf(','), next_i = data.indexOf(',',++prev_i);
this.name = data.substring(prev_i,next_i); // Name
prev_i = next_i + 1; next_i = data.indexOf(',',prev_i);
this.alt = Flight.alt_parse(data.substring(prev_i,next_i)); // Tandem Alt
prev_i = next_i + 1; next_i = data.indexOf(',',prev_i);
if(this.alt == -1) this.alt = Flight.alt_parse(data.substring(prev_i,next_i)); // Solo Alt
prev_i = next_i + 1; next_i = data.indexOf(',',prev_i);
switch(data.charAt(prev_i)) { // Tug Pilot
case '*' : prev_i++;
default :
this.work[0]= name_i(data.substring(prev_i,next_i));
}
prev_i = next_i + 1; next_i = data.indexOf(',',prev_i);
switch(data.charAt(prev_i)) { // Crew
case ',' : this.work[1]=' '; break;
default :
this.work[1]= name_i(data.substring(prev_i,next_i));
}
prev_i = next_i + 1; next_i = data.indexOf(',',prev_i);
switch(data.charAt(prev_i)) { // Tandem Pilot
case ',' : this.work[2]=' '; break;
case '@' : prev_i++;
default :
this.work[2]= name_i(data.substring(prev_i,next_i));
}
this.notes = data.substring(data.lastIndexOf(',')+1); // Notes
this.solo = this.work[2]==' ';
}
public void setCol(int col,String val) {
switch(col){
case 1 : // Name
this.name = val;
break;
case 2 : // Notes
this.notes = val;
break;
case 3 : // Alt
this.alt = Integer.parseInt(val);
break;
case 4 : // Work
this.work[0] = val.charAt(0);
this.work[1] = val.charAt(1);
this.work[2] = val.charAt(2);
break;
}
}
public String toString() {
String alt_str = "";
switch(this.alt) {
case 0 : alt_str = "0"; break;
case 1 : alt_str = "0.5"; break;
case 2 : alt_str = "1"; break;
case 3 : alt_str = "2"; break;
case 4 : alt_str = "4"; break;
}
StringBuffer str = new StringBuffer();
str.append(',').append(this.name).append(','); // Name
if(this.work[2]!=' ') {
str.append(alt_str).append(',').append(','); // Tandem Alt #
} else {
str.append(',').append(alt_str).append(','); // Solo Alt #
}
str.append(i_name(this.work[0])).append(','); // Tug Pilot
if(this.work[1]!=' ') str.append(i_name(this.work[1])); // Crew
str.append(',');
if(this.work[2]!=' ') str.append(i_name(this.work[2])); // Tandem Pilot
str.append(',');
str.append(this.notes); // Rental/Notes
return str.toString();
}
public String getAltstr(){
switch(this.alt) {
case 0 : return "X";
case 1 : return "Pat";
case 2 : return "25";
case 3 : return "35";
case 4 : return "1m";
case 5 : return "10k";
}
return "Err";
}
private static int alt_parse(String alt) {
if(alt.length()==0) { return -1; };
if(alt.length()>1)
switch(alt.charAt(0)) {
case '0': return 1; // 0.5
case '1': return 3; // 1.5 - 3500
}
switch(alt.charAt(0)) {
case '0' : return 0; // 0
case '1' : return 2; // 1
case '2' : return 4; // 2
case '4' : return 5; // 4
}
return -1;
}
}
/* --ToDo--
* -- Code --
* Add convert between solo/tandem
* fix reload all after update.py
* Add view/edit previous logs
* -- Scripts --
* Sync
* Add effortless Sync
* match_*
* case "s s"
*/