[sample] tableview + property listens(databinding) + default labelprovide

[sample] tableview + property listens(databinding) + default labelprovide

public class TableViewer4 {

    public static void creatShellArae(Shell shell) {
        Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
        table.setHeaderVisible(true);
        for(int i=0; i < 3; i++) {
            TableColumn column = new TableColumn(table, SWT.NONE);
            column.setText("column " + i); column.setWidth(100);
        }

        TableViewer viewer = new TableViewer(table);
        viewer.setContentProvider(new MyContentProvider());

        DataModel input = new DataModel();
        input.add("data1");
        input.add("data2");
        
        viewer.setInput(input);
        
        input.add("data3");
        
    }

    public static void main(String[] args) {
        Display display = Display.getDefault();
        Shell shell = new Shell(display);
        shell.setSize(500, 300);
        shell.setLayout(new FillLayout());

        creatShellArae(shell);

        shell.open();
        shell.layout();

        while (!shell.isDisposed())
            if (!display.readAndDispatch())
                display.sleep();

    }

}

class MyContentProvider implements IStructuredContentProvider, PropertyChangeListener {
    private TableViewer viewer;

    public Object[] getElements(Object inputElement) {
        return ((DataModel) inputElement).elements();
    }

    public void dispose() {}

    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        this.viewer = (TableViewer) viewer;

        if (oldInput instanceof DataModel)
            ((DataModel) oldInput).removePropertyChangeListener(this);

        if (newInput instanceof DataModel) {
            ((DataModel) newInput).addPropertyChangeListener(this);
        }
    }

    public void propertyChange(PropertyChangeEvent event) {
        String eventPropertyName = event.getPropertyName();
        if (DataModel.ADD_ELEMENT.equals(eventPropertyName))
            viewer.add(event.getNewValue());
        if (DataModel.REMOVE_ELEMENT.equals(eventPropertyName))
            viewer.remove(event.getNewValue());
    }
}

 

你可能感兴趣的:(tableview)