From dc44d733b56335f56f2fae23acf2c04277a0ce2f Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Fri, 23 Mar 2012 21:45:58 +0000 Subject: Weeks somewhat working... --- src/net/cbaines/suca/CalendarApplet.java | 91 ++++++-- src/net/cbaines/suca/SotonCalendarParser.java | 304 ++++++++++++++++++-------- 2 files changed, 289 insertions(+), 106 deletions(-) diff --git a/src/net/cbaines/suca/CalendarApplet.java b/src/net/cbaines/suca/CalendarApplet.java index 0357bbf..329c180 100644 --- a/src/net/cbaines/suca/CalendarApplet.java +++ b/src/net/cbaines/suca/CalendarApplet.java @@ -13,6 +13,7 @@ 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; @@ -38,8 +39,6 @@ public class CalendarApplet extends JApplet { // The panel used for the file fetch stuff private JPanel csvFileFetchPanel; - private JLabel csvFileFetchInstructions; - private JTextField usernameTextField; private JPasswordField passwordTextField; private JButton go; @@ -62,6 +61,15 @@ public class CalendarApplet extends JApplet { 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; @@ -139,24 +147,12 @@ public class CalendarApplet extends JApplet { calendarCreationOptions = new JPanel(); - JButton createCalendarButton = new JButton("Create Calendar"); + createCalendarButton = new JButton("Create Calendar"); createCalendarButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { - - SotonCalendarParser calParser = new SotonCalendarParser(); - - CalendarBuilder builder = new CalendarBuilder(calParser); - try { - Calendar calendar = builder.build(new StringReader(calendarCSVString)); - calendarICALString = calendar.toString(); - calendarPreviewTextArea.setText(calendarICALString); - System.out.println(calendarICALString); - } catch (ParserException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } + createCalendarButton.setEnabled(false); + new CreationThread().start(); } }); calendarCreationOptions.add(createCalendarButton); @@ -169,11 +165,13 @@ public class CalendarApplet extends JApplet { JFileChooser fc = new JFileChooser(); int returnVal = fc.showSaveDialog(instance); // this = Applet if (returnVal == JFileChooser.APPROVE_OPTION) { - File aFile = fc.getSelectedFile(); + File file = fc.getSelectedFile(); try { - BufferedWriter out = new BufferedWriter(new FileWriter(aFile)); + BufferedWriter out = new BufferedWriter(new FileWriter(file)); out.write(calendarICALString); + out.flush(); + out.close(); } catch (IOException e) { e.printStackTrace(); } @@ -184,6 +182,16 @@ public class CalendarApplet extends JApplet { }); 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(); @@ -215,6 +223,8 @@ public class CalendarApplet extends JApplet { String csvString; try { + fetchProgress.setValue(0); + fetchProgress.setIndeterminate(true); fetchProgress.setVisible(true); csvString = new SotonCalendarFetcher(usernameTextField.getText(), String.valueOf(passwordTextField @@ -241,4 +251,47 @@ public class CalendarApplet extends JApplet { } } + 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(); + } + + } + } + } diff --git a/src/net/cbaines/suca/SotonCalendarParser.java b/src/net/cbaines/suca/SotonCalendarParser.java index beb7aa5..a98fd81 100644 --- a/src/net/cbaines/suca/SotonCalendarParser.java +++ b/src/net/cbaines/suca/SotonCalendarParser.java @@ -17,7 +17,6 @@ import net.fortuna.ical4j.model.Component; import net.fortuna.ical4j.model.Date; import net.fortuna.ical4j.model.DateTime; import net.fortuna.ical4j.model.Property; -import net.fortuna.ical4j.util.UidGenerator; /** * http://www.kanzaki.com/docs/ical/ @@ -28,40 +27,35 @@ import net.fortuna.ical4j.util.UidGenerator; */ public class SotonCalendarParser implements CalendarParser { - private boolean MERGE_DUPLICATE_EVENTS_WITH_SEPARATE_LOCATIONS = true; + private boolean mergeDuplicateEventsWithSeperateLocations = true; - private boolean SHOW_SEMESTER_WEEKS = true; - private boolean SHOW_SEMESTERS = true; - private boolean SHOW_TERMS = true; + private boolean showSemesterWeeks = true; + private boolean showSemesters = true; + private boolean showTerms = true; - private boolean MERGE_HEADERS = false; + private boolean mergeHeaders = false; - private final static String TAG = "SotonCalendarParser"; + // private final static String TAG = "SotonCalendarParser"; - public void parse(InputStream in, ContentHandler handler) - throws IOException, ParserException { + public void parse(InputStream in, ContentHandler handler) throws IOException, ParserException { parse(new InputStreamReader(in), handler); } - public void parse(Reader in, ContentHandler handler) throws IOException, - ParserException { + public void parse(Reader in, ContentHandler handler) throws IOException, ParserException { parse(new BufferedReader(in, 3), handler); } /** * Parse the csv file * - * Line: - * "Monday","09:00","13:00","COMP1004 Comp Lab1/01","Weal, M","25 / 1009" - * ,"1-11, 15" + * Line: "Monday","09:00","13:00","COMP1004 Comp Lab1/01","Weal, M","25 / 1009" ,"1-11, 15" * * @param in * @param handler * @throws IOException * @throws ParserException */ - public void parse(BufferedReader in, ContentHandler handler) - throws IOException, ParserException { + public void parse(BufferedReader in, ContentHandler handler) throws IOException, ParserException { try { log("Year " + Calendar.getInstance().get(Calendar.YEAR)); int currentYear = Calendar.getInstance().get(Calendar.YEAR); @@ -77,8 +71,7 @@ public class SotonCalendarParser implements CalendarParser { handler.propertyValue("-//Chris Baines + Henco Appel//SouthamptonUniversityOrganiser//EN"); handler.endProperty(Property.NAME); - Calendar startOfTerm = UniTermDates.getStartOf( - UniTermDates.SEMESTER_1, currentYear - 1); + Calendar startOfTerm = UniTermDates.getStartOf(UniTermDates.SEMESTER_1, currentYear - 1); logCalendar("Semester 1 start time ", startOfTerm); @@ -105,7 +98,7 @@ public class SotonCalendarParser implements CalendarParser { logCalendar("Week 0 start time ", startOfTerm); - UidGenerator ug = new UidGenerator("1"); + // UidGenerator ug = new UidGenerator("1"); ArrayList events = new ArrayList(); @@ -114,18 +107,14 @@ public class SotonCalendarParser implements CalendarParser { if (!line.trim().equals("")) { log(line); String[] lectureInfo = line.split("\",\""); - lectureInfo[0] = lectureInfo[0].substring(1, - lectureInfo[0].length()); - lectureInfo[6] = lectureInfo[6].substring(0, - lectureInfo[6].length() - 1); + lectureInfo[0] = lectureInfo[0].substring(1, lectureInfo[0].length()); + lectureInfo[6] = lectureInfo[6].substring(0, lectureInfo[6].length() - 1); Event event = new Event(lectureInfo); int index; - if ((index = events.indexOf(event)) != -1 - && MERGE_DUPLICATE_EVENTS_WITH_SEPARATE_LOCATIONS) { + if ((index = events.indexOf(event)) != -1 && mergeDuplicateEventsWithSeperateLocations) { event = events.get(index); if (!event.location.equals(lectureInfo[5])) { - event.location = event.location + " and " - + lectureInfo[5]; + event.location = event.location + " and " + lectureInfo[5]; } events.set(index, event); } else { @@ -150,8 +139,7 @@ public class SotonCalendarParser implements CalendarParser { // Time of creation handler.startProperty(Property.DTSTAMP); - handler.propertyValue(new DateTime(java.util.Calendar - .getInstance().getTime()).toString()); + handler.propertyValue(new DateTime(java.util.Calendar.getInstance().getTime()).toString()); handler.endProperty(Property.DTSTAMP); // Start time @@ -188,8 +176,7 @@ public class SotonCalendarParser implements CalendarParser { startTime.add(Calendar.WEEK_OF_YEAR, startWeek); handler.startProperty(Property.DTSTART); - handler.propertyValue(new DateTime(startTime.getTime()) - .toString()); + handler.propertyValue(new DateTime(startTime.getTime()).toString()); handler.endProperty(Property.DTSTART); logCalendar(" Start time (week " + startWeek + ") ", startTime); @@ -206,8 +193,7 @@ public class SotonCalendarParser implements CalendarParser { endTime.set(Calendar.MINUTE, endMinute); handler.startProperty(Property.DTEND); - handler.propertyValue(new DateTime(endTime.getTime()) - .toString()); + handler.propertyValue(new DateTime(endTime.getTime()).toString()); handler.endProperty(Property.DTEND); logCalendar(" End time ", endTime); @@ -251,12 +237,10 @@ public class SotonCalendarParser implements CalendarParser { recurance.add(Calendar.WEEK_OF_YEAR, i); - rDate.append(new DateTime(recurance.getTime()) - .toString()); + rDate.append(new DateTime(recurance.getTime()).toString()); rDate.append(","); - logCalendar(" Recurance (week " + i + ") ", - recurance); + logCalendar(" Recurance (week " + i + ") ", recurance); } } else { int week = Integer.valueOf(rule.trim()); @@ -264,12 +248,27 @@ public class SotonCalendarParser implements CalendarParser { recurance = (Calendar) startOfTerm.clone(); recurance.add(Calendar.WEEK_OF_YEAR, week); - rDate.append(new DateTime(recurance.getTime()) - .toString()); + if (event.dayOfWeek.equals("Monday")) { + // Nothing to do + } else if (event.dayOfWeek.equals("Tuesday")) { + recurance.add(Calendar.DAY_OF_WEEK, 1); + } else if (event.dayOfWeek.equals("Wednesday")) { + recurance.add(Calendar.DAY_OF_WEEK, 2); + } else if (event.dayOfWeek.equals("Thursday")) { + recurance.add(Calendar.DAY_OF_WEEK, 3); + } else if (event.dayOfWeek.equals("Friday")) { + recurance.add(Calendar.DAY_OF_WEEK, 4); + } else { + log("ERROR!!!"); + } + + recurance.set(Calendar.HOUR_OF_DAY, startHour); + recurance.set(Calendar.MINUTE, startMinute); + + rDate.append(new DateTime(recurance.getTime()).toString()); rDate.append(","); - logCalendar(" Recurance (week " + week + ") ", - recurance); + logCalendar(" Recurance (week " + week + ") ", recurance); } } @@ -307,24 +306,23 @@ public class SotonCalendarParser implements CalendarParser { } - if (MERGE_HEADERS) { + if (mergeHeaders) { for (int year = UniTermDates.YEAR_BEGINING_2011; year < UniTermDates.YEAR_BEGINING_2019; year++) { - if (SHOW_SEMESTER_WEEKS) { + if (showSemesterWeeks) { } } } else { ArrayList periods = new ArrayList(5); - String[] periodNames = { "Autumn Term", "Spring Term", - "Summer Term", "Semester 1", "Semester 2" }; + String[] periodNames = { "Autumn Term", "Spring Term", "Summer Term", "Semester 1", "Semester 2" }; - if (SHOW_SEMESTERS) { + if (showSemesters) { periods.add(UniTermDates.SEMESTER_1); periods.add(UniTermDates.SEMESTER_2); } - if (SHOW_TERMS) { + if (showTerms) { periods.add(UniTermDates.AUTUMN_TERM); periods.add(UniTermDates.SPRING_TERM); periods.add(UniTermDates.SUMMER_TERM); @@ -347,43 +345,110 @@ public class SotonCalendarParser implements CalendarParser { // Time of creation handler.startProperty(Property.DTSTAMP); - handler.propertyValue(new DateTime(java.util.Calendar - .getInstance().getTime()).toString()); + handler.propertyValue(new DateTime(java.util.Calendar.getInstance().getTime()).toString()); handler.endProperty(Property.DTSTAMP); // Start time - logCalendar(" Start time " + periodNames[period] + " ", - UniTermDates.getStartOf(period, year)); + logCalendar(" Start time " + periodNames[period] + " ", UniTermDates.getStartOf(period, year)); handler.startProperty(Property.DTSTART); handler.parameter("VALUE", "DATE"); - handler.propertyValue(new Date(UniTermDates.getStartOf( - period, year).getTime()).toString()); + handler.propertyValue(new Date(UniTermDates.getStartOf(period, year).getTime()).toString()); handler.endProperty(Property.DTSTART); // End time - logCalendar(" End time " + periodNames[period] + " ", - UniTermDates.getEndOf(period, year)); + logCalendar(" End time " + periodNames[period] + " ", UniTermDates.getEndOf(period, year)); handler.startProperty(Property.DTEND); handler.parameter("VALUE", "DATE"); - handler.propertyValue(new Date(UniTermDates.getEndOf( - period, year).getTime()).toString()); + handler.propertyValue(new Date(UniTermDates.getEndOf(period, year).getTime()).toString()); handler.endProperty(Property.DTEND); handler.endComponent(Component.VEVENT); } } - if (SHOW_SEMESTER_WEEKS) { + if (showSemesterWeeks) { + + for (int year = UniTermDates.YEAR_BEGINING_2011; year < UniTermDates.YEAR_BEGINING_2019; year++) { + + // Semester 1 + Calendar semester1StartDate = UniTermDates.getStartOf(UniTermDates.SEMESTER_1, year); + Calendar semester1EndDate = UniTermDates.getEndOf(UniTermDates.SEMESTER_1, year); + + int dayOfTheWeek = semester1StartDate.get(Calendar.DAY_OF_WEEK); + log("Semester 1, year " + year + " starts on the " + dayOfTheWeek); + int difference2 = 0; + if (dayOfTheWeek == Calendar.MONDAY) { + difference2 = 8; + } else if (dayOfTheWeek == Calendar.TUESDAY) { + difference2 = 7; + } else if (dayOfTheWeek == Calendar.WEDNESDAY) { + difference2 = 6; + } else if (dayOfTheWeek == Calendar.THURSDAY) { + difference2 = 5; + } else if (dayOfTheWeek == Calendar.FRIDAY) { + difference2 = 4; + } else if (dayOfTheWeek == Calendar.SATURDAY) { + difference2 = 3; + } else if (dayOfTheWeek == Calendar.SUNDAY) { + difference2 = 2; + } else { + log("Error calculating difference"); + } + log("Moving forward " + difference2); + semester1StartDate.add(Calendar.DAY_OF_WEEK, difference2); + + int week = 1; + while (semester1StartDate.before(semester1EndDate)) { + handler.startComponent(Component.VEVENT); + // handler.startProperty(Property.UID); + // handler.propertyValue(ug.generateUid().toString()); + // handler.endProperty(Property.UID); + + // Summary + handler.startProperty(Property.SUMMARY); + handler.propertyValue("Week " + week); + handler.endProperty(Property.SUMMARY); + + // Time of creation + handler.startProperty(Property.DTSTAMP); + handler.propertyValue(new DateTime(java.util.Calendar.getInstance().getTime()).toString()); + handler.endProperty(Property.DTSTAMP); + + // Start time + + logCalendar(" Start time " + "Week " + week + " ", semester1StartDate); + + handler.startProperty(Property.DTSTART); + handler.parameter("VALUE", "DATE"); + handler.propertyValue(new Date(semester1StartDate.getTime()).toString()); + handler.endProperty(Property.DTSTART); + + semester1StartDate.add(Calendar.WEEK_OF_YEAR, 1); + + // End time + + logCalendar(" End time " + "Week " + week + " ", semester1StartDate); + + handler.startProperty(Property.DTEND); + handler.parameter("VALUE", "DATE"); + handler.propertyValue(new Date(semester1StartDate.getTime()).toString()); + handler.endProperty(Property.DTEND); + + handler.endComponent(Component.VEVENT); - for (int period : periods) { - for (int year = UniTermDates.YEAR_BEGINING_2011; year < UniTermDates.YEAR_BEGINING_2019; year++) { + week++; + } - log("Looking at " + periodNames[period]); + // Semester 2 + Calendar semester2StartDate = UniTermDates.getStartOf(UniTermDates.SEMESTER_2, year); + Calendar semester2EndDate = UniTermDates.getEndOf(UniTermDates.SEMESTER_2, year); + week = 1; + while (semester2StartDate.before(semester2EndDate)) { handler.startComponent(Component.VEVENT); // handler.startProperty(Property.UID); // handler.propertyValue(ug.generateUid().toString()); @@ -391,42 +456,37 @@ public class SotonCalendarParser implements CalendarParser { // Summary handler.startProperty(Property.SUMMARY); - handler.propertyValue(periodNames[period]); + handler.propertyValue("Week " + week); handler.endProperty(Property.SUMMARY); // Time of creation handler.startProperty(Property.DTSTAMP); - handler.propertyValue(new DateTime( - java.util.Calendar.getInstance().getTime()) - .toString()); + handler.propertyValue(new DateTime(java.util.Calendar.getInstance().getTime()).toString()); handler.endProperty(Property.DTSTAMP); // Start time - logCalendar(" Start time " + periodNames[period] - + " ", - UniTermDates.getStartOf(period, year)); + logCalendar(" Start time " + "Week " + week + " ", semester2StartDate); handler.startProperty(Property.DTSTART); handler.parameter("VALUE", "DATE"); - handler.propertyValue(new Date(UniTermDates - .getStartOf(period, year).getTime()) - .toString()); + handler.propertyValue(new Date(semester2StartDate.getTime()).toString()); handler.endProperty(Property.DTSTART); + semester2StartDate.add(Calendar.WEEK_OF_YEAR, 1); + // End time - logCalendar(" End time " + periodNames[period] - + " ", UniTermDates.getEndOf(period, year)); + logCalendar(" End time " + "Week " + week + " ", semester2StartDate); handler.startProperty(Property.DTEND); handler.parameter("VALUE", "DATE"); - handler.propertyValue(new Date(UniTermDates - .getEndOf(period, year).getTime()) - .toString()); + handler.propertyValue(new Date(semester2StartDate.getTime()).toString()); handler.endProperty(Property.DTEND); handler.endComponent(Component.VEVENT); + + week++; } } } @@ -453,15 +513,89 @@ public class SotonCalendarParser implements CalendarParser { minute = "0" + minute; } - log(prefix + hour + ":" + minute + " " + cal.get(Calendar.DAY_OF_MONTH) - + "/" + (cal.get(Calendar.MONTH) + 1) + "/" - + cal.get(Calendar.YEAR)); + log(prefix + hour + ":" + minute + " " + cal.get(Calendar.DAY_OF_MONTH) + "/" + (cal.get(Calendar.MONTH) + 1) + + "/" + cal.get(Calendar.YEAR)); } private static void log(String string) { System.out.println(string); } + /** + * @return the mergeDuplicateEventsWithSeperateLocations + */ + public boolean isMergeDuplicateEventsWithSeperateLocations() { + return mergeDuplicateEventsWithSeperateLocations; + } + + /** + * @param mergeDuplicateEventsWithSeperateLocations + * the mergeDuplicateEventsWithSeperateLocations to set + */ + public void setMergeDuplicateEventsWithSeperateLocations(boolean mergeDuplicateEventsWithSeperateLocations) { + this.mergeDuplicateEventsWithSeperateLocations = mergeDuplicateEventsWithSeperateLocations; + } + + /** + * @return the showSemesterWeeks + */ + public boolean isShowSemesterWeeks() { + return showSemesterWeeks; + } + + /** + * @param showSemesterWeeks + * the showSemesterWeeks to set + */ + public void setShowSemesterWeeks(boolean showSemesterWeeks) { + this.showSemesterWeeks = showSemesterWeeks; + } + + /** + * @return the showSemesters + */ + public boolean isShowSemesters() { + return showSemesters; + } + + /** + * @param showSemesters + * the showSemesters to set + */ + public void setShowSemesters(boolean showSemesters) { + this.showSemesters = showSemesters; + } + + /** + * @return the showTerms + */ + public boolean isShowTerms() { + return showTerms; + } + + /** + * @param showTerms + * the showTerms to set + */ + public void setShowTerms(boolean showTerms) { + this.showTerms = showTerms; + } + + /** + * @return the mergeHeaders + */ + public boolean isMergeHeaders() { + return mergeHeaders; + } + + /** + * @param mergeHeaders + * the mergeHeaders to set + */ + public void setMergeHeaders(boolean mergeHeaders) { + this.mergeHeaders = mergeHeaders; + } + private class Event { String dayOfWeek; String startTime; @@ -489,16 +623,12 @@ public class SotonCalendarParser implements CalendarParser { final int prime = 31; int result = 1; result = prime * result + getOuterType().hashCode(); - result = prime * result - + ((dayOfWeek == null) ? 0 : dayOfWeek.hashCode()); - result = prime * result - + ((endTime == null) ? 0 : endTime.hashCode()); + result = prime * result + ((dayOfWeek == null) ? 0 : dayOfWeek.hashCode()); + result = prime * result + ((endTime == null) ? 0 : endTime.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); - result = prime * result - + ((startTime == null) ? 0 : startTime.hashCode()); + result = prime * result + ((startTime == null) ? 0 : startTime.hashCode()); result = prime * result + ((weeks == null) ? 0 : weeks.hashCode()); - result = prime * result - + ((lecturer == null) ? 0 : lecturer.hashCode()); + result = prime * result + ((lecturer == null) ? 0 : lecturer.hashCode()); return result; } -- cgit v1.2.3