📄 main.java
字号:
} if ( eventInstance.hasTime () ) { eventDate.setDateOnly ( false ); eventDate.setHour ( eventInstance.getHour () ); eventDate.setMinute ( eventInstance.getMinute () ); eventDate.setSecond ( eventInstance.getSecond () ); } else { eventDate.setDateOnly ( true ); } updateToolbar (); this.eventViewPanel.update ( eventDate, se.getEvent (), se.getCalendar () ); // Select the calendar on the left that the selected event belongs to. if ( se.getCalendar () != null ) { int ind = -1; for ( int i = 0; i < this.dataRepository.getCalendars ().size () && ind < 0; i++ ) { Calendar c = this.dataRepository.getCalendars ().elementAt ( i ); if ( c.equals ( se.getCalendar () ) ) ind = i; } if ( ind >= 0 ) this.calendarJList.setSelectedIndex ( ind ); } } public void calendarAdded ( Calendar c ) { updateCalendarJList (); updateCategoryJList (); saveCalendars ( getDataDirectory () ); this.dataRepository.rebuild (); this.calendarPanel.repaint (); } public void calendarUpdated ( Calendar c ) { updateCalendarJList (); updateCategoryJList (); saveCalendars ( getDataDirectory () ); this.dataRepository.rebuild (); this.calendarPanel.repaint (); } public void calendarDeleted ( Calendar c ) { updateCalendarJList (); updateCategoryJList (); saveCalendars ( getDataDirectory () ); this.dataRepository.rebuild (); this.calendarPanel.repaint (); } public void displaySettingsChanged () { this.calendarPanel.setShowTime ( prefs.getDisplayHourInMonthView () ); Font oldFont = this.calendarPanel.getFont (); Font defaultFont = this.getFont (); Font newFont = new Font ( oldFont.getFamily (), oldFont.getStyle (), defaultFont.getSize () + prefs.getDisplayFontSize () ); this.calendarPanel.setFont ( newFont ); this.dataRepository.rebuild (); this.calendarPanel.repaint (); this.eventViewPanel.setAllFonts ( newFont ); updateToolbar (); updateLookAndFeel (); } public void updateLookAndFeel () { String laf = prefs.getAppearanceLookAndFeel (); try { if ( laf != null ) { UIManager.setLookAndFeel ( laf ); SwingUtilities.updateComponentTreeUI ( parent ); } } catch ( Exception e ) { System.out.println ( "Unable to L&F " + laf + ": " + e.toString () ); } } public void eventDoubleClicked ( EventInstance eventInstance ) { if ( eventInstance != null ) { SingleEvent se = (SingleEvent) eventInstance; boolean canEdit = ( se.getCalendar ().getType () == Calendar.LOCAL_CALENDAR ) || ( se.getCalendar ().getType () == Calendar.REMOTE_ICAL_CALENDAR && se .getCalendar ().getCanWrite () ); if ( !canEdit ) { showError ( "You cannot edit events\non the following calendar" + ": " + "\n\n" + se.getCalendar ().getName () ); } else { new EditEventWindow ( parent, dataRepository, se.getEvent (), se .getCalendar () ); } } } public void dateDoubleClicked ( int year, int month, int dayOfMonth ) { try { Date d = new Date ( "DTSTART", year, month, dayOfMonth ); new EditEventWindow ( parent, dataRepository, d, null ); } catch ( BogusDataException e1 ) { e1.printStackTrace (); } } public void eventUnselected () { updateToolbar (); this.eventViewPanel.clear (); } URL getResource ( String name ) { return this.getClass ().getClassLoader ().getResource ( name ); } void getVersionFromChangeLog () { if ( this.version != null ) return; URL url = getResource ( "ChangeLog" ); if ( url == null ) { System.err.println ( "Error: could not find ChangeLog in your CLASSPATH" ); return; } try { InputStream is = url.openStream (); BufferedReader reader = new BufferedReader ( new InputStreamReader ( is ) ); while ( this.version == null ) { String line = reader.readLine (); if ( line == null ) break; // not found line = line.trim (); if ( line.toUpperCase ().startsWith ( "VERSION" ) ) { String[] args = line.split ( "-" ); this.version = args[0].trim (); } } is.close (); } catch ( IOException e1 ) { e1.printStackTrace (); this.version = "Unknown Version" + ": " + e1.getMessage (); } return; } public void viewChangeLog () { URL url = getResource ( "ChangeLog" ); try { InputStream is = url.openStream (); BufferedReader reader = new BufferedReader ( new InputStreamReader ( is ) ); String line; StringBuffer sb = new StringBuffer (); while ( ( line = reader.readLine () ) != null ) { sb.append ( line ); sb.append ( "\n" ); } is.close (); final JDialog d = new JDialog (); d.getContentPane ().setLayout ( new BorderLayout () ); d.setTitle ( "ChangeLog" ); d.setSize ( 500, 400 ); d.setLocationByPlatform ( true ); JPanel buttonPanel = new JPanel (); buttonPanel.setLayout ( new FlowLayout () ); JButton b = new JButton ( "Close" ); b.addActionListener ( // Anonymous class as a listener. new ActionListener () { public void actionPerformed ( ActionEvent e ) { d.dispose (); } } ); buttonPanel.add ( b ); d.getContentPane ().add ( buttonPanel, BorderLayout.SOUTH ); JTextArea te = new JTextArea ( sb.toString () ); Font f = new Font ( te.getFont ().getFamily (), Font.PLAIN, 10 ); te.setFont ( f ); te.setEditable ( false ); JScrollPane sp = new MyScrollPane ( te ); sp.getVerticalScrollBar ().setValue ( 0 ); JPanel p = new JPanel (); p.setLayout ( new BorderLayout () ); p.setBorder ( BorderFactory.createTitledBorder ( "ChangeLog" ) ); p.add ( sp, BorderLayout.CENTER ); d.getContentPane ().add ( p, BorderLayout.CENTER ); d.show (); } catch ( Exception e1 ) { e1.printStackTrace (); showMessage ( "Error" + ":\n" + e1.getMessage () ); } } public void viewLicense () { URL url = getResource ( LICENSE_FILE ); if ( url == null ) { System.err.println ( "Unable to find license file" + ": " + LICENSE_FILE ); return; } try { final JDialog d = new JDialog (); d.getContentPane ().setLayout ( new BorderLayout () ); d.setTitle ( "k5nCal License" ); d.setSize ( 500, 400 ); d.setLocationByPlatform ( true ); JPanel buttonPanel = new JPanel (); buttonPanel.setLayout ( new FlowLayout () ); JButton b = new JButton ( "Close" ); b.addActionListener ( // Anonymous class as a listener. new ActionListener () { public void actionPerformed ( ActionEvent e ) { d.dispose (); } } ); buttonPanel.add ( b ); d.getContentPane ().add ( buttonPanel, BorderLayout.SOUTH ); HelpPanel licenseText = new HelpPanel ( url ); d.getContentPane ().add ( licenseText, BorderLayout.CENTER ); d.show (); } catch ( Exception e1 ) { e1.printStackTrace (); showMessage ( "Error" + ":\n" + e1.getMessage () ); } } /** * @param args */ public static void main ( String[] args ) { Vector<String> remoteNames = new Vector<String> (); Vector<String> remoteURLs = new Vector<String> (); // If running on a Mac, change some system properties. if ( System.getProperty ( "mrj.version" ) != null ) { System.setProperty ( "com.apple.mrj.application.apple.menu.about.name", "k5nCal" ); System.setProperty ( "apple.laf.useScreenMenuBar", "true" ); System.setProperty ( "com.apple.mrj.application.growbox.intrudes", "false" ); System.setProperty ( "apple.awt.antialiasing", "true" ); } // Check for command line options for ( int i = 0; i < args.length; i++ ) { if ( args[i].equals ( "-addcalendar" ) ) { if ( args.length >= i + 1 ) { String name = args[++i].trim (); String url = args[++i].trim (); if ( url.startsWith ( "http://" ) || url.startsWith ( "https://" ) ) { remoteNames.addElement ( name ); remoteURLs.addElement ( url ); } else { System.err.println ( "Ignoring invalid url" + ": " + url ); } } else { System.err .println ( "Error: -addcalendar requires name and URL parameter" ); System.exit ( 1 ); } } else { System.err.println ( "Unknown command line option" + ": " + args[i] ); } } Main app = new Main (); if ( System.getProperty ( "mrj.version" ) != null ) { MacStuff mac = new MacStuff ( app ); } // Add calendars if not there... for ( int i = 0; i < remoteURLs.size (); i++ ) { String name = remoteNames.elementAt ( i ); String url = remoteURLs.elementAt ( i ); if ( app.dataRepository.hasCalendarWithURL ( url ) ) { System.out.println ( "Ignoring add calendar from duplicate URL" + ": " + url ); } else { // auto-add the calendar.... app.addCalendarFromCommandLine ( name, url ); } } } private void addCalendarFromCommandLine ( String name, String urlStr ) { URL url = null; try { url = new URL ( urlStr ); } catch ( Exception e1 ) { showError ( "Invalid URL" + ":\n" + e1.getMessage () ); return; } showStatusMessage ( "Downloading calendar" + ": " + name ); final Calendar cal = new Calendar ( getDataDirectory (), name, url, 30 ); cal.setBackgroundColor ( Color.blue ); cal.setForegroundColor ( getForegroundColorForBackground ( Color.blue ) ); cal.setBorderColor ( getForegroundColorForBackground ( Color.blue ) ); cal.setLastUpdatedAsNow (); cal.setUrl ( url ); SwingWorker addWorker = new SwingWorker () { private String error = null; private String statusMsg = null; public Object construct () { File file = new File ( getDataDirectory (), cal.getFilename () ); HttpClientStatus result = HttpClient.getRemoteCalendar ( cal.getUrl (), null, null, file ); switch ( result.getStatus () ) { case HttpClientStatus.HTTP_STATUS_SUCCESS: break; case HttpClientStatus.HTTP_STATUS_AUTH_REQUIRED: showError ( "Authorization required.\nPlease provide a username\nand password." ); return null; case HttpClientStatus.HTTP_STATUS_NOT_FOUND: showError ( "Invalid calendar URL (not found).\n\nServer response" + ": " + result.getMessage () ); return null; default: case HttpClientStatus.HTTP_STATUS_OTHER_ERROR: showError ( "Error downloading calendar.\n\nServer response" + ": " + result.getMessage () ); return null; } return null; } public void finished () { if ( error == null ) dataRepository.addCalendar ( getDataDirectory (), cal, false ); // Update UI if ( error != null ) showError ( error ); if ( this.statusMsg != null ) showStatusMessage ( statusMsg ); if ( error == null ) { // If no error, then save calendar update cal.setLastUpdatedAsNow (); saveCalendars ( dataDir ); dataRepository.updateCalendar ( getDataDirectory (), cal ); } } }; addWorker.start (); // updating calendar... dataRepository.updateCalendar ( getDataDirectory (), cal ); showStatusMessage ( "Calendar added" + ": " + name ); }}/** * Create a class to use as a file filter for exporting to ics. */class ICSFileChooserFilter extends javax.swing.filechooser.FileFilter { public boolean accept ( File f ) { if ( f.isDirectory () ) return true; String name = f.getName (); if ( name.toLowerCase ().endsWith ( ".ics" ) ) return true; return false; } public String getDescription () { return "*.ics (iCalendar Files)"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -