`
oceansad
  • 浏览: 1973 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

swt/jface使用EditingSupport 为同一列提供不同类型的编辑器CellEditor

阅读更多
自定义MyEditingSupport实现EditingSupport,代码如下
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;

public class MyEditingSupport extends EditingSupport {
	private static final String[] CROPPRING_SEASONS = { "1", "2" ,"3", "4", "5" };
	private static final String[] GENERATION = { "F1DH", "F2" ,"F3", "F4", "F5" ,"F6", "F7" ,"F8"};
	private static final String[] ROUNDS_INTER = { "1", "2" ,"3", "4:3" };
	private static final String[] ROUNDS_SELFING = { "1", "2" ,"3", "4", "5" ,"6" ,"7" ,"8:5" };
	private CellEditor[] editors;
	private StructuredViewer viewer;

	public MyEditingSupport(TableViewer viewer) {
		super(viewer);
		this.viewer = viewer;
		editors = new CellEditor[10];
		editors[0] = new ComboBoxCellEditor(viewer.getTable(), CROPPRING_SEASONS, SWT.READ_ONLY);
		editors[1] = new TextCellEditor(viewer.getTable());
		editors[2] = new ComboBoxCellEditor(viewer.getTable(), GENERATION, SWT.READ_ONLY);
		editors[3] = new ComboBoxCellEditor(viewer.getTable(), GENERATION, SWT.READ_ONLY);
		editors[4] = new ComboBoxCellEditor(viewer.getTable(), ROUNDS_INTER, SWT.READ_ONLY);
		editors[5] = new ComboBoxCellEditor(viewer.getTable(), ROUNDS_SELFING, SWT.READ_ONLY);
		editors[6] = new TextCellEditor(viewer.getTable());
		editors[7] = new TextCellEditor(viewer.getTable());
		editors[8] = new TextCellEditor(viewer.getTable());
		editors[9] = new TextCellEditor(viewer.getTable());
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		InputInfoItem item = (InputInfoItem) element;
		for(int i = 1 ; i<= 10 ; i++){
			if(item.getId().equals(String.valueOf(i)))
				return editors[i-1];
		}
		/*if (item.getId().equals("1")) {
			return editors[0];
		} else if (item.getId().equals("2")) {
			return editors[1];
		} */
		return null;
	}

	@Override
	protected boolean canEdit(Object element) {
		return true;
	}

	@Override
	protected Object getValue(Object element) {
		InputInfoItem item = (InputInfoItem) element;
		if (item.getId().equals("1")) {
			for(int i = 0 ; i<CROPPRING_SEASONS.length ; i++){
				if(CROPPRING_SEASONS[i].equals(item.getValue()))
					return new Integer(i);
			}
        } else if (item.getId().equals("2")) {
        	return item.getValue();
        } else if (item.getId().equals("3")) {
        	for(int i = 0 ; i<GENERATION.length ; i++){
				if(GENERATION[i].equals(item.getValue()))
					return new Integer(i);
			}
        } else if (item.getId().equals("4")) {
        	for(int i = 0 ; i<GENERATION.length ; i++){
				if(GENERATION[i].equals(item.getValue()))
					return new Integer(i);
			}
        } else if (item.getId().equals("5")) {
        	for(int i = 0 ; i<ROUNDS_INTER.length ; i++){
				if(ROUNDS_INTER[i].equals(item.getValue()))
					return new Integer(i);
			}
        } else if (item.getId().equals("6")) {
        	for(int i = 0 ; i<ROUNDS_SELFING.length ; i++){
				if(ROUNDS_SELFING[i].equals(item.getValue()))
					return new Integer(i);
			}
        } else if (item.getId().equals("7")) {
        	return item.getValue();
        } else if (item.getId().equals("8")) {
        	return item.getValue();
        } else if (item.getId().equals("9")) {
        	return item.getValue();
        } else if (item.getId().equals("10")) {
        	return item.getValue();
        }
		return null;
	}

	@Override
	protected void setValue(Object element, Object value) {
		InputInfoItem item = (InputInfoItem) element;
		if(item.getId().equals("1")) {
			item.setValue(CROPPRING_SEASONS[Integer.parseInt(value.toString())]);
		} else if(item.getId().equals("2")) {
			item.setValue(value.toString());
		} else if(item.getId().equals("3")) {
			item.setValue(GENERATION[Integer.parseInt(value.toString())]);
		} else if(item.getId().equals("4")) {
			item.setValue(GENERATION[Integer.parseInt(value.toString())]);
		} else if(item.getId().equals("5")) {
			item.setValue(ROUNDS_INTER[Integer.parseInt(value.toString())]);
		} else if(item.getId().equals("6")) {
			item.setValue(ROUNDS_SELFING[Integer.parseInt(value.toString())]);
		} else if(item.getId().equals("7")) {
			item.setValue(value.toString());
		} else if(item.getId().equals("8")) {
			item.setValue(value.toString());
		} else if(item.getId().equals("9")) {
			item.setValue(value.toString());
		} else if(item.getId().equals("10")) {
			item.setValue(value.toString());
		} 
        this.viewer.refresh();
	}
}


表格共有三列、十行,为第三列添加EditingSupport
tableViewer = new TableViewer(this,SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(60);
		tableColumn.setText("ID");
		TableColumn tableColumn1 = new TableColumn(table, SWT.NONE);
		tableColumn1.setWidth(220);
		tableColumn1.setText("Name");
		TableColumn tableColumn2 = new TableColumn(table, SWT.NONE);
		tableColumn2.setWidth(80);
		tableColumn2.setText("Value");
		this.setLayout(new FillLayout());
		this.setBackground(new Color(Display.getCurrent(), 255, 255, 255));
		

		[color=red]TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer,tableColumn2);
	    tableViewerColumn.setEditingSupport(new MyEditingSupport(tableViewer));[/color]		
	    tableViewer.setLabelProvider();
	    tableViewer.setContentProvider();
		
	    tableViewer.setInput(inputInfoList);
分享到:
评论

相关推荐

    swt/jface中文教程

    SWT/JFace 是一种功能强大且实用的 GUI 开发工具套件,通过本教程,你将学习如何使用 SWT/JFace 来构建高效、美观的 GUI 应用程序。本教程将涵盖 SWT/JFace 的概览、目的、许可证和平台支持、SWT 和 JFace 的区别、...

    SWT/JFace专题 --- SWT/JFace概述

    7. 视图(Views)和编辑器(Editors):在Eclipse RCP框架下,如何使用JFace创建可重用的视图和专业化的编辑器。 8. 事件处理:讲解如何注册和处理SWT和JFace事件,如SelectionEvent、FocusEvent等。 9. SWT和JFace...

    swt/jface.jar

    压缩包子文件的文件名称列表中包含的多个jar文件是Eclipse项目的一部分,它们为不同功能提供了支持: 1. `org.eclipse.ui.workbench_3.3.1.M20070921-1200.jar`:这是Eclipse工作台UI框架的核心库,包含了用于构建...

    SWT/Jface 开发入门指南

    SWT/Jface开发入门指南是一篇专为初学者编写的教程,旨在帮助他们快速搭建开发环境并掌握使用SWT和JFace编写图形化应用程序的基本技巧。以下是该教程所涵盖的关键知识点: 1. **SWT和JFace简介**: - SWT全称...

    SWT/Jface API 3.4

    SWT 是一个直接与操作系统进行交互的底层库,提供了丰富的控件和组件,而 JFace 建立在 SWT 之上,提供了一层抽象,简化了 GUI 的设计和实现,使其更易于管理和维护。 在 SWT/Jface API 3.4 版本中,我们关注以下几...

    SWT/JFACE客户端登录窗口例子

    在这个“SWT/JFACE 客户端登录窗口例子”中,我们可以预期会看到如何使用这两个库来创建一个基本的登录窗口。通常,这样的窗口会包含用户名和密码输入框,以及登录、取消等按钮。以下是一些可能涉及的知识点: 1. *...

    Eclipse SWT/JFace 核心应用 带书签

    Eclipse SWT/JFace 核心应用 带书签 Eclipse SWT/JFace

    SWT/JFace开发实例

    SWT/JFace开发实例

    swt/jface in action +中文版+英文版+源码 下载

    本文将深入探讨这两个技术,并结合《SWT/JFace in Action》这本书的相关内容,提供一个全面的知识框架。 SWT是Java的GUI工具包,它提供了原生的控件,使得Java应用程序的用户界面可以与操作系统深度融合,从而获得...

    eclipse swt/jface核心应用源码

    Eclipse SWT/JFace是Eclipse框架中的两个关键组件,它们为构建用户界面提供了强大的支持。SWT(Standard Widget Toolkit)是Eclipse的本机GUI库,而JFace是基于SWT构建的更高层次的抽象层,它简化了UI开发过程。 ...

    Eclipse SWT/JFace 核心应用光盘源码

    JFace则是建立在SWT之上的一个高级UI框架,它简化了SWT的使用,提供了数据绑定、对话框、视图、表和树等控件的抽象。JFace通过模型-视图-控制器(MVC)的设计模式,帮助开发者更好地组织代码,减少重复工作,并且...

    swt/jface实例开发

    【SWT/JFace实例开发】是一份针对Java GUI编程的学习资源,主要聚焦于SWT(Standard Widget Toolkit)和JFace这两个强大的图形用户界面库。SWT是Eclipse项目的一部分,它提供了一套与操作系统直接交互的原生控件,...

    SWT/JFace学习文档

    JFace则是建立在SWT之上的一个高级UI框架,它简化了SWT的使用,提供了数据绑定、视图模型以及事件处理机制,使开发者能够更专注于业务逻辑而不是UI细节。 1.2 SWT/JFACE 与 SWING 的特点及区别 1.2.1 Swing的特点 ...

    Eclipse swt/jface核心应用源码(带视频)

    JFace 是建立在SWT之上的一个抽象层,它提供了一些高级服务,如数据绑定、视图和对话框的构建器,以及模型-视图-控制器(MVC)的设计模式,这使得开发者可以更专注于业务逻辑,而不是GUI的细节。JFace还引入了视图、...

    SWT/JFACE API

    1. **视图和编辑器**:JFace为Eclipse插件开发提供了基础框架,支持创建视图(View)和编辑器(Editor)。 2. **数据绑定**:JFace的数据绑定功能比SWT更强大,可以方便地处理模型和视图之间的数据同步,如`...

    SWT/JFace从入门到精通

    这意味着使用SWT创建的界面与目标操作系统的外观和行为一致,提供了一种跨平台的解决方案。SWT的主要组件包括按钮、文本框、列表、树、表格等基本UI元素。通过这些元素,开发者可以构建出与本地应用程序相似的用户...

    Eclipse SWT/JFace 核心应用

    同时,Eclipse RCP(Rich Client Platform)框架就是基于SWT和JFace构建的,它为开发者提供了一个完整的框架,用于创建功能强大的桌面应用程序。 在Eclipse SWT/JFace核心应用中,以下几个关键知识点不容忽视: 1....

    Eclipse SWT JFace核心应用_pdf_含标签_目录

    《Eclipse SWT/Jface核心应用》全面介绍了SWT、JFace和RCP的相关知识。全书共分5篇,第1篇介绍了SWT产生的背景以及SWT的一些基本概念和基础知识。第2篇介绍了SWT基本控件的使用,以及事件处理、布局等SWT基本知识的...

    SWT/JFace 3.5 API (HTML)

    SWT和JFace的API文档详细列出了所有可用的类、方法和属性,以及它们的参数、返回值和使用示例,为开发者提供了详细的参考。 4. HTML格式:HTML(HyperText Markup Language)是网页的标准标记语言,用于组织网页...

Global site tag (gtag.js) - Google Analytics