-
Notifications
You must be signed in to change notification settings - Fork 0
/
Event.java
148 lines (121 loc) · 4.8 KB
/
Event.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
/**
* Name: Christina Black, Kelly Hutchison, Nicole Hart
* Date: 11/6/2012
* Section: 002
* Project: Calendar
*/
package calendar;
import calendar.MyButton;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
/**
* An event holds the date, time, and location of a user's
* desired Event
* @author Christina Black <cblack at psu.edu>
*/
public class Event extends JPanel implements ActionListener, ItemListener {
private static final String hours[] = {"", "24", "23", "22", "21", "20", "19", "18", "17", "16", "15",
"14", "13", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1"};
private static final String mins[] = {"","00","15","30","45"};
private JPanel days = new JPanel();
private JButton ok;
private JFrame frame = new JFrame();
protected JPanel textField = new JPanel();
private JTextField message;
private JComboBox startHour = new JComboBox(hours);
private JComboBox startMin = new JComboBox(mins);
private JComboBox endHour = new JComboBox(hours);
private JComboBox endMin = new JComboBox(mins);
protected String sHour = "";
protected String sMin = "";
protected String eHour = "";
protected String eMin = "";
protected String location;
private MyButton dayIndex;
private int curMonth;
private int year;
//Default Constructor
public Event(MyButton dayIndex, int curMonth, int year) {
frame.setLocation(430, 300);
textField.setBorder(BorderFactory.createEmptyBorder(20,0,0,0));
days.setBorder(BorderFactory.createEmptyBorder(10,5,10,5));
BorderLayout layout = new BorderLayout(10, 20);
setLayout(layout);
this.dayIndex = dayIndex;
this.curMonth = curMonth;
this.year = year;
message = new JTextField(25);
message.setText("Enter your event name here");
message.selectAll();
textField.add(message);
frame.add(textField, BorderLayout.NORTH);
days.add(new JLabel("Select the start time of your event"));
startHour.addItemListener(this);
startMin.addItemListener(this);
days.add(startHour);
days.add(startMin);
days.add(new JLabel("Select the end time of your event"));
endHour.addItemListener(this);
endMin.addItemListener(this);
days.add(endHour);
days.add(endMin);
frame.add(days, BorderLayout.CENTER);
ok = new JButton("OK");
ok.addActionListener(this);
frame.add(ok, BorderLayout.SOUTH);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(400, 300);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e){
if (e.getSource() == ok) {
try{
if("".equals(sHour) || "".equals(eHour) || "".equals(sMin) || "".equals(eMin)){
JOptionPane.showMessageDialog(null, "You must enter a time for your event"
+ "to start at!");
}
else if(Integer.parseInt(sHour) > Integer.parseInt(eHour)) {
throw new EventException("");
}
location = message.getText();
frame.setVisible(false);
frame.dispose();
// Store the alarm
DBManager.connect();
Connection myConnection = DBManager.getConnection();
Statement stmt = myConnection.createStatement();
stmt.execute("insert into event (name, startTime, endTime, datetime) values('" + location + "', '" + sHour + ":" + sMin + "', '"
+ eHour + ":" + eMin + "', '" + curMonth + dayIndex.getDay() + year + "')");
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
} catch (EventException ee) {
JOptionPane.showMessageDialog(null, "You must enter a start time"
+ " after your end time.");
}
}
}
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getSource() == startHour){
sHour = (String) startHour.getSelectedItem();
}
if(e.getSource() == endHour){
eHour = (String) endHour.getSelectedItem();
}
if(e.getSource() == startMin){
sMin = (String) startMin.getSelectedItem();
}
if(e.getSource() == endMin){
eMin = (String) endMin.getSelectedItem();
}
}
@Override
public String toString(){
return("EVENT: " + location + " at " + sHour + ":" + sMin + " to " + eHour + ":" + eMin);
}
}