📄 htmlmonthview.java
字号:
/*--------------------------------------------------------------------------*
| 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.components.calendarview.html;
import java.util.*;
import org.rapla.components.calendarview.Block;
import org.rapla.components.calendarview.Builder;
public class HTMLMonthView extends AbstractHTMLView {
public final static int ROWS = 6; //without the header row
public final static int COLUMNS = 7;
/** shared calendar instance. Only used for temporary stored values. */
int daysInMonth;
HTMLSmallDaySlot[] slots;
void calcMinMaxDates(Date date) {
blockCalendar.setTime( date );
blockCalendar.set(Calendar.DAY_OF_MONTH, 1);
blockCalendar.set(Calendar.HOUR_OF_DAY,0);
blockCalendar.set(Calendar.MINUTE,0);
blockCalendar.set(Calendar.SECOND,0);
blockCalendar.set(Calendar.MILLISECOND,0);
this.startDate = blockCalendar.getTime();
blockCalendar.set(Calendar.MILLISECOND,1);
this.daysInMonth = blockCalendar.getActualMaximum( Calendar.DAY_OF_MONTH ) ;
blockCalendar.set(Calendar.MILLISECOND,0);
blockCalendar.add(Calendar.DATE, this.daysInMonth);
this.endDate = blockCalendar.getTime();
}
public Collection getBlocks() {
ArrayList list = new ArrayList();
for (int i=0;i<slots.length;i++) {
list.addAll(slots[i]);
}
return Collections.unmodifiableCollection( list );
}
private boolean isExcluded(int column) {
int weekDay = weekdayMapper.dayForIndex( column);
if (excludeDays == null || !excludeDays.contains(new Integer(weekDay))) {
return false;
}
// find first occurance of weekDay in month
blockCalendar.setTime(startDate);
blockCalendar.set(Calendar.DAY_OF_WEEK, weekDay);
if (blockCalendar.get(Calendar.DATE) > 15) {
blockCalendar.add(Calendar.DATE, 7);
}
int startField = blockCalendar.get(Calendar.DATE) -1;
for ( int i=startField;i < slots.length;i+=7 ) {
if (!slots[i].isEmpty() ) {
return false;
}
}
return true;
}
public void rebuild() {
String[] headerNames = weekdayMapper.getNames();
// we need to clone the calendar, because we modify the calendar object int the getExclude() method
Calendar counter = (Calendar) blockCalendar.clone();
// calculate the blocks
Iterator it= builders.iterator();
while (it.hasNext()) {
Builder b= (Builder)it.next();
b.prepareBuild(getStartDate(),getEndDate());
}
slots = new HTMLSmallDaySlot[ daysInMonth ];
for (int i=0;i<slots.length;i++) {
slots[i] = new HTMLSmallDaySlot(String.valueOf( i + 1));
}
it= builders.iterator();
while (it.hasNext()) {
Builder b= (Builder)it.next();
if (b.isEnabled()) { b.build(this); }
}
counter.setTime(startDate);
int lastRow = 0;
HTMLSmallDaySlot[][] table = new HTMLSmallDaySlot[ROWS][COLUMNS];
for (int i=0; i<daysInMonth; i++) {
int weekday = counter.get(Calendar.DAY_OF_WEEK);
int column = weekdayMapper.indexForDay( weekday );
int row = (counter.get(Calendar.DATE) + 6 - column ) / 7;
table[row][column] = slots[i];
lastRow = row;
slots[i].sort();
counter.add(Calendar.DATE,1);
}
StringBuffer result = new StringBuffer();
result.append("<table class=\"month_table\">\n");
result.append("<tr>\n");
for (int i=0;i<COLUMNS;i++) {
if (isExcluded(i)) {
continue;
}
result.append("<td class=\"month_header\" width=\"14%\">");
result.append("<nobr>");
result.append(headerNames[i]);
result.append("</nobr>");
result.append("</td>");
}
result.append("\n</tr>");
for (int row=0; row<=lastRow; row++) {
boolean excludeRow = true;
// calculate if we can exclude the row
for (int column = 0; column<COLUMNS; column ++) {
if ( table[row][column] != null && !isExcluded( column )) {
excludeRow = false;
}
}
if ( excludeRow )
continue;
result.append("<tr>\n");
for (int column = 0; column<COLUMNS; column ++) {
if ( isExcluded( column )) {
continue;
}
HTMLSmallDaySlot slot = table[row][column];
if ( slot == null ) {
result.append("<td class=\"month_cell\" height=\"40\"></td>\n");
} else {
result.append("<td class=\"month_cell\" valign=\"top\" height=\"40\">\n");
slot.paint( result );
result.append("</td>\n");
}
}
result.append("</tr>\n");
}
result.append("</table>");
m_html = result.toString();
}
public void addBlock(Block block,int slot) {
checkBlock( block );
blockCalendar.setTime(block.getStart());
int day = blockCalendar.get(Calendar.DATE);
slots[day-1].putBlock( block );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -