当前位置: 代码迷 >> Eclipse >> Eclipse RCP入门(创设一个日期选择器)
  详细解决方案

Eclipse RCP入门(创设一个日期选择器)

热度:96   发布时间:2016-04-23 02:13:30.0
Eclipse RCP入门(创建一个日期选择器)
直接上代码。

package org.autumn.rcp.learn;import java.util.Calendar;import java.util.regex.Pattern;import org.eclipse.jface.dialogs.Dialog;import org.eclipse.swt.SWT;import org.eclipse.swt.events.MouseAdapter;import org.eclipse.swt.events.MouseEvent;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.graphics.Color;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.Rectangle;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Combo;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Label;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Table;import org.eclipse.swt.widgets.TableColumn;import org.eclipse.swt.widgets.TableItem;import org.eclipse.swt.widgets.Text;public class DatepickerDialog extends Dialog {	private Text date;	private Table table;	private TableItem selectedTableItem;	private String[] months = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" };	private int[] daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };	private String[] days = { "日", "一", "二", "三", "四", "五", "六" };	private int year;	private int month;	private int day;	protected DatepickerDialog(Shell parentShell) {		super(parentShell);	}	public DatepickerDialog(Shell shell, Text date) {		this(shell);		this.date = date;		init();	}	private void init() {		String dateStr = date.getText();		if (Pattern.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$", dateStr)) {			String[] str = dateStr.split("-");			year = Integer.parseInt(str[0]);			month = Integer.parseInt(str[1]) - 1;			day = Integer.parseInt(str[2]);		} else {			year = Calendar.getInstance().get(Calendar.YEAR);			month = Calendar.getInstance().get(Calendar.MONTH);			day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);		}	}	@Override	protected Control createContents(Composite parent) {		parent.setLayout(new GridLayout(1, false));		createYearMonthComposite(parent);		table = new Table(parent, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER_SOLID);		table.setHeaderVisible(true);		table.setLinesVisible(true);		createColumns(); // Create the columns		fillDayCell();		addTableListeners();		return parent;	}	private void fillDayCell() {		for (TableItem item : table.getItems()) {			item.dispose();		}		Calendar calendar = Calendar.getInstance();		calendar.set(year, month, 1);		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);		for (int i = 0, d = 0; i <= 5; i++) {			TableItem item = new TableItem(table, SWT.NONE);			for (int j = 0, n = table.getColumnCount(); j < n; j++) {				if ((d == 0 && j < (dayOfWeek - 1)) || d >= daysOfMonth[month]) {					continue;				}				item.setText(j, (++d + ""));				if (d == day) { // 今天					Color blue = getShell().getDisplay().getSystemColor(SWT.COLOR_BLUE);					item.setForeground(j, blue);				} else if (j == 0 || j == n - 1) { // 周末					Color red = getShell().getDisplay().getSystemColor(SWT.COLOR_RED);					item.setForeground(j, red);				}			}		}	}	private void createYearMonthComposite(Composite parent) {		Composite composite = new Composite(parent, SWT.NONE);		GridLayout gl = new GridLayout(4, false);		gl.horizontalSpacing = 15;		composite.setLayout(gl);		// 月份		final Combo monthCombo = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);		monthCombo.setItems(months);		monthCombo.select(month);		monthCombo.addSelectionListener(new SelectionAdapter() {			public void widgetSelected(SelectionEvent e) {				month = monthCombo.getSelectionIndex();				fillDayCell();			}		});		// 年		final Text yearText = new Text(composite, SWT.BORDER);		yearText.setText("" + year);		handleLeapYearCondition();		Label plus = new Label(composite, SWT.NONE);		plus.setText(" + ");		plus.addMouseListener(new MouseAdapter() {			public void mouseDown(MouseEvent e) {				yearText.setText(++year + "");				handleLeapYearCondition();				fillDayCell();			}		});		Label minus = new Label(composite, SWT.NONE);		minus.setText(" - ");		minus.addMouseListener(new MouseAdapter() {			public void mouseDown(MouseEvent e) {				yearText.setText(--year + "");				handleLeapYearCondition();				fillDayCell();			}		});	}	private void handleLeapYearCondition() {		if (isLeapYear()) {			daysOfMonth[1] = 29;		} else {			daysOfMonth[1] = 28;		}	}	private void addTableListeners() {		// 鼠标悬停时给选中单元格加黄色背景		table.addListener(SWT.MouseHover, new Listener() {			@Override			public void handleEvent(Event event) {				Point pt = new Point(event.x, event.y);				selectedTableItem = table.getItem(pt);				if (null == selectedTableItem) {					return;				}				int column = getSelectedColumn(pt);				String text = selectedTableItem.getText(column);				if (null == text || text.trim().equals("")) {					return;				}				Color yellow = getShell().getDisplay().getSystemColor(SWT.COLOR_YELLOW);				selectedTableItem.setBackground(column, yellow);			}		});		// 鼠标移动时给选中单元格加白色背景		table.addListener(SWT.MouseMove, new Listener() {			@Override			public void handleEvent(Event event) {				Point pt = new Point(event.x, event.y);				selectedTableItem = table.getItem(pt);				if (null == selectedTableItem) {					return;				}				Color white = getShell().getDisplay().getSystemColor(SWT.COLOR_WHITE);				selectedTableItem.setBackground(getSelectedColumn(pt), white);			}		});		// 鼠标进入时返回选中的日期字符串		table.addListener(SWT.MouseDown, new Listener() {			@Override			public void handleEvent(Event event) {				Point pt = new Point(event.x, event.y);				selectedTableItem = table.getItem(pt);				if (null == selectedTableItem) {					return;				}				String day = selectedTableItem.getText(getSelectedColumn(pt));				date.setText(year + "-" + (month + 1) + "-" + day);				setReturnCode(OK);				close();			}		});	}	private boolean isLeapYear() {		return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));	}	private int getSelectedColumn(Point pt) {		int column = 0;		for (int i = 0, n = table.getColumnCount(); i < n; i++) {			Rectangle rect = selectedTableItem.getBounds(i);			if (rect.contains(pt)) {				column = i;				break;			}		}		return column;	}	private TableColumn[] createColumns() {		TableColumn[] columns = new TableColumn[days.length];		for (int i = 0, n = columns.length; i < n; i++) {			columns[i] = new TableColumn(table, SWT.RIGHT);			// This text will appear in the column header			columns[i].setText(days[i]);			columns[i].pack();		}		return columns;	}}


效果图如下:


1 楼 tangweicong 2012-03-04  
楼主,我想做个table的行鼠标悬停背景颜色改变的效果,运行了你的代码,那效果太烂了啊!!给我出出主意吧
2 楼 cutesunshineriver 2012-03-05  
tangweicong 写道
楼主,我想做个table的行鼠标悬停背景颜色改变的效果,运行了你的代码,那效果太烂了啊!!给我出出主意吧


SWT本身是有提供日期小部件的,是org.eclipse.swt.widgets.DateTime这个类。使用示例语法如下:DateTime date = new DateTime(composite, SWT.CALENDAR)。
3 楼 tangweicong 2012-03-06  
cutesunshineriver 写道
tangweicong 写道
楼主,我想做个table的行鼠标悬停背景颜色改变的效果,运行了你的代码,那效果太烂了啊!!给我出出主意吧


SWT本身是有提供日期小部件的,是org.eclipse.swt.widgets.DateTime这个类。使用示例语法如下:DateTime date = new DateTime(composite, SWT.CALENDAR)。

我不是想做日期控件,我只是想做一个展示数据的table,要求它的行有网页那样的鼠标悬停效果
  相关解决方案