Search in sources :

Example 11 with IRowIdAccessor

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

the class _5054_SelectionProviderExample method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    Composite panel = new Composite(parent, SWT.NONE);
    panel.setLayout(new GridLayout(2, true));
    // property names of the Person class
    String[] propertyNames = { "lastName", "firstName" };
    // mapping from property to label, needed for column header labels
    Map<String, String> propertyToLabelMap = new HashMap<>();
    propertyToLabelMap.put("lastName", "Lastname");
    propertyToLabelMap.put("firstName", "Firstname");
    IColumnPropertyAccessor<Person> columnPropertyAccessor = new ReflectiveColumnPropertyAccessor<>(propertyNames);
    IRowIdAccessor<Person> rowIdAccessor = new IRowIdAccessor<Person>() {

        @Override
        public Serializable getRowId(Person rowObject) {
            return rowObject.getId();
        }
    };
    // create the first table
    // create the body layer stack
    final IRowDataProvider<Person> firstBodyDataProvider = new ListDataProvider<>(getSimpsonsList(), columnPropertyAccessor);
    final DataLayer firstBodyDataLayer = new DataLayer(firstBodyDataProvider);
    final SelectionLayer firstSelectionLayer = new SelectionLayer(firstBodyDataLayer);
    ViewportLayer firstViewportLayer = new ViewportLayer(firstSelectionLayer);
    // use a RowSelectionModel that will perform row selections and is able
    // to identify a row via unique ID
    firstSelectionLayer.setSelectionModel(new RowSelectionModel<>(firstSelectionLayer, firstBodyDataProvider, rowIdAccessor));
    // create the column header layer stack
    IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
    DataLayer firstColumnHeaderDataLayer = new DataLayer(columnHeaderDataProvider);
    ColumnHeaderLayer firstColumnHeaderLayer = new ColumnHeaderLayer(firstColumnHeaderDataLayer, firstViewportLayer, firstSelectionLayer);
    // register custom label styling to indicate if the table is active
    firstColumnHeaderDataLayer.setConfigLabelAccumulator(new IConfigLabelAccumulator() {

        @Override
        public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
            if (_5054_SelectionProviderExample.this.isFirstSelectionProvider) {
                configLabels.addLabelOnTop(ACTIVE_LABEL);
            }
        }
    });
    // set the region labels to make default configurations work, e.g.
    // selection
    CompositeLayer firstCompositeLayer = new CompositeLayer(1, 2);
    firstCompositeLayer.setChildLayer(GridRegion.COLUMN_HEADER, firstColumnHeaderLayer, 0, 0);
    firstCompositeLayer.setChildLayer(GridRegion.BODY, firstViewportLayer, 0, 1);
    final NatTable firstNatTable = new NatTable(panel, firstCompositeLayer, false);
    firstNatTable.addConfiguration(new DefaultNatTableStyleConfiguration());
    firstNatTable.addConfiguration(new ActiveTableStyleConfiguration());
    firstNatTable.configure();
    // set the modern theme
    firstNatTable.setTheme(new ModernNatTableThemeConfiguration());
    // add overlay painter for full borders
    firstNatTable.addOverlayPainter(new NatTableBorderOverlayPainter());
    // create the second table
    // create the body layer stack
    final IRowDataProvider<Person> secondBodyDataProvider = new ListDataProvider<>(getFlandersList(), columnPropertyAccessor);
    final DataLayer secondBodyDataLayer = new DataLayer(secondBodyDataProvider);
    final SelectionLayer secondSelectionLayer = new SelectionLayer(secondBodyDataLayer);
    ViewportLayer secondViewportLayer = new ViewportLayer(secondSelectionLayer);
    // use a RowSelectionModel that will perform row selections and is able
    // to identify a row via unique ID
    secondSelectionLayer.setSelectionModel(new RowSelectionModel<>(secondSelectionLayer, secondBodyDataProvider, rowIdAccessor));
    // create the column header layer stack
    DataLayer secondColumnHeaderDataLayer = new DataLayer(columnHeaderDataProvider);
    ILayer secondColumnHeaderLayer = new ColumnHeaderLayer(secondColumnHeaderDataLayer, secondViewportLayer, secondSelectionLayer);
    // register custom label styling to indicate if the table is active
    secondColumnHeaderDataLayer.setConfigLabelAccumulator(new IConfigLabelAccumulator() {

        @Override
        public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
            if (!_5054_SelectionProviderExample.this.isFirstSelectionProvider) {
                configLabels.addLabelOnTop(ACTIVE_LABEL);
            }
        }
    });
    // set the region labels to make default configurations work, e.g.
    // selection
    CompositeLayer secondCompositeLayer = new CompositeLayer(1, 2);
    secondCompositeLayer.setChildLayer(GridRegion.COLUMN_HEADER, secondColumnHeaderLayer, 0, 0);
    secondCompositeLayer.setChildLayer(GridRegion.BODY, secondViewportLayer, 0, 1);
    final NatTable secondNatTable = new NatTable(panel, secondCompositeLayer, false);
    secondNatTable.addConfiguration(new DefaultNatTableStyleConfiguration());
    secondNatTable.addConfiguration(new ActiveTableStyleConfiguration());
    secondNatTable.configure();
    // set the modern theme
    secondNatTable.setTheme(new ModernNatTableThemeConfiguration());
    // add overlay painter for full borders
    secondNatTable.addOverlayPainter(new NatTableBorderOverlayPainter());
    // set ISelectionProvider
    final RowSelectionProvider<Person> selectionProvider = new RowSelectionProvider<>(firstSelectionLayer, firstBodyDataProvider);
    // add a listener to the selection provider, in an Eclipse application
    // you would do this e.g. getSite().getPage().addSelectionListener()
    selectionProvider.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            log("Selection changed:");
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            @SuppressWarnings("rawtypes") Iterator it = selection.iterator();
            while (it.hasNext()) {
                Person selected = (Person) it.next();
                log("  " + selected.getFirstName() + " " + selected.getLastName());
            }
        }
    });
    // layout widgets
    GridDataFactory.fillDefaults().grab(true, true).applyTo(firstNatTable);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(secondNatTable);
    // add a region for buttons
    Composite buttonArea = new Composite(panel, SWT.NONE);
    buttonArea.setLayout(new RowLayout());
    GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(buttonArea);
    // create a button to enable selection provider change
    Button button = new Button(buttonArea, SWT.PUSH);
    button.setText("Change selection provider");
    button.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            _5054_SelectionProviderExample.this.isFirstSelectionProvider = !_5054_SelectionProviderExample.this.isFirstSelectionProvider;
            if (_5054_SelectionProviderExample.this.isFirstSelectionProvider) {
                selectionProvider.updateSelectionProvider(firstSelectionLayer, firstBodyDataProvider);
            } else {
                selectionProvider.updateSelectionProvider(secondSelectionLayer, secondBodyDataProvider);
            }
            // refresh both tables to update the active rendering in the
            // column header/ this is not necessary for updating the
            // selection provider
            firstNatTable.doCommand(new VisualRefreshCommand());
            secondNatTable.doCommand(new VisualRefreshCommand());
        }
    });
    // add a log area to the example to show the log entries
    Text output = setupTextArea(panel);
    GridDataFactory.fillDefaults().grab(true, true).span(2, 1).applyTo(output);
    return panel;
}
Also used : ListDataProvider(org.eclipse.nebula.widgets.nattable.data.ListDataProvider) LabelStack(org.eclipse.nebula.widgets.nattable.layer.LabelStack) HashMap(java.util.HashMap) ColumnHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer) IConfigLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.IConfigLabelAccumulator) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ViewportLayer(org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer) IDataProvider(org.eclipse.nebula.widgets.nattable.data.IDataProvider) IRowIdAccessor(org.eclipse.nebula.widgets.nattable.data.IRowIdAccessor) GridLayout(org.eclipse.swt.layout.GridLayout) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) Button(org.eclipse.swt.widgets.Button) DefaultNatTableStyleConfiguration(org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration) RowLayout(org.eclipse.swt.layout.RowLayout) Iterator(java.util.Iterator) SelectionEvent(org.eclipse.swt.events.SelectionEvent) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) NatTableBorderOverlayPainter(org.eclipse.nebula.widgets.nattable.painter.NatTableBorderOverlayPainter) VisualRefreshCommand(org.eclipse.nebula.widgets.nattable.command.VisualRefreshCommand) ModernNatTableThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.ModernNatTableThemeConfiguration) Composite(org.eclipse.swt.widgets.Composite) ILayer(org.eclipse.nebula.widgets.nattable.layer.ILayer) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Text(org.eclipse.swt.widgets.Text) CompositeLayer(org.eclipse.nebula.widgets.nattable.layer.CompositeLayer) RowSelectionProvider(org.eclipse.nebula.widgets.nattable.selection.RowSelectionProvider) ReflectiveColumnPropertyAccessor(org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor) SelectionLayer(org.eclipse.nebula.widgets.nattable.selection.SelectionLayer) DefaultColumnHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider) Person(org.eclipse.nebula.widgets.nattable.dataset.person.Person)

Example 12 with IRowIdAccessor

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

the class _781_DragAndDropExample method createTable.

private NatTable createTable(Composite parent, List<Person> data) {
    // property names of the Person class
    String[] propertyNames = { "firstName", "lastName" };
    // mapping from property to label, needed for column header labels
    Map<String, String> propertyToLabelMap = new HashMap<>();
    propertyToLabelMap.put("firstName", "Firstname");
    propertyToLabelMap.put("lastName", "Lastname");
    IColumnPropertyAccessor<Person> columnPropertyAccessor = new ReflectiveColumnPropertyAccessor<>(propertyNames);
    IRowDataProvider<Person> bodyDataProvider = new ListDataProvider<>(data, columnPropertyAccessor);
    final DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
    ColumnReorderLayer reorderLayer = new ColumnReorderLayer(bodyDataLayer);
    final SelectionLayer selectionLayer = new SelectionLayer(reorderLayer);
    // set row selection model with single selection enabled
    selectionLayer.setSelectionModel(new RowSelectionModel<>(selectionLayer, bodyDataProvider, new IRowIdAccessor<Person>() {

        @Override
        public Serializable getRowId(Person rowObject) {
            return rowObject.getId();
        }
    }, false));
    ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
    ILayer columnHeaderLayer = new ColumnHeaderLayer(new DataLayer(new DummyColumnHeaderDataProvider(bodyDataProvider)), viewportLayer, selectionLayer);
    CompositeLayer compositeLayer = new CompositeLayer(1, 2);
    compositeLayer.setChildLayer(GridRegion.COLUMN_HEADER, columnHeaderLayer, 0, 0);
    compositeLayer.setChildLayer(GridRegion.BODY, viewportLayer, 0, 1);
    NatTable natTable = new NatTable(parent, compositeLayer);
    // add DnD support
    DragAndDropSupport dndSupport = new DragAndDropSupport(natTable, selectionLayer, data);
    Transfer[] transfer = { TextTransfer.getInstance() };
    natTable.addDragSupport(DND.DROP_COPY, transfer, dndSupport);
    natTable.addDropSupport(DND.DROP_COPY, transfer, dndSupport);
    // adding a full border
    natTable.addOverlayPainter(new NatTableBorderOverlayPainter(natTable.getConfigRegistry()));
    return natTable;
}
Also used : ListDataProvider(org.eclipse.nebula.widgets.nattable.data.ListDataProvider) HashMap(java.util.HashMap) ColumnHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer) ILayer(org.eclipse.nebula.widgets.nattable.layer.ILayer) ColumnReorderLayer(org.eclipse.nebula.widgets.nattable.reorder.ColumnReorderLayer) ViewportLayer(org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer) CompositeLayer(org.eclipse.nebula.widgets.nattable.layer.CompositeLayer) DummyColumnHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DummyColumnHeaderDataProvider) ReflectiveColumnPropertyAccessor(org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor) IRowIdAccessor(org.eclipse.nebula.widgets.nattable.data.IRowIdAccessor) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) SelectionLayer(org.eclipse.nebula.widgets.nattable.selection.SelectionLayer) TextTransfer(org.eclipse.swt.dnd.TextTransfer) Transfer(org.eclipse.swt.dnd.Transfer) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) NatTableBorderOverlayPainter(org.eclipse.nebula.widgets.nattable.painter.NatTableBorderOverlayPainter) Person(org.eclipse.nebula.widgets.nattable.dataset.person.Person)

Example 13 with IRowIdAccessor

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

the class Selection_events method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    this.gridLayer = new SelectionExampleGridLayer();
    this.nattable = new NatTable(parent, this.gridLayer, false);
    this.nattable.addConfiguration(new DefaultNatTableStyleConfiguration());
    this.nattable.addConfiguration(new HeaderMenuConfiguration(this.nattable));
    this.nattable.addConfiguration(new DefaultSelectionStyleConfiguration());
    // Custom selection configuration
    SelectionLayer selectionLayer = this.gridLayer.getSelectionLayer();
    selectionLayer.setSelectionModel(new RowSelectionModel<>(selectionLayer, this.gridLayer.getBodyDataProvider(), new IRowIdAccessor<RowDataFixture>() {

        @Override
        public Serializable getRowId(RowDataFixture rowObject) {
            return rowObject.getSecurity_id();
        }
    }));
    selectionLayer.addConfiguration(new RowOnlySelectionConfiguration<RowDataFixture>());
    this.nattable.addConfiguration(new RowOnlySelectionBindings());
    this.nattable.configure();
    addCustomSelectionBehaviour();
    // Layout widgets
    parent.setLayout(new GridLayout(1, true));
    this.nattable.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
    setupTextArea(parent);
    return this.nattable;
}
Also used : SelectionExampleGridLayer(org.eclipse.nebula.widgets.nattable.examples.fixtures.SelectionExampleGridLayer) DefaultSelectionStyleConfiguration(org.eclipse.nebula.widgets.nattable.selection.config.DefaultSelectionStyleConfiguration) HeaderMenuConfiguration(org.eclipse.nebula.widgets.nattable.ui.menu.HeaderMenuConfiguration) RowDataFixture(org.eclipse.nebula.widgets.nattable.dataset.fixture.data.RowDataFixture) IRowIdAccessor(org.eclipse.nebula.widgets.nattable.data.IRowIdAccessor) GridLayout(org.eclipse.swt.layout.GridLayout) SelectionLayer(org.eclipse.nebula.widgets.nattable.selection.SelectionLayer) DefaultNatTableStyleConfiguration(org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration) GridData(org.eclipse.swt.layout.GridData) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) RowOnlySelectionBindings(org.eclipse.nebula.widgets.nattable.selection.config.RowOnlySelectionBindings)

Example 14 with IRowIdAccessor

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

the class Real_time_data_updates method createExampleControl.

/**
 * @see GlazedListsGridLayer to see the required stack setup. Basically the
 *      {@link DataLayer} needs to be wrapped up with a
 *      {@link GlazedListsEventLayer} and the backing list needs to be an
 *      {@link EventList}
 */
@Override
public Control createExampleControl(Composite parent) {
    this.eventList = GlazedLists.eventList(RowDataListFixture.getList(this.defaultDatasetSize));
    ConfigRegistry configRegistry = new ConfigRegistry();
    GlazedListsGridLayer<RowDataFixture> glazedListsGridLayer = new GlazedListsGridLayer<>(this.eventList, RowDataListFixture.getPropertyNames(), RowDataListFixture.getPropertyToLabelMap(), configRegistry);
    this.nattable = new NatTable(parent, glazedListsGridLayer, false);
    this.nattable.setConfigRegistry(configRegistry);
    this.nattable.addConfiguration(new DefaultNatTableStyleConfiguration());
    this.nattable.addConfiguration(new SingleClickSortConfiguration());
    SelectionLayer selectionLayer = glazedListsGridLayer.getBodyLayerStack().getSelectionLayer();
    ListDataProvider<RowDataFixture> bodyDataProvider = glazedListsGridLayer.getBodyDataProvider();
    // Select complete rows
    RowOnlySelectionConfiguration<RowDataFixture> selectionConfig = new RowOnlySelectionConfiguration<>();
    selectionLayer.addConfiguration(selectionConfig);
    this.nattable.addConfiguration(new RowOnlySelectionBindings());
    // Preserve selection on updates and sort
    selectionLayer.setSelectionModel(new RowSelectionModel<>(selectionLayer, bodyDataProvider, new IRowIdAccessor<RowDataFixture>() {

        @Override
        public Serializable getRowId(RowDataFixture rowObject) {
            return rowObject.getSecurity_id();
        }
    }));
    this.nattable.configure();
    // Layout widgets
    parent.setLayout(new GridLayout(1, true));
    this.nattable.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
    setupTextArea(parent);
    setupButtons(parent);
    return this.nattable;
}
Also used : GlazedListsGridLayer(org.eclipse.nebula.widgets.nattable.examples.fixtures.GlazedListsGridLayer) RowDataFixture(org.eclipse.nebula.widgets.nattable.dataset.fixture.data.RowDataFixture) RowOnlySelectionConfiguration(org.eclipse.nebula.widgets.nattable.selection.config.RowOnlySelectionConfiguration) IRowIdAccessor(org.eclipse.nebula.widgets.nattable.data.IRowIdAccessor) ConfigRegistry(org.eclipse.nebula.widgets.nattable.config.ConfigRegistry) GridLayout(org.eclipse.swt.layout.GridLayout) SelectionLayer(org.eclipse.nebula.widgets.nattable.selection.SelectionLayer) DefaultNatTableStyleConfiguration(org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration) SingleClickSortConfiguration(org.eclipse.nebula.widgets.nattable.sort.config.SingleClickSortConfiguration) GridData(org.eclipse.swt.layout.GridData) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) RowOnlySelectionBindings(org.eclipse.nebula.widgets.nattable.selection.config.RowOnlySelectionBindings)

Aggregations

IRowIdAccessor (org.eclipse.nebula.widgets.nattable.data.IRowIdAccessor)14 RowDataFixture (org.eclipse.nebula.widgets.nattable.dataset.fixture.data.RowDataFixture)8 ListDataProvider (org.eclipse.nebula.widgets.nattable.data.ListDataProvider)7 DataLayer (org.eclipse.nebula.widgets.nattable.layer.DataLayer)7 Before (org.junit.Before)7 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)6 ReflectiveColumnPropertyAccessor (org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor)6 SelectionLayer (org.eclipse.nebula.widgets.nattable.selection.SelectionLayer)6 ViewportLayer (org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer)6 HashMap (java.util.HashMap)4 DefaultNatTableStyleConfiguration (org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration)4 Person (org.eclipse.nebula.widgets.nattable.dataset.person.Person)4 ColumnHeaderLayer (org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer)4 ILayer (org.eclipse.nebula.widgets.nattable.layer.ILayer)4 NatTableFixture (org.eclipse.nebula.widgets.nattable.test.fixture.NatTableFixture)4 IDataProvider (org.eclipse.nebula.widgets.nattable.data.IDataProvider)3 DefaultColumnHeaderDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider)3 GridLayout (org.eclipse.swt.layout.GridLayout)3 ConfigRegistry (org.eclipse.nebula.widgets.nattable.config.ConfigRegistry)2 IConfigRegistry (org.eclipse.nebula.widgets.nattable.config.IConfigRegistry)2