Search in sources :

Example 56 with DataLayer

use of org.eclipse.nebula.widgets.nattable.layer.DataLayer in project nebula.widgets.nattable by eclipse.

the class _5051_SelectionLayerExample method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    // property names of the Person class
    String[] propertyNames = { "firstName", "lastName", "gender", "married", "birthday", "address.street", "address.housenumber", "address.postalCode", "address.city" };
    IColumnPropertyAccessor<PersonWithAddress> columnPropertyAccessor = new ExtendedReflectiveColumnPropertyAccessor<>(propertyNames);
    IDataProvider bodyDataProvider = new ListDataProvider<>(PersonService.getPersonsWithAddress(50), columnPropertyAccessor);
    DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
    SelectionLayer selectionLayer = new SelectionLayer(bodyDataLayer);
    ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
    // as the selection mouse bindings are registered for the region label
    // GridRegion.BODY
    // we need to set that region label to the viewport so the selection via
    // mouse is working correctly
    viewportLayer.setRegionName(GridRegion.BODY);
    NatTable natTable = new NatTable(parent, viewportLayer);
    return natTable;
}
Also used : ListDataProvider(org.eclipse.nebula.widgets.nattable.data.ListDataProvider) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) SelectionLayer(org.eclipse.nebula.widgets.nattable.selection.SelectionLayer) ExtendedReflectiveColumnPropertyAccessor(org.eclipse.nebula.widgets.nattable.data.ExtendedReflectiveColumnPropertyAccessor) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) PersonWithAddress(org.eclipse.nebula.widgets.nattable.dataset.person.PersonWithAddress) IDataProvider(org.eclipse.nebula.widgets.nattable.data.IDataProvider) ViewportLayer(org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer)

Example 57 with DataLayer

use of org.eclipse.nebula.widgets.nattable.layer.DataLayer in project nebula.widgets.nattable by eclipse.

the class _5053_SelectionEventsExample method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    // property names of the Person class
    String[] propertyNames = { "firstName", "lastName", "gender", "married", "birthday" };
    // mapping from property to label, needed for column header labels
    Map<String, String> propertyToLabelMap = new HashMap<>();
    propertyToLabelMap.put("firstName", "Firstname");
    propertyToLabelMap.put("lastName", "Lastname");
    propertyToLabelMap.put("gender", "Gender");
    propertyToLabelMap.put("married", "Married");
    propertyToLabelMap.put("birthday", "Birthday");
    IColumnPropertyAccessor<Person> columnPropertyAccessor = new ReflectiveColumnPropertyAccessor<>(propertyNames);
    final List<Person> data = PersonService.getPersons(10);
    // create the body layer stack
    final IRowDataProvider<Person> bodyDataProvider = new ListDataProvider<>(data, columnPropertyAccessor);
    final DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
    final SelectionLayer selectionLayer = new SelectionLayer(bodyDataLayer);
    ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
    // create the column header layer stack
    IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
    ILayer columnHeaderLayer = new ColumnHeaderLayer(new DataLayer(columnHeaderDataProvider), viewportLayer, selectionLayer);
    // create the row header layer stack
    IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
    ILayer rowHeaderLayer = new RowHeaderLayer(new DefaultRowHeaderDataLayer(new DefaultRowHeaderDataProvider(bodyDataProvider)), viewportLayer, selectionLayer);
    // create the corner layer stack
    ILayer cornerLayer = new CornerLayer(new DataLayer(new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider)), rowHeaderLayer, columnHeaderLayer);
    // create the grid layer composed with the prior created layer stacks
    GridLayer gridLayer = new GridLayer(viewportLayer, columnHeaderLayer, rowHeaderLayer, cornerLayer);
    final NatTable natTable = new NatTable(parent, gridLayer);
    // Events are fired whenever selection occurs. These can be use to
    // trigger external actions as required.
    // 
    // This adds a custom ILayerListener that will listen and handle
    // selection events on NatTable level
    natTable.addLayerListener(new ILayerListener() {

        // Default selection behavior selects cells by default.
        @Override
        public void handleLayerEvent(ILayerEvent event) {
            if (event instanceof CellSelectionEvent) {
                CellSelectionEvent cellEvent = (CellSelectionEvent) event;
                log("Selected cell: [" + cellEvent.getRowPosition() + ", " + cellEvent.getColumnPosition() + "], " + natTable.getDataValueByPosition(cellEvent.getColumnPosition(), cellEvent.getRowPosition()));
            } else if (event instanceof ColumnSelectionEvent) {
                ColumnSelectionEvent columnEvent = (ColumnSelectionEvent) event;
                log("Selected Column: " + columnEvent.getColumnPositionRanges());
            } else if (event instanceof RowSelectionEvent) {
                // directly ask the SelectionLayer about the selected rows
                // and access the data via IRowDataProvider
                Collection<Range> selections = selectionLayer.getSelectedRowPositions();
                StringBuilder builder = new StringBuilder("Selected Persons: ").append(selectionLayer.getSelectedRowPositions()).append("[");
                for (Range r : selections) {
                    for (int i = r.start; i < r.end; i++) {
                        Person p = bodyDataProvider.getRowObject(i);
                        if (p != null) {
                            if (!builder.toString().endsWith("[")) {
                                builder.append(", ");
                            }
                            builder.append(p.getFirstName()).append(" ").append(p.getLastName());
                        }
                    }
                }
                builder.append("]");
                log(builder.toString());
            }
        }
    });
    // Layout widgets
    parent.setLayout(new GridLayout(1, true));
    natTable.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
    // add a log area to the example to show the log entries
    setupTextArea(parent);
    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) ILayerListener(org.eclipse.nebula.widgets.nattable.layer.ILayerListener) DefaultCornerDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider) ViewportLayer(org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer) IDataProvider(org.eclipse.nebula.widgets.nattable.data.IDataProvider) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) GridLayout(org.eclipse.swt.layout.GridLayout) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) ILayer(org.eclipse.nebula.widgets.nattable.layer.ILayer) ColumnSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.ColumnSelectionEvent) DefaultRowHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) ReflectiveColumnPropertyAccessor(org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor) ILayerEvent(org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent) RowHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer) CornerLayer(org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer) CellSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.CellSelectionEvent) SelectionLayer(org.eclipse.nebula.widgets.nattable.selection.SelectionLayer) RowSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent) GridData(org.eclipse.swt.layout.GridData) Collection(java.util.Collection) GridLayer(org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer) DefaultColumnHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider) Person(org.eclipse.nebula.widgets.nattable.dataset.person.Person)

Example 58 with DataLayer

use of org.eclipse.nebula.widgets.nattable.layer.DataLayer in project nebula.widgets.nattable by eclipse.

the class _5055_SelectionTraversalExample method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    // property names of the Person class
    String[] propertyNames = { "firstName", "lastName", "gender", "married", "birthday" };
    IColumnPropertyAccessor<Person> columnPropertyAccessor = new ReflectiveColumnPropertyAccessor<>(propertyNames);
    IRowDataProvider<Person> bodyDataProvider = new ListDataProvider<>(PersonService.getPersons(3), columnPropertyAccessor);
    Composite panel = new Composite(parent, SWT.NONE);
    panel.setLayout(new GridLayout());
    GridDataFactory.fillDefaults().grab(true, true).applyTo(panel);
    // 1. AXIS traversal - NatTable default
    DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
    SelectionLayer selectionLayer = new SelectionLayer(bodyDataLayer);
    ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
    // as the selection mouse bindings are registered for the region label
    // GridRegion.BODY we need to set that region label to the viewport so
    // the selection via mouse is working correctly
    viewportLayer.setRegionName(GridRegion.BODY);
    NatTable natTable = new NatTable(panel, viewportLayer);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
    // 2. AXIS CYCLE traversal
    bodyDataLayer = new DataLayer(bodyDataProvider);
    selectionLayer = new SelectionLayer(bodyDataLayer);
    viewportLayer = new ViewportLayer(selectionLayer);
    // as the selection mouse bindings are registered for the region label
    // GridRegion.BODY we need to set that region label to the viewport so
    // the selection via mouse is working correctly
    viewportLayer.setRegionName(GridRegion.BODY);
    // register a MoveCellSelectionCommandHandler with
    // AXIS_CYCLE_TRAVERSAL_STRATEGY
    viewportLayer.registerCommandHandler(new MoveCellSelectionCommandHandler(selectionLayer, ITraversalStrategy.AXIS_CYCLE_TRAVERSAL_STRATEGY));
    natTable = new NatTable(panel, viewportLayer);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
    // 3. TABLE traversal
    bodyDataLayer = new DataLayer(bodyDataProvider);
    selectionLayer = new SelectionLayer(bodyDataLayer);
    viewportLayer = new ViewportLayer(selectionLayer);
    // as the selection mouse bindings are registered for the region label
    // GridRegion.BODY we need to set that region label to the viewport so
    // the selection via mouse is working correctly
    viewportLayer.setRegionName(GridRegion.BODY);
    // register a MoveCellSelectionCommandHandler with
    // TABLE_TRAVERSAL_STRATEGY
    viewportLayer.registerCommandHandler(new MoveCellSelectionCommandHandler(selectionLayer, ITraversalStrategy.TABLE_TRAVERSAL_STRATEGY));
    natTable = new NatTable(panel, viewportLayer);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
    // 4. TABLE CYCLE traversal
    bodyDataLayer = new DataLayer(bodyDataProvider);
    selectionLayer = new SelectionLayer(bodyDataLayer);
    viewportLayer = new ViewportLayer(selectionLayer);
    // as the selection mouse bindings are registered for the region label
    // GridRegion.BODY we need to set that region label to the viewport so
    // the selection via mouse is working correctly
    viewportLayer.setRegionName(GridRegion.BODY);
    // register a MoveCellSelectionCommandHandler with
    // TABLE_CYCLE_TRAVERSAL_STRATEGY
    viewportLayer.registerCommandHandler(new MoveCellSelectionCommandHandler(selectionLayer, ITraversalStrategy.TABLE_CYCLE_TRAVERSAL_STRATEGY));
    natTable = new NatTable(panel, viewportLayer);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
    // 5. mixed traversal
    // on left/right we will use TABLE CYCLE
    // on up/down we will use AXIS CYCLE
    bodyDataLayer = new DataLayer(bodyDataProvider);
    selectionLayer = new SelectionLayer(bodyDataLayer);
    viewportLayer = new ViewportLayer(selectionLayer);
    // as the selection mouse bindings are registered for the region label
    // GridRegion.BODY we need to set that region label to the viewport so
    // the selection via mouse is working correctly
    viewportLayer.setRegionName(GridRegion.BODY);
    // register a MoveCellSelectionCommandHandler with
    // TABLE_CYCLE_TRAVERSAL_STRATEGY for horizontal traversal
    // and AXIS_CYCLE_TRAVERSAL_STRATEGY for vertical traversal
    // NOTE:
    // You could achieve the same by registering a command handler
    // with TABLE_CYCLE_TRAVERSAL_STRATEGY and registering
    // MoveSelectionActions with a customized ITraversalStrategy, e.g.
    // AXIS_CYCLE_TRAVERSAL_STRATEGY
    viewportLayer.registerCommandHandler(new MoveCellSelectionCommandHandler(selectionLayer, ITraversalStrategy.TABLE_CYCLE_TRAVERSAL_STRATEGY, ITraversalStrategy.AXIS_CYCLE_TRAVERSAL_STRATEGY));
    natTable = new NatTable(panel, viewportLayer);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
    // 6. edit traversal traversal
    // on left/right we will use TABLE CYCLE
    // on up/down we will use AXIS CYCLE
    bodyDataLayer = new DataLayer(bodyDataProvider);
    selectionLayer = new SelectionLayer(bodyDataLayer);
    viewportLayer = new ViewportLayer(selectionLayer);
    // as the selection mouse bindings are registered for the region label
    // GridRegion.BODY we need to set that region label to the viewport so
    // the selection via mouse is working correctly
    viewportLayer.setRegionName(GridRegion.BODY);
    final ColumnOverrideLabelAccumulator columnLabelAccumulator = new ColumnOverrideLabelAccumulator(bodyDataLayer);
    bodyDataLayer.setConfigLabelAccumulator(columnLabelAccumulator);
    registerColumnLabels(columnLabelAccumulator);
    // add some edit configuration
    viewportLayer.addConfiguration(new DefaultEditBindings());
    viewportLayer.addConfiguration(new DefaultEditConfiguration());
    viewportLayer.addConfiguration(new EditorConfiguration());
    natTable = new NatTable(panel, viewportLayer);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
    // register a MoveCellSelectionCommandHandler with
    // TABLE_CYCLE_TRAVERSAL_STRATEGY for horizontal traversal
    // and AXIS_CYCLE_TRAVERSAL_STRATEGY for vertical traversal
    // NOTE:
    // You could achieve the same by registering a command handler
    // with TABLE_CYCLE_TRAVERSAL_STRATEGY and registering
    // MoveSelectionActions with a customized ITraversalStrategy, e.g.
    // AXIS_CYCLE_TRAVERSAL_STRATEGY
    viewportLayer.registerCommandHandler(new MoveCellSelectionCommandHandler(selectionLayer, new EditTraversalStrategy(ITraversalStrategy.TABLE_CYCLE_TRAVERSAL_STRATEGY, natTable), new EditTraversalStrategy(ITraversalStrategy.AXIS_CYCLE_TRAVERSAL_STRATEGY, natTable)));
    return panel;
}
Also used : ListDataProvider(org.eclipse.nebula.widgets.nattable.data.ListDataProvider) EditTraversalStrategy(org.eclipse.nebula.widgets.nattable.selection.EditTraversalStrategy) Composite(org.eclipse.swt.widgets.Composite) ViewportLayer(org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer) DefaultEditBindings(org.eclipse.nebula.widgets.nattable.edit.config.DefaultEditBindings) ReflectiveColumnPropertyAccessor(org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor) MoveCellSelectionCommandHandler(org.eclipse.nebula.widgets.nattable.selection.MoveCellSelectionCommandHandler) GridLayout(org.eclipse.swt.layout.GridLayout) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) DefaultEditConfiguration(org.eclipse.nebula.widgets.nattable.edit.config.DefaultEditConfiguration) SelectionLayer(org.eclipse.nebula.widgets.nattable.selection.SelectionLayer) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) ColumnOverrideLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.ColumnOverrideLabelAccumulator) Person(org.eclipse.nebula.widgets.nattable.dataset.person.Person)

Example 59 with DataLayer

use of org.eclipse.nebula.widgets.nattable.layer.DataLayer in project nebula.widgets.nattable by eclipse.

the class _5062_CompositeHoverStylingExample method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    // property names of the Person class
    String[] propertyNames = { "firstName", "lastName", "gender", "married", "birthday" };
    // mapping from property to label, needed for column header labels
    Map<String, String> propertyToLabelMap = new HashMap<>();
    propertyToLabelMap.put("firstName", "Firstname");
    propertyToLabelMap.put("lastName", "Lastname");
    propertyToLabelMap.put("gender", "Gender");
    propertyToLabelMap.put("married", "Married");
    propertyToLabelMap.put("birthday", "Birthday");
    // build the body layer stack
    // Usually you would create a new layer stack by extending
    // AbstractIndexLayerTransform and setting the ViewportLayer
    // as underlying layer. But in this case using the ViewportLayer
    // directly as body layer is also working.
    IDataProvider bodyDataProvider = new DefaultBodyDataProvider<>(PersonService.getPersons(10), propertyNames);
    DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
    HoverLayer hoverLayer = new HoverLayer(bodyDataLayer);
    SelectionLayer selectionLayer = new SelectionLayer(hoverLayer);
    ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
    // build the column header layer
    IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
    DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
    HoverLayer columnHoverLayer = new HoverLayer(columnHeaderDataLayer, false);
    ColumnHeaderLayer columnHeaderLayer = new ColumnHeaderLayer(columnHoverLayer, viewportLayer, selectionLayer, false);
    // add ColumnHeaderHoverLayerConfiguration to ensure that hover styling
    // and resizing is working together
    columnHeaderLayer.addConfiguration(new ColumnHeaderHoverLayerConfiguration(columnHoverLayer));
    CompositeLayer compLayer = new CompositeLayer(1, 2);
    compLayer.setChildLayer(GridRegion.COLUMN_HEADER, columnHeaderLayer, 0, 0);
    compLayer.setChildLayer(GridRegion.BODY, viewportLayer, 0, 1);
    // turn the auto configuration off as we want to add our hover styling
    // configuration
    NatTable natTable = new NatTable(parent, compLayer, false);
    // as the autoconfiguration of the NatTable is turned off, we have to
    // add the DefaultNatTableStyleConfiguration manually
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
    // add the style configuration for hover
    natTable.addConfiguration(new AbstractRegistryConfiguration() {

        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            Style style = new Style();
            style.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR, GUIHelper.getColor(217, 232, 251));
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.HOVER);
            Image bgImage = GUIHelper.getImageByURL("columnHeaderBg", getClass().getResource("/org/eclipse/nebula/widgets/nattable/examples/resources/column_header_bg.png"));
            Image hoverBgImage = GUIHelper.getImageByURL("hoverColumnHeaderBg", getClass().getResource("/org/eclipse/nebula/widgets/nattable/examples/resources/hovered_column_header_bg.png"));
            TextPainter txtPainter = new TextPainter(false, false);
            ICellPainter bgImagePainter = new BackgroundImagePainter(txtPainter, bgImage, GUIHelper.getColor(192, 192, 192));
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, bgImagePainter, DisplayMode.NORMAL, GridRegion.COLUMN_HEADER);
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, bgImagePainter, DisplayMode.NORMAL, GridRegion.CORNER);
            ICellPainter hoveredHeaderPainter = new BackgroundImagePainter(txtPainter, hoverBgImage, GUIHelper.getColor(192, 192, 192));
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, hoveredHeaderPainter, DisplayMode.HOVER, GridRegion.COLUMN_HEADER);
        }
    });
    natTable.configure();
    return natTable;
}
Also used : ColumnHeaderHoverLayerConfiguration(org.eclipse.nebula.widgets.nattable.hover.config.ColumnHeaderHoverLayerConfiguration) HashMap(java.util.HashMap) ColumnHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer) AbstractRegistryConfiguration(org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration) IDataProvider(org.eclipse.nebula.widgets.nattable.data.IDataProvider) HoverLayer(org.eclipse.nebula.widgets.nattable.hover.HoverLayer) ViewportLayer(org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer) Image(org.eclipse.swt.graphics.Image) DefaultBodyDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultBodyDataProvider) ICellPainter(org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) DefaultColumnHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer) DefaultNatTableStyleConfiguration(org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration) Style(org.eclipse.nebula.widgets.nattable.style.Style) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) BackgroundImagePainter(org.eclipse.nebula.widgets.nattable.painter.cell.BackgroundImagePainter) TextPainter(org.eclipse.nebula.widgets.nattable.painter.cell.TextPainter) DefaultColumnHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer) CompositeLayer(org.eclipse.nebula.widgets.nattable.layer.CompositeLayer) SelectionLayer(org.eclipse.nebula.widgets.nattable.selection.SelectionLayer) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) DefaultColumnHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider)

Example 60 with DataLayer

use of org.eclipse.nebula.widgets.nattable.layer.DataLayer in project nebula.widgets.nattable by eclipse.

the class _423_ThemeStylingExample method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);
    container.setLayout(new GridLayout());
    // property names of the Person class
    String[] propertyNames = { "firstName", "lastName", "gender", "married", "birthday" };
    // mapping from property to label, needed for column header labels
    Map<String, String> propertyToLabelMap = new HashMap<>();
    propertyToLabelMap.put("firstName", "Firstname");
    propertyToLabelMap.put("lastName", "Lastname");
    propertyToLabelMap.put("gender", "Gender");
    propertyToLabelMap.put("married", "Married");
    propertyToLabelMap.put("birthday", "Birthday");
    // build the body layer stack
    // Usually you would create a new layer stack by extending
    // AbstractIndexLayerTransform and setting the ViewportLayer
    // as underlying layer. But in this case using the ViewportLayer
    // directly as body layer is also working.
    final ListDataProvider<Person> bodyDataProvider = new DefaultBodyDataProvider<>(PersonService.getPersons(10), propertyNames);
    final DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
    HoverLayer bodyHoverLayer = new HoverLayer(bodyDataLayer);
    SelectionLayer selectionLayer = new SelectionLayer(bodyHoverLayer);
    ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
    // add labels to provider conditional styling
    AggregateConfigLabelAccumulator labelAccumulator = new AggregateConfigLabelAccumulator();
    labelAccumulator.add(new ColumnLabelAccumulator());
    labelAccumulator.add(new IConfigLabelAccumulator() {

        @Override
        public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
            Person p = bodyDataProvider.getRowObject(rowPosition);
            if (p != null) {
                configLabels.addLabel(p.getGender().equals(Gender.FEMALE) ? FEMALE_LABEL : MALE_LABEL);
            }
        }
    });
    bodyDataLayer.setConfigLabelAccumulator(labelAccumulator);
    // build the column header layer
    IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
    final DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
    HoverLayer columnHoverLayer = new HoverLayer(columnHeaderDataLayer, false);
    final ColumnHeaderLayer columnHeaderLayer = new ColumnHeaderLayer(columnHoverLayer, viewportLayer, selectionLayer, false);
    // add ColumnHeaderHoverLayerConfiguration to ensure that hover styling
    // and resizing is working together
    columnHeaderLayer.addConfiguration(new ColumnHeaderHoverLayerConfiguration(columnHoverLayer));
    // build the row header layer
    IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
    DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
    HoverLayer rowHoverLayer = new HoverLayer(rowHeaderDataLayer, false);
    RowHeaderLayer rowHeaderLayer = new RowHeaderLayer(rowHoverLayer, viewportLayer, selectionLayer, false);
    // add RowHeaderHoverLayerConfiguration to ensure that hover styling and
    // resizing is working together
    rowHeaderLayer.addConfiguration(new RowHeaderHoverLayerConfiguration(rowHoverLayer));
    // build the corner layer
    IDataProvider cornerDataProvider = new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider);
    DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
    final CornerLayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, columnHeaderLayer);
    // build the grid layer
    final GridLayer gridLayer = new GridLayer(viewportLayer, columnHeaderLayer, rowHeaderLayer, cornerLayer);
    final NatTable natTable = new NatTable(container, gridLayer);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
    // adding a full border
    natTable.addOverlayPainter(new NatTableBorderOverlayPainter(natTable.getConfigRegistry()));
    Composite buttonPanel = new Composite(container, SWT.NONE);
    buttonPanel.setLayout(new GridLayout(3, true));
    GridDataFactory.fillDefaults().grab(true, false).applyTo(buttonPanel);
    final ThemeConfiguration defaultTheme = new DefaultNatTableThemeConfiguration();
    final ThemeConfiguration modernTheme = new ModernNatTableThemeConfiguration();
    final ThemeConfiguration darkTheme = new DarkNatTableThemeConfiguration();
    final ThemeConfiguration conditionalDefaultTheme = new DefaultNatTableThemeConfiguration();
    conditionalDefaultTheme.addThemeExtension(new ConditionalStylingThemeExtension());
    final ThemeConfiguration conditionalModernTheme = new ModernNatTableThemeConfiguration();
    conditionalModernTheme.addThemeExtension(new ConditionalStylingThemeExtension());
    final ThemeConfiguration conditionalDarkTheme = new DarkNatTableThemeConfiguration();
    conditionalDarkTheme.addThemeExtension(new ConditionalStylingThemeExtension());
    final ThemeConfiguration hoverTheme = new HoverThemeConfiguration();
    final ThemeConfiguration fontTheme = new FontStylingThemeConfiguration();
    Button defaultThemeButton = new Button(buttonPanel, SWT.PUSH);
    defaultThemeButton.setText("NatTable Default Theme");
    defaultThemeButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            natTable.setTheme(defaultTheme);
            // reset to default state
            cleanupNonThemeSettings(gridLayer, bodyDataLayer, columnHeaderDataLayer);
        }
    });
    Button modernThemeButton = new Button(buttonPanel, SWT.PUSH);
    modernThemeButton.setText("NatTable Modern Theme");
    modernThemeButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            natTable.setTheme(modernTheme);
            // reset to default state
            cleanupNonThemeSettings(gridLayer, bodyDataLayer, columnHeaderDataLayer);
        }
    });
    Button darkThemeButton = new Button(buttonPanel, SWT.PUSH);
    darkThemeButton.setText("NatTable Dark Theme");
    darkThemeButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            natTable.setTheme(darkTheme);
            // reset to default state
            cleanupNonThemeSettings(gridLayer, bodyDataLayer, columnHeaderDataLayer);
        }
    });
    Button conditionalThemeButton = new Button(buttonPanel, SWT.PUSH);
    conditionalThemeButton.setText("Conditional Default Theme");
    conditionalThemeButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            natTable.setTheme(conditionalDefaultTheme);
            // reset to default state
            cleanupNonThemeSettings(gridLayer, bodyDataLayer, columnHeaderDataLayer);
        }
    });
    Button conditionalModernThemeButton = new Button(buttonPanel, SWT.PUSH);
    conditionalModernThemeButton.setText("Conditional Modern Theme");
    conditionalModernThemeButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            natTable.setTheme(conditionalModernTheme);
            // reset to default state
            cleanupNonThemeSettings(gridLayer, bodyDataLayer, columnHeaderDataLayer);
        }
    });
    Button conditionalDarkThemeButton = new Button(buttonPanel, SWT.PUSH);
    conditionalDarkThemeButton.setText("Conditional Dark Theme");
    conditionalDarkThemeButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            natTable.setTheme(conditionalDarkTheme);
            // reset to default state
            cleanupNonThemeSettings(gridLayer, bodyDataLayer, columnHeaderDataLayer);
        }
    });
    Button hoverThemeButton = new Button(buttonPanel, SWT.PUSH);
    hoverThemeButton.setText("Hover Theme");
    hoverThemeButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            natTable.setTheme(hoverTheme);
            // reset to default state
            cleanupNonThemeSettings(gridLayer, bodyDataLayer, columnHeaderDataLayer);
        }
    });
    Button fontThemeButton = new Button(buttonPanel, SWT.PUSH);
    fontThemeButton.setText("Increased Font Theme");
    fontThemeButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            natTable.setTheme(fontTheme);
            // we are simply increasing the default width and height in this
            // example we could also register TextPainters that calculate
            // their height by content but they are not able to shrink again
            columnHeaderDataLayer.setDefaultRowHeight(30);
            columnHeaderDataLayer.setDefaultColumnWidth(130);
            bodyDataLayer.setDefaultRowHeight(30);
            bodyDataLayer.setDefaultColumnWidth(130);
        }
    });
    return container;
}
Also used : LabelStack(org.eclipse.nebula.widgets.nattable.layer.LabelStack) ColumnHeaderHoverLayerConfiguration(org.eclipse.nebula.widgets.nattable.hover.config.ColumnHeaderHoverLayerConfiguration) HashMap(java.util.HashMap) ColumnHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer) IConfigLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.IConfigLabelAccumulator) DefaultCornerDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider) HoverLayer(org.eclipse.nebula.widgets.nattable.hover.HoverLayer) ViewportLayer(org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer) IDataProvider(org.eclipse.nebula.widgets.nattable.data.IDataProvider) DefaultBodyDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultBodyDataProvider) AggregateConfigLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.AggregateConfigLabelAccumulator) DarkNatTableThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.DarkNatTableThemeConfiguration) GridLayout(org.eclipse.swt.layout.GridLayout) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) DefaultColumnHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) NatTableBorderOverlayPainter(org.eclipse.nebula.widgets.nattable.painter.NatTableBorderOverlayPainter) ModernNatTableThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.ModernNatTableThemeConfiguration) DefaultNatTableThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.DefaultNatTableThemeConfiguration) ModernNatTableThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.ModernNatTableThemeConfiguration) ThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.ThemeConfiguration) DarkNatTableThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.DarkNatTableThemeConfiguration) Composite(org.eclipse.swt.widgets.Composite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) DefaultColumnHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer) DefaultRowHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider) DefaultNatTableThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.DefaultNatTableThemeConfiguration) RowHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer) CornerLayer(org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer) SelectionLayer(org.eclipse.nebula.widgets.nattable.selection.SelectionLayer) RowHeaderHoverLayerConfiguration(org.eclipse.nebula.widgets.nattable.hover.config.RowHeaderHoverLayerConfiguration) GridLayer(org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer) ColumnLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.ColumnLabelAccumulator) DefaultColumnHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider) Person(org.eclipse.nebula.widgets.nattable.dataset.person.Person)

Aggregations

DataLayer (org.eclipse.nebula.widgets.nattable.layer.DataLayer)227 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)150 IDataProvider (org.eclipse.nebula.widgets.nattable.data.IDataProvider)99 DefaultNatTableStyleConfiguration (org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration)96 ColumnHeaderLayer (org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer)93 ILayer (org.eclipse.nebula.widgets.nattable.layer.ILayer)91 HashMap (java.util.HashMap)83 DefaultColumnHeaderDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider)82 DefaultCornerDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider)78 RowHeaderLayer (org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer)77 ViewportLayer (org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer)77 CornerLayer (org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer)76 DefaultRowHeaderDataLayer (org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer)75 SelectionLayer (org.eclipse.nebula.widgets.nattable.selection.SelectionLayer)75 DefaultRowHeaderDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider)74 GridLayer (org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer)74 DefaultColumnHeaderDataLayer (org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer)70 ListDataProvider (org.eclipse.nebula.widgets.nattable.data.ListDataProvider)64 Test (org.junit.Test)62 GridLayout (org.eclipse.swt.layout.GridLayout)56