import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableColumn;
import com.huawei.jface.po.Person;
public class MainWindow extends ApplicationWindow{
private TableViewer tableViewer;
private List<Person> personList;
public static final int ID =0;
public static final int NAME = 1;
public static final int GENDER = 2;
public static final int COLOR= 3;
public static final String[] COLUMN_NAME={"ID号","姓名","性别","喜爱颜色"};
public MainWindow() {
super(null);
// TODO Auto-generated constructor stub
initData();
this.addMenuBar();
this.addStatusLine();
}
private void initData() {
personList = new ArrayList<Person>();
personList.add(new Person(1, "张三", "男", "灰色"));
personList.add(new Person(2, "李四", "女", "红色"));
personList.add(new Person(3, "香香", "女", "白色"));
}
@Override
protected void configureShell(Shell shell) {
// TODO Auto-generated method stub
super.configureShell(shell);
shell.setImage(new Image(shell.getDisplay(), "main_title.jpg"));
shell.setText("TableViewer事例");
int screenW = Toolkit.getDefaultToolkit().getScreenSize().width;
int screenH = Toolkit.getDefaultToolkit().getScreenSize().height;
shell.setSize(500, 300);
int shellW = shell.getBounds().width;
int shellH = shell.getBounds().height;
if(shellW>screenW){
shellW=screenW;
}
if(shellH>screenH){
shellH=screenH;
}
shell.setLocation((screenW-shellW)/2, (screenH-shellH)/2);
}
@Override
protected Control createContents(Composite parent) {
// parent.setLayout(new RowLayout(SWT.VERTICAL));
// Text text = new Text(parent,SWT.BORDER|SWT.MULTI|SWT.V_SCROLL);
// RowData data = new RowData();
// data.width = 450;
// data.height=100;
// text.setLayoutData(data);
//创建TableViewer对象
tableViewer = new TableViewer(parent,SWT.FULL_SELECTION|SWT.CENTER);
//创建表头
for(int i=0;i<COLUMN_NAME.length;i++){
TableColumn tableColumn = new TableColumn(tableViewer.getTable(), SWT.LEFT);
tableColumn.setText(COLUMN_NAME[i]);
tableViewer.getTable().getColumn(i).pack();
}
//设置表头和表格线可见
tableViewer.getTable().setHeaderVisible(true);
tableViewer.getTable().setLinesVisible(true);
//将list转换为array
tableViewer.setContentProvider(new TableContentProvider());
//对每列数据进行数据类型转换
tableViewer.setLabelProvider(new TableLableProvider());
tableViewer.setInput(personList);
createContextMenu();
editorTableViewer();
return parent;
}
private void editorTableViewer() {
// 设置列属性
tableViewer.setColumnProperties(COLUMN_NAME);
// 设置单元格编辑器对象数组
CellEditor[] cellEditors = new CellEditor[4];
cellEditors[0] = null;
cellEditors[1] = new TextCellEditor(tableViewer.getTable());
cellEditors[2] = new TextCellEditor(tableViewer.getTable());
cellEditors[3] = new TextCellEditor(tableViewer.getTable());
//设置单元格编辑器
tableViewer.setCellEditors(cellEditors);
//设置单元格修改器
tableViewer.setCellModifier(new ICellModifier() {
@Override
public void modify(Object element, String property, Object value) {
if(element instanceof Item){
element=((Item)element).getData();
Person person=(Person)element;
if(property.equals(COLUMN_NAME[1])){
person.setName((String)value);
}else if(property.equals(COLUMN_NAME[2])){
person.setGender((String)value);
}else if(property.equals(COLUMN_NAME[3])){
person.setColor((String)value);
}
tableViewer.refresh();
}
}
//当处于编辑状态时所显示的值
public Object getValue(Object element, String property) {
Person person =(Person)element;
if(property.equals(COLUMN_NAME[1])){
return person.getName();
}else if(property.equals(COLUMN_NAME[2])){
return person.getGender();
}else if(property.equals(COLUMN_NAME[3])){
return person.getColor();
}
return null;
}
//如果是第一列,不能编辑
public boolean canModify(Object element, String property) {
if(property.equals(COLUMN_NAME[0])){
return false;
}
return true;
}
});
}
class TableContentProvider implements IStructuredContentProvider{
@Override
public Object[] getElements(Object inputElement) {
// TODO Auto-generated method stub
return ((List)inputElement).toArray();
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
// TODO Auto-generated method stub
}
}
class TableLableProvider implements ITableLabelProvider{
@Override
public Image getColumnImage(Object arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
public String getColumnText(Object element, int columnIndex) {
// TODO Auto-generated method stub
Person person =(Person)element;
if(columnIndex == ID){
return person.getID()+"";
}else if(columnIndex == NAME){
return person.getName();
}else if(columnIndex == GENDER){
return person.getGender();
}else if(columnIndex ==COLOR){
return person.getColor();
}
return "";
}
@Override
public void addListener(ILabelProviderListener arg0) {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public boolean isLabelProperty(Object arg0, String arg1) {
// TODO Auto-generated method stub
return false;
}
@Override
public void removeListener(ILabelProviderListener arg0) {
// TODO Auto-generated method stub
}
}
class AddAction extends Action{
public AddAction(){
setText("添加");
}
@Override
public void run() {
Person person = new Person();
person.setID(tableViewer.getTable().getItemCount()+1);
person.setName("蒙蒙");
person.setGender("女");
person.setColor("黄色");
tableViewer.add(person);
}
}
class DeleteAction extends Action{
public DeleteAction(){
setText("删除");
}
@Override
public void run() {
StructuredSelection selection = (StructuredSelection)tableViewer.getSelection();
Person person=(Person)selection.getFirstElement();
getStatusLineManager().setMessage(person.getName()+" "+person.getGender()+" "+person.getColor());
tableViewer.remove(person);
}
}
class Refresh extends Action{
public Refresh(){
setText("刷新");
}
@Override
public void run() {
tableViewer.refresh();
}
}
public void createContextMenu(){
MenuManager menuBar = new MenuManager();
menuBar.add(new AddAction());
menuBar.add(new DeleteAction());
menuBar.add(new Refresh());
Menu menu =menuBar.createContextMenu(getShell());
tableViewer.getTable().setMenu(menu);
}
@Override
protected MenuManager createMenuManager() {
MenuManager menuBar = new MenuManager();
MenuManager editorMenu = new MenuManager("编辑(&E)");
menuBar.add(editorMenu);
editorMenu.add(new AddAction());
editorMenu.add(new DeleteAction());
return menuBar;
}
@Override
protected StatusLineManager getStatusLineManager() {
// TODO Auto-generated method stub
return super.getStatusLineManager();
}
@Override
protected StatusLineManager createStatusLineManager() {
StatusLineManager statusLineManager =new StatusLineManager();
return statusLineManager;
}
public static void main(String[] args) {
MainWindow main = new MainWindow();
main.setBlockOnOpen(true);
main.open();
Display.getCurrent().dispose();
}
}
package com.huawei.jface.po;
public class Person {
private int ID;
private String name;
private String gender;
private String color;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(int iD, String name, String gender, String color) {
super();
ID = iD;
this.name = name;
this.gender = gender;
this.color = color;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}