📄 repository.java
字号:
/** * Rebuild internal cached data after one or more calendar. Update the * EventInstance objects array. Update the Vector of existing categories. The * following objects will be updated: categories, cachedEvents */ private void rebuildPrivateData () { if ( !needsRebuilding ) return; AppPreferences prefs = AppPreferences.getInstance (); boolean showCancelled = prefs.getDisplayCancelledEvents (); boolean showTentative = prefs.getDisplayTentativeEvents (); this.categories = new Vector<String> (); this.cachedEvents = new HashMap<String, Vector> (); HashMap<String, String> catH = new HashMap<String, String> (); for ( int i = 0; i < dataFiles.size (); i++ ) { DataFile df = (DataFile) dataFiles.elementAt ( i ); //System.out.println ( "DataFile#" + i + ": " + df.toString () // + ( this.getCalendars ().elementAt ( i ).isSelected () ? "(selected)" // : "" ) ); // System.out.println ( " df.getEventCount () =" + df.getEventCount () // ); for ( int j = 0; j < df.getEventCount (); j++ ) { Event event = df.eventEntryAt ( j ); if ( event.getStartDate () != null ) { boolean matchesCategoryFilter = false; if ( this.categoryFilterEnabled ) { // Does category match filter? if ( event.getCategories () == null && this.categoryFilterIncludeUncat ) { matchesCategoryFilter = true; } else if ( event.getCategories () == null ) { matchesCategoryFilter = false; } else { String[] cats = splitCategories ( event.getCategories () .getValue () ); for ( int k = 0; k < cats.length && !matchesCategoryFilter; k++ ) { if ( this.categoryFilter.containsKey ( cats[k].toUpperCase () .trim () ) ) matchesCategoryFilter = true; } } } else { matchesCategoryFilter = true; } boolean display = true; switch ( event.getStatus () ) { case Event.STATUS_CANCELLED: display = showCancelled; break; case Event.STATUS_TENTATIVE: display = showTentative; break; } if ( df.calendar.isSelected () && matchesCategoryFilter && display ) { SingleEvent se = null; if ( event.isValid () && event.getStartDate () != null ) { Date startDate = event.getStartDate (); String title = event.getSummary ().getValue (); String description = event.getDescription () != null ? event .getDescription ().getValue () : title; if ( startDate.isDateOnly () ) { se = new SingleEvent ( title, description, startDate.getYear (), startDate.getMonth (), startDate .getDay () ); } else { se = new SingleEvent ( title, description, startDate.getYear (), startDate.getMonth (), startDate .getDay (), startDate.getHour (), startDate .getMinute (), startDate.getSecond () ); } se.setEvent ( event ); se.setCalendar ( df.calendar ); se.bg = df.calendar.getBackgroundColor (); se.border = df.calendar.getBorderColor (); se.fg = df.calendar.getForegroundColor (); String YMD = Utils.DateToYYYYMMDD ( startDate ); Vector dateVector = null; if ( cachedEvents.containsKey ( YMD ) ) { dateVector = (Vector) cachedEvents.get ( YMD ); } else { dateVector = new Vector (); cachedEvents.put ( YMD, dateVector ); } dateVector.addElement ( se ); // Add recurrance events Vector more = event.getRecurranceDates (); for ( int k = 0; more != null && k < more.size (); k++ ) { Date d2 = (Date) more.elementAt ( k ); if ( startDate.isDateOnly () ) { se = new SingleEvent ( title, description, d2.getYear (), d2 .getMonth (), d2.getDay () ); } else { se = new SingleEvent ( title, description, d2.getYear (), d2 .getMonth (), d2.getDay (), d2.getHour (), d2 .getMinute (), d2.getSecond () ); } se.setEvent ( event ); se.setCalendar ( df.calendar ); se.bg = df.calendar.getBackgroundColor (); se.border = df.calendar.getBorderColor (); se.fg = df.calendar.getForegroundColor (); YMD = Utils.DateToYYYYMMDD ( d2 ); dateVector = null; if ( cachedEvents.containsKey ( YMD ) ) { dateVector = (Vector) cachedEvents.get ( YMD ); } else { dateVector = new Vector (); cachedEvents.put ( YMD, dateVector ); } dateVector.addElement ( se ); } } } } Categories cats = event.getCategories (); if ( cats != null && cats.getValue () != null ) { String[] catArray = splitCategories ( cats.getValue () ); for ( int k = 0; catArray != null && k < catArray.length; k++ ) { String c1 = catArray[k].trim (); if ( c1.length () > 0 ) { String c1up = c1.toUpperCase (); if ( !catH.containsKey ( c1up ) ) { this.categories.addElement ( c1 ); catH.put ( c1up, c1up ); } } } } } } this.needsRebuilding = false; } public Vector getEventInstancesForDate ( int year, int month, int day ) { if ( needsRebuilding ) this.rebuildPrivateData (); try { Date date = new Date ( "DTSTART", year, month, day ); String YMD = Utils.DateToYYYYMMDD ( date ); return (Vector) cachedEvents.get ( YMD ); } catch ( BogusDataException e1 ) { e1.printStackTrace (); return null; } } /** * Save the specified Event object. If the Event is part of an existing * iCalendar file, the entire file will be written out. If this Event object * is new, then a new iCalendar file will be created. Note: It is up to the * caller to update the Sequence object each time a Event entry is saved. The * "LAST-MODIFIED" setting will be updated automatically. * * @param j * @throws IOException */ public void saveEvent ( Calendar calendar, Event event ) throws IOException { boolean added = false; DataFile dataFile = (DataFile) event.getUserData (); if ( dataFile == null ) { // New event. // Add this event entry to the calendar's file dataFile = dataFileCalendarHash.get ( calendar ); if ( dataFile == null ) { System.err.println ( "Error: could not find file for calendar " + calendar ); return; } //System.out.println ( "Added event '" + event.getSummary ().getValue () // + "' to data file: " + dataFile ); dataFile.addEvent ( event ); added = true; } event.setLastModified ( Date.getCurrentDateTime ( "LAST-MODIFIED" ) ); event.setUserData ( dataFile ); dataFile.write (); this.needsRebuilding = true; if ( added ) { for ( int i = 0; this.changeListeners != null && i < this.changeListeners.size (); i++ ) { RepositoryChangeListener l = this.changeListeners.elementAt ( i ); l.eventAdded ( event ); } } else { // If we are updating, then the Event to be updated should // already be updated in the DataStore. for ( int i = 0; this.changeListeners != null && i < this.changeListeners.size (); i++ ) { RepositoryChangeListener l = this.changeListeners.elementAt ( i ); l.eventUpdated ( event ); } } } /** * Delete the specified Event object. * * @param j * @throws IOException */ public boolean deleteEvent ( Calendar calendar, Event e ) throws IOException { boolean deleted = false; DataFile dataFile = (DataFile) e.getUserData (); if ( dataFile == null ) { // New event. Nothing to do... System.err.println ( "Not found..." ); } else { // Event to be deleted should be in the DataStore. if ( dataFile.removeEvent ( e ) ) { deleted = true; dataFile.write (); this.needsRebuilding = true; for ( int i = 0; this.changeListeners != null && i < this.changeListeners.size (); i++ ) { RepositoryChangeListener l = (RepositoryChangeListener) this.changeListeners .elementAt ( i ); l.eventDeleted ( e ); } } else { // System.out.println ( "Not deleted" ); } } return deleted; } public void notifyDisplayPreferencesChange () { this.needsRebuilding = true; for ( int i = 0; this.changeListeners != null && i < this.changeListeners.size (); i++ ) { RepositoryChangeListener l = (RepositoryChangeListener) this.changeListeners .elementAt ( i ); l.displaySettingsChanged (); } } /** * Is there a calendar with the specified URL already in the repository? * * @param url * The URL to check for * @return */ public boolean hasCalendarWithURL ( String url ) { for ( int i = 0; i < this.calendars.size (); i++ ) { Calendar c = this.calendars.elementAt ( i ); if ( c.getUrl () == null ) continue; String urlS = c.getUrl ().toString (); if ( url.equals ( urlS ) ) { return true; } } return false; } /** * Ask to be notified when changes are made to the Repository. * * @param l */ public void addChangeListener ( RepositoryChangeListener l ) { if ( this.changeListeners == null ) this.changeListeners = new Vector<RepositoryChangeListener> (); this.changeListeners.addElement ( l ); } public Vector<String> getCategories () { if ( needsRebuilding ) this.rebuildPrivateData (); return this.categories; } private static String[] splitCategories ( String categories ) { if ( looseCategoryHandling && categories.indexOf ( ',' ) < 0 && categories.indexOf ( ' ' ) > 0 ) { return categories.trim ().split ( "[, ]" ); } else { return categories.trim ().split ( "," ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -