C:\Users\Man\Desktop\IA_FinaL_Submission_000307-0092\Product\IA_Netbeans_Project\src\ia\man\Planner.java |
1
2
3
4
5
6 package ia.man;
7
8 import java.awt.BorderLayout;
9 import java.awt.Color;
10 import java.awt.Component;
11 import java.awt.Dimension;
12 import java.awt.Toolkit;
13 import java.io.BufferedReader;
14 import java.io.BufferedWriter;
15 import java.io.File;
16 import java.io.FileNotFoundException;
17 import java.io.FileReader;
18 import java.io.FileWriter;
19 import java.io.IOException;
20 import static java.lang.System.in;
21 import java.text.DateFormat;
22 import java.text.SimpleDateFormat;
23 import java.time.DayOfWeek;
24 import java.time.LocalDate;
25 import java.time.YearMonth;
26 import java.time.format.DateTimeFormatter;
27 import java.time.format.TextStyle;
28 import java.time.temporal.WeekFields;
29 import java.util.Date;
30 import java.util.Locale;
31 import java.util.Objects;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34 import javax.swing.BorderFactory;
35 import javax.swing.ButtonGroup;
36 import javax.swing.JButton;
37 import javax.swing.JFrame;
38 import javax.swing.JLabel;
39 import javax.swing.JOptionPane;
40 import javax.swing.JPanel;
41 import javax.swing.JScrollPane;
42 import javax.swing.JTable;
43 import javax.swing.ListSelectionModel;
44 import javax.swing.SwingConstants;
45 import javax.swing.UIManager;
46 import javax.swing.UnsupportedLookAndFeelException;
47 import javax.swing.WindowConstants;
48 import javax.swing.event.ListSelectionListener;
49 import javax.swing.table.DefaultTableCellRenderer;
50 import javax.swing.table.DefaultTableModel;
51 import javax.swing.table.JTableHeader;
52
53
54
55 @author
56
57
58 public class Planner extends javax.swing.JFrame {
59
60
61
62
63 User iUser = new User();
64
65 private Planner() {
66 throw new UnsupportedOperationException("Not supported yet.");
67 }
68
69 public Planner(String id,String user,String email,String pass) {
70
71 initComponents();
72
73 iUser.AssingValue(id, user, email, pass);
74
75 Toolkit toolkit = getToolkit();
76 Dimension size = toolkit.getScreenSize();
77 setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2);
78
79 setDefaultCloseOperation(Register.DISPOSE_ON_CLOSE);
80
81 ButtonGroup rgroup = new ButtonGroup();
82 rgroup.add(trivialRadioButton);
83 rgroup.add(moderateRadioButton);
84 rgroup.add(urgentRadioButton);
85
86 monthTable.setDefaultRenderer(LocalDate.class, new CalendarTableRenderer());
87 monthTable.setRowHeight(30);
88
89 tbTaskList.getColumnModel().getColumn(0).setMaxWidth(50);
90 tbTaskList.getColumnModel().getColumn(0).setMinWidth(50);
91 tbTaskList.getColumnModel().getColumn(0).setWidth(50);
92 tbTaskList.getColumnModel().getColumn(0).setPreferredWidth(50);
93
94 tbTaskList.getColumnModel().getColumn(7).setMaxWidth(0);
95 tbTaskList.getColumnModel().getColumn(7).setMinWidth(0);
96 tbTaskList.getColumnModel().getColumn(7).setWidth(0);
97 tbTaskList.getColumnModel().getColumn(7).setPreferredWidth(0);
98
99 updateMonthView(realLocalDate);
100
101 LocalDate ld = (LocalDate) monthTable.getValueAt(monthTable.getSelectedRow(), monthTable.getSelectedColumn());
102 DateFormat dateFormat = new SimpleDateFormat("MM-dd");
103 DateFormat yearFormat = new SimpleDateFormat("yyyy");
104 Date date = new Date();
105
106 String year = yearFormat.format(date);
107 int bc_year = Integer.parseInt(year) - 543;
108
109 String Current_Date = String.valueOf(bc_year) + "-" + dateFormat.format(date);
110
111 lblDate.setText(Current_Date);
112 getTask(Current_Date);
113
114 txtId.setVisible(false);
115
116 }
117
424 private void trivialRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
425
426 }
427
428 private void dateTFActionPerformed(java.awt.event.ActionEvent evt) {
429
430 }
431
432 private void addTaskButtonMouseClicked(java.awt.event.MouseEvent evt) {
433 try {
434 addTask();
435 } catch (IOException ex) {
436 Logger.getLogger(Planner.class.getName()).log(Level.SEVERE, null, ex);
437 }
438 }
439
440 private void prevMouseClicked(java.awt.event.MouseEvent evt) {
441
442 updateMonthView(getCurrentLocalDate().minusMonths(1));
443 }
444
445 private void nextMouseClicked(java.awt.event.MouseEvent evt) {
446
447 updateMonthView(getCurrentLocalDate().plusMonths(1));
448 }
449
450 private void monthTableMouseClicked(java.awt.event.MouseEvent evt) {
451
452
453 LocalDate ld = (LocalDate) monthTable.getValueAt(monthTable.getSelectedRow(), monthTable.getSelectedColumn());
454 lblDate.setText(ld.toString());
455 dateTF.setText(ld.toString());
456 getTask(ld.toString());
457
458 }
459
460 private void tbTaskListMouseClicked(java.awt.event.MouseEvent evt) {
461
462
463 String ID = tbTaskList.getValueAt(tbTaskList.getSelectedRow(),7).toString();
464 getTaskbyID(ID);
465
466 }
467
468 private void deleteButtonMouseClicked(java.awt.event.MouseEvent evt) {
469 try {
470 deleteTask();
471 } catch (IOException ex) {
472 Logger.getLogger(Planner.class.getName()).log(Level.SEVERE, null, ex);
473 }
474 }
475
476 private void finishedTaskButtonMouseClicked(java.awt.event.MouseEvent evt) {
477 try {
478 finishTask();
479 } catch (IOException ex) {
480 Logger.getLogger(Planner.class.getName()).log(Level.SEVERE, null, ex);
481 }
482 }
483
484
485
486
487 public final LocalDate realLocalDate = LocalDate.now();
488
489 private final JLabel monthLabel = new JLabel("", SwingConstants.CENTER);
490
491 private LocalDate currentLocalDate;
492
493 public LocalDate getCurrentLocalDate() {
494 return currentLocalDate;
495 }
496
497 public void updateMonthView(LocalDate localDate) {
498 currentLocalDate = localDate;
499 monthLabel.setText(localDate.format(DateTimeFormatter.ofPattern("YYYY / MM").withLocale(Locale.getDefault())));
500 monthTable.setModel(new CalendarViewTableModel(localDate));
501 }
502
503 private void tbTaskList(String[][] data, String[] header) {
504 throw new UnsupportedOperationException("Not supported yet.");
505 }
506
507 private class CalendarTableRenderer extends DefaultTableCellRenderer {
508 @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean selected, boolean focused, int row, int column) {
509 super.getTableCellRendererComponent(table, value, selected, focused, row, column);
510 setHorizontalAlignment(SwingConstants.CENTER);
511 if (value instanceof LocalDate) {
512 LocalDate d = (LocalDate) value;
513 setText(Objects.toString(d.getDayOfMonth()));
514 if (YearMonth.from(d).equals(YearMonth.from(getCurrentLocalDate()))) {
515 setForeground(Color.BLACK);
516 } else {
517 setForeground(Color.GRAY);
518 }
519 DayOfWeek dow = d.getDayOfWeek();
520 if (d.isEqual(realLocalDate)) {
521 setBackground(new Color(220, 255, 220));
522 } else if (dow == DayOfWeek.SUNDAY) {
523 setBackground(new Color(255, 220, 220));
524 } else if (dow == DayOfWeek.SATURDAY) {
525 setBackground(new Color(220, 220, 255));
526 } else {
527 setBackground(Color.WHITE);
528 }
529 }
530 return this;
531 }
532 }
533
534 public static void main(String args[]) {
535
536
537
538
539
540 try {
541 for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
542 if ("Nimbus".equals(info.getName())) {
543 javax.swing.UIManager.setLookAndFeel(info.getClassName());
544 break;
545 }
546 }
547 } catch (ClassNotFoundException ex) {
548 java.util.logging.Logger.getLogger(Planner.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
549 } catch (InstantiationException ex) {
550 java.util.logging.Logger.getLogger(Planner.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
551 } catch (IllegalAccessException ex) {
552 java.util.logging.Logger.getLogger(Planner.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
553 } catch (javax.swing.UnsupportedLookAndFeelException ex) {
554 java.util.logging.Logger.getLogger(Planner.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
555 }
556
557
558
559
560 java.awt.EventQueue.invokeLater(new Runnable() {
561 public void run() {
562 new Planner().setVisible(true);
563 }
564 });
565
566 }
567
568 public void addTask() throws FileNotFoundException, IOException{
569
570 String path = "Planner.txt";
571
572 String urgency = "";
573
574 if(trivialRadioButton.isSelected()){
575 urgency = "Trivial";
576 }else if(moderateRadioButton.isSelected()){
577 urgency = "Moderate";
578 }else if(urgentRadioButton.isSelected()){
579 urgency = "Urgent";
580 }
581
582 String taskRegister = addTaskTF.getText();
583 String dateOfTask = dateTF.getText();
584
585 int timeStart = timeFromComboBox.getSelectedIndex();
586 int timeEnd = timeToComboBox.getSelectedIndex();
587
588 BufferedReader reader = new BufferedReader(new FileReader("Planner.txt"));
589 int id = 0;
590 while (reader.readLine() != null) id++;
591 id += 1;
592 reader.close();
593
594 File file = new File(path);
595 FileWriter writer;
596 try {
597 writer = new FileWriter(file, true);
598 writer.write(String.valueOf(id));
599 writer.write("|");
600 writer.write(iUser.getID());
601 writer.write("|");
602 writer.write(dateOfTask);
603 writer.write("|");
604 writer.write(String.valueOf(timeStart));
605 writer.write("|");
606 writer.write(String.valueOf(timeEnd));
607 writer.write("|");
608 writer.write(urgency);
609 writer.write("|");
610 writer.write(taskRegister);
611 writer.write("|");
612 writer.write("No");
613
614
615
616
617
618 writer.write("\n");
619 writer.close();
620 System.out.println("Write success!");
621
622 int m = JOptionPane.showConfirmDialog(
623 null,
624 "Save Complete",
625 "Alert",
626 JOptionPane.YES_NO_OPTION);
627
628
629
630 getTask(dateOfTask);
631
632 } catch (IOException e) {
633
634 e.printStackTrace();
635 }
636 }
637
638
639 public void getTask(String pDate)
640 {
641
642 String path = "Planner.txt";
643
644 File file = new File(path);
645
646 String line = null,
647 id = null,
648 userId = null,
649 date = null,
650 startTime = null,
651 endTime = null,
652 urgency = null,
653 task_name = null,
654 status = null;
655
656 try {
657
658 BufferedReader br = new BufferedReader(new FileReader(file));
659 DefaultTableModel model = (DefaultTableModel)tbTaskList.getModel();
660 model.setRowCount(0);
661
662 int i = 0;
663 while ((line = br.readLine()) != null)
664 {
665
666 String[] array = line.split("\\|");
667
668 id = array[0];
669 userId = array[1];
670 date = array[2];
671
672 startTime = getTimeComboBox(Integer.parseInt(array[3]));
673 endTime = getTimeComboBox(Integer.parseInt(array[4]));
674
675 urgency = array[5];
676 task_name = array[6];
677 status = array[7];
678
679 if(date.equals(pDate))
680 {
681 model.addRow(new Object[0]);
682 model.setValueAt(i+1, i, 0);
683 model.setValueAt(date, i, 1);
684 model.setValueAt(startTime, i, 2);
685 model.setValueAt(endTime, i, 3);
686 model.setValueAt(urgency, i, 4);
687 model.setValueAt(task_name, i, 5);
688 model.setValueAt(status, i, 6);
689 model.setValueAt(id, i, 7);
690
691 i++;
692 }
693
694 array = null;
695
696 }
697
698 br.close();
699
700 } catch (IOException e) {
701 e.printStackTrace();
702 }
703
704 }
705
706
707 public void getTaskbyID(String ID)
708 {
709
710 String path = "Planner.txt";
711
712 File file = new File(path);
713
714 Boolean trivialVal = false,
715 moderateVal = false,
716 urgentVal = false;
717
718 String line = null,
719 id = null,
720 userId = null,
721 date = null,
722 urgency = null,
723 task_name = null,
724 status = null;
725
726 int startTime = 0,
727 endTime = 0;
728
729 try {
730
731 BufferedReader br = new BufferedReader(new FileReader(file));
732
733 while ((line = br.readLine()) != null)
734 {
735
736 String[] array = line.split("\\|");
737
738 id = array[0];
739 userId = array[1];
740 date = array[2];
741 startTime = Integer.parseInt(array[3]);
742 endTime = Integer.parseInt(array[4]);
743 urgency = array[5];
744 task_name = array[6];
745 status = array[7];
746
747
748 if(id.equals(ID))
749 {
750 txtId.setText(id);
751 addTaskTF.setText(task_name);
752 dateTF.setText(date);
753
754 if(urgency.equals("Trivial"))
755 {
756 trivialVal = true;
757 }
758 else if(urgency.equals("Moderate"))
759 {
760 moderateVal = true;
761 }
762 else
763 {
764 urgentVal = true;
765 }
766
767 trivialRadioButton.setSelected(trivialVal);
768 moderateRadioButton.setSelected(moderateVal);
769 urgentRadioButton.setSelected(urgentVal);
770 timeFromComboBox.setSelectedIndex(startTime);
771 timeToComboBox.setSelectedIndex(endTime);
772
773 }
774
775 array = null;
776
777 }
778
779 br.close();
780
781 } catch (IOException e) {
782
783 e.printStackTrace();
784 }
785
786 }
787
788
789 public String getTimeComboBox(int id)
790 {
791 String timeReturn = "";
792
793 String[] array = {
794 "00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00",
795 "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00",
796 "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00",
797 "21:00", "22:00", "23:00"
798 };
799
800 timeReturn = array[id];
801 return timeReturn;
802 }
803
804
805 public void deleteTask() throws IOException
806 {
807 String line = null,
808 id = null;
809
810 String pFileOld = "Planner.txt";
811 String pFileTemp = "Planner_temp.txt";
812
813 id = txtId.getText();
814
815 int n = JOptionPane.showConfirmDialog(
816 null,
817 "Delete Task ?",
818 "Alert",
819 JOptionPane.YES_NO_OPTION);
820
821 if(n==0)
822 {
823 File FileOld = new File(pFileOld);
824 File FileTemp = new File(pFileTemp);
825 BufferedWriter bw = new BufferedWriter( new FileWriter(FileTemp) );
826 BufferedReader br = new BufferedReader( new FileReader(FileOld) );
827 while((line = br.readLine()) != null)
828 {
829 String[] arrFile = line.split("\\|");
830
831 if(!arrFile[0].equals(id))
832 {
833 bw.write(line);
834 bw.flush();
835 bw.newLine();
836 }
837 }
838
839 bw.close();
840 br.close();
841
842 FileOld.delete();
843 boolean success = FileTemp.renameTo(FileOld);
844 }
845
846 getTask(lblDate.getText());
847
848 }
849
850
851 public void finishTask() throws IOException
852 {
853 String line = null,
854 id = null;
855
856 String pFileOld = "Planner.txt";
857 String pFileTemp = "Planner_temp.txt";
858
859 id = txtId.getText();
860
861 String urgency = "";
862
863 if(trivialRadioButton.isSelected()){
864 urgency = "Trivial";
865 }else if(moderateRadioButton.isSelected()){
866 urgency = "Moderate";
867 }else if(urgentRadioButton.isSelected()){
868 urgency = "Urgent";
869 }
870
871 String taskRegister = addTaskTF.getText();
872 String dateOfTask = dateTF.getText();
873
874 int timeStart = timeFromComboBox.getSelectedIndex();
875 int timeEnd = timeToComboBox.getSelectedIndex();
876
877 int n = JOptionPane.showConfirmDialog(
878 null,
879 "Finish Task ?",
880 "Alert",
881 JOptionPane.YES_NO_OPTION);
882
883
884 if(n==0)
885 {
886 File FileOld = new File(pFileOld);
887 File FileTemp = new File(pFileTemp);
888
889 BufferedWriter bw = new BufferedWriter( new FileWriter(FileTemp) );
890 BufferedReader br = new BufferedReader( new FileReader(FileOld) );
891
892 while((line = br.readLine()) != null)
893 {
894 String[] arrFile = line.split("\\|");
895
896 if(arrFile[0].equals(id))
897 {
898 bw.write(id+"|"+iUser.getID()+"|"+dateOfTask+"|"+String.valueOf(timeStart)+"|"+String.valueOf(timeEnd)+"|"+urgency+"|"+taskRegister+"|Yes");
899 }
900 else
901 {
902 bw.write(line);
903 }
904 bw.flush();
905 bw.newLine();
906 }
907
908 bw.close();
909 br.close();
910
911 FileOld.delete();
912 boolean success = FileTemp.renameTo(FileOld);
913
914 getTask(lblDate.getText());
915
916 }
917 }
918
919
920 // Variables declaration - do not modify
921 private javax.swing.JButton addTaskButton;
922 private javax.swing.JTextField addTaskTF;
923 private javax.swing.JLabel dateLabel;
924 private javax.swing.JTextField dateTF;
925 private javax.swing.JButton deleteButton;
926 private javax.swing.JButton finishedTaskButton;
927 private javax.swing.JPanel jPanel1;
928 private javax.swing.JPanel jPanel2;
929 private javax.swing.JScrollPane jScrollPane1;
930 private javax.swing.JScrollPane jScrollPane2;
931 private javax.swing.JLabel lblDate;
932 private javax.swing.JRadioButton moderateRadioButton;
933 private javax.swing.JTable monthTable;
934 private javax.swing.JButton next;
935 private javax.swing.JPanel p;
936 private javax.swing.JButton prev;
937 private javax.swing.JTable tbTaskList;
938 private javax.swing.JComboBox<String> timeFromComboBox;
939 private javax.swing.JLabel timeLabel;
940 private javax.swing.JLabel timeLabel1;
941 private javax.swing.JComboBox<String> timeToComboBox;
942 private javax.swing.JRadioButton trivialRadioButton;
943 private javax.swing.JTextField txtId;
944 private javax.swing.JLabel urgencyLabel;
945 private javax.swing.JRadioButton urgentRadioButton;
946 // End of variables declaration
947 }
948
949
950 class CalendarViewTableModel extends DefaultTableModel {
951
952 private final LocalDate startDate;
953 private final WeekFields weekFields = WeekFields.of(Locale.getDefault());
954
955 protected CalendarViewTableModel(LocalDate date) {
956 super();
957 LocalDate firstDayOfMonth = YearMonth.from(date).atDay(1);
958
959 int dowv = firstDayOfMonth.get(weekFields.dayOfWeek()) - 1;
960 startDate = firstDayOfMonth.minusDays(dowv);
961 }
962
963 @Override public Class<?> getColumnClass(int column) {
964 return LocalDate.class;
965 }
966
967 @Override public String getColumnName(int column) {
968 return weekFields.getFirstDayOfWeek().plus(column)
969 .getDisplayName(TextStyle.SHORT_STANDALONE, Locale.getDefault());
970 }
971
972 @Override public int getRowCount() {
973 return 6;
974 }
975
976 @Override public int getColumnCount() {
977 return 7;
978 }
979
980 @Override public Object getValueAt(int row, int column) {
981 return startDate.plusDays(row * getColumnCount() + column);
982 }
983
984 @Override public boolean isCellEditable(int row, int column) {
985 return false;
986 }
987
988 }
989
990
991
992