📄 editeventwindow.java
字号:
status.setSelectedIndex ( 0 ); break; } JPanel statusSubPanel = new JPanel ( new BorderLayout () ); statusSubPanel.add ( status, BorderLayout.WEST ); statusPanel.add ( statusSubPanel ); upperPanel.add ( statusPanel ); JPanel calPanel = new JPanel (); calPanel.setLayout ( new ProportionalLayout ( proportions, ProportionalLayout.HORIZONTAL_LAYOUT ) ); prompt = new JLabel ( "Calendar: " ); prompt.setHorizontalAlignment ( SwingConstants.RIGHT ); calPanel.add ( prompt ); Vector<Calendar> writableCalendars = new Vector<Calendar> (); for ( int i = 0; i < this.repo.getCalendars ().size (); i++ ) { Calendar c = this.repo.getCalendars ().elementAt ( i ); if ( c.getType () == Calendar.LOCAL_CALENDAR ) { writableCalendars.addElement ( c ); } else if ( c.getType () == Calendar.REMOTE_ICAL_CALENDAR && c.getCanWrite () ) { writableCalendars.addElement ( c ); } } // TODO: show error if no local calendars found calendar = new JComboBox ( writableCalendars ); calendar.setRenderer ( new ComboBoxRenderer () ); if ( selectedCalendar == null ) calendar.setSelectedIndex ( 0 ); else calendar.setSelectedItem ( selectedCalendar ); JPanel calSubPanel = new JPanel ( new BorderLayout () ); calSubPanel.add ( calendar, BorderLayout.WEST ); calPanel.add ( calSubPanel ); upperPanel.add ( calPanel ); JPanel catPanel = new JPanel (); catPanel.setLayout ( new ProportionalLayout ( proportions, ProportionalLayout.HORIZONTAL_LAYOUT ) ); prompt = new JLabel ( "Categories: " ); prompt.setHorizontalAlignment ( SwingConstants.RIGHT ); catPanel.add ( prompt ); categories = new JTextField (); if ( event != null && event.getCategories () != null ) categories.setText ( event.getCategories ().getValue () ); catPanel.add ( categories ); upperPanel.add ( catPanel ); allButButtons.add ( upperPanel, BorderLayout.NORTH ); // TODO: eventually add some edit buttons/icons here when // we support more than plain text. JPanel descrPanel = new JPanel (); descrPanel.setLayout ( new BorderLayout () ); description = new JTextArea (); description.setLineWrap ( true ); description.setWrapStyleWord ( true ); if ( event != null && event.getDescription () != null ) description.setText ( event.getDescription ().getValue () ); description.setCaretPosition ( 0 ); JScrollPane scrollPane = new MyScrollPane ( description ); descrPanel.add ( scrollPane, BorderLayout.CENTER ); allButButtons.add ( descrPanel, BorderLayout.CENTER ); if ( newEvent ) allDay.setSelected ( false ); else allDay.setSelected ( event.getStartDate ().isDateOnly () ); toggleAllDay (); getContentPane ().add ( allButButtons, BorderLayout.CENTER ); } protected MaskFormatter createFormatter ( String s ) { MaskFormatter formatter = null; try { formatter = new MaskFormatter ( s ); } catch ( java.text.ParseException exc ) { System.err.println ( "formatter is bad: " + exc.getMessage () ); System.exit ( -1 ); } return formatter; } void save () { // Verify a calendar was selected Calendar c = (Calendar) this.calendar.getSelectedItem (); if ( c == null ) { JOptionPane.showMessageDialog ( parent, "You must select a calendar.", "Error", JOptionPane.ERROR_MESSAGE ); return; } // Verify a valid date was entered java.util.Calendar calendar = dateChooser.getCalendar (); if ( calendar == null ) { JOptionPane.showMessageDialog ( parent, "You have not entered a valid start date.", "Error", JOptionPane.ERROR_MESSAGE ); } this.event.getStartDate ().setYear ( calendar.get ( java.util.Calendar.YEAR ) ); this.event.getStartDate ().setMonth ( calendar.get ( java.util.Calendar.MONTH ) + 1 ); this.event.getStartDate ().setDay ( calendar.get ( java.util.Calendar.DAY_OF_MONTH ) ); // handle repeat type switch ( repeatType.getSelectedIndex () ) { case REPEAT_NONE: this.event.setRrule ( null ); break; case REPEAT_DAILY: this.event.setRrule ( new Rrule ( Rrule.FREQ_DAILY ) ); break; case REPEAT_WEEKLY: this.event.setRrule ( new Rrule ( Rrule.FREQ_WEEKLY ) ); break; case REPEAT_MONTHLY: this.event.setRrule ( new Rrule ( Rrule.FREQ_MONTHLY ) ); break; case REPEAT_YEARLY: this.event.setRrule ( new Rrule ( Rrule.FREQ_YEARLY ) ); break; default: System.err.println ( "Error: unknown repeat type" ); return; } if ( this.allDay.isSelected () ) { this.event.getStartDate ().setDateOnly ( true ); } else { this.event.getStartDate ().setDateOnly ( false ); int h = 0, m = 0; try { h = Integer.parseInt ( timeHour.getText () ); m = Integer.parseInt ( timeMinute.getText () ); } catch ( NumberFormatException e1 ) { JOptionPane.showMessageDialog ( parent, "You have not entered a valid start time.", "Error", JOptionPane.ERROR_MESSAGE ); return; } if ( h > 23 || m > 59 ) { JOptionPane.showMessageDialog ( parent, "You have not entered a valid start time.", "Error", JOptionPane.ERROR_MESSAGE ); return; } if ( ampm.getText ().equals ( "AM" ) ) { if ( h == 12 ) h = 0; } else { // PM if ( h < 12 ) h += 12; } this.event.getStartDate ().setHour ( h ); this.event.getStartDate ().setMinute ( m ); this.event.getStartDate ().setSecond ( 0 ); } // Note: LAST-MODIFIED gets updated by call to saveEvent if ( seq != null ) { // TODO: some have suggested that the sequence number should // only change if the date/time or location is modified. event.setSequence ( seq ); seq = null; } try { this.event.getDescription ().setValue ( description.getText () ); this.event.getSummary ().setValue ( subject.getText ().trim () ); String cats = categories.getText ().trim (); if ( cats.length () == 0 ) this.event.setCategories ( null ); else this.event.getCategories ().setValue ( cats ); String loc = location.getText ().trim (); if ( loc.length () == 0 ) this.event.setLocation ( null ); else this.event.getLocation ().setValue ( loc ); String urlStr = url.getText ().trim (); if ( urlStr.length () == 0 ) this.event.setUrl ( null ); else this.event.getUrl ().setValue ( urlStr ); IntegerChoice ic = (IntegerChoice) status.getSelectedItem (); this.event.setStatus ( ic.value ); // Did the event move from one calendar to another? if ( c.equals ( this.selectedCalendar ) ) { // No, this event is on the same calendar as before. // Download the old calendar from the server before we save (if // required). syncCalendarIfRequired ( c ); repo.saveEvent ( c, this.event ); putRemoteCalendarIfRequired ( c ); } else { // New event or event was moved from one calendar to another. if ( !this.newEvent ) { // Calendar moved from one calendar to another. // Delete from old calendar, buy sync with remote first if required. syncCalendarIfRequired ( this.selectedCalendar ); // TODO: if this is remote calendar, we may want to cancel the event // rather than delete it. repo.deleteEvent ( this.selectedCalendar, this.event ); // Clear out the user data for the event (where the calendar // info is stored.) this.event.setUserData ( null ); putRemoteCalendarIfRequired ( this.selectedCalendar ); } // Download the calendar that the event was moved to (if required). syncCalendarIfRequired ( c ); // Add to new calendar repo.saveEvent ( c, this.event ); putRemoteCalendarIfRequired ( c ); // If the above call fails, what should we do??? } } catch ( IOException e2 ) { // TODO: add error handler that pops up a window here e2.printStackTrace (); } this.dispose (); } private boolean syncCalendarIfRequired ( Calendar cal ) { if ( cal.getUrl () != null && cal.getCanWrite () && cal.getSyncBeforePublish () ) { System.out.println ( "Get remote calendar: " + cal ); DataFile df = repo.getDataFileForCalendar ( cal ); File file = new File ( df.getAbsolutePath () + ".sync" ); HttpClientStatus result = HttpClient.getRemoteCalendar ( cal.getUrl (), cal.getAuthUsername (), cal.getAuthPassword (), file ); if ( result.getStatus () == HttpClientStatus.HTTP_STATUS_SUCCESS && file.exists () ) { // Rename file. df.delete (); file.renameTo ( df ); repo.updateCalendar ( df.getParentFile (), cal ); return true; // success } else { // Cannot proceed showError ( "Error syncing remote calendar\nbefore writing." ); return false; } } else { // do not need to sync return true; // success } } private boolean putRemoteCalendarIfRequired ( Calendar cal ) { if ( cal.getUrl () != null && cal.getCanWrite () ) { System.out.println ( "put remote calendar: " + cal ); DataFile df = repo.getDataFileForCalendar ( cal ); HttpClientStatus result = HttpClient.putRemoteCalendar ( cal.getUrl (), cal.getAuthUsername (), cal.getAuthPassword (), df ); if ( result.getStatus () == HttpClientStatus.HTTP_STATUS_SUCCESS ) { return true; // success } else { // Cannot proceed showError ( "Error writing calendar to server" ); return false; } } else { // Calendar not writable to server return true; } } void toggleAllDay () { this.event.getStartDate ().setDateOnly ( allDay.isSelected () ); // Hide/Unhide the time edit timeAt.setVisible ( !allDay.isSelected () ); timeHour.setVisible ( !allDay.isSelected () ); timeSep.setVisible ( !allDay.isSelected () ); timeMinute.setVisible ( !allDay.isSelected () ); ampm.setVisible ( !allDay.isSelected () ); } void close () { // TODO: check for unsaved changes this.dispose (); } void showError ( String message ) { System.err.println ( "Error: " + message ); JOptionPane.showMessageDialog ( parent, message, "Error", JOptionPane.ERROR_MESSAGE ); } 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 (); } /** * Save current window width, height so we can restore on next run. */ public void saveWindowPreferences () { prefs.setEditWindowWidth ( this.getWidth () ); prefs.setEditWindowHeight ( this.getHeight () ); prefs.setEditWindowX ( this.getX () ); prefs.setEditWindowY ( this.getY () ); }}class ToggleLabel extends JLabel implements MouseListener { String[] choices; int selected; Color fg, mouseOverColor; private Cursor defaultCursor = null; private Cursor handCursor = null; public ToggleLabel(String[] choices) { super ( choices[0] ); this.choices = choices; this.selected = 0; this.addMouseListener ( this ); this.fg = this.getForeground (); this.mouseOverColor = new Color ( 0, 0, 255 ); } public void setSelected ( String str ) { for ( int i = 0; i < choices.length; i++ ) { if ( str.equals ( choices[i] ) ) { this.selected = i; this.setText ( choices[i] ); } } } public void mousePressed ( MouseEvent e ) { } public void mouseReleased ( MouseEvent e ) { } public void mouseClicked ( MouseEvent e ) { this.selected++; this.selected %= choices.length; this.setText ( choices[this.selected] ); } public void mouseEntered ( MouseEvent e ) { this.setForeground ( mouseOverColor ); // change cursor if ( this.defaultCursor == null ) this.defaultCursor = this.getCursor (); if ( this.handCursor == null ) this.handCursor = new Cursor ( Cursor.HAND_CURSOR ); this.setCursor ( this.handCursor ); } public void mouseExited ( MouseEvent e ) { this.setForeground ( fg ); this.setCursor ( this.defaultCursor ); }}/** * Override JDateChooser so that we can expand the minimum width, which normally * doesn't leave enough width to display the full text of the date. */class MyDateChooser extends JDateChooser { public MyDateChooser() { super (); } public Dimension getPreferredSize () { Dimension d = super.getPreferredSize (); return new Dimension ( d.width + 10, d.height ); }}/* * Override the default ListCellRenderer for the Calendar JComboBox so that we * can include a small box icon to the left of the Calendar name that displays * the Calendar color. */class ComboBoxRenderer extends JLabel implements ListCellRenderer { public ComboBoxRenderer() { setOpaque ( true ); setHorizontalAlignment ( CENTER ); setVerticalAlignment ( CENTER ); } public Component getListCellRendererComponent ( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus ) { if ( isSelected ) { setBackground ( list.getSelectionBackground () ); setForeground ( list.getSelectionForeground () ); } else { setBackground ( list.getBackground () ); setForeground ( list.getForeground () ); } Calendar c = (Calendar) value; setFont ( list.getFont () ); setText ( c.getName () ); this.setHorizontalAlignment ( SwingConstants.LEFT ); setIcon ( Utils.buildColoredIcon ( c.getBackgroundColor (), c .getForegroundColor () ) ); return this; } /** * Build an icon that shows the Calendar's colors. * * @param fill * The main color (Calendar.bg) * @param border * The border color (Calendar.fg) * @return The new ImageIcon for the specified colors */ private ImageIcon buildIcon ( Color fill, Color border ) { int WIDTH = 16; int HEIGHT = 16; BufferedImage bufimage = new BufferedImage ( WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB ); Graphics g = bufimage.getGraphics (); g.setColor ( fill ); g.fillRect ( 0, 0, WIDTH - 1, HEIGHT - 1 ); // Draw border g.setColor ( border ); g.drawLine ( 0, 0, WIDTH - 1, 0 ); g.drawLine ( WIDTH - 1, 0, WIDTH - 1, HEIGHT - 1 ); g.drawLine ( WIDTH - 1, HEIGHT - 1, 0, HEIGHT - 1 ); g.drawLine ( 0, HEIGHT - 1, 0, 0 ); g.dispose (); return new ImageIcon ( bufimage ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -