📄 repository.java
字号:
/* * k5nCal - Java Swing Desktop Calendar App * Copyright (C) 2005-2007 Craig Knudsen, craig@k5n.us * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */package us.k5n.k5ncal.data;import java.io.File;import java.io.IOException;import java.util.HashMap;import java.util.Vector;import us.k5n.ical.BogusDataException;import us.k5n.ical.Categories;import us.k5n.ical.Date;import us.k5n.ical.Event;import us.k5n.ical.ParseError;import us.k5n.ical.Utils;import us.k5n.k5ncal.AppPreferences;import us.k5n.ui.calendar.CalendarDataRepository;/** * The Repository class manages all loading and saving of data files. All * methods are intended to work with just Event objects. However, if an * iCalendar file is loaded with non-Event objects, they should be preserved in * the data if it is written back out. * * @author Craig Knudsen, craig@k5n.us * @version $Id: Repository.java,v 1.6 2008/01/15 20:35:20 cknudsen Exp $ */public class Repository implements CalendarDataRepository { File directory; Vector<Calendar> calendars; Vector<DataFile> dataFiles; HashMap<String, DataFile> dataFileHash; HashMap<Calendar, DataFile> dataFileCalendarHash; int parseErrorCount = 0; int eventCount = 0; private HashMap<String, String> categoryFilter = null; private boolean categoryFilterIncludeUncat = false; private boolean categoryFilterEnabled = false; private HashMap<String, Vector> cachedEvents; private Vector<RepositoryChangeListener> changeListeners; private Vector<String> categories; // Vector of String categories private boolean needsRebuilding = true; private static boolean looseCategoryHandling = true; public Repository(File dir, Vector<Calendar> calendars, boolean strictParsing) { this.directory = dir; this.calendars = calendars; this.dataFiles = new Vector<DataFile> (); this.dataFileHash = new HashMap<String, DataFile> (); this.dataFileCalendarHash = new HashMap<Calendar, DataFile> (); this.cachedEvents = new HashMap<String, Vector> (); this.changeListeners = new Vector<RepositoryChangeListener> (); this.categories = new Vector<String> (); // Load all calendars for ( int i = 0; calendars != null && i < calendars.size (); i++ ) { Calendar c = calendars.elementAt ( i ); File file = new File ( dir, c.getFilename () ); DataFile f = new DataFile ( file.getAbsolutePath (), c, strictParsing ); if ( f != null ) { this.dataFileCalendarHash.put ( c, f ); this.addDataFile ( f, c ); } } this.needsRebuilding = true; rebuildPrivateData (); } public void addCalendar ( File dir, Calendar c, boolean strictParsing ) { // make sure we don't already have it boolean found = false; for ( int i = 0; i < this.calendars.size (); i++ ) { Calendar c1 = this.calendars.elementAt ( i ); if ( c1.equals ( c ) ) { // already have it. Probably added by the caller to the shared Vector found = true; } } File file = new File ( dir, c.getFilename () ); DataFile f = new DataFile ( file.getAbsolutePath (), c, strictParsing ); if ( f != null ) { this.addDataFile ( f, c ); } if ( !found ) { this.calendars.addElement ( c ); for ( int i = 0; i < this.changeListeners.size (); i++ ) { RepositoryChangeListener l = this.changeListeners.elementAt ( i ); l.calendarAdded ( c ); } } needsRebuilding = true; } // Call this when you have updated the calendar name or other // attributes. public void updateCalendar ( File dir, Calendar c ) { boolean found = false; for ( int i = 0; i < this.calendars.size (); i++ ) { Calendar c1 = this.calendars.elementAt ( i ); if ( c1.equals ( c ) ) { // found it found = true; this.calendars.setElementAt ( c, i ); DataFile df = this.dataFiles.elementAt ( i ); df.refresh (); } } if ( found ) { for ( int i = 0; i < this.changeListeners.size (); i++ ) { RepositoryChangeListener l = this.changeListeners.elementAt ( i ); l.calendarUpdated ( c ); } } needsRebuilding = true; } public void removeCalendar ( File dir, Calendar c ) { boolean found = false; for ( int i = 0; i < this.calendars.size () && !found; i++ ) { Calendar c1 = this.calendars.elementAt ( i ); if ( c1.equals ( c ) ) { // found this.calendars.remove ( i ); found = true; } } // Now remove DataFile found = false; for ( int i = 0; i < this.dataFiles.size () && !found; i++ ) { DataFile df = this.dataFiles.elementAt ( i ); if ( df.calendar.equals ( c ) ) { File f = new File ( dir, df.calendar.getFilename () ); removeDataFile ( df ); found = true; f.delete (); } } if ( !found ) { System.err.println ( "removeCalendar: not found " + c ); } else { needsRebuilding = true; for ( int i = 0; i < this.changeListeners.size (); i++ ) { RepositoryChangeListener l = this.changeListeners.elementAt ( i ); l.calendarDeleted ( c ); } } } /** * Get a Vector of Calendar objects for all calendars. * * @return */ public Vector<Calendar> getCalendars () { return calendars; } public DataFile getDataFileForCalendar ( Calendar c ) { for ( int i = 0; i < this.calendars.size (); i++ ) { Calendar c1 = this.calendars.elementAt ( i ); if ( c1.equals ( c ) ) { // found it return this.dataFiles.elementAt ( i ); } } // not found return null; } private void removeDataFile ( DataFile f ) { for ( int i = 0; i < this.dataFiles.size (); i++ ) { DataFile df = this.dataFiles.elementAt ( i ); if ( df.equals ( f ) ) { eventCount -= f.getEventCount (); parseErrorCount -= f.getParseErrorCount (); this.dataFileHash.remove ( f.getName ().toLowerCase () ); this.dataFiles.remove ( i ); this.needsRebuilding = true; return; } } // Not found System.err.println ( "removeDataFile: not found" ); } private void addDataFile ( DataFile f, Calendar c ) { boolean found = false; for ( int i = 0; i < this.dataFiles.size (); i++ ) { DataFile df = this.dataFiles.elementAt ( i ); if ( df.equals ( f ) ) found = true; } if ( found ) { System.err.println ( "addDataFile: not adding duplicate" ); } else { this.dataFiles.addElement ( f ); eventCount += f.getEventCount (); parseErrorCount += f.getParseErrorCount (); // Store in HashMap using just the filename (19991231.ics) // as the key this.dataFileHash.put ( f.getName ().toLowerCase (), f ); this.dataFileCalendarHash.put ( c, f ); this.needsRebuilding = true; } } public Vector<ParseError> getErrorsAt ( int ind ) { return this.dataFiles.elementAt ( ind ).getErrors (); } /** * Get all Event objects. * * @return */ public Vector<Event> getAllEntries () { Vector<Event> ret = new Vector<Event> (); for ( int i = 0; i < dataFiles.size (); i++ ) { DataFile df = dataFiles.elementAt ( i ); for ( int j = 0; j < df.getEventCount (); j++ ) { Event event = df.eventEntryAt ( j ); ret.addElement ( event ); } } return ret; } /** * Get all Event objects from calendars that are currently visible. * * @return */ public Vector<Event> getVisibleEntries () { Vector<Event> ret = new Vector<Event> (); for ( int i = 0; i < dataFiles.size (); i++ ) { DataFile df = dataFiles.elementAt ( i ); if ( df.calendar.isSelected () ) { for ( int j = 0; j < df.getEventCount (); j++ ) { Event event = df.eventEntryAt ( j ); ret.addElement ( event ); } } } return ret; } public void clearCategoryFilter () { this.categoryFilterEnabled = false; this.categoryFilter = null; } public void setCategoryFilter ( boolean includeUncategorized, Vector<String> cats ) { this.categoryFilterEnabled = true; this.categoryFilterIncludeUncat = includeUncategorized; this.categoryFilter = new HashMap<String, String> (); for ( int i = 0; i < cats.size (); i++ ) { String cat = cats.elementAt ( i ).toUpperCase ().trim (); this.categoryFilter.put ( cat, cat ); } } /** * Rebuild internal cached data after one or more calendar */ public void rebuild () { this.needsRebuilding = true; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -