`
cutesunshineriver
  • 浏览: 196219 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

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;
	}

}


效果图如下:


  • 大小: 30.5 KB
分享到:
评论
3 楼 tangweicong 2012-03-06  
cutesunshineriver 写道
tangweicong 写道
楼主,我想做个table的行鼠标悬停背景颜色改变的效果,运行了你的代码,那效果太烂了啊!!给我出出主意吧


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

我不是想做日期控件,我只是想做一个展示数据的table,要求它的行有网页那样的鼠标悬停效果
2 楼 cutesunshineriver 2012-03-05  
tangweicong 写道
楼主,我想做个table的行鼠标悬停背景颜色改变的效果,运行了你的代码,那效果太烂了啊!!给我出出主意吧


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

相关推荐

    eclipse RCP入门示例介绍

    Eclipse RCP入门,初级的RCP开发介绍。

    eclipse RCP 入门教程

    eclipse RCP 入门教程 ,大家不要错过哦

    Eclipse RCP开发教程

    Eclipse RCP开发教程,RCP入门教程,教你如何使用SWT Eclipse RCP开发教程,RCP入门教程,教你如何使用SWT Eclipse RCP开发教程,RCP入门教程,教你如何使用SWT Eclipse RCP开发教程,RCP入门教程,教你如何使用SWT

    Eclipse RCP 软件打包发布方法

    Eclipse RCP 软件打包发布方法。之前花了5分下了一个教材,更不不好用。现在自己摸索写了一个,绝对赞~

    eclipse rcp入门

    eclipse中rcp开发入门

    Eclipse+RCP入门

    Eclipse+RCP入门资料Eclipse+RCP入门资料Eclipse+RCP入门资料

    Eclipse RCP 开发入门

    一个 Eclipse RCP 的入门教程

    eclipse rcp 自学教程

    clipse RCP允许开发者使用eclipse结构风格设计...将涉及以下内容:创建第一个RCP程序,创建菜单和工具栏,查看,编辑,对话,外部JAR的用法,向一个RCP应用程序产品中添加标志和帮助。 每一章可能都基本独立于其他章节

    Eclipse RCP入门教程

    Eclipse rcp教程实例,RCP的全称是Rich Client Platform,可开发Java桌面应用可以把开发的焦点转移到系统的逻辑功能上,而不是界面上。

    Eclipse RCP入门

    Eclipse RCP入门,rcp入门教程.

    Eclipse Rcp

    Eclipse RCP富客户端平台,基于Eclipse开发的。

    eclipse 3.6 rcp 开发

    将涉及以下内容:创建第一个RCP程序,创建菜单和工具栏,查看,编辑,对话,外部JAR的用法,向一个RCP应用程序产品中添加标志和帮助。每一章都基本独立于其他章节。欢迎访问我的网站——www.xeclipse.com。

    Eclipse RCP与Spring OSGi技术详解与最佳实践

    12章)系统讲解了Eclipse RCP应用开发的基础知识、Eclipse RCP软件产品各个组成部分的构建方法,以及Eclipse RCP扩展的使用和扩展点的开发,掌握这些技术知识的读者将能构建一个结构完整的Eclipse RCP软件,并解决...

    开发您的第一个 Eclipse RCP 应用程序

    Eclipse Rich Client Platform (RCP) 的目标是在各种不是集成开发环境 (IDE) 的最终用户应用程序中使用 Eclipse。随着 Eclipse V3.1 的发布,创建 RCP 应用程序变得...本教程将指导您一步步创建自己的 RCP 应用程序。

    EclipseRcp 例子程序

    EclipseRcp 例子程序

    eclipse rcp check table

    eclipse rcp check table

    JAVA3D动画 Eclipse RCP

    在Eclipse RCP中创建JAVA3D动画程序工程包。

    Eclipse RCP 属性编辑器实例

    Eclipse RCP 属性编辑器实例!!!!!!!

    eclipse RCP mp3工程

    eclipse RCP的mp3工程,非常棒的一个rcp应用程序,学习学习,快来下

    eclipse rcp开发入六教程及培训资料

    网络中最全面最合适学习或开发...包含eclipse rcp开发入门教程; eclipse rcp基础教程;eclipse rcp开发自学教程; eclipse rcp开发培训教程及ppt等相关资料;教程中包含一步步操作实例,包含对开发原理的讲解与说明;

Global site tag (gtag.js) - Google Analytics