📄 raplabuilder.java
字号:
}
}
}
}
// block should not be included if it doesn't contain one
// of the allocatables
if (!allocatableMatched && !emptyBlockVisible )
continue;
// An appointment should not be visible, if no
// selected allocatable is connected to this appointment.
for (int i=0; i<appointments.length; i++)
{
if ( emptyBlockVisible
|| wildcardConnected
|| connectedAppointments.contains(appointments[i]))
{
// Add appointment to the blocks
appointments[i].createBlocks(start, end, preparedBlocks, excludeExceptions);
// Here the other exceptions should be added
}
}
}
return preparedBlocks;
}
static public AppointmentBlockArray splitBlocks(AppointmentBlockArray preparedBlocks, Date startDate, Date endDate) {
int size = preparedBlocks.size();
AppointmentBlockArray result = new AppointmentBlockArray();
for (int i=0;i<size;i++) {
long blockStart = preparedBlocks.getStartAt(i);
long blockEnd = preparedBlocks.getEndAt(i);
Appointment appointment = preparedBlocks.getAppointmentAt(i);
boolean isException = preparedBlocks.isException(i);
if (DateTools.isSameDay(blockStart, blockEnd)) {
result.add( blockStart, blockEnd, appointment, isException);
} else {
long firstBlockDate = Math.max(blockStart, startDate.getTime());
long lastBlockDate = Math.min(blockEnd, endDate.getTime());
long currentBlockDate = firstBlockDate;
while ( currentBlockDate >= blockStart && DateTools.cutDate( currentBlockDate ) < lastBlockDate) {
long start;
long end;
if (DateTools.isSameDay(blockStart, currentBlockDate)) {
start= blockStart;
} else {
start = DateTools.cutDate(currentBlockDate);
}
if (DateTools.isSameDay(blockEnd, currentBlockDate) && !DateTools.isMidnight(blockEnd)) {
end = blockEnd;
}else {
end = DateTools.fillDate( currentBlockDate ) -1;
}
//System.out.println("Adding Block " + new Date(start) + " - " + new Date(end));
result.add ( start, end, appointment,isException);
currentBlockDate+= DateTools.MILLISECONDS_PER_DAY;
}
}
}
return result;
}
/** selects all blocks that should be visible and calculates the max start- and end-time */
public void prepareBuild(Date start,Date end) {
boolean excludeExceptions = isExceptionsExcluded();
HashSet allReservations = new HashSet( selectedReservations);
allReservations.addAll( allReservationsForAllocatables);
preparedBlocks = getAffectedAppointments(allReservations, selectedAllocatables, start, end, isEmptyBlockVisible, excludeExceptions);
preparedBlocks = splitBlocks(preparedBlocks, start, end);
// calculate new start and end times
max =0;
min =24;
for (int i=0; i<preparedBlocks.size(); i++)
{
int starthour = DateTools.getHourOfDay(preparedBlocks.getStartAt(i));
int startminute = DateTools.getMinuteOfHour(preparedBlocks.getStartAt(i));
int endhour = DateTools.getHourOfDay(preparedBlocks.getEndAt(i));
int endminute = DateTools.getMinuteOfHour(preparedBlocks.getEndAt(i));
if ((starthour != 0 || startminute != 0) && starthour<min)
min = starthour;
if (endhour<min && (endhour >0 || endminute>0))
min = Math.max(0,endhour-1);
if ((endhour != 0 || endminute != 0) && (endhour != 23 && endminute!=59) && endhour>max)
max = Math.min(24,endhour + 1);
if (starthour>=max)
max = Math.min(24,starthour +1);
}
}
public int getMin() {
Assert.notNull(preparedBlocks, "call prepareBuild first");
return min;
}
public int getMax() {
Assert.notNull(preparedBlocks, "call prepareBuild first");
return max;
}
abstract Block createBlock(RaplaBlockContext blockContext, Date start, Date end);
public void build(CalendarView wv) {
ArrayList blocks = new ArrayList();
BuildContext buildContext = new BuildContext(this, blocks);
Assert.notNull(preparedBlocks, "call prepareBuild first");
int size = preparedBlocks.size();
for (int i=0;i<size;i++) {
Date start = new Date( preparedBlocks.getStartAt(i) );
Date end = new Date( preparedBlocks.getEndAt(i) );
Appointment appointment = preparedBlocks.getAppointmentAt( i);
RaplaBlockContext[] blockContext = getBlocksForAppointment( appointment, buildContext );
for ( int j=0;j< blockContext.length; j++) {
blocks.add( createBlock(blockContext[j], start, end));
}
}
buildStrategy.build(wv, blocks);
}
private RaplaBlockContext[] getBlocksForAppointment(Appointment appointment, BuildContext buildContext) {
boolean isEventSelected = selectedReservations.contains( appointment.getReservation());
RaplaBlockContext firstContext = new RaplaBlockContext( appointment, this, buildContext, null, isEventSelected );
List selectedAllocatables = firstContext.getColoredAllocatables();
if ( !splitByAllocatables || selectedAllocatables.size() < 2) {
return new RaplaBlockContext[] { firstContext };
}
RaplaBlockContext[] context = new RaplaBlockContext[ selectedAllocatables.size() ];
for ( int i= 0;i<context.length;i ++) {
context[i] = new RaplaBlockContext( appointment, this, buildContext, (Allocatable) selectedAllocatables.get( i ), isEventSelected);
}
return context;
}
private boolean isMovable(Reservation reservation) {
return reservation.getOwner().equals(editingUser)
|| (editingUser != null && editingUser.isAdmin());
}
/** This context contains the shared information for all RaplaBlocks.*/
static class BuildContext {
boolean bResourceVisible = true;
boolean bPersonVisible = true;
boolean bRepeatingVisible = true;
boolean bTimeVisible = false;
Map colors;
I18nBundle i18n;
RaplaLocale raplaLocale;
RaplaContext serviceManager;
Logger logger;
User user;
List blocks;
Category canReadOthersGroup;
public BuildContext(RaplaBuilder builder, List blocks)
{
this.blocks = blocks;
this.raplaLocale = builder.getRaplaLocale();
this.bResourceVisible = builder.bResourceVisible;
this.bPersonVisible= builder.bPersonVisible;
this.bTimeVisible= builder.isTimeVisible();
this.bRepeatingVisible= builder.bRepeatingVisible;
this.colors = (Map) builder.colors.clone();
this.i18n =builder.getI18n();
this.serviceManager = builder.getContext();
this.logger = builder.getLogger();
this.user = builder.editingUser;
try {
this.canReadOthersGroup = builder.getClientFacade().getUserGroupsCategory().getCategory( Permission.GROUP_CAN_READ_EVENTS_FROM_OTHERS);
} catch (RaplaException ex)
{
// we assume that there is no other group
}
}
public RaplaLocale getRaplaLocale() {
return raplaLocale;
}
public RaplaContext getServiceManager() {
return serviceManager;
}
public Icon getRepeatingIcon() {
return i18n.getIcon("icon.repeating");
}
public List getBlocks()
{
return blocks;
}
public ImageIcon getExceptionBackgroundIcon() {
return i18n.getIcon("icon.exceptionBackground");
}
public I18nBundle getI18n() {
return i18n;
}
public boolean isTimeVisible() {
return bTimeVisible;
}
public boolean isPersonVisible() {
return bPersonVisible;
}
public boolean isRepeatingVisible() {
return bRepeatingVisible;
}
public boolean isResourceVisible() {
return bResourceVisible;
}
public String lookupColorString(Allocatable allocatable) {
if (allocatable == null)
return RaplaColorList.DEFAULT_COLOR_AS_STRING;
return (String) colors.get(allocatable);
}
}
/** This context contains the shared information for one particular RaplaBlock.*/
static class RaplaBlockContext {
ArrayList coloredAllocatables = new ArrayList(3);
ArrayList matchingAllocatables = new ArrayList(3);
Appointment appointment;
boolean movable;
BuildContext buildContext;
boolean isEventSelected;
public RaplaBlockContext(Appointment appointment,RaplaBuilder builder,BuildContext buildContext, Allocatable coloredAllocatable, boolean isEventSelected) {
this.buildContext = buildContext;
this.appointment = appointment;
this.isEventSelected = isEventSelected;
Reservation reservation = appointment.getReservation();
this.movable = builder.isMovable( reservation );
// Prefer resources when grouping
addAllocatables(builder, reservation.getResources(), coloredAllocatable);
addAllocatables(builder, reservation.getPersons(), coloredAllocatable);
}
private void addAllocatables(RaplaBuilder builder, Allocatable[] allocatables,Allocatable coloredAllocatable) {
Reservation reservation = appointment.getReservation();
for (int i=0; i<allocatables.length; i++) {
if ( !reservation.hasAllocated( allocatables[i], appointment ) ) {
continue;
}
matchingAllocatables.add(allocatables[i]);
if ( builder.isResourceColoring && builder.selectedAllocatables.contains(allocatables[i])) {
if ( coloredAllocatable == null || coloredAllocatable.equals( allocatables[i]) ) {
coloredAllocatables.add(allocatables[i]);
}
}
}
}
public boolean isMovable() {
return movable;// && !isAnonymous;
}
public Appointment getAppointment() {
return appointment;
}
public List getColoredAllocatables() {
return coloredAllocatables;
}
public List getAllocatables() {
return matchingAllocatables;
}
public boolean isVisible(Allocatable allocatable) {
User user = buildContext.user;
if ( user != null && !allocatable.canRead( user) ) {
return false;
}
return matchingAllocatables.contains(allocatable);
}
public BuildContext getBuildContext() {
return buildContext;
}
/**
* @return null if no allocatables found
*/
public Allocatable getGroupAllocatable() {
// Look if block belongs to a group according to the selected allocatables
List allocatables = getColoredAllocatables();
// Look if block belongs to a group according to its reserved allocatables
if ( allocatables.size() == 0)
allocatables = getAllocatables();
if ( allocatables.size() == 0)
return null;
return (Allocatable) allocatables.get( 0 );
}
public boolean isEventSelected() {
return isEventSelected;
}
public boolean isAnonymous() {
User user = buildContext.user;
Reservation event = appointment.getReservation();
if ( user == null || user.equals( event.getOwner()) || user.isAdmin())
{
return false;
}
if ( buildContext.canReadOthersGroup == null || user.belongsTo( buildContext.canReadOthersGroup))
{
return false;
}
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -