forked from serenapascual/cs151-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreatePromptView.java
More file actions
192 lines (182 loc) · 7.13 KB
/
CreatePromptView.java
File metadata and controls
192 lines (182 loc) · 7.13 KB
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
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author Himanshu Mehta, Serena Pascual and Cherie Sew
*
*/
public class CreatePromptView {
/**
* Pop up prompt for creating new event
* @param model - MVC model with list of events
*/
public CreatePromptView(final EventModel model) {
final JFrame frame = new JFrame();
final JPanel panel = new JPanel();
final Calendar cal = model.getCal();
final SimpleDateFormat startTime = new SimpleDateFormat("hh:mm aa");
final Calendar endTime = new GregorianCalendar();
endTime.setTime(cal.getTime());
endTime.add(Calendar.MINUTE, 30);
startTime.format(cal.getTime());
final JLabel title = new JLabel("Title: ");
final JTextField titleField = new JTextField("Untitled event",20);
final JLabel date = new JLabel("Date (MM/DD/YYYY): ");
final JTextField dateField = new JTextField((cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.YEAR), 20);
final JLabel start = new JLabel("Start Time:");
final JTextField startField = new JTextField(startTime.format(cal.getTime()), 20);
final JLabel end = new JLabel("End Time:");
final JTextField endField = new JTextField(startTime.format(endTime.getTime()),20);
final JButton saveButton = new JButton("Save");
final JButton cancelButton = new JButton("Cancel");
cancelButton.setOpaque(true);
cancelButton.setBorderPainted(false);
cancelButton.setFocusable(false);
cancelButton.setBackground(new Color(236, 151, 31));
cancelButton.setForeground(Color.WHITE);
cancelButton.setFont(new Font("Tahoma", Font.BOLD, 14));
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
saveButton.setOpaque(true);
saveButton.setBorderPainted(false);
saveButton.setFocusable(false);
saveButton.setBackground(new Color(68, 157, 68));
saveButton.setForeground(Color.WHITE);
saveButton.setFont(new Font("Tahoma", Font.BOLD, 14));
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String title = titleField.getText();
String date = dateField.getText();
String start = startField.getText();
String end = endField.getText();
// used to track whether an error dialog has popped up to prevent multiple
boolean dialogOpened = false;
int startHour = 0;
int startMin = 0;
int endHour = 0;
int endMin = 0;
String startAmPm = "";
String endAmPm = "";
while (startHour == 0 && startMin == 0 && endHour == 0 && endMin == 0) {
try {
String[] startArr = start.split(":");
startHour = Integer.parseInt(startArr[0]);
startMin = Integer.parseInt(startArr[1].substring(0, 2));
startAmPm = startArr[1].substring(2, 4);
String[] endArr = end.split(":");
endHour = Integer.parseInt(endArr[0]);
endMin = Integer.parseInt(endArr[1].substring(0, 2));
endAmPm = endArr[1].substring(2, 4);
}
catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(frame,
"Please enter a time in 12-hour format (HH:MMam/pm).",
"Invalid input",
JOptionPane.WARNING_MESSAGE);
dialogOpened = true;
}
}
// format start time and end time to 24-hour time
if (startHour == 12 && startAmPm.toUpperCase().equals("AM")) startHour = 0;
if (startHour == 12 && startAmPm.toUpperCase().equals("PM")) startHour = 12;
else if (startAmPm.toUpperCase().equals("PM")) startHour += 12;
if (endHour == 12 && endAmPm.toUpperCase().equals("AM")) endHour = 0;
if (endHour == 12 && endAmPm.toUpperCase().equals("PM")) endHour = 12;
else if (endAmPm.toUpperCase().equals("PM")) endHour += 12;
String startHourStr = "" + startHour;
String startMinStr = "" + startMin;
String endHourStr = "" + endHour;
String endMinStr = "" + endMin;
if (startHour < 10) {
startHourStr = "0" + startHour;
}
if (startMin < 10) {
startMinStr = "0" + startMin;
}
if (endHour < 10) {
endHourStr = "0" + endHour;
}
if (endMin < 10) {
endMinStr = "0" + endMin;
}
start = startHourStr + startMinStr;
end = endHourStr + endMinStr;
String[] dateArr = date.split("/");
int year = Integer.parseInt(dateArr[2]);
int day = Integer.parseInt(dateArr[1]);
int month = Integer.parseInt(dateArr[0]);
Event eventNew = new Event(title, year, month, day, start, end);
boolean conflict = false;
if (((startHour >= 0 && startHour <= 24) == false
|| (endHour >= 0 && endHour <= 24) == false
|| (startMin >= 0 && startMin <= 59) == false
|| (endMin >= 0 && endMin <= 59) == false)
&& dialogOpened == false) {
JOptionPane.showMessageDialog(frame,
"Please enter a time in 12-hour format (HH:MMam/pm).",
"Invalid input",
JOptionPane.WARNING_MESSAGE);
dialogOpened = true;
}
else if ((eventNew.getEnd().before(eventNew.getStart())
|| eventNew.getStart().equals(eventNew.getEnd()))
&& dialogOpened == false) {
JOptionPane.showMessageDialog(frame,
"The end time cannot come before the start time.",
"Time Conflict",
JOptionPane.WARNING_MESSAGE);
conflict = true;
dialogOpened = true;
}
for (Event events: model.getEvents()) {
if(events.compareTo(eventNew) == 0 && dialogOpened == false) {
JOptionPane.showMessageDialog(frame,
"Event times cannot overlap. Please check your existing events.",
"Time Conflict",
JOptionPane.WARNING_MESSAGE);
conflict = true;
dialogOpened = true;
break;
}
}
if(!conflict) {
model.addEvent(eventNew);
frame.dispose();
}
conflict = false;
dialogOpened = false;
}
});
titleField.selectAll();
panel.setLayout(new GridLayout(5, 2));
panel.add(title);
panel.add(titleField);
panel.add(date);
panel.add(dateField);
panel.add(start);
panel.add(startField);
panel.add(end);
panel.add(endField);
panel.add(saveButton);
panel.add(cancelButton);
frame.add(panel);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
}
}