?? calendar.java
字號:
/**
* package com.mc.tables.client;
*/
package com.mc.tables.client;
/**
* import lib
*/
import java.util.Date;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ChangeListenerCollection;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.SourcesChangeEvents;
import com.google.gwt.user.client.ui.Widget;
/**
* class Calendar
* @author Administrator
* 日 歷
*/
public class Calendar extends Composite
implements ClickListener, SourcesChangeEvents {
/**
* class NavBar
* @author Administrator
* 日歷標題 月和年的變化
*/
private class NavBar extends Composite implements ClickListener {
public final DockPanel bar = new DockPanel();
public final Button prevMonth = new Button("<", this);
public final Button prevYear = new Button("<<", this);
public final Button nextYear = new Button(">>", this);
public final Button nextMonth = new Button(">", this);
public final HTML title = new HTML();
private final Calendar calendar;
public NavBar(Calendar calendar) {
this.calendar = calendar;
initWidget(bar);
bar.setStyleName("navbar");
title.setStyleName("header");
HorizontalPanel prevButtons = new HorizontalPanel();
prevButtons.add(prevMonth);
prevButtons.add(prevYear);
HorizontalPanel nextButtons = new HorizontalPanel();
nextButtons.add(nextYear);
nextButtons.add(nextMonth);
bar.add(prevButtons, DockPanel.WEST);
bar.setCellHorizontalAlignment(prevButtons, DockPanel.ALIGN_LEFT);
bar.add(nextButtons, DockPanel.EAST);
bar.setCellHorizontalAlignment(nextButtons, DockPanel.ALIGN_RIGHT);
bar.add(title, DockPanel.CENTER);
bar.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
bar.setCellHorizontalAlignment(title, HasAlignment.ALIGN_CENTER);
bar.setCellVerticalAlignment(title, HasAlignment.ALIGN_MIDDLE);
bar.setCellWidth(title, "100%");
}
public void onClick(Widget sender) {
if (sender == prevMonth) {
calendar.prevMonth();
} else if (sender == prevYear) {
calendar.prevYear();
} else if (sender == nextYear) {
calendar.nextYear();
} else if (sender == nextMonth) {
calendar.nextMonth();
}
}
}
/**
* class YearMonth
* @author Administrator
* 日歷標題 年和月的下來框
*/
private class YearMonth extends Composite implements ChangeListener{
public final DockPanel bar = new DockPanel();
public final ListBox tyear =new ListBox();
public final ListBox tmonth = new ListBox();
private final Calendar calendar;
public YearMonth(Calendar cal){
this.calendar = cal;
initWidget(bar);
tyear.addChangeListener(this);
tmonth.addChangeListener(this);
tmonth.setWidth("100%");
tyear.setWidth("100%");
addMonth(tmonth);
addYear(tyear);
bar.setSpacing(2);
bar.add(tmonth, DockPanel.WEST);
bar.setCellHorizontalAlignment(tmonth, DockPanel.ALIGN_LEFT);
bar.setCellWidth(tmonth, "50%");
bar.add(tyear, DockPanel.CENTER);
bar.setCellHorizontalAlignment(tyear, DockPanel.ALIGN_RIGHT);
bar.setCellWidth(tyear, "50%");
bar.setStyleName("ymbar");
}
/**
* 初始化月份列框
* @param list 下拉框
*/
private void addMonth(ListBox list){
list.addItem("一月",0+"");
list.addItem("二月",1+"");
list.addItem("三月",2+"");
list.addItem("四月",3+"");
list.addItem("五月",4+"");
list.addItem("六月",5+"");
list.addItem("七月",6+"");
list.addItem("八月",7+"");
list.addItem("九月",8+"");
list.addItem("十月",9+"");
list.addItem("十一月",10+"");
list.addItem("十二月",11+"");
}
/**
* 初始化年份列框
* @param list 下拉框
*/
private void addYear(ListBox list){
for(int i =0 ;i<30;i++){
list.addItem(2000+i+"",2000+i+"");
}
}
/**
* 設(shè)置年份和月份
* @param y 年份
* @param m 月份
*/
public void setYearMonth(int y,int m){
int yindex = y-1900;
int mindex = m;
final int showYearCount = 3;
if(yindex>=0 ){
if(yindex < showYearCount)
{
tyear.clear();
for(int i =0 ;i<=(yindex+showYearCount);i++){
tyear.addItem(1900+i+"",1900+i+"");
}
tyear.setItemSelected(yindex, true);
tmonth.setItemSelected(mindex, true);
}else{
tyear.clear();
for(int i = -showYearCount ;i<=showYearCount;i++){
tyear.addItem(y+i+"",y+i+"");
}
tyear.setItemSelected(showYearCount, true);
tmonth.setItemSelected(mindex, true);
}
}
else
{
tyear.clear();
for(int i =0 ;i<showYearCount;i++){
tyear.addItem(1900+i+"",1900+i+"");
}
tyear.setItemSelected(0, true);
tmonth.setItemSelected(0, true);
calendar.setYearMonth(1900, 0);
}
}
/**
* 下拉框值改變的事件
*/
public void onChange(Widget sender) {
// TODO Auto-generated method stub
//if(sender == tyear){
String s = tyear.getValue(tyear.getSelectedIndex());
String d = tmonth.getValue(tmonth.getSelectedIndex());
int y = Integer.parseInt(s);
int m = Integer.parseInt(d);
calendar.setYearMonth(y, m);
//}
}
}
/**
* static class CellHTML
* @author Administrator
* 特殊的單元格值
*/
private static class CellHTML extends HTML {
private int day;
public CellHTML(String text, int day) {
super(text);
this.day = day;
}
public int getDay() {
return day;
}
}
private final NavBar navbar = new NavBar(this);
private final YearMonth ym = new YearMonth(this);
private final DockPanel outer = new DockPanel();
/**
* 表格 6 行,7 列
*/
private final Grid grid = new Grid(6, 7) {
public boolean clearCell(int row, int column) {
boolean retValue = super.clearCell(row, column);
Element td = getCellFormatter().getElement(row, column);
DOM.setInnerHTML(td, "");
return retValue;
}
};
private Date date = new Date();
private ChangeListenerCollection changeListeners;
//星期
private String[] days = new String[] { "日", "一", "二",
"三", "四", "五", "六" };
//月份
private String[] months = new String[] { "一月", "二月", "三月",
"四月", "五月", "六月", "七月", "八月", "九月", "十月",
"十一月", "十二月" };
public Calendar() {
initWidget(outer);
grid.setStyleName("table");
grid.setCellSpacing(0);
grid.setCellSpacing(0);
outer.add(ym, DockPanel.NORTH);
outer.add(grid, DockPanel.CENTER);
drawCalendar();
setStyleName("CalendarWidget");
}
/**
* 帶參數(shù)的構(gòu)造函數(shù)
* @param date 指定的日期
*/
public Calendar(Date date){
this();
this.date = date;
drawCalendar();
}
/**
* 設(shè)置日期頭的日期
* @param y 年
* @param m 月
*/
private void setTitleYM(int y,int m){
ym.setYearMonth(y, m);
}
/**
* 初始化日歷
* 繪出日歷
*/
private void drawCalendar() {
int year = getYear();
int month = getMonth();
int day = getDay();
setTitleYM(year,month);
//setHeaderText(year, month);
grid.getRowFormatter().setStyleName(0, "weekheader");
for (int i = 0; i < days.length; i++) {
grid.getCellFormatter().setStyleName(0, i, "days");
//grid.setText(0, i, days[i].substring(0, 3));
grid.setText(0, i, days[i]);
}
Date now = new Date();
int sameDay = now.getDate();
int today = (now.getMonth() == month && now.getYear()+1900 == year) ? sameDay : 0;
int firstDay = new Date(year - 1900, month, 1).getDay();
int numOfDays = getDaysInMonth(year, month);
int j = 0;
for (int i = 1; i < 6; i++) {
for (int k = 0; k < 7; k++, j++) {
int displayNum = (j - firstDay + 1);
if (j < firstDay || displayNum > numOfDays) {
grid.getCellFormatter().setStyleName(i, k, "empty");
grid.setHTML(i, k, " ");
} else {
HTML html = new CellHTML(
"<span>" + String.valueOf(displayNum) + "</span>",
displayNum);
html.addClickListener(this);
grid.getCellFormatter().setStyleName(i, k, "cell");
if (displayNum == today) {
grid.getCellFormatter().addStyleName(i, k, "today");
} else if (displayNum == day) {
grid.getCellFormatter().addStyleName(i, k, "day");
}
grid.setWidget(i, k, html);
}
}
}
}
/**
* 設(shè)置標題
* @param year 年
* @param month 月
*/
protected void setHeaderText(int year, int month) {
navbar.title.setText(months[month] + ", " + year);
}
/**
* 獲得某一個月的天數(shù)
* @param year
* @param month
* @return
*/
private int getDaysInMonth(int year, int month) {
switch (month) {
case 1:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return 29; // leap year
else
return 28;
case 3:
return 30;
case 5:
return 30;
case 8:
return 30;
case 10:
return 30;
default:
return 31;
}
}
/**
* 前一個月
*/
public void prevMonth() {
int month = getMonth() - 1;
if (month < 0) {
setDate(getYear() - 1, 11, getDay());
} else {
setMonth(month);
}
drawCalendar();
}
/**
* 后一個月
*/
public void nextMonth() {
int month = getMonth() + 1;
if (month > 11) {
setDate(getYear() + 1, 0, getDay());
} else {
setMonth(month);
}
drawCalendar();
}
/**
* 設(shè)置指定月份
* @param y 年
* @param m 月
*/
public void setYearMonth(int y,int m){
setDate(y, m, 1);
drawCalendar();
}
/**
* 設(shè)置指定日期
* @param y 年
* @param m 月
* @param d 日
*/
public void setYearMonth(int y,int m,int d){
setDate(y, m, d);
drawCalendar();
}
/**
* 前一年
*/
public void prevYear() {
setYear(getYear() - 1);
drawCalendar();
}
/**
* 后一年
*/
public void nextYear() {
setYear(getYear() + 1);
drawCalendar();
}
/**
* 設(shè)置指定日期
* @param year 年
* @param month 月
* @param day 日
*/
private void setDate(int year, int month, int day) {
date = new Date(year - 1900, month, day);
}
private void setYear(int year) {
date.setYear(year - 1900);
}
private void setMonth(int month) {
date.setMonth(month);
}
public int getYear() {
return 1900 + date.getYear();
}
public int getMonth() {
return date.getMonth();
}
public int getDay() {
return date.getDate();
}
public Date getDate() {
return date;
}
/**
* 點擊每一天時 觸發(fā)此函數(shù)執(zhí)行
*/
public void onClick(Widget sender) {
CellHTML cell = (CellHTML)sender;
setDate(getYear(), getMonth(), cell.getDay());
drawCalendar();
if (changeListeners != null) {
changeListeners.fireChange(this);
}
}
public void addChangeListener(ChangeListener listener) {
if (changeListeners == null)
changeListeners = new ChangeListenerCollection();
changeListeners.add(listener);
}
public void removeChangeListener(ChangeListener listener) {
if (changeListeners != null)
changeListeners.remove(listener);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -