swt TableViewer

http://blog.163.com/bluefield_wild/blog/static/8182709520085612235336/

 

 

package list;

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

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ColumnPixelData;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
public class TableViewerDemo{
    // 作为公共变量以便在事件中访问
    TableViewer tableViewer = null;

    public TableViewerDemo() {
        Display display = Display.getDefault();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout());
        shell.setText("TableViewerDemo");


        // 按钮面板
        final Composite compControl = new Composite(shell, SWT.NONE);
        compControl.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));
        final RowLayout rowLayout = new RowLayout();
        rowLayout.justify = true;
        compControl.setLayout(rowLayout);

        // 按钮监听器
        ButtonSelectionListener buttonSelectionListener = new ButtonSelectionListener();
        
        final Button btnCreate = new Button(compControl, SWT.NONE);
        btnCreate.setText("读取");
        btnCreate.addSelectionListener(buttonSelectionListener);

        final Button btnUpdate = new Button(compControl, SWT.NONE);
        btnUpdate.setText("修改");
        btnUpdate.addSelectionListener(buttonSelectionListener);

        final Button btnInsert = new Button(compControl, SWT.NONE);
        btnInsert.setText("增加");
        btnInsert.addSelectionListener(buttonSelectionListener);

        final Button btnDelete = new Button(compControl, SWT.NONE);
        btnDelete.setText("删除");
        btnDelete.addSelectionListener(buttonSelectionListener);

        // 表格面板
        Composite compTableViewer = new Composite(shell, SWT.NONE);
        compTableViewer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        compTableViewer.setLayout(new FillLayout());

        // 调用自定义方法生成表格
        tableViewer = createTableViewer(compTableViewer);

        shell.setSize(600, 400);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    /** *//**
     * 按钮监听器
     */
    private class ButtonSelectionListener extends SelectionAdapter {

        public void widgetSelected(SelectionEvent event) {
            String buttonText = null;

            // 取得按钮上的文字
            if (event.getSource() instanceof Button) {
                buttonText = ((Button) event.getSource()).getText();
            } else {
                return;
            }

            // 取得用户所选择的第一行, 若没有选择则为null
            IStructuredSelection selection = (IStructuredSelection) tableViewer
                    .getSelection();
            PeopleEntity people =  (PeopleEntity) selection.getFirstElement();
            boolean isSelected = (people != null);

            // 根据按钮上的文字来区分不同的按钮, 并产生相应的事件
            if (buttonText.equals("读取")) {
                if (isSelected) {
                    String strInfo = "ID: " + people.getId() + " Name: "
                            + people.getName();
                    MessageDialog.openInformation(null, "您所选择的内容:", strInfo);
                } else {
                    MessageDialog.openInformation(null, "您所选择的内容:", "未选择行!");                    
                }
                
            } else if (buttonText.equals("修改")) {
                if (isSelected) {
                    int id = people.getId();
                    people.setId(id+1);                    
                } else {
                    MessageDialog.openInformation(null, "您所选择的内容:", "未选择行!");                    
                }

            } else if (buttonText.equals("增加")) {
                people = new PeopleEntity(0, "临时用户");
                ((List)tableViewer.getInput()).add(people);

            } else if (buttonText.equals("删除")) {
                if (isSelected) {
                    // 先预先移动到下一行
                    Table table = tableViewer.getTable();
                    int i = table.getSelectionIndex(); // 取得当前所选行的序号,如没有则返回-1
                    table.setSelection(i + 1); // 当前选择行移下一行
                    ((List)tableViewer.getInput()).remove(people);        
                } else {
                    MessageDialog.openInformation(null, "您所选择的内容:", "未选择行!");                    
                }

            }
            // 刷新表格, 否则没有反应
            tableViewer.refresh();
        }

    }

    /** *//**
     * 生成TableViewer对象
     */
    private TableViewer createTableViewer(Composite compTableViewer) {
        // 第一步: 定义一个TableViewer对象, 并设置相应式样
        TableViewer tableViewer = new TableViewer(compTableViewer, SWT.BORDER
                | SWT.MULTI | SWT.FULL_SELECTION);

        // 第二步: 通过TableViewer中的Table对其布局
        Table table = tableViewer.getTable();
        table.setHeaderVisible(true);
        table.setLinesVisible(true);
        TableLayout tableLayout = new TableLayout();
        table.setLayout(tableLayout);

        // 第三步: 建立TableViewer中的列
        tableLayout.addColumnData(new ColumnPixelData(80));
        new TableColumn(table, SWT.NONE).setText("ID");
        tableLayout.addColumnData(new ColumnPixelData(80));
        new TableColumn(table, SWT.NONE).setText("Name");

        // 第四步: 设置内容器
        tableViewer.setContentProvider(new TableViewerContentProvider());

        // 第五步: 设置标签器
        tableViewer.setLabelProvider(new TableViewerLabelProvider());

        // 第六步: 用数据充实表格
        List<PeopleEntity> list = new ArrayList<PeopleEntity>();
        list.add(new PeopleEntity(1, "小张"));
        list.add(new PeopleEntity(2, "小王"));
        tableViewer.setInput(list);

        // 返回
        return tableViewer;
    }

    /** *//**
     * 生成表格的内容器
     */
    private class TableViewerContentProvider implements
            IStructuredContentProvider {

        /** *//**
         * 此方法接收输入到表格的数据集合,然后转化成数组返回,重修数组元素就是一个实体类对象,也即一条记录.
         * 参数element就是通过setInput(getPeoples())输入的对象,
         * 因为getPeoples()返回的是一个List,所以参数elements的类型也是List
         */
        public Object[] getElements(Object element) {
            if (element instanceof List) {
                return ((List) element).toArray();
            } else {
                return new Object[0];
            }
        }

        public void dispose() {
        }

        public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
        }

    }

    /** *//**
     * 生成表格的标签器
     */
    private class TableViewerLabelProvider implements ITableLabelProvider {
        /** *//**
         * 由此方法决定数据记录在表格的第一列是显示什么文字
         */
        public String getColumnText(Object element, int column) {
            PeopleEntity people = (PeopleEntity) element;
            if (column == 0) {
                return String.valueOf(people.getId());
            } else if (column == 1) {
                return people.getName();
            }
            return "";
        }

        public void addListener(ILabelProviderListener arg0){
        }

        public void dispose() {
        }

        public boolean isLabelProperty(Object arg0, String arg1) {
            return false;
        }

        public void removeListener(ILabelProviderListener arg0) {
        }

        public Image getColumnImage(Object arg0, int arg1) {
            return null;
        }

    }

    // 实体类, 这里仅举一个简单的例子, 读者可以根据实际充实
    private class PeopleEntity {
        private int id = 0;

        private String name = null;

        private PeopleEntity(){
        }

        public PeopleEntity(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getId() {
            return id;
        }

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

        public String getName() {
            return name;
        }

    }
    /** *//**
     * @param args
     */
    public static void main(String[] args) {
        new TableViewerDemo();
    }

}

posted @ 2015-08-25 19:46  Brook Legend  阅读(549)  评论(0编辑  收藏  举报