⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swtcalendar.java

📁 mywork是rcp开发的很好的例子
💻 JAVA
字号:
/*
 *  SWTCalendar.java  - A calendar component for SWT
 *  Author: Mark Bryan Yu
 *  Modified by: Sergey Prigogin
 *  swtcalendar.sourceforge.net
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package net.sf.component.calendar;

import java.util.Calendar;
import java.util.Locale;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
/**
 * @author levin
 * @fix 20080811 增加一个当天的快捷按钮
 */
public class SWTCalendar extends Composite {
	/**
	 * Style constant for making Sundays red.
	 */
	public static final int RED_SUNDAY = SWTDayChooser.RED_SUNDAY;

	/**
	 * Style constant for making weekends red.
	 */
	public static final int RED_WEEKEND = SWTDayChooser.RED_WEEKEND;
	
	//是否使用农历
	public static final int CHINESE = SWTDayChooser.CHINESE;

	private boolean settingDate;

	private Spinner yearChooser;

	private SWTMonthChooser monthChooser;

	private SWTDayChooser dayChooser;
	//显示猴年马月
	private Label chineseLabel;
	private CalendarGB calendarGB=new CalendarGB();
	
	private boolean settingYearMonth;
	private int style;

	/**
	 * Constructs a calendar control.
	 * 
	 * @param parent
	 *            a parent container.
	 * @param style
	 *            FLAT to make the buttons flat, or NONE.
	 */
	public SWTCalendar(Composite parent, int style) {
		super(parent, (style & ~(SWT.FLAT | RED_WEEKEND |CHINESE)));
		this.style=style;
		Calendar calendar = Calendar.getInstance();
		{
			final GridLayout gridLayout = new GridLayout();
			gridLayout.marginHeight = 0;
			gridLayout.marginWidth = 0;
			gridLayout.horizontalSpacing = 2;
			gridLayout.verticalSpacing = 2;
			setLayout(gridLayout);
		}
		final Composite header = new Composite(this, SWT.NONE);
		{
			{
				final GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
				header.setLayoutData(gridData);
				final GridLayout gridLayout = new GridLayout();
				gridLayout.numColumns = 3;
				gridLayout.marginWidth = 0;
				gridLayout.marginHeight = 0;
				header.setLayout(gridLayout);
			}
			final Button prevMonthButton = new Button(header, SWT.ARROW | SWT.LEFT | SWT.CENTER | (style & SWT.FLAT));
			prevMonthButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL));
			prevMonthButton.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					previousMonth();
				}
			});
			final Composite composite = new Composite(header, SWT.NONE);
			composite.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_CENTER));
			{
				final GridLayout gridLayout = new GridLayout();
				gridLayout.numColumns = 4;
				gridLayout.marginWidth = 0;
				gridLayout.marginHeight = 0;
				composite.setLayout(gridLayout);
			}
			header.setTabList(new Control[] { composite });
			monthChooser = new SWTMonthChooser(composite);
			monthChooser.setLayoutData(new GridData(GridData.FILL_VERTICAL));
			monthChooser.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					if (!settingYearMonth) {
						dayChooser.setMonth(monthChooser.getMonth());
					}
				}
			});
			yearChooser = new Spinner(composite, SWT.BORDER);
			yearChooser.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL));
			yearChooser.setMinimum(1);
			yearChooser.setMaximum(9999);
			yearChooser.setValue(calendar.get(Calendar.YEAR));
			yearChooser.addModifyListener(new ModifyListener() {
				public void modifyText(ModifyEvent e) {
					if (!settingYearMonth) {
						dayChooser.setYear(yearChooser.getValue());
					}
				}
			});
			chineseLabel=new Label(composite,SWT.FLAT);
			Button todayButton=new Button(composite,SWT.FLAT);
			todayButton.setText("Today");
			todayButton.addSelectionListener(new SelectionAdapter(){
				public void widgetSelected(SelectionEvent e) {
					setCalendar(Calendar.getInstance());
				}
			});
			if((style & CHINESE ) != 0){
				calendarGB.setGregorian(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH));
				chineseLabel.setText(calendarGB.getYearName());
				todayButton.setText("今天");
			}
			final Button nextMonthButton = new Button(header, SWT.ARROW | SWT.RIGHT | SWT.CENTER | (style & SWT.FLAT));
			nextMonthButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL));
			nextMonthButton.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					nextMonth();
				}
			});
		}
		{
			dayChooser = new SWTDayChooser(this, SWT.BORDER | style);
			GridData gridData = new GridData(GridData.FILL_BOTH);
			gridData.horizontalSpan = 3;
			dayChooser.setLayoutData(gridData);
			dayChooser.addSWTCalendarListener(new SWTCalendarListener() {
				public void dateChanged(SWTCalendarEvent event) {
					refreshYearMonth(event.getCalendar());
				}
			});
		}
		setTabList(new Control[] { header, dayChooser });
		setFont(parent.getFont());
	}

	public SWTCalendar(Composite parent) {
		this(parent, SWT.FLAT);
	}

	public void setCalendar(Calendar cal) {
		settingDate = true;
		try {
			refreshYearMonth(cal);
			dayChooser.setCalendar(cal);
		} finally {
			settingDate = false;
		}
	}

	private void refreshYearMonth(Calendar cal) {
		settingYearMonth = true;
		yearChooser.setValue(cal.get(Calendar.YEAR));
		monthChooser.setMonth(cal.get(Calendar.MONTH));
		if((style & CHINESE) != 0){
			calendarGB.setGregorian(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH));
			chineseLabel.setText(calendarGB.getYearName());
		}
		settingYearMonth = false;
	}

	public void nextMonth() {
		Calendar cal = dayChooser.getCalendar();
		cal.add(Calendar.MONTH, 1);
		refreshYearMonth(cal);
		dayChooser.setCalendar(cal);
	}

	public void previousMonth() {
		Calendar cal = dayChooser.getCalendar();
		cal.add(Calendar.MONTH, -1);
		refreshYearMonth(cal);
		dayChooser.setCalendar(cal);
	}

	public Calendar getCalendar() {
		return dayChooser.getCalendar();
	}

	public void addSWTCalendarListener(SWTCalendarListener listener) {
		dayChooser.addSWTCalendarListener(listener);
	}

	public void removeSWTCalendarListener(SWTCalendarListener listener) {
		dayChooser.removeSWTCalendarListener(listener);
	}

	public void setLocale(Locale locale) {
		monthChooser.setLocale(locale);
		dayChooser.setLocale(locale);
		yearChooser.setValue(getCalendar().get(Calendar.YEAR));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.swt.widgets.Control#setFont(org.eclipse.swt.graphics.Font)
	 */
	public void setFont(Font font) {
		super.setFont(font);
		monthChooser.setFont(font);
		yearChooser.setFont(font);
		dayChooser.setFont(font);
	}

	public boolean isSettingDate() {
		return settingDate;
	}

	public Map<String, String> getChineseHoliday() {
		return dayChooser.getChineseHoliday();
	}

	public void setChineseHoliday(Map<String, String> chineseHoliday) {
		dayChooser.setChineseHoliday(chineseHoliday);
	}

	public Map<String, String> getGregorianHoliday() {
		return dayChooser.getGregorianHoliday();
	}

	public void setGregorianHoliday(Map<String, String> gregorianHoliday) {
		dayChooser.setGregorianHoliday(gregorianHoliday);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -