-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.java
156 lines (135 loc) · 4.09 KB
/
Controller.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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Controller {
//Variables--------------------------------------------------
private Model m;
private View v;
private static SimpleDateFormat MonthDYYYY = new SimpleDateFormat("MMMM d, YYYY"); //printing out the date in "[Name of Month] [Date], [YYYY]"
private static SimpleDateFormat DMonthDYYYY = new SimpleDateFormat("EEEE MM/d/YYYY");
private static SimpleDateFormat fileFormatter = new SimpleDateFormat("MMMM_YYYY");
private static SimpleDateFormat Month = new SimpleDateFormat("MMMM"); //printing out the date in "[Name of Month] [Date], [YYYY]"
private static Pattern importer = Pattern.compile("\\{Date:\\s(\\d{8})\\t\\tTask:\\s(.*)\\}");
//Methods----------------------------------------------------
public void addModel(Model model)
{
this.m = model;
}
public void addView(View view)
{
this.v = view;
}
public void addBoth(Model model, View view)
{
this.m = model;
this.v = view;
}
public void updateTF()
{
v.drawTF();
}
public void changeSelectedDay(String s)
{
GregorianCalendar selectedMonth = m.getSelectedMonth();
selectedMonth.set(selectedMonth.get(Calendar.YEAR), selectedMonth.get(Calendar.MONTH), Integer.parseInt(s));
m.setSelectedDay(selectedMonth);
//System.out.println(MonthDYYYY.format(new Date(selectedMonth.get(Calendar.YEAR)-1900, selectedMonth.get(Calendar.MONTH), selectedMonth.get(Calendar.DATE))));
v.drawTF();
}
public void addMonth()
{
GregorianCalendar g = this.m.getSelectedMonth();
g.add(Calendar.MONTH, 1);
this.m.setSelectedMonth(g);
this.v.drawCF();
}
public void subMonth()
{
GregorianCalendar g = this.m.getSelectedMonth();
g.add(Calendar.MONTH, -1);
this.m.setSelectedMonth(g);
this.v.drawCF();
}
public void addYear()
{
GregorianCalendar g = this.m.getSelectedMonth();
g.add(Calendar.YEAR, 1);
this.m.setSelectedMonth(g);
this.v.drawCF();
}
public void subYear()
{
GregorianCalendar g = this.m.getSelectedMonth();
g.add(Calendar.YEAR, -1);
this.m.setSelectedMonth(g);
this.v.drawCF();
}
public String selectedMonthToString()
{
return Month.format(new Date(0,this.m.getSelectedMonth().get(Calendar.MONTH)+1,0));
}
public String formatToday()
{
GregorianCalendar today = m.getToday();
return MonthDYYYY.format(new Date(today.get(Calendar.YEAR)-1900, today.get(Calendar.MONTH), today.get(Calendar.DATE)));
}
public String formatTaskDate() //used when updating the TaskView Frame
{
GregorianCalendar selectedDay = m.getSelectedDay();
return DMonthDYYYY.format(new Date(selectedDay.get(Calendar.YEAR)-1900, selectedDay.get(Calendar.MONTH), selectedDay.get(Calendar.DATE)));
}
public void export()
{
GregorianCalendar selectedDay = m.getSelectedDay();
String fileName = fileFormatter.format(new Date(selectedDay.get(Calendar.YEAR)-1900, selectedDay.get(Calendar.MONTH), selectedDay.get(Calendar.DATE)));
Path p = Paths.get(fileName+".txt");
ArrayList<String> s = m.exportList();
try {
Files.write(p, s, Charset.forName("UTF-8"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void importer(String s)
{
if(this.m == null)
{
return;
}
try {
Scanner reader = new Scanner(new File(s));
while(reader.hasNext())
{
String toConvert = reader.nextLine();
Matcher m = importer.matcher(toConvert);
if(m.matches())
{
this.m.addTask(new Dates(m.group(2), Integer.parseInt(m.group(1))));
}
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return;
}
}
//Constructors-----------------------------------------------
public Controller()
{
m = null;
v = null;
}
}