aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suca/CalendarApplet.java
blob: 329c180a09e3c19196b074e27e5e5cda9605163c (plain)
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
package net.cbaines.suca;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;

import javax.security.auth.login.LoginException;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import net.fortuna.ical4j.data.CalendarBuilder;
import net.fortuna.ical4j.data.ParserException;
import net.fortuna.ical4j.model.Calendar;

@SuppressWarnings("serial")
public class CalendarApplet extends JApplet {

	private String calendarCSVString = null;
	private String calendarICALString = null;

	private JTabbedPane stageTabbedPane;

	// The panel used for the file fetch stuff
	private JPanel csvFileFetchPanel;

	private JTextField usernameTextField;
	private JPasswordField passwordTextField;
	private JButton go;

	private JProgressBar fetchProgress;

	private JButton csvFileSelectorButton;

	private JTextArea csvTextArea;

	private CalendarApplet instance;

	// Panel for the stage controls

	private JPanel controlsPanel;

	// Panel for the calendar creation

	private JPanel calendarCreationPanel;

	private JPanel calendarCreationOptions;

	private JProgressBar creationProgress;
	private JButton createCalendarButton;
	private JCheckBox mergeDuplicateEventsWithSeperateLocations = new JCheckBox(
			"Merge Duplicate Events with Seperate Locations", true);
	private JCheckBox showTerms = new JCheckBox("Show Terms", true);
	private JCheckBox showSemesters = new JCheckBox("Show Semesters", true);
	private JCheckBox showSemesterWeeks = new JCheckBox("Show Semester Weeks", true);
	private JCheckBox mergeHeaders = new JCheckBox("Merge Headers", false);

	// This is more of a development feature....?
	private JTextArea calendarPreviewTextArea;

	public void init() {
		// Execute a job on the event-dispatching thread:
		// creating this applet's GUI.
		try {
			javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					createGUI();
				}
			});
		} catch (Exception e) {
			System.err.println("createGUI didn't successfully complete");
		}
	}

	void createGUI() {
		try {

			stageTabbedPane = new JTabbedPane();

			// Create the first screen

			csvFileFetchPanel = new JPanel(new GridLayout(1, 2));

			JTabbedPane csvFileFetchMethodsTabbedPane = new JTabbedPane();

			JPanel loginPanel = new JPanel();
			JLabel usernameLabel = new JLabel("Username");
			loginPanel.add(usernameLabel);
			usernameTextField = new JTextField(10);
			loginPanel.add(usernameTextField);

			JLabel passwordLabel = new JLabel("Password");
			loginPanel.add(passwordLabel);
			passwordTextField = new JPasswordField(10);
			loginPanel.add(passwordTextField);

			go = new JButton("Go");
			loginPanel.add(go);

			fetchProgress = new JProgressBar();
			fetchProgress.setIndeterminate(true);
			fetchProgress.setVisible(false);
			loginPanel.add(fetchProgress);

			csvFileFetchMethodsTabbedPane.addTab("Download", loginPanel);

			JPanel csvFileLoadPanel = new JPanel();

			csvFileSelectorButton = new JButton("Load csv file");
			csvFileLoadPanel.add(csvFileSelectorButton);

			csvFileFetchMethodsTabbedPane.addTab("Load", csvFileLoadPanel);
			csvFileFetchMethodsTabbedPane.setEnabledAt(1, false);

			csvFileFetchPanel.add(csvFileFetchMethodsTabbedPane);

			csvTextArea = new JTextArea(100, 100);
			csvFileFetchPanel.add(new JScrollPane(csvTextArea));

			go.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					go.setEnabled(false);
					new FetchThread().start();
				}
			});

			stageTabbedPane.addTab("Calendar Fetch", csvFileFetchPanel);

			// Calendar creation panel
			calendarCreationPanel = new JPanel(new GridLayout(1, 2));

			calendarCreationOptions = new JPanel();

			createCalendarButton = new JButton("Create Calendar");
			createCalendarButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					createCalendarButton.setEnabled(false);
					new CreationThread().start();
				}
			});
			calendarCreationOptions.add(createCalendarButton);

			JButton saveCalendarButton = new JButton("Save Calendar");
			saveCalendarButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {

					JFileChooser fc = new JFileChooser();
					int returnVal = fc.showSaveDialog(instance); // this = Applet
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						File file = fc.getSelectedFile();

						try {
							BufferedWriter out = new BufferedWriter(new FileWriter(file));
							out.write(calendarICALString);
							out.flush();
							out.close();
						} catch (IOException e) {
							e.printStackTrace();
						}

					}

				}
			});
			calendarCreationOptions.add(saveCalendarButton);

			calendarCreationOptions.add(mergeDuplicateEventsWithSeperateLocations);
			calendarCreationOptions.add(showTerms);
			calendarCreationOptions.add(showSemesters);
			calendarCreationOptions.add(showSemesterWeeks);
			calendarCreationOptions.add(mergeHeaders);
			creationProgress = new JProgressBar();
			creationProgress.setIndeterminate(true);
			creationProgress.setVisible(false);
			calendarCreationOptions.add(creationProgress);

			calendarCreationPanel.add(calendarCreationOptions);

			calendarPreviewTextArea = new JTextArea();

			calendarCreationPanel.add(new JScrollPane(calendarPreviewTextArea));

			stageTabbedPane.addTab("Calendar Creation", calendarCreationPanel);

			controlsPanel = new JPanel();
			controlsPanel.add(new JButton("Previous"));
			controlsPanel.add(new JButton("Next"));

			getContentPane().add(stageTabbedPane, BorderLayout.CENTER);
			getContentPane().add(controlsPanel, BorderLayout.SOUTH);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private class FetchThread extends Thread {

		public FetchThread() {
			super();
		}

		@Override
		public void run() {
			System.out.println("Starting fetch thread");

			String csvString;
			try {
				fetchProgress.setValue(0);
				fetchProgress.setIndeterminate(true);
				fetchProgress.setVisible(true);

				csvString = new SotonCalendarFetcher(usernameTextField.getText(), String.valueOf(passwordTextField
						.getPassword())).fetchAndParseTimetable();

				System.out.println("Finished fetch");

				fetchProgress.setIndeterminate(false);
				fetchProgress.setValue(100);
				fetchProgress.setString("Finished");

				calendarCSVString = csvString;
				csvTextArea.setText(calendarCSVString);
				go.setEnabled(true);

				System.out.println("Finished fetch thread");

			} catch (LoginException e) {
				fetchProgress.setIndeterminate(false);
				fetchProgress.setValue(0);
				fetchProgress.setString("Login Error");
			}

		}
	}

	private class CreationThread extends Thread {

		public CreationThread() {
			super();
		}

		@Override
		public void run() {
			System.out.println("Starting creation thread");

			creationProgress.setVisible(true);
			creationProgress.setValue(0);
			creationProgress.setIndeterminate(true);

			SotonCalendarParser calParser = new SotonCalendarParser();
			calParser.setMergeDuplicateEventsWithSeperateLocations(mergeDuplicateEventsWithSeperateLocations
					.isSelected());
			calParser.setMergeHeaders(mergeHeaders.isSelected());
			calParser.setShowSemesters(showSemesters.isSelected());
			calParser.setShowSemesterWeeks(showSemesterWeeks.isSelected());
			calParser.setShowTerms(showTerms.isSelected());

			CalendarBuilder builder = new CalendarBuilder(calParser);
			try {
				Calendar calendar = builder.build(new StringReader(calendarCSVString));
				calendarICALString = calendar.toString();
				calendarPreviewTextArea.setText(calendarICALString);
				System.out.println(calendarICALString);

				createCalendarButton.setEnabled(true);

				creationProgress.setIndeterminate(false);
				creationProgress.setValue(100);
				creationProgress.setString("Finished");
			} catch (ParserException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

}