📄 swingmonthview.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Gereon Fassbender, 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.swing;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import javax.swing.JComponent;
import javax.swing.JLabel;
import org.rapla.components.calendarview.Block;
import org.rapla.components.calendarview.Builder;
import org.rapla.components.layout.TableLayout;
/** Graphical component for displaying a calendar like monthview.
*
* @version CVS $Revision: 1.4 $ $Date: 2006/08/15 16:14:47 $
*/
public class SwingMonthView extends AbstractSwingCalendar
{
private static final long serialVersionUID = 1L;
public final static int ROWS = 6; //without the header row
public final static int COLUMNS = 7;
private SmallDaySlot[] slots;
DraggingHandler draggingHandler = new DraggingHandler(this, false);
SelectionHandler selectionHandler = new SelectionHandler(this);
private int daysInMonth;
public SwingMonthView() {
this(true);
}
public SwingMonthView(boolean showScrollPane) {
super( showScrollPane );
}
void calcMinMaxDates(Date date) {
Calendar calendar = createCalendar();
calendar.setTime( date );
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MILLISECOND,0);
this.setStartDate( calendar.getTime() );
calendar.set(Calendar.MILLISECOND,1);
this.daysInMonth = calendar.getActualMaximum( Calendar.DAY_OF_MONTH ) ;
calendar.set(Calendar.MILLISECOND,0);
calendar.add(Calendar.DATE, this.daysInMonth);
this.setEndDate( calendar.getTime() );
}
private boolean isExcluded(int column) {
Calendar calendar = createCalendar();
int weekDay = weekdayMapper.dayForIndex( column);
if (excludeDays == null || !excludeDays.contains(new Integer(weekDay))) {
return false;
}
// find first occurance of weekDay in month
calendar.setTime(getStartDate());
calendar.set(Calendar.DAY_OF_WEEK, weekDay);
if (calendar.get(Calendar.DATE) > 15) {
calendar.add(Calendar.DATE, 7);
}
int startField = calendar.get(Calendar.DATE) -1;
for ( int i=startField;i < slots.length;i+=7 ) {
if (!slots[i].isEmpty() ) {
return false;
}
}
return true;
}
public Collection getBlocks(int dayOfMonth) {
int index = dayOfMonth-1;
return Collections.unmodifiableCollection(slots[ index ].getBlocks());
}
public Collection getBlocks() {
ArrayList list = new ArrayList();
for (int i=0;i<slots.length;i++) {
list.addAll(slots[i].getBlocks());
}
return Collections.unmodifiableCollection( list );
}
public void setEditable(boolean b) {
super.setEditable( b);
if ( slots == null )
return;
// Hide the rest
for (int i= 0;i<slots.length;i++) {
SmallDaySlot slot = slots[i];
if (slot == null) continue;
slot.setEditable(b);
}
}
public void rebuild() {
// we need to clone the calendar, because we modify the calendar object in the getExclude() method
Calendar counter = createCalendar();
// calculate the blocks
Iterator it= builders.iterator();
while (it.hasNext()) {
Builder b= (Builder)it.next();
b.prepareBuild(getStartDate(),getEndDate() );
}
// create fields
slots = new SmallDaySlot[daysInMonth];
counter.setTime(getStartDate());
for (int i=0; i<daysInMonth; i++) {
createField(i, counter.getTime());
counter.add(Calendar.DATE,1);
}
// clear everything
jHeader.removeAll();
jCenter.removeAll();
// build Blocks
it= builders.iterator();
while (it.hasNext()) {
Builder b= (Builder)it.next();
if (b.isEnabled()) { b.build(this); }
}
TableLayout tableLayout= new TableLayout();
jCenter.setLayout(tableLayout);
// add headers
for (int i=0;i<COLUMNS;i++) {
if ( !isExcluded(i) ) {
tableLayout.insertColumn(i, slotSize );
int weekday = weekdayMapper.dayForIndex( i);
jHeader.add( createSlotHeader( weekday ) );
} else {
tableLayout.insertColumn(i, 0);
}
}
for (int i=0;i<ROWS;i++) {
tableLayout.insertRow(i, TableLayout.PREFERRED );
}
// add Fields
counter.setTime(getStartDate());
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;
if ( !isExcluded( column ) ) {
jCenter.add( slots[i] , "" + column + "," + row);
}
counter.add(Calendar.DATE,1);
}
for (int i=0; i<daysInMonth; i++) {
slots[i].sort();
}
jHeader.validate();
jCenter.validate();
revalidate();
repaint();
}
private void createField(int pos, Date date) {
SmallDaySlot c= new SmallDaySlot("" + (pos + 1),slotSize, getNumberColor( date));
c.setEditable(isEditable());
c.setDraggingHandler(draggingHandler);
c.addMouseListener(selectionHandler);
c.addMouseMotionListener(selectionHandler);
slots[pos]= c;
};
public static Color DATE_NUMBER_COLOR = Color.gray;
protected Color getNumberColor( Date date)
{
return DATE_NUMBER_COLOR;
}
/** override this method, if you want to create your own header. */
protected JComponent createSlotHeader(int weekday) {
JLabel jLabel = new JLabel();
jLabel.setBorder(isEditable() ? SLOTHEADER_BORDER : null);
int slot = weekdayMapper.indexForDay( weekday);
jLabel.setText( weekdayMapper.getNames()[slot] );
jLabel.setFont(new Font("SansSerif", Font.PLAIN, 13));
jLabel.setHorizontalAlignment(JLabel.CENTER);
jLabel.setOpaque(false);
jLabel.setForeground(Color.black);
Dimension dim = new Dimension(this.slotSize,20);
jLabel.setPreferredSize( dim);
jLabel.setMinimumSize( dim );
jLabel.setMaximumSize( dim );
return jLabel;
}
public void addBlock(Block bl, int slot) {
checkBlock( bl );
blockCalendar.setTime(bl.getStart());
int date = blockCalendar.get(Calendar.DATE);
// System.out.println("Put " + bl.getStart() + " into field " + (date -1));
slots[date-1].putBlock((SwingBlock)bl);
}
public int getSlotNr( DaySlot slot) {
for (int i=0;i<slots.length;i++)
if (slots[i] == slot)
return i;
throw new IllegalStateException("Slot not found in List");
}
int getRowsPerDay() {
return 1;
}
DaySlot getDay(int nr) {
if ( nr >=0 && nr< slots.length)
return slots[nr];
else
return null;
}
int getDayCount() {
return slots.length;
}
int calcSlotNr(int x, int y) {
for (int i=0;i<slots.length;i++) {
if (slots[i] == null)
continue;
Point p = slots[i].getLocation();
if ((p.x <= x)
&& (x <= p.x + slots[i].getWidth())
&& (p.y <= y)
&& (y <= p.y + slots[i].getHeight())
) {
return i;
}
}
return -1;
}
SmallDaySlot calcSlot(int x,int y) {
int nr = calcSlotNr(x, y);
if (nr == -1) {
return null;
} else {
return slots[nr];
}
}
Date createDate(DaySlot slot, int row, boolean startOfRow) {
Calendar calendar = createCalendar();
calendar.setTime( getStartDate() );
int dayOfMonth = getSlotNr( slot ) +1;
calendar.set( Calendar.DAY_OF_MONTH, dayOfMonth);
if ( !startOfRow ) {
calendar.add( Calendar.DATE , 1 );
}
calendar.set( Calendar.HOUR_OF_DAY, 0 );
calendar.set( Calendar.MINUTE, 0 );
calendar.set( Calendar.SECOND, 0 );
calendar.set( Calendar.MILLISECOND, 0 );
return calendar.getTime();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -