📄 swingweekview.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.Font;
import java.awt.Point;
import java.awt.Rectangle;
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.Locale;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import org.rapla.components.calendarview.Block;
import org.rapla.components.calendarview.Builder;
import org.rapla.components.calendarview.swing.scaling.IRowScale;
import org.rapla.components.calendarview.swing.scaling.LinearRowScale;
import org.rapla.components.calendarview.swing.scaling.VariableRowScale;
/** Graphical component for displaying a calendar like weekview.
* @version CVS $Revision: 1.6 $ $Date: 2006/09/08 09:45:39 $
*/
public class SwingWeekView extends AbstractSwingCalendar
{
private static final long serialVersionUID = 1L;
public final static int SLOT_GAP= 8;
int COLUMNS = 7;
LargeDaySlot[] daySlots= new LargeDaySlot[COLUMNS];
private int startHour= 0;
private int endHour= 24;
BoxLayout boxLayout2= new BoxLayout(jCenter, BoxLayout.X_AXIS);
JComponent timeScale = new TimeScale();
IRowScale rowScale = new LinearRowScale();
private boolean showNonEmptyExcluded;
public SwingWeekView() {
this(true);
}
public SwingWeekView(boolean showScrollPane) {
super(showScrollPane);
jCenter.setLayout(boxLayout2);
jCenter.setAlignmentY(TOP_ALIGNMENT);
jCenter.setAlignmentX(LEFT_ALIGNMENT);
if ( showScrollPane ) {
scrollPane.setRowHeaderView(timeScale);
} else {
add(timeScale,"0,1");
}
}
public void setShowNonEmptyExcludedDays( boolean showNonEmptyExcluded) {
this.showNonEmptyExcluded = showNonEmptyExcluded;
if ( getStartDate() != null )
calcMinMaxDates( getStartDate() );
}
public void setWorktime(int startHour, int endHour) {
this.startHour = startHour;
this.endHour = endHour;
if (getStartDate() != null)
calcMinMaxDates( getStartDate() );
}
public void setLocale(Locale locale) {
super.setLocale( locale );
if ( timeScale != null )
timeScale.setLocale( locale );
}
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++) {
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
if (!excludeDays.contains( new Integer( weekday ) ) || this.showNonEmptyExcluded) {
if ( this.getStartDate() == null )
this.setStartDate( calendar.getTime() );
calendar.add(Calendar.DATE,1);
this.setEndDate( calendar.getTime() );
} else {
calendar.add(Calendar.DATE,1);
}
}
}
public void scrollDateVisible(Date date) {
Calendar calendar = createCalendar();
calendar.setTime(date);
int slot = weekdayMapper.indexForDay( calendar.get(Calendar.DAY_OF_WEEK) );
// Abort in case the multiSlot has not been added
if (!jCenter.isAncestorOf( daySlots[slot]) ) {
return;
}
LargeDaySlot scrollSlot = daySlots[slot];
Rectangle viewRect = scrollPane.getViewport().getViewRect();
Rectangle slotRect = scrollSlot.getBounds();
// test if already visible
if (slotRect.x>=viewRect.x &&
(slotRect.x + slotRect.width)<
(viewRect.x + viewRect.width )
)
{
return;
}
scrollSlot.scrollRectToVisible(new Rectangle(0
,viewRect.y
,scrollSlot.getWidth()
,10));
}
public void scrollToStart() {
int y = rowScale.getStartWorktimePixel();
int x = 0;
scrollPane.getViewport().setViewPosition(new Point(x,y));
}
public Collection getBlocks() {
ArrayList list = new ArrayList();
for (int i=0;i<COLUMNS;i++) {
list.addAll(daySlots[i].getBlocks());
}
return Collections.unmodifiableCollection( list );
}
public Collection getBlocks(int weekday) {
int index = weekdayMapper.indexForDay( weekday );
return daySlots[ index ].getBlocks();
}
/** The granularity of the selection rows.
* <ul>
* <li>1: 1 rows per hour = 1 Hour</li>
* <li>2: 2 rows per hour = 1/2 Hour</li>
* <li>3: 3 rows per hour = 20 Minutes</li>
* <li>4: 4 rows per hour = 15 Minutes</li>
* <li>6: 6 rows per hour = 10 Minutes</li>
* <li>12: 12 rows per hour = 5 Minutes</li>
* </ul>
* Default is 4.
*/
public void setRowsPerHour(int rowsPerHour) {
if ( rowScale instanceof LinearRowScale)
((LinearRowScale)rowScale).setRowsPerHour(rowsPerHour);
}
/** @see #setRowsPerHour */
public int getRowsPerHour() {
if ( rowScale instanceof LinearRowScale)
return ((LinearRowScale)rowScale).getRowsPerHour();
return 0;
}
/** The size of each row (in pixel). Default is 15.*/
public void setRowSize(int rowSize) {
if ( rowScale instanceof LinearRowScale)
((LinearRowScale)rowScale).setRowSize(rowSize);
}
public int getRowSize() {
if ( rowScale instanceof LinearRowScale)
return ((LinearRowScale)rowScale).getRowSize();
return 0;
}
public void setBackground(Color color) {
super.setBackground(color);
if (timeScale != null)
timeScale.setBackground(color);
}
public void setEditable(boolean b) {
super.setEditable( b );
// Hide the rest
for (int i= 0;i<daySlots.length;i++) {
LargeDaySlot slot = daySlots[i];
if (slot == null) continue;
slot.setEditable(b);
slot.getHeader().setBorder(b ? SLOTHEADER_BORDER : null);
}
}
/** must be called after the slots are filled*/
private boolean isExcluded( int column) {
int weekday = weekdayMapper.dayForIndex( column );
if (!excludeDays.contains(new Integer( weekday )))
return false;
if ( !this.showNonEmptyExcluded)
return true;
return daySlots[column].isEmpty();
}
public void rebuild() {
// clear everything
jHeader.removeAll();
jCenter.removeAll();
int start = startHour;
int end = endHour;
// calculate the blocks
Iterator it= builders.iterator();
while (it.hasNext()) {
Builder b= (Builder)it.next();
b.prepareBuild(getStartDate(),getEndDate());
if (! bEditable) {
start = Math.min(b.getMin(),start);
end = Math.max(b.getMax(),end);
if (start<0)
throw new IllegalStateException("builder.getMin() is smaller than 0");
if (end>24)
throw new IllegalStateException("builder.getMax() is greater than 24");
}
}
//rowScale = new VariableRowScale();
if ( rowScale instanceof LinearRowScale)
{
LinearRowScale linearScale = (LinearRowScale) rowScale;
int pixelPerHour = linearScale.getRowsPerHour() * linearScale.getRowSize();
timeScale.setBackground(getBackground());
linearScale.setTimeZone( timeZone );
if ( isEditable())
{
((TimeScale)timeScale).setTimeIntervall(0, 24, pixelPerHour);
linearScale.setTimeIntervall( 0, 24);
}
else
{
((TimeScale)timeScale).setTimeIntervall(start, end, pixelPerHour);
linearScale.setTimeIntervall( start, end);
}
linearScale.setWorktime( this.startHour, this.endHour);
}
else
{
timeScale.setBackground(getBackground());
((TimeScale)timeScale).setTimeIntervall(0, 24, 60);
VariableRowScale periodScale = (VariableRowScale) rowScale;
periodScale.setTimeZone( timeZone );
}
// create Slots
Calendar calendar = createCalendar();
calendar.setTime(getStartDate());
calendar.set( Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
DraggingHandler draggingHandler = new DraggingHandler(this, rowScale,true);
SelectionHandler selectionHandler = new SelectionHandler(this);
for (int i=0; i<COLUMNS; i++) {
createMultiSlot(i, calendar.getTime(), draggingHandler, selectionHandler);
calendar.add(Calendar.DATE,1);
}
// build Blocks
it= builders.iterator();
while (it.hasNext()) {
Builder b= (Builder)it.next();
if (b.isEnabled()) { b.build(this); }
}
// add Slots
for (int i=0; i<daySlots.length; i++) {
if ( isExcluded(i) ) {
continue;
}
addToWeekView(daySlots[i]);
}
jHeader.add(Box.createGlue());
jHeader.validate();
jCenter.validate();
revalidate();
repaint();
}
private void createMultiSlot(int pos, Date date, DraggingHandler draggingHandler, SelectionHandler selectionHandler) {
JComponent header = createSlotHeader(date);
LargeDaySlot c= new LargeDaySlot(slotSize,rowScale, header);
c.setEditable(isEditable());
c.setTimeIntervall();
c.setDraggingHandler(draggingHandler);
c.addMouseListener(selectionHandler);
c.addMouseMotionListener(selectionHandler);
daySlots[pos]= c;
};
/** override this method, if you want to create your own slot header. */
protected JComponent createSlotHeader(Date date) {
JLabel jLabel = new JLabel();
jLabel.setBorder(isEditable() ? SLOTHEADER_BORDER : null);
jLabel.setText(formatDayOfWeekDateMonth(date,locale,getTimeZone()));
jLabel.setFont(new Font("SansSerif", Font.PLAIN, 13));
jLabel.setHorizontalAlignment(JLabel.CENTER);
jLabel.setOpaque(false);
jLabel.setForeground(Color.black);
return jLabel;
}
public void addBlock(Block bl, int slot) {
checkBlock( bl );
blockCalendar.setTime(bl.getStart());
int day = blockCalendar.get(Calendar.DAY_OF_WEEK);
int slotNr = weekdayMapper.indexForDay(day);
daySlots[slotNr].putBlock((SwingBlock)bl, slot);
}
private void addToWeekView(LargeDaySlot slot) {
jHeader.add(slot.getHeader());
jHeader.add(Box.createHorizontalStrut(SLOT_GAP));
jCenter.add(slot);
jCenter.add(Box.createHorizontalStrut(SLOT_GAP));
}
public int getSlotNr(DaySlot slot) {
for (int i=0;i<daySlots.length;i++) {
if (daySlots[i] == slot) {
return i;
}
}
throw new IllegalStateException("Slot not found in List");
}
int getRowsPerDay() {
return rowScale.getRowsPerDay();
}
DaySlot getDay( int nr ) {
if ( nr >=0 && nr< daySlots.length)
return daySlots[nr];
else
return null;
}
int getDayCount() {
return daySlots.length;
}
int calcSlotNr(int x, int y) {
for (int i=0;i<daySlots.length;i++) {
if (getDay(i) == null)
continue;
Point p = getDay(i).getLocation();
if (p.x <= x
&& x <= p.x + daySlots[i].getWidth()
&& p.y <= y
&& y <= p.y + daySlots[i].getHeight()
)
return i;
}
return -1;
}
LargeDaySlot calcSlot(int x, int y) {
int nr = calcSlotNr(x, y);
if (nr == -1)
return null;
else
return daySlots[nr];
}
Date createDate(DaySlot slot,int index, boolean startOfRow) {
if (!startOfRow)
index++;
int weekday = weekdayMapper.dayForIndex( getSlotNr( slot ));
Calendar calendar = createCalendar();
calendar.setTime(getStartDate());
calendar.set(Calendar.DAY_OF_WEEK,weekday);
calendar.set(Calendar.HOUR_OF_DAY,rowScale.calcHour(index));
calendar.set(Calendar.MINUTE,rowScale.calcMinute(index));
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MILLISECOND,0);
return calendar.getTime();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -