📄 swingcompactweekview.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 java.util.List;
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 weekview.
* This view doesn't show the times and arragenes the different slots
* verticaly.
* @version CVS $Revision: 1.4 $ $Date: 2006/08/15 16:14:47 $
*/
public class SwingCompactWeekView extends AbstractSwingCalendar
{
private static final long serialVersionUID = 1L;
public final static int COLUMNS = 7;
private SmallDaySlot[] slots = new SmallDaySlot[0];
private ArrayList rows = new ArrayList();
DraggingHandler draggingHandler = new DraggingHandler(this, false);
SelectionHandler selectionHandler = new SelectionHandler(this);
String[] slotNames = new String[] {};
private boolean[] excluded = new boolean[COLUMNS];
public SwingCompactWeekView() {
this(true);
}
public SwingCompactWeekView(boolean showScrollPane) {
super( showScrollPane );
}
void calcMinMaxDates(Date date) {
Calendar calendar = createCalendar();
calendar.setTime(date);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MILLISECOND,0);
calendar.set(Calendar.DAY_OF_WEEK,calendar.getFirstDayOfWeek());
this.setStartDate( null );
this.setEndDate( null );
for (int i=0;i<COLUMNS;i++) {
if ( this.getStartDate() == null ) {
this.setStartDate( calendar.getTime() );
}
calendar.add(Calendar.DATE,1);
this.setEndDate( calendar.getTime() );
}
}
/** must be called after the slots are filled*/
private boolean isExcluded( int column) {
return excluded[ column ];
}
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 setSlots(String[] slotNames) {
this.slotNames = slotNames;
}
public void rebuild() {
rows.clear();
for ( int i=0; i<slotNames.length; i++ ) {
addRow();
}
for (int i=0;i<COLUMNS;i++) {
int weekday = weekdayMapper.dayForIndex( i );
if ( excludeDays.contains(new Integer( weekday )) ) {
excluded[i] = true;
} else {
excluded[i] = false;
}
}
// calculate the blocks
Iterator it= builders.iterator();
while (it.hasNext()) {
Builder b= (Builder)it.next();
b.prepareBuild(getStartDate(), getEndDate());
}
// 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);
Calendar calendar = createCalendar();
calendar.setTime(getStartDate());
calendar.set( Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
if ( slotNames.length > 0) {
tableLayout.insertColumn(0, slotSize);
jHeader.add( createSlotHeader( null ), "0,0,l,t" );
} else {
tableLayout.insertColumn(0, 0);
}
// add headers
for (int column=0;column<COLUMNS;column++) {
if ( !isExcluded(column) ) {
tableLayout.insertColumn(column + 1, slotSize );
jHeader.add( createSlotHeader( calendar.getTime() ) );
} else {
tableLayout.insertColumn(column + 1, 0);
}
calendar.add(Calendar.DATE,1);
}
int rowsize = rows.size();
slots = new SmallDaySlot[rowsize * COLUMNS];
for (int row=0;row<rowsize;row++) {
tableLayout.insertRow(row, TableLayout.PREFERRED );
jCenter.add(createSlotLabel( row ), "" + 0 + "," + row + ",f,t");
for (int column=0;column < COLUMNS; column++) {
List blocks = (List) rows.get( row );
int fieldNumber = row * COLUMNS + column;
slots[fieldNumber] = createField( blocks, column );
if ( !isExcluded( column ) ) {
jCenter.add(slots[fieldNumber], "" + (column + 1) + "," + row);
}
}
}
for (int i=0; i<slots.length; i++) {
slots[i].sort();
}
jCenter.validate();
jHeader.validate();
revalidate();
repaint();
}
protected JComponent createSlotLabel(int slot) {
JLabel label = new JLabel();
if ( slot < slotNames.length ) {
label.setText( slotNames[ slot ] );
}
return label;
}
/** override this method, if you want to create your own slot header. */
protected JComponent createSlotHeader(Date date) {
JLabel jLabel = new JLabel();
jLabel.setFont(new Font("SansSerif", Font.PLAIN, 13));
jLabel.setHorizontalAlignment(JLabel.CENTER);
jLabel.setOpaque(false);
jLabel.setForeground(Color.black);
if (date != null ) {
jLabel.setText(formatDayOfWeekDateMonth(date,locale,getTimeZone()));
jLabel.setBorder(isEditable() ? SLOTHEADER_BORDER : null);
}
Dimension dim = new Dimension(this.slotSize,20);
jLabel.setPreferredSize( dim);
jLabel.setMinimumSize( dim );
jLabel.setMaximumSize( dim );
return jLabel;
}
private SmallDaySlot createField(List blocks, int column) {
SmallDaySlot c= new SmallDaySlot("",slotSize, Color.BLACK);
c.setEditable(isEditable());
c.setDraggingHandler(draggingHandler);
c.addMouseListener(selectionHandler);
c.addMouseMotionListener(selectionHandler);
if ( blocks != null) {
Iterator it = blocks.iterator();
while (it.hasNext()){
SwingBlock block = (SwingBlock)it.next();
blockCalendar.setTime(block.getStart());
int weekday = blockCalendar.get(Calendar.DAY_OF_WEEK);
if ( weekdayMapper.indexForDay(weekday) == column ) {
c.putBlock( block );
}
}
}
return c;
};
public void addBlock(Block block, int slot) {
checkBlock( block );
while ( rows.size() <= slot ) {
addRow();
}
ArrayList blocks = (ArrayList) rows.get( slot );
blocks.add( block );
blockCalendar.setTime(block.getStart());
int weekday = blockCalendar.get(Calendar.DAY_OF_WEEK);
excluded[weekdayMapper.indexForDay( weekday )] = false;
}
private void addRow() {
rows.add( rows.size(), new ArrayList());
}
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");
}
private int getDayOfWeek(DaySlot slot) {
return weekdayMapper.dayForIndex( getSlotNr( slot ) %COLUMNS );
}
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() );
calendar.set( Calendar.DAY_OF_WEEK, getDayOfWeek(slot) );
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 + -