📄 main.java
字号:
void showMessage ( String message ) { JOptionPane.showMessageDialog ( parent, message, "Notice", JOptionPane.INFORMATION_MESSAGE ); } void showError ( String message ) { System.err.println ( "Error" + ": " + message ); JOptionPane.showMessageDialog ( parent, message, "Error", JOptionPane.ERROR_MESSAGE ); } void fatalError ( String message ) { System.err.println ( "Fatal error" + ": " + message ); JOptionPane.showMessageDialog ( parent, message, "Fatal Error", JOptionPane.ERROR_MESSAGE ); System.exit ( 1 ); } protected JButton makeNavigationButton ( String imageName, String actionCommand, String toolTipText, String altText ) { JButton button; String imgLocation = null; URL imageURL = null; // Look for the image. imgLocation = "images/" + imageName; if ( imageName != null ) { imageURL = getResource ( imgLocation ); } if ( imageURL != null ) { // image found button = new JButton ( altText ); button.setIcon ( new ImageIcon ( imageURL, altText ) ); } else { // no image found button = new JButton ( altText ); if ( imageName != null ) System.err.println ( "Resource not found" + ": " + imgLocation ); } button.setVerticalTextPosition ( JButton.BOTTOM ); button.setHorizontalTextPosition ( JButton.CENTER ); button.setActionCommand ( actionCommand ); button.setToolTipText ( toolTipText ); // Decrease font size by 2 if we have an icon if ( imageURL != null ) { Font f = button.getFont (); Font newFont = new Font ( f.getFamily (), Font.PLAIN, f.getSize () - 2 ); button.setFont ( newFont ); } return button; } /** * Set the Look and Feel to be Windows. */ public void setInitialLAF () { String laf = prefs.getAppearanceLookAndFeel (); try { if ( laf != null ) UIManager.setLookAndFeel ( laf ); } catch ( Exception e ) { System.out.println ( "Unable to L&F " + laf + ": " + e.toString () ); } } public void selectLookAndFeel ( Component toplevel, Frame dialogParent ) { LookAndFeel lafCurrent = UIManager.getLookAndFeel (); // System.out.println ( "Current L&F: " + lafCurrent ); UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels (); String[] choices = new String[info.length]; int sel = 0; for ( int i = 0; i < info.length; i++ ) { System.out.println ( " " + info[i].toString () ); choices[i] = info[i].getClassName (); if ( info[i].getClassName ().equals ( lafCurrent.getClass ().getName () ) ) sel = i; } Object uiSelection = JOptionPane.showInputDialog ( dialogParent, "Select Look and Feel", "Look and Feel", JOptionPane.INFORMATION_MESSAGE, null, choices, choices[sel] ); UIManager.LookAndFeelInfo selectedLAFInfo = null; for ( int i = 0; i < info.length; i++ ) { if ( uiSelection.equals ( choices[i] ) ) selectedLAFInfo = info[i]; } if ( selectedLAFInfo != null ) { try { // System.out.println ( "Changing L&F: " + selectedLAFInfo ); UIManager.setLookAndFeel ( selectedLAFInfo.getClassName () ); // SwingUtilities.updateComponentTreeUI ( parent ); // parent.pack (); } catch ( Exception e ) { System.err.println ( "Unabled to load L&F: " + e.toString () ); } } else { System.err.println ( "No L&F selected" ); } } private Color getForegroundColorForBackground ( Color bg ) { Color ret = Color.white; if ( bg.getRed () > 128 && bg.getGreen () > 128 && bg.getRed () > 128 ) ret = Color.black; return ret; } protected void editLocalCalendar ( final Calendar c ) { final JDialog addLocal = new JDialog ( this ); addLocal.setLocationRelativeTo ( null ); final JTextField nameField = new JTextField ( 30 ); final ColorButton colorField = new ColorButton (); int[] props = { 1, 2 }; if ( c != null ) { nameField.setText ( c.getName () ); } addLocal.setTitle ( c != null ? "Edit Local Calendar" : "Add Local Calendar" ); addLocal.setModal ( true ); Container content = addLocal.getContentPane (); content.setLayout ( new BorderLayout () ); JPanel buttonPanel = new JPanel (); buttonPanel.setLayout ( new FlowLayout () ); JButton cancel = new JButton ( "Cancel" ); cancel.addActionListener ( new ActionListener () { public void actionPerformed ( ActionEvent event ) { addLocal.dispose (); } } ); buttonPanel.add ( cancel ); JButton ok = new JButton ( c == null ? "Add" : "Save" ); ok.addActionListener ( new ActionListener () { public void actionPerformed ( ActionEvent event ) { try { String name = nameField.getText (); if ( name == null || name.trim ().length () == 0 ) { showError ( "You must provide a calendar name" ); return; } Color color = colorField.getSelectedColor (); showStatusMessage ( "Creating calendar..." ); Calendar cal = null; if ( c == null ) { cal = new Calendar ( getDataDirectory (), name ); } else { cal = c; } cal.setBackgroundColor ( color ); cal.setBorderColor ( getForegroundColorForBackground ( color ) ); cal.setForegroundColor ( getForegroundColorForBackground ( color ) ); cal.setLastUpdatedAsNow (); File file = new File ( getDataDirectory (), cal.getFilename () ); if ( c == null ) { // Create empty iCalendar file FileWriter writer = new FileWriter ( file ); ICalendarParser icalParser = new ICalendarParser ( ICalendarParser.PARSE_STRICT ); icalParser.toICalendar (); writer.write ( icalParser.toICalendar () ); writer.close (); } if ( c == null ) { showStatusMessage ( "New local calendar added" + ": " + name ); dataRepository.addCalendar ( getDataDirectory (), cal, false ); // This will call us back with calendarAdded (below) } else { // updating calendar... dataRepository.updateCalendar ( getDataDirectory (), cal ); showStatusMessage ( "Updated local calendar" + ": " + name ); } // System.out.println ( "Created cal file: " + file ); } catch ( Exception e1 ) { showError ( "Error writing calendar" + ": " + e1.getMessage () ); return; } addLocal.dispose (); } } ); buttonPanel.add ( ok ); content.add ( buttonPanel, BorderLayout.SOUTH ); JPanel main = new JPanel (); main.setBorder ( BorderFactory .createTitledBorder ( c == null ? "New Local Calendar" : "Edit Local Calendar" ) ); main.setLayout ( new GridLayout ( 2, 1 ) ); JPanel namePanel = new JPanel (); namePanel.setLayout ( new ProportionalLayout ( props, ProportionalLayout.HORIZONTAL_LAYOUT ) ); namePanel.add ( new JLabel ( "Calendar Name" + ": " ) ); namePanel.add ( nameField ); main.add ( namePanel ); JPanel colorPanel = new JPanel (); colorPanel.setLayout ( new ProportionalLayout ( props, ProportionalLayout.HORIZONTAL_LAYOUT ) ); colorPanel.add ( new JLabel ( "Background Color" + ": " ) ); JPanel colorSub = new JPanel (); colorSub.setLayout ( new BorderLayout () ); colorField.setSelectedColor ( c == null ? Color.blue : c .getBackgroundColor () ); colorSub.add ( colorField, BorderLayout.WEST ); colorPanel.add ( colorSub ); main.add ( colorPanel ); content.add ( main, BorderLayout.CENTER ); addLocal.pack (); addLocal.setVisible ( true ); } protected void importCSV () { new ImportDialog ( this, ImportDialog.IMPORT_CSV, getDataDirectory (), dataRepository ); } protected void importICalendar () { new ImportDialog ( this, ImportDialog.IMPORT_ICS, getDataDirectory (), dataRepository ); } protected void exportAll () { export ( "Export All", dataRepository.getAllEntries () ); } protected void exportVisible () { export ( "Export Visible", dataRepository.getVisibleEntries () ); } private void export ( String title, Vector eventEntries ) { JFileChooser fileChooser; File outFile = null; if ( lastExportDirectory == null ) fileChooser = new JFileChooser (); else fileChooser = new JFileChooser ( lastExportDirectory ); fileChooser.setFileSelectionMode ( JFileChooser.FILES_ONLY ); fileChooser.setFileFilter ( new ICSFileChooserFilter () ); fileChooser.setDialogTitle ( "Select Output File" ); fileChooser.setApproveButtonText ( "Save as ICS File" ); fileChooser .setApproveButtonToolTipText ( "Export entries to iCalendar file" ); int ret = fileChooser.showSaveDialog ( this ); if ( ret == JFileChooser.APPROVE_OPTION ) { outFile = fileChooser.getSelectedFile (); } else { // Cancel return; } // If no file extension provided, use ".ics" String basename = outFile.getName (); if ( basename.indexOf ( '.' ) < 0 ) { // No filename extension provided, so add ".csv" to it outFile = new File ( outFile.getParent (), basename + ".ics" ); } // System.out.println ( "Selected File: " + outFile.toString () ); lastExportDirectory = outFile.getParentFile (); if ( outFile.exists () && !outFile.canWrite () ) { JOptionPane.showMessageDialog ( parent, "You do not have the proper\npermissions to write to" + ":\n\n" + outFile.toString () + "\n\n" + "Please select another file.", "Save Error", JOptionPane.PLAIN_MESSAGE ); return; } if ( outFile.exists () ) { if ( JOptionPane.showConfirmDialog ( parent, "Overwrite existing file?" + "\n\n" + outFile.toString (), "Overwrite Confirm", JOptionPane.YES_NO_OPTION ) != 0 ) { JOptionPane.showMessageDialog ( parent, "Export canceled.", "Export canceled", JOptionPane.PLAIN_MESSAGE ); return; } } try { PrintWriter writer = new PrintWriter ( new FileWriter ( outFile ) ); // Now write! ICalendarParser p = new ICalendarParser ( PARSE_LOOSE ); DataStore dataStore = p.getDataStoreAt ( 0 ); for ( int i = 0; i < eventEntries.size (); i++ ) { Event j = (Event) eventEntries.elementAt ( i ); dataStore.storeEvent ( j ); } writer.write ( p.toICalendar () ); writer.close (); JOptionPane.showMessageDialog ( parent, "Exported to" + ":\n\n" + outFile.toString (), "Export", JOptionPane.PLAIN_MESSAGE ); } catch ( IOException e ) { JOptionPane.showMessageDialog ( parent, "An error was encountered\nwriting to the file" + ":\n\n" + e.getMessage (), "Save Error", JOptionPane.PLAIN_MESSAGE ); e.printStackTrace (); } } public void componentHidden ( ComponentEvent ce ) { } public void componentShown ( ComponentEvent ce ) { } // Handle moving of main window public void componentMoved ( ComponentEvent ce ) { saveWindowPreferences (); } public void componentResized ( ComponentEvent ce ) { saveWindowPreferences (); } public void propertyChange ( PropertyChangeEvent pce ) { // System.out.println ( "property Change: " + pce ); if ( pce.getPropertyName ().equals ( JSplitPane.DIVIDER_LOCATION_PROPERTY ) ) { saveWindowPreferences (); } } /** * Save current window width, height so we can restore on next run. */ public void saveWindowPreferences () { prefs.setMainWindowX ( this.getX () ); prefs.setMainWindowY ( this.getY () ); prefs.setMainWindowWidth ( this.getWidth () ); prefs.setMainWindowHeight ( this.getHeight () ); prefs.setMainWindowLeftVerticalSplitPosition ( leftVerticalSplit .getDividerLocation () ); prefs.setMainWindowHorizontalSplitPosition ( horizontalSplit .getDividerLocation () ); } public void eventAdded ( Event event ) { this.updateCategoryJList (); handleCalendarFilterSelection (); this.eventViewPanel.clear (); } public void eventUpdated ( Event event ) { this.updateCategoryJList (); handleCalendarFilterSelection (); this.eventViewPanel.clear (); } public void eventDeleted ( Event event ) { this.updateCategoryJList (); handleCalendarFilterSelection (); this.eventViewPanel.clear (); } public void eventSelected ( EventInstance eventInstance ) { SingleEvent se = (SingleEvent) eventInstance; Date eventDate = null; try { eventDate = new Date ( "DTSTART", eventInstance.getYear (), eventInstance .getMonth (), eventInstance.getDayOfMonth () ); } catch ( BogusDataException e1 ) { e1.printStackTrace (); return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -