Search in sources :

Example 1 with IColumnPropertyAccessor

use of org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor in project nebula.widgets.nattable by eclipse.

the class Derived_or_computed_column_data method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    List<Person> myList = new ArrayList<>();
    myList.add(new Person("Homer", "Simpson", "Sargeant", 1234567890L));
    myList.add(new Person("Waylon", "Smithers", "Admiral", 6666666666L));
    myList.add(new Person("Bart", "Smithers", "General", 9125798342L));
    myList.add(new Person("Nelson", "Muntz", "Private", 0000000001L));
    myList.add(new Person("John", "Frink", "Lieutenant", 3141592654L));
    String[] propertyNames = { "firstName", "lastName", "rank", "serialNumber" };
    final IColumnPropertyAccessor<Person> columnPropertyAccessor = new ReflectiveColumnPropertyAccessor<>(propertyNames);
    // Add derived 'fullName' column
    final IColumnPropertyAccessor<Person> derivedColumnPropertyAccessor = new IColumnPropertyAccessor<Person>() {

        @Override
        public Object getDataValue(Person rowObject, int columnIndex) {
            if (columnIndex < columnPropertyAccessor.getColumnCount()) {
                return columnPropertyAccessor.getDataValue(rowObject, columnIndex);
            } else if (columnIndex == columnPropertyAccessor.getColumnCount()) {
                return columnPropertyAccessor.getDataValue(rowObject, 0) + " " + columnPropertyAccessor.getDataValue(rowObject, 1);
            } else {
                return null;
            }
        }

        @Override
        public void setDataValue(Person rowObject, int columnIndex, Object newValue) {
            columnPropertyAccessor.setDataValue(rowObject, columnIndex, newValue);
        }

        @Override
        public int getColumnCount() {
            return columnPropertyAccessor.getColumnCount() + 1;
        }

        @Override
        public String getColumnProperty(int columnIndex) {
            if (columnIndex < columnPropertyAccessor.getColumnCount()) {
                return columnPropertyAccessor.getColumnProperty(columnIndex);
            } else if (columnIndex == columnPropertyAccessor.getColumnCount()) {
                return "fullName";
            } else {
                return null;
            }
        }

        @Override
        public int getColumnIndex(String propertyName) {
            if ("fullName".equals(propertyName)) {
                return columnPropertyAccessor.getColumnCount() + 1;
            } else {
                return columnPropertyAccessor.getColumnIndex(propertyName);
            }
        }
    };
    final IDataProvider listDataProvider = new ListDataProvider<>(myList, derivedColumnPropertyAccessor);
    // Column header data provider includes derived properties
    IDataProvider columnHeaderDataProvider = new IDataProvider() {

        @Override
        public Object getDataValue(int columnIndex, int rowIndex) {
            return derivedColumnPropertyAccessor.getColumnProperty(columnIndex);
        }

        @Override
        public void setDataValue(int columnIndex, int rowIndex, Object newValue) {
        // noop
        }

        @Override
        public int getColumnCount() {
            return derivedColumnPropertyAccessor.getColumnCount();
        }

        @Override
        public int getRowCount() {
            return 1;
        }
    };
    ILayer layer = new DefaultGridLayer(listDataProvider, columnHeaderDataProvider);
    return new NatTable(parent, layer);
}
Also used : ListDataProvider(org.eclipse.nebula.widgets.nattable.data.ListDataProvider) ILayer(org.eclipse.nebula.widgets.nattable.layer.ILayer) ArrayList(java.util.ArrayList) IDataProvider(org.eclipse.nebula.widgets.nattable.data.IDataProvider) IColumnPropertyAccessor(org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor) ReflectiveColumnPropertyAccessor(org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) DefaultGridLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultGridLayer)

Aggregations

ArrayList (java.util.ArrayList)1 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)1 IColumnPropertyAccessor (org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor)1 IDataProvider (org.eclipse.nebula.widgets.nattable.data.IDataProvider)1 ListDataProvider (org.eclipse.nebula.widgets.nattable.data.ListDataProvider)1 ReflectiveColumnPropertyAccessor (org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor)1 DefaultGridLayer (org.eclipse.nebula.widgets.nattable.grid.layer.DefaultGridLayer)1 ILayer (org.eclipse.nebula.widgets.nattable.layer.ILayer)1