`
cutesunshineriver
  • 浏览: 198735 次
  • 性别: 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详细教程

    2. **选择RCP Application**:在列表中选择“Eclipse Application”,然后点击“New”按钮创建一个新的启动配置。 3. **设置启动参数**:配置应用的基本信息,比如主插件和启动类等。 4. **运行应用**:点击“Run”...

    Eclipse-RCP中文入门教程

    完成这些步骤后,Eclipse会自动创建一个新的RCP应用项目。 ##### 2. 插件清单编辑器 - **配置RCP应用**:插件清单编辑器提供了一个图形化界面来配置RCP应用的各种属性。 - **运行预览**:在插件清单编辑器的...

    Eclipse RCP 开发入门

    - **创建项目**:打开 Eclipse,新建一个插件项目。项目名建议遵循一定的命名规则,因为插件文件 `plugin.xml` 中的插件 ID 将基于此项目名生成。 - **选择模板**:选择合适的模板可以快速搭建项目基础结构。本例中...

    Eclipse RCP 插件开发指南

    Eclipse RCP 是一个灵活的平台,它提供了构建桌面应用程序所需的所有组件和服务。通过使用Eclipse RCP,开发者可以创建高度可定制和扩展的应用程序,这些应用不仅具有强大的功能,而且还能与其他Eclipse插件无缝集成...

    Eclipse RCP.pdf清晰版

    通过以上介绍,我们可以看到Eclipse RCP是一个非常强大且灵活的框架,它不仅能够加速桌面应用程序的开发,还能让开发者专注于业务逻辑而不是繁琐的界面设计。对于希望利用Eclipse平台构建复杂应用的开发者来说,掌握...

    一个简单的RCP入门教程

    在提供的PDF教程《一个简单的RCP入门教程》中,你可能会学习到如何创建一个基本的RCP应用,可能包括一个简单的视图,展示“Hello, RCP!”这样的欢迎信息。这个例子将帮助你理解和实践RCP开发流程,包括插件的结构、...

    Eclipse 4 RCP - Tutorial.pdf

    - **创建项目**:详细步骤介绍如何通过Eclipse IDE中的向导创建一个新的Eclipse 4 RCP项目。 - **启动应用程序**:介绍了如何在Eclipse IDE中运行新创建的应用程序,以及调试过程中需要注意的事项。 #### 5. ...

    Eclipse RCP Plug-in开发自学教程(Eclipse3.6)

    本教程是针对Eclipse 3.6版本的,提供了一个详细的学习路径,适合初学者入门。教程内容涵盖从基础到进阶的多个方面,每个章节尽量独立,便于学习和查阅。 1. **富客户端平台** - **概述**:介绍Eclipse RCP的概念...

    RCP入门教程

    此外,Eclipse RCP入门.pdf文档是一个很好的起点,提供了实践指导和示例代码。 通过这个简单的入门教程,你应该对Eclipse RCP有了初步的认识。随着对RCP的深入理解和实践,你会发现它能帮助你构建出强大且定制化的...

    Eclipse RCP Plugin 开发自学入门指南(CEC首发)

    将涉及以下内容:创建第一个 RCP 程序,创建菜单和工具栏,查看,编辑,对话,外部 JAR 的用法,向一个 RCP应用程序产品中添加标志和帮助。 每一章都基本独立于其他章节。 目录 1 富客户端平台 ....................

    rcp入门里面包含英文和中文两个版本的

    标题"rcp入门里面包含英文和中文两个版本的"指出这是一个关于RCP(Rich Client Platform)的入门教程,其中包含了英文和中文两种语言的学习资料。这表明教程面向的是初学者,旨在帮助他们理解并掌握RCP的基础知识。 ...

    RCP入门 View Editor

    **RCP入门:View与Editor详解** RCP(Rich Client Platform)是Eclipse框架的核心组成部分,它为开发者提供了一个构建复杂桌面应用的基础架构。RCP允许开发者专注于业务逻辑的实现,而不必过于关注用户界面的设计,...

    rcp入门

    **标题:“RCP入门”** ...总的来说,"RCP入门"这篇文章和配套资源为学习Eclipse RCP提供了一个很好的起点,涵盖了从基础概念到实际操作的各个方面,帮助开发者建立起对RCP的理解并开始自己的开发之旅。

    Eclipse从入门到精通

    - RCP(Rich Client Platform)是Eclipse提供的一个富客户端开发框架。 - 通过RCP可以构建具有丰富用户界面的桌面应用。 - **RCP项目实例**: - 本篇给出一个基于数据库开发和面向对象分析设计的完整RCP项目实例...

    RCP中文入门教程

    RCP(Rich Client Platform)是Eclipse框架的一部分,它提供了一个构建桌面应用程序的基础,使得开发者可以创建出具有丰富用户界面的应用,而这些应用不仅限于在Eclipse集成开发环境中运行。RCP应用由一系列插件组成...

Global site tag (gtag.js) - Google Analytics