实现可编辑的表格TableViewer

package com.soyatec;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;

public class TableViewerTest {
	private Shell shell;
	private Display display;
	private TableViewer viewer;
	private List<User> listValues = new ArrayList<User>(0);

	private TableViewerTest() {

		initListValue();

	}

	// 初始化数据
	private void initListValue() {
		User user = new User("唐僧", 58, 0, "湖南", true);
		listValues.add(user);
		user = new User("六小龄童", 38, 1, "湖北", false);
		listValues.add(user);
		user = new User("猪八戒", 18, 0, "河南", true);
		listValues.add(user);
		user = new User("沙悟净", 8, 1, "东北", false);
		listValues.add(user);
	}

	public static void main(String[] args) {
		new TableViewerTest().createTableViewer();
	}

	// 构建表格
	private void createTableViewer() {
		shell = new Shell();
		shell.setText("TableViewer");
		shell.setSize(500, 300);
		display = Display.getDefault();
		shell.setLayout(new GridLayout());
		Composite composite = new Composite(shell, SWT.NONE);
		GridLayoutFactory.swtDefaults().numColumns(1).applyTo(composite);
		GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		viewer = new TableViewer(composite, SWT.FULL_SELECTION);
		Table table = viewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setLayoutData(new GridData(GridData.FILL_BOTH));
		String[] heards = new String[] { "name", "age", "sex", "address",
				"select all" };
		for (int i = 0; i <= 4; i++) {
			TableViewerColumn column = new TableViewerColumn(viewer, SWT.LEFT);
			column.getColumn().setText(heards[i]);
			column.getColumn().setWidth(100);
			column.setEditingSupport(new TableViewerEditingSupport(viewer, i));
		}
		viewer.setLabelProvider(new TableLableProvider());
		viewer.setContentProvider(new TableContentProvider());
		viewer.setInput(listValues);
		shell.layout();
		shell.open();
		while (!shell.isDisposed())
			if (!display.readAndDispatch())
				display.sleep();
	}

	// 定义模型
	static class User {
		String name;
		int age;
		int sex;
		String address;
		boolean isSelected;

		public User(String name, int age, int sex, String address,
				boolean isSelected) {
			super();
			this.name = name;
			this.age = age;
			this.sex = sex;
			this.address = address;
			this.isSelected = isSelected;
		}

		public boolean isSelected() {
			return isSelected;
		}

		public void setSelected(boolean isSelected) {
			this.isSelected = isSelected;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}

		public int getSex() {
			return sex;
		}

		public void setSex(int sex) {
			this.sex = sex;
		}

		public String getAddress() {
			return address;
		}

		public void setAddress(String address) {
			this.address = address;
		}
	}

	static class TableLableProvider extends LabelProvider implements
			ITableLabelProvider {
		final Image check = ImageDescriptor.createFromFile(
				TableViewerTest.class, "checked.gif").createImage();
		final Image uncheck = ImageDescriptor.createFromFile(
				TableViewerTest.class, "unchecked.gif").createImage();

		@Override
		public Image getColumnImage(Object element, int columnIndex) {
			User user = (User) element;
			if (columnIndex == 4)
				if (user.isSelected)
					return check;
				else
					return uncheck;
			return super.getImage(element);
		}

		@Override
		public String getColumnText(Object element, int columnIndex) {
			User user = (User) element;
			switch (columnIndex) {
			case 0:
				return user.getName();
			case 1:
				return String.valueOf(user.getAge());
			case 2:
				if (user.getSex() == 0)
					return "男";
				else
					return "女";
			case 3:
				return user.getAddress();
			case 4:
				return String.valueOf(user.isSelected());
			}
			return null;
		}

	}

	static class TableContentProvider implements IStructuredContentProvider {

		@Override
		public Object[] getElements(Object inputElement) {
			if (inputElement instanceof Collection<?>) {
				return ((Collection<?>) inputElement).toArray(new Object[0]);
			}
			return null;
		}

		@Override
		public void dispose() {
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

		}
	}

	static class TableViewerEditingSupport extends EditingSupport {

		int column;
		private TableViewer columnViewer;
		private CellEditor editor;

		public TableViewerEditingSupport(ColumnViewer viewers, int column) {
			super(viewers);
			this.columnViewer = (TableViewer) viewers;
			switch (column) {
			case 0:
				editor = new TextCellEditor(columnViewer.getTable(), SWT.SINGLE
						| SWT.BORDER);
				break;
			case 1:
				editor = new TextCellEditor(columnViewer.getTable(), SWT.SINGLE
						| SWT.BORDER);
				break;

			case 2:
				editor = new ComboBoxCellEditor(columnViewer.getTable(),
						new String[] { "男", "女" }, SWT.READ_ONLY | SWT.SINGLE
								| SWT.BORDER);
				break;
			case 3:
				editor = new ComboBoxCellEditor(columnViewer.getTable(),
						new String[] { "湖南", "湖北", "河南", "东北" }, SWT.READ_ONLY
								| SWT.SINGLE | SWT.BORDER);
				break;
			case 4:
				editor = new CheckboxCellEditor(columnViewer.getTable(),
						SWT.SINGLE | SWT.CHECK);
				break;

			}
			this.column = column;
		}

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

		@Override
		protected CellEditor getCellEditor(Object element) {
			return editor;
		}

		@Override
		protected Object getValue(Object element) {
			User user = (User) element;
			switch (column) {
			case 0:
				return user.getName();
			case 1:
				return user.getAge() + "";
			case 2:
				if (user.getSex() == 0)
					return 0;
				else
					return 1;
			case 3:
				if (user.getAddress().equals("湖南"))
					return 0;
				else if (user.getAddress().equals("湖北"))
					return 1;
				else if (user.getAddress().equals("河南"))
					return 2;
				else
					return 3;
			case 4:
				return user.isSelected();
			}
			return null;
		}

		@Override
		protected void setValue(Object element, Object value) {
			User user = (User) element;
			switch (column) {
			case 0:
				user.setName(value.toString());
				break;
			case 1:
				user.setAge(Integer.parseInt(value.toString()));
				break;
			case 2:
				if (Integer.parseInt(value.toString()) == 0)
					user.setSex(0);
				else
					user.setSex(1);
				break;
			case 3:
				if (Integer.parseInt(value.toString()) == 0)
					user.setAddress("湖南");
				else if (Integer.parseInt(value.toString()) == 1)
					user.setAddress("湖北");
				else if (Integer.parseInt(value.toString()) == 2)
					user.setAddress("河南");
				else
					user.setAddress("东北");
				break;
			case 4:
				user.setSelected(Boolean.valueOf(value.toString()));
				break;
			}
			columnViewer.refresh(true);
		}
	}
}

 

你可能感兴趣的:(eclipse)