copyplugininit.java
来自「Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI」· Java 代码 · 共 171 行
JAVA
171 行
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| 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. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.plugin.periodcopy;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.ListModel;
import org.rapla.entities.Entity;
import org.rapla.entities.domain.Appointment;
import org.rapla.entities.domain.Period;
import org.rapla.entities.domain.Repeating;
import org.rapla.entities.domain.Reservation;
import org.rapla.facade.PeriodModel;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.CalendarModel;
import org.rapla.gui.MenuExtensionPoint;
import org.rapla.gui.RaplaGUIComponent;
import org.rapla.gui.toolkit.DialogUI;
import org.rapla.gui.toolkit.RaplaMenuItem;
import org.rapla.gui.toolkit.RaplaSeparator;
import org.rapla.plugin.RaplaExtensionPoints;
public class CopyPluginInit extends RaplaGUIComponent
{
public CopyPluginInit(RaplaContext sm) throws RaplaException {
super(sm);
setChildBundleName( PeriodCopyPlugin.RESOURCE_FILE);
MenuExtensionPoint menu = (MenuExtensionPoint) getService( RaplaExtensionPoints.EDIT_MENU_EXTENSION_POINT);
menu.insert( new RaplaSeparator("info_end"));
menu.insert(createInfoMenu());
}
private RaplaMenuItem createInfoMenu( ) {
RaplaMenuItem item = new RaplaMenuItem("copy_events");
// ResourceBundle bundle = ResourceBundle.getBundle( "org.rapla.plugin.periodcopy.PeriodCopy");
final String label =getString("copy_events") ;
//bundle.getString("copy_events");
item.setText( label );
item.setIcon( getIcon("icon.copy") );
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
final CopyDialog useCase = new CopyDialog(getContext());
String[] buttons = new String[]{getString("abort"), getString("copy") };
final DialogUI dialog = DialogUI.create( getContext(),getMainComponent(),true, useCase.getComponent(), buttons);
dialog.setTitle( label);
dialog.setSize( 400, 400);
dialog.getButton( 0).setIcon( getIcon("icon.abort"));
dialog.getButton( 1).setIcon( getIcon("icon.copy"));
dialog.getButton( 1).setEnabled(false);
final JComboBox sourceBox = useCase.getSourceChooser();
final JComboBox destBox = useCase.getDestChooser();
PeriodModel periodModel = getPeriodModel();
final CalendarModel model = (CalendarModel) getService( CalendarModel.ROLE);
Period period = (Period) periodModel.getNearestPeriodForStartDate( model.getSelectedDate());
if ( period != null) {
sourceBox.setSelectedItem( period );
destBox.setSelectedItem( period );
useCase.getReservationList().setModel( getReservationList( model, period) );
}
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int destIndex = destBox.getSelectedIndex();
int sourceIndex = sourceBox.getSelectedIndex();
dialog.getButton( 1).setEnabled( destIndex >= 0 && sourceIndex >=0 && destIndex != sourceIndex);
Period sourcePeriod = (Period)sourceBox.getSelectedItem();
try {
useCase.getReservationList().setModel( getReservationList( model, sourcePeriod) );
} catch (Exception ex ) {
showException( ex, getMainComponent());
}
}
};
sourceBox.addActionListener( listener );
destBox.addActionListener( listener );
dialog.startNoPack();
if ( dialog.getSelectedIndex() == 1) {
Period sourcePeriod = (Period)sourceBox.getSelectedItem();
Period destPeriod = (Period)destBox.getSelectedItem();
if (sourcePeriod != null && destPeriod != null)
copy( model, sourcePeriod,destPeriod );
}
} catch (Exception ex) {
showException( ex, getMainComponent() );
}
}
});
return item;
}
private ListModel getReservationList(CalendarModel model, Period period) throws RaplaException {
DefaultListModel listModel = new DefaultListModel();
if ( period != null) {
Reservation[] reservations = model.getReservations( period.getStart(), period.getEnd() );
for ( int i=0; i<reservations.length; i++) {
listModel.addElement( reservations[i].getName( getLocale() ) );
}
}
return listModel;
}
public void copy(CalendarModel model, Period sourcePeriod, Period destPeriod) throws RaplaException {
Reservation[] reservations = model.getReservations( sourcePeriod.getStart(), sourcePeriod.getEnd() );
List newReservations = new ArrayList();
for (int i=0;i< reservations.length;i++) {
Reservation r = (Reservation) getModification().clone( reservations[i]);
Appointment[] appointments = r.getAppointments();
for ( int j=0;j<appointments.length;j++) {
Appointment app = appointments[j];
Repeating repeating = app.getRepeating();
if ( repeating == null ) {
r.removeAppointment( app );
continue;
}
Date oldStart = app.getStart();
Date newStart ;
if ( repeating.getType().equals ( Repeating.DAILY)) {
newStart = getRaplaLocale().toDate( destPeriod.getStart(), oldStart );
} else {
Calendar calendar = getRaplaLocale().createCalendar();
calendar.setTime( oldStart);
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
calendar = getRaplaLocale().createCalendar();
calendar.setTime(destPeriod.getStart());
calendar.set( Calendar.DAY_OF_WEEK, weekday);
if ( calendar.getTime().before( destPeriod.getStart())) {
calendar.add( Calendar.DATE, 7);
}
Date firstOccOfWeekday = calendar.getTime();
newStart = getRaplaLocale().toDate( firstOccOfWeekday, oldStart );
}
app.move( newStart) ;
if ( !repeating.isFixedNumber())
repeating.setEnd( destPeriod.getEnd());
// System.out.println(reservations[i].getName( getRaplaLocale().getLocale()));
}
if ( r.getAppointments().length > 0) {
newReservations.add( r );
}
}
getModification().storeObjects( (Entity[]) newReservations.toArray( Entity.ENTITY_ARRAY) );
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?