Search in sources :

Example 1 with UiBindingRegistry

use of org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry in project nebula.widgets.nattable by eclipse.

the class _5064_GridHeaderHoverStylingExample 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);
    SelectionLayer selectionLayer = new SelectionLayer(bodyDataLayer);
    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));
    // 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);
    ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, columnHeaderLayer);
    // build the grid layer
    GridLayer gridLayer = new GridLayer(viewportLayer, columnHeaderLayer, rowHeaderLayer, cornerLayer);
    // turn the auto configuration off as we want to add our header menu
    // configuration
    NatTable natTable = new NatTable(parent, gridLayer, false);
    // as the autoconfiguration of the NatTable is turned off, we have to
    // add the DefaultNatTableStyleConfiguration manually
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
    natTable.addConfiguration(new AbstractUiBindingConfiguration() {

        @Override
        public void configureUiBindings(UiBindingRegistry uiBindingRegistry) {
            uiBindingRegistry.registerMouseMoveBinding(new MouseEventMatcher(GridRegion.BODY), new ClearHoverStylingAction());
        }
    });
    // 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.COLOR_RED);
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.HOVER, GridRegion.ROW_HEADER);
            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"));
            Image selectedBgImage = GUIHelper.getImageByURL("selectedColumnHeaderBg", getClass().getResource("/org/eclipse/nebula/widgets/nattable/examples/resources/selected_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);
            ICellPainter selectedHeaderPainter = new BackgroundImagePainter(txtPainter, selectedBgImage, GUIHelper.getColor(192, 192, 192));
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, selectedHeaderPainter, DisplayMode.SELECT, 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) DefaultCornerDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider) IDataProvider(org.eclipse.nebula.widgets.nattable.data.IDataProvider) ViewportLayer(org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer) HoverLayer(org.eclipse.nebula.widgets.nattable.hover.HoverLayer) 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) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) DefaultColumnHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer) DefaultNatTableStyleConfiguration(org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) Style(org.eclipse.nebula.widgets.nattable.style.Style) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) BackgroundImagePainter(org.eclipse.nebula.widgets.nattable.painter.cell.BackgroundImagePainter) UiBindingRegistry(org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry) MouseEventMatcher(org.eclipse.nebula.widgets.nattable.ui.matcher.MouseEventMatcher) ILayer(org.eclipse.nebula.widgets.nattable.layer.ILayer) TextPainter(org.eclipse.nebula.widgets.nattable.painter.cell.TextPainter) DefaultColumnHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer) DefaultRowHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider) AbstractUiBindingConfiguration(org.eclipse.nebula.widgets.nattable.config.AbstractUiBindingConfiguration) RowHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer) ClearHoverStylingAction(org.eclipse.nebula.widgets.nattable.hover.action.ClearHoverStylingAction) CornerLayer(org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer) SelectionLayer(org.eclipse.nebula.widgets.nattable.selection.SelectionLayer) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) RowHeaderHoverLayerConfiguration(org.eclipse.nebula.widgets.nattable.hover.config.RowHeaderHoverLayerConfiguration) GridLayer(org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer) DefaultColumnHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider)

Example 2 with UiBindingRegistry

use of org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry in project nebula.widgets.nattable by eclipse.

the class _811_GroupBySummaryFixedSummaryRowExample method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);
    container.setLayout(new GridLayout());
    // create a new ConfigRegistry which will be needed for GlazedLists
    // handling
    final ConfigRegistry configRegistry = new ConfigRegistry();
    // property names of the ExtendedPersonWithAddress class
    String[] propertyNames = { "firstName", "lastName", "age", "money", "married", "gender", "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("age", "Age");
    propertyToLabelMap.put("money", "Money");
    propertyToLabelMap.put("married", "Married");
    propertyToLabelMap.put("gender", "Gender");
    propertyToLabelMap.put("birthday", "Birthday");
    final IColumnPropertyAccessor<ExtendedPersonWithAddress> columnPropertyAccessor = new ExtendedReflectiveColumnPropertyAccessor<>(propertyNames);
    // to enable the group by summary feature, the GroupByDataLayer needs to
    // know the ConfigRegistry
    List<ExtendedPersonWithAddress> persons = PersonService.getExtendedPersonsWithAddress(10);
    final BodyLayerStack<ExtendedPersonWithAddress> bodyLayerStack = new BodyLayerStack<>(persons, columnPropertyAccessor, configRegistry);
    bodyLayerStack.getBodyDataLayer().setConfigLabelAccumulator(new ColumnLabelAccumulator());
    // build the column header layer
    IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
    DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
    ILayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayerStack, bodyLayerStack.getSelectionLayer());
    // add sorting
    SortHeaderLayer<ExtendedPersonWithAddress> sortHeaderLayer = new SortHeaderLayer<>(columnHeaderLayer, new GlazedListsSortModel<>(bodyLayerStack.getSortedList(), columnPropertyAccessor, configRegistry, columnHeaderDataLayer), false);
    // connect sortModel to GroupByDataLayer to support sorting by group by
    // summary values
    bodyLayerStack.getBodyDataLayer().initializeTreeComparator(sortHeaderLayer.getSortModel(), bodyLayerStack.getTreeLayer(), true);
    // build the row header layer
    // Adding the specialized DefaultSummaryRowHeaderDataProvider to
    // indicate the summary row in the row header
    IDataProvider rowHeaderDataProvider = new DefaultSummaryRowHeaderDataProvider(bodyLayerStack.getBodyDataProvider(), "\u2211");
    final DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
    ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, bodyLayerStack, bodyLayerStack.getSelectionLayer());
    // build the corner layer
    IDataProvider cornerDataProvider = new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider);
    DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
    ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, sortHeaderLayer);
    // build the grid layer
    GridLayer gridLayer = new GridLayer(bodyLayerStack, sortHeaderLayer, rowHeaderLayer, cornerLayer, false);
    FixedSummaryRowLayer summaryRowLayer = new FixedSummaryRowLayer(bodyLayerStack.getGlazedListsEventLayer(), gridLayer, configRegistry, false);
    summaryRowLayer.setSummaryRowLabel("\u2211");
    summaryRowLayer.setConfigLabelAccumulator(new AbstractOverrider() {

        @Override
        public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
            if (columnPosition == 0) {
                // our label is more important regarding styling than the
                // summary row labels
                configLabels.addLabelOnTop(ROW_HEADER_SUMMARY_ROW);
            }
        }
    });
    // ensure the body data layer uses a layer painter with correct
    // configured clipping
    bodyLayerStack.getBodyDataLayer().setLayerPainter(new GridLineCellLayerPainter(false, true));
    // set the group by header on top of the grid
    CompositeLayer compositeGridLayer = new CompositeLayer(1, 3);
    final GroupByHeaderLayer groupByHeaderLayer = new GroupByHeaderLayer(bodyLayerStack.getGroupByModel(), gridLayer, columnHeaderDataProvider);
    compositeGridLayer.setChildLayer(GroupByHeaderLayer.GROUP_BY_REGION, groupByHeaderLayer, 0, 0);
    compositeGridLayer.setChildLayer("Grid", gridLayer, 0, 1);
    compositeGridLayer.setChildLayer("Summary", summaryRowLayer, 0, 2);
    // add the export configuration to the composite layer
    compositeGridLayer.registerCommandHandler(new ExportCommandHandler(compositeGridLayer));
    compositeGridLayer.addConfiguration(new IConfiguration() {

        @Override
        public void configureUiBindings(UiBindingRegistry uiBindingRegistry) {
            uiBindingRegistry.registerKeyBinding(new KeyEventMatcher(SWT.MOD1, 'e'), new ExportAction());
        }

        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            PoiExcelExporter exporter = new HSSFExcelExporter();
            exporter.setApplyBackgroundColor(false);
            configRegistry.registerConfigAttribute(ExportConfigAttributes.EXPORTER, exporter);
            configRegistry.registerConfigAttribute(ExportConfigAttributes.EXPORT_FORMATTER, new DefaultExportFormatter());
            configRegistry.registerConfigAttribute(ExportConfigAttributes.DATE_FORMAT, // $NON-NLS-1$
            "m/d/yy h:mm");
        }

        @Override
        public void configureLayer(ILayer layer) {
        }
    });
    // turn the auto configuration off as we want to add our header menu
    // configuration
    final NatTable natTable = new NatTable(container, compositeGridLayer, false);
    // as the autoconfiguration of the NatTable is turned off, we have to
    // add the DefaultNatTableStyleConfiguration and the ConfigRegistry
    // manually
    natTable.setConfigRegistry(configRegistry);
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
    // add some additional styling
    natTable.addConfiguration(new AbstractRegistryConfiguration() {

        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new CheckBoxPainter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 4);
            IStyle style = new Style();
            style.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.RIGHT);
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 2);
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
            configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultDoubleDisplayConverter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
            // the main styling of the summary row cell in the row header is
            // done via summary row default style, but we need to override
            // the alignment
            style = new Style();
            style.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.CENTER);
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, ROW_HEADER_SUMMARY_ROW);
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.SELECT, ROW_HEADER_SUMMARY_ROW);
        }
    });
    // add sorting configuration
    natTable.addConfiguration(new SingleClickSortConfiguration());
    this.sumMoneyGroupBySummaryProvider = new SummationGroupBySummaryProvider<>(columnPropertyAccessor);
    this.avgMoneyGroupBySummaryProvider = new AverageMoneyGroupBySummaryProvider();
    // create a new IDataProvider that operates on the basic underlying list
    // this is necessary because the IDataProvider in the body layer stack
    // is operating on the TreeList, and on collapsing a node, the children
    // will be not visible, which has effect on the summary value.
    final IDataProvider summaryDataProvider = new ListDataProvider<>(bodyLayerStack.getSortedList(), columnPropertyAccessor);
    this.sumMoneySummaryProvider = new SummationSummaryProvider(summaryDataProvider, false);
    this.avgMoneySummaryProvider = new AverageMoneySummaryProvider(summaryDataProvider);
    // add group by summary configuration
    natTable.addConfiguration(new AbstractRegistryConfiguration() {

        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            // GroupBy summary configuration
            configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, _811_GroupBySummaryFixedSummaryRowExample.this.sumMoneyGroupBySummaryProvider, DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 3);
            configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, new AverageAgeGroupBySummaryProvider(), DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 2);
            configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_CHILD_COUNT_PATTERN, "[{0}] - ({1})");
            // SummaryRow configuration
            configRegistry.registerConfigAttribute(SummaryRowConfigAttributes.SUMMARY_PROVIDER, _811_GroupBySummaryFixedSummaryRowExample.this.sumMoneySummaryProvider, DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
            configRegistry.registerConfigAttribute(SummaryRowConfigAttributes.SUMMARY_PROVIDER, new AverageAgeSummaryProvider(summaryDataProvider), DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 2);
            configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new SummaryDisplayConverter(new DefaultDoubleDisplayConverter()), DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
        }
    });
    // add group by header configuration
    natTable.addConfiguration(new GroupByHeaderMenuConfiguration(natTable, groupByHeaderLayer));
    natTable.addConfiguration(new AbstractHeaderMenuConfiguration(natTable) {

        @Override
        protected PopupMenuBuilder createColumnHeaderMenu(NatTable natTable) {
            return super.createColumnHeaderMenu(natTable).withHideColumnMenuItem().withShowAllColumnsMenuItem().withStateManagerMenuItemProvider();
        }

        @Override
        protected PopupMenuBuilder createCornerMenu(NatTable natTable) {
            return super.createCornerMenu(natTable).withShowAllColumnsMenuItem().withStateManagerMenuItemProvider();
        }
    });
    // adds the key bindings that allow space bar to be pressed to
    // expand/collapse tree nodes
    natTable.addConfiguration(new TreeLayerExpandCollapseKeyBindings(bodyLayerStack.getTreeLayer(), bodyLayerStack.getSelectionLayer()));
    natTable.configure();
    // set the modern theme to visualize the summary better
    final ThemeConfiguration defaultTheme = new DefaultNatTableThemeConfiguration();
    defaultTheme.addThemeExtension(new DefaultGroupByThemeExtension());
    final ThemeConfiguration modernTheme = new ModernNatTableThemeConfiguration();
    modernTheme.addThemeExtension(new ModernGroupByThemeExtension());
    final ThemeConfiguration darkTheme = new DarkNatTableThemeConfiguration();
    darkTheme.addThemeExtension(new DarkGroupByThemeExtension());
    natTable.setTheme(modernTheme);
    // add a border on every side of the table
    natTable.addOverlayPainter(new NatTableBorderOverlayPainter());
    natTable.registerCommandHandler(new DisplayPersistenceDialogCommandHandler(natTable));
    GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
    Composite buttonPanel = new Composite(container, SWT.NONE);
    buttonPanel.setLayout(new RowLayout());
    GridDataFactory.fillDefaults().grab(true, false).applyTo(buttonPanel);
    Button toggleHeaderButton = new Button(buttonPanel, SWT.PUSH);
    toggleHeaderButton.setText("Toggle Group By Header");
    toggleHeaderButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            groupByHeaderLayer.setVisible(!groupByHeaderLayer.isVisible());
        }
    });
    Button collapseAllButton = new Button(buttonPanel, SWT.PUSH);
    collapseAllButton.setText("Collapse All");
    collapseAllButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            natTable.doCommand(new TreeCollapseAllCommand());
        }
    });
    Button expandAllButton = new Button(buttonPanel, SWT.PUSH);
    expandAllButton.setText("Expand All");
    expandAllButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            natTable.doCommand(new TreeExpandAllCommand());
        }
    });
    Button toggleMoneySummaryButton = new Button(buttonPanel, SWT.PUSH);
    toggleMoneySummaryButton.setText("Toggle Money Group Summary (SUM/AVG)");
    toggleMoneySummaryButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            // clear the group by summary cache so the new summary
            // calculation gets triggered
            bodyLayerStack.getBodyDataLayer().clearCache();
            _811_GroupBySummaryFixedSummaryRowExample.this.useMoneySum = !_811_GroupBySummaryFixedSummaryRowExample.this.useMoneySum;
            if (_811_GroupBySummaryFixedSummaryRowExample.this.useMoneySum) {
                configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, _811_GroupBySummaryFixedSummaryRowExample.this.sumMoneyGroupBySummaryProvider, DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 3);
                configRegistry.registerConfigAttribute(SummaryRowConfigAttributes.SUMMARY_PROVIDER, _811_GroupBySummaryFixedSummaryRowExample.this.sumMoneySummaryProvider, DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
            } else {
                configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, _811_GroupBySummaryFixedSummaryRowExample.this.avgMoneyGroupBySummaryProvider, DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 3);
                configRegistry.registerConfigAttribute(SummaryRowConfigAttributes.SUMMARY_PROVIDER, _811_GroupBySummaryFixedSummaryRowExample.this.avgMoneySummaryProvider, DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
            }
            natTable.doCommand(new VisualRefreshCommand());
        }
    });
    Button toggleThemeButton = new Button(buttonPanel, SWT.PUSH);
    toggleThemeButton.setText("Toggle Theme");
    toggleThemeButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (_811_GroupBySummaryFixedSummaryRowExample.this.currentTheme == 0) {
                natTable.setTheme(modernTheme);
                _811_GroupBySummaryFixedSummaryRowExample.this.currentTheme++;
            } else if (_811_GroupBySummaryFixedSummaryRowExample.this.currentTheme == 1) {
                natTable.setTheme(darkTheme);
                _811_GroupBySummaryFixedSummaryRowExample.this.currentTheme++;
            } else if (_811_GroupBySummaryFixedSummaryRowExample.this.currentTheme == 2) {
                natTable.setTheme(defaultTheme);
                _811_GroupBySummaryFixedSummaryRowExample.this.currentTheme = 0;
            }
        }
    });
    Button button = new Button(buttonPanel, SWT.PUSH);
    button.setText("Add Row");
    button.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            bodyLayerStack.getSortedList().add(PersonService.createExtendedPersonWithAddress(bodyLayerStack.getSortedList().size()));
        }
    });
    return container;
}
Also used : SummaryDisplayConverter(org.eclipse.nebula.widgets.nattable.summaryrow.SummaryDisplayConverter) ExtendedPersonWithAddress(org.eclipse.nebula.widgets.nattable.dataset.person.ExtendedPersonWithAddress) HashMap(java.util.HashMap) AbstractRegistryConfiguration(org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration) DefaultGroupByThemeExtension(org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.DefaultGroupByThemeExtension) DefaultCornerDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider) DarkNatTableThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.DarkNatTableThemeConfiguration) ConfigRegistry(org.eclipse.nebula.widgets.nattable.config.ConfigRegistry) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) DefaultColumnHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer) GroupByDataLayer(org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.GroupByDataLayer) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) TreeCollapseAllCommand(org.eclipse.nebula.widgets.nattable.tree.command.TreeCollapseAllCommand) Button(org.eclipse.swt.widgets.Button) DefaultNatTableStyleConfiguration(org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration) ExportCommandHandler(org.eclipse.nebula.widgets.nattable.export.command.ExportCommandHandler) SelectionEvent(org.eclipse.swt.events.SelectionEvent) NatTableBorderOverlayPainter(org.eclipse.nebula.widgets.nattable.painter.NatTableBorderOverlayPainter) GroupByHeaderLayer(org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.GroupByHeaderLayer) UiBindingRegistry(org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry) AbstractOverrider(org.eclipse.nebula.widgets.nattable.layer.cell.AbstractOverrider) DarkGroupByThemeExtension(org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.DarkGroupByThemeExtension) SortHeaderLayer(org.eclipse.nebula.widgets.nattable.sort.SortHeaderLayer) SummationSummaryProvider(org.eclipse.nebula.widgets.nattable.summaryrow.SummationSummaryProvider) DefaultColumnHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer) CompositeLayer(org.eclipse.nebula.widgets.nattable.layer.CompositeLayer) IStyle(org.eclipse.nebula.widgets.nattable.style.IStyle) PoiExcelExporter(org.eclipse.nebula.widgets.nattable.extension.poi.PoiExcelExporter) ExportAction(org.eclipse.nebula.widgets.nattable.export.action.ExportAction) GridLayer(org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer) ColumnLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.ColumnLabelAccumulator) GridLineCellLayerPainter(org.eclipse.nebula.widgets.nattable.painter.layer.GridLineCellLayerPainter) IConfiguration(org.eclipse.nebula.widgets.nattable.config.IConfiguration) ListDataProvider(org.eclipse.nebula.widgets.nattable.data.ListDataProvider) DefaultSummaryRowHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultSummaryRowHeaderDataProvider) LabelStack(org.eclipse.nebula.widgets.nattable.layer.LabelStack) DefaultExportFormatter(org.eclipse.nebula.widgets.nattable.export.excel.DefaultExportFormatter) ColumnHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer) IDataProvider(org.eclipse.nebula.widgets.nattable.data.IDataProvider) GroupByHeaderMenuConfiguration(org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.GroupByHeaderMenuConfiguration) DefaultDoubleDisplayConverter(org.eclipse.nebula.widgets.nattable.data.convert.DefaultDoubleDisplayConverter) GridLayout(org.eclipse.swt.layout.GridLayout) DisplayPersistenceDialogCommandHandler(org.eclipse.nebula.widgets.nattable.persistence.command.DisplayPersistenceDialogCommandHandler) KeyEventMatcher(org.eclipse.nebula.widgets.nattable.ui.matcher.KeyEventMatcher) ExtendedReflectiveColumnPropertyAccessor(org.eclipse.nebula.widgets.nattable.data.ExtendedReflectiveColumnPropertyAccessor) CheckBoxPainter(org.eclipse.nebula.widgets.nattable.painter.cell.CheckBoxPainter) TreeLayerExpandCollapseKeyBindings(org.eclipse.nebula.widgets.nattable.tree.config.TreeLayerExpandCollapseKeyBindings) RowLayout(org.eclipse.swt.layout.RowLayout) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) IStyle(org.eclipse.nebula.widgets.nattable.style.IStyle) Style(org.eclipse.nebula.widgets.nattable.style.Style) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) VisualRefreshCommand(org.eclipse.nebula.widgets.nattable.command.VisualRefreshCommand) ModernNatTableThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.ModernNatTableThemeConfiguration) DefaultNatTableThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.DefaultNatTableThemeConfiguration) ThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.ThemeConfiguration) DarkNatTableThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.DarkNatTableThemeConfiguration) ModernNatTableThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.ModernNatTableThemeConfiguration) Composite(org.eclipse.swt.widgets.Composite) ILayer(org.eclipse.nebula.widgets.nattable.layer.ILayer) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) DefaultNatTableThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.DefaultNatTableThemeConfiguration) FixedSummaryRowLayer(org.eclipse.nebula.widgets.nattable.summaryrow.FixedSummaryRowLayer) RowHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer) TreeExpandAllCommand(org.eclipse.nebula.widgets.nattable.tree.command.TreeExpandAllCommand) CornerLayer(org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer) SingleClickSortConfiguration(org.eclipse.nebula.widgets.nattable.sort.config.SingleClickSortConfiguration) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) AbstractHeaderMenuConfiguration(org.eclipse.nebula.widgets.nattable.ui.menu.AbstractHeaderMenuConfiguration) DefaultColumnHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider) PopupMenuBuilder(org.eclipse.nebula.widgets.nattable.ui.menu.PopupMenuBuilder) HSSFExcelExporter(org.eclipse.nebula.widgets.nattable.extension.poi.HSSFExcelExporter) ModernGroupByThemeExtension(org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.ModernGroupByThemeExtension)

Example 3 with UiBindingRegistry

use of org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry in project nebula.widgets.nattable by eclipse.

the class EditableGridExample method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    DefaultGridLayer gridLayer = new DefaultGridLayer(RowDataListFixture.getList(), RowDataListFixture.getPropertyNames(), RowDataListFixture.getPropertyToLabelMap());
    DataLayer columnHeaderDataLayer = (DataLayer) gridLayer.getColumnHeaderDataLayer();
    columnHeaderDataLayer.setConfigLabelAccumulator(new ColumnLabelAccumulator());
    final DataLayer bodyDataLayer = (DataLayer) gridLayer.getBodyDataLayer();
    IDataProvider dataProvider = bodyDataLayer.getDataProvider();
    // NOTE: Register the accumulator on the body data layer.
    // This ensures that the labels are bound to the column index and are
    // unaffected by column order.
    final ColumnOverrideLabelAccumulator columnLabelAccumulator = new ColumnOverrideLabelAccumulator(bodyDataLayer);
    bodyDataLayer.setConfigLabelAccumulator(columnLabelAccumulator);
    NatTable natTable = new NatTable(parent, gridLayer, false);
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
    natTable.addConfiguration(new HeaderMenuConfiguration(natTable));
    natTable.addConfiguration(editableGridConfiguration(columnLabelAccumulator, dataProvider));
    final ColumnHeaderCheckBoxPainter columnHeaderCheckBoxPainter = new ColumnHeaderCheckBoxPainter(bodyDataLayer);
    final ICellPainter column9HeaderPainter = new BeveledBorderDecorator(new CellPainterDecorator(new TextPainter(), CellEdgeEnum.RIGHT, columnHeaderCheckBoxPainter));
    natTable.addConfiguration(new AbstractRegistryConfiguration() {

        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, column9HeaderPainter, DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 9);
        }

        @Override
        public void configureUiBindings(UiBindingRegistry uiBindingRegistry) {
            uiBindingRegistry.registerFirstSingleClickBinding(new CellPainterMouseEventMatcher(GridRegion.COLUMN_HEADER, MouseEventMatcher.LEFT_BUTTON, columnHeaderCheckBoxPainter), new ToggleCheckBoxColumnAction(columnHeaderCheckBoxPainter, bodyDataLayer));
        }
    });
    natTable.configure();
    return natTable;
}
Also used : AbstractRegistryConfiguration(org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration) HeaderMenuConfiguration(org.eclipse.nebula.widgets.nattable.ui.menu.HeaderMenuConfiguration) CellPainterMouseEventMatcher(org.eclipse.nebula.widgets.nattable.ui.matcher.CellPainterMouseEventMatcher) TextPainter(org.eclipse.nebula.widgets.nattable.painter.cell.TextPainter) IDataProvider(org.eclipse.nebula.widgets.nattable.data.IDataProvider) CellPainterDecorator(org.eclipse.nebula.widgets.nattable.painter.cell.decorator.CellPainterDecorator) ICellPainter(org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) BeveledBorderDecorator(org.eclipse.nebula.widgets.nattable.painter.cell.decorator.BeveledBorderDecorator) ToggleCheckBoxColumnAction(org.eclipse.nebula.widgets.nattable.edit.action.ToggleCheckBoxColumnAction) DefaultNatTableStyleConfiguration(org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) ColumnHeaderCheckBoxPainter(org.eclipse.nebula.widgets.nattable.painter.cell.ColumnHeaderCheckBoxPainter) ColumnLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.ColumnLabelAccumulator) ColumnOverrideLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.ColumnOverrideLabelAccumulator) DefaultGridLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultGridLayer) UiBindingRegistry(org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry)

Example 4 with UiBindingRegistry

use of org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry in project nebula.widgets.nattable by eclipse.

the class CSSExample method postConstruct.

@PostConstruct
public void postConstruct(Composite parent, Shell shell) {
    parent.setLayout(new GridLayout());
    // property names of the Person class
    // property names of the Person class
    String[] propertyNames = { "firstName", "lastName", "password", "description", "age", "money", "married", "gender", "address.street", "address.city", "favouriteFood", "favouriteDrinks" };
    // 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("password", "Password");
    propertyToLabelMap.put("description", "Description");
    propertyToLabelMap.put("age", "Age");
    propertyToLabelMap.put("money", "Money");
    propertyToLabelMap.put("married", "Married");
    propertyToLabelMap.put("gender", "Gender");
    propertyToLabelMap.put("address.street", "Street");
    propertyToLabelMap.put("address.city", "City");
    propertyToLabelMap.put("favouriteFood", "Food");
    propertyToLabelMap.put("favouriteDrinks", "Drinks");
    IDataProvider bodyDataProvider = new ListDataProvider<>(PersonService.getExtendedPersonsWithAddress(10), new ExtendedReflectiveColumnPropertyAccessor<ExtendedPersonWithAddress>(propertyNames));
    DefaultGridLayer gridLayer = new DefaultGridLayer(bodyDataProvider, new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap));
    // unregister the default registered commandhandler to make the fill
    // handle work here for the age column
    gridLayer.unregisterCommandHandler(CopyDataToClipboardCommand.class);
    gridLayer.getBodyLayer().unregisterCommandHandler(CopyDataToClipboardCommand.class);
    final DataLayer bodyDataLayer = (DataLayer) gridLayer.getBodyDataLayer();
    AggregateConfigLabelAccumulator accumulator = new AggregateConfigLabelAccumulator();
    // create the ColumnLabelAccumulator with IDataProvider to be able to
    // tell the CSS engine about the added labels
    accumulator.add(new ColumnLabelAccumulator(bodyDataProvider));
    ColumnOverrideLabelAccumulator columnLabelAccumulator = new ColumnOverrideLabelAccumulator(bodyDataLayer);
    columnLabelAccumulator.registerColumnOverrides(5, CustomLineBorderDecorator.RIGHT_LINE_BORDER_LABEL);
    accumulator.add(columnLabelAccumulator);
    bodyDataLayer.setConfigLabelAccumulator(accumulator);
    NatTable natTable = new NatTable(parent, gridLayer, false);
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
    natTable.addConfiguration(new FillHandleConfiguration(gridLayer.getBodyLayer().getSelectionLayer()));
    gridLayer.addConfiguration(new DefaultEditConfiguration());
    gridLayer.addConfiguration(new DefaultEditBindings());
    natTable.addConfiguration(new AbstractRegistryConfiguration() {

        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, EditableRule.ALWAYS_EDITABLE, DisplayMode.NORMAL, "COLUMN_4");
            configRegistry.registerConfigAttribute(EditConfigAttributes.DATA_VALIDATOR, new DataValidator() {

                @Override
                public boolean validate(int columnIndex, int rowIndex, Object newValue) {
                    if (newValue instanceof Integer && ((Integer) newValue).intValue() > 100) {
                        return false;
                    }
                    return true;
                }
            }, DisplayMode.NORMAL, "COLUMN_4");
        }
    });
    natTable.setData(CSSSWTConstants.CSS_CLASS_NAME_KEY, "basic");
    // application model menu configuration
    menuService.registerContextMenu(natTable, "org.eclipse.nebula.widgets.nattable.examples.e4.popupmenu.0");
    // get the menu registered by EMenuService
    final Menu e4Menu = natTable.getMenu();
    // remove the menu reference from NatTable instance
    natTable.setMenu(null);
    natTable.addConfiguration(new AbstractUiBindingConfiguration() {

        @Override
        public void configureUiBindings(UiBindingRegistry uiBindingRegistry) {
            // add NatTable menu items
            // and register the DisposeListener
            new PopupMenuBuilder(natTable, e4Menu).withInspectLabelsMenuItem().build();
            // register the UI binding
            uiBindingRegistry.registerMouseDownBinding(new MouseEventMatcher(SWT.NONE, GridRegion.BODY, MouseEventMatcher.RIGHT_BUTTON), new PopupMenuAction(e4Menu));
        }
    });
    natTable.configure();
    GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
    // add a custom painter for key errortext
    int[] yErrorOffsets = { 0, 1, 2, 1 };
    CellPainterFactory.getInstance().registerContentPainter("errortext", (properties, underlying) -> {
        return new TextPainter(true, true, false) {

            @Override
            protected void paintDecoration(IStyle cellStyle, GC gc, int x, int y, int length, int fontHeight) {
                int underlineY = y + fontHeight - (gc.getFontMetrics().getDescent() / 2);
                Color previousColor = gc.getForeground();
                gc.setForeground(GUIHelper.COLOR_RED);
                int startX = x;
                underlineY--;
                int index = 0;
                while (startX <= (x + length)) {
                    gc.drawPoint(startX, underlineY + yErrorOffsets[(index % 4)]);
                    index++;
                    startX++;
                }
                gc.setForeground(previousColor);
            }
        };
    });
    showSourceLinks(parent, getClass().getName());
}
Also used : ListDataProvider(org.eclipse.nebula.widgets.nattable.data.ListDataProvider) ExtendedPersonWithAddress(org.eclipse.nebula.widgets.nattable.dataset.person.ExtendedPersonWithAddress) HashMap(java.util.HashMap) AbstractRegistryConfiguration(org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration) IDataProvider(org.eclipse.nebula.widgets.nattable.data.IDataProvider) AggregateConfigLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.AggregateConfigLabelAccumulator) FillHandleConfiguration(org.eclipse.nebula.widgets.nattable.fillhandle.config.FillHandleConfiguration) DefaultEditBindings(org.eclipse.nebula.widgets.nattable.edit.config.DefaultEditBindings) GridLayout(org.eclipse.swt.layout.GridLayout) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) PopupMenuAction(org.eclipse.nebula.widgets.nattable.ui.menu.PopupMenuAction) DefaultEditConfiguration(org.eclipse.nebula.widgets.nattable.edit.config.DefaultEditConfiguration) DefaultNatTableStyleConfiguration(org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) Menu(org.eclipse.swt.widgets.Menu) ColumnOverrideLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.ColumnOverrideLabelAccumulator) GC(org.eclipse.swt.graphics.GC) UiBindingRegistry(org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry) MouseEventMatcher(org.eclipse.nebula.widgets.nattable.ui.matcher.MouseEventMatcher) Color(org.eclipse.swt.graphics.Color) TextPainter(org.eclipse.nebula.widgets.nattable.painter.cell.TextPainter) AbstractUiBindingConfiguration(org.eclipse.nebula.widgets.nattable.config.AbstractUiBindingConfiguration) IStyle(org.eclipse.nebula.widgets.nattable.style.IStyle) DataValidator(org.eclipse.nebula.widgets.nattable.data.validate.DataValidator) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) DefaultColumnHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider) ColumnLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.ColumnLabelAccumulator) PopupMenuBuilder(org.eclipse.nebula.widgets.nattable.ui.menu.PopupMenuBuilder) DefaultGridLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultGridLayer) PostConstruct(javax.annotation.PostConstruct)

Example 5 with UiBindingRegistry

use of org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry in project nebula.widgets.nattable by eclipse.

the class _308_DataModificationExample method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    // set the GridLayout because the FillLayout seems to introduce a
    // scrollbar rendering issue on changing the content
    parent.setLayout(new GridLayout());
    // property names of the Person class
    String[] personPropertyNames = { "firstName", "lastName", "gender", "married", "birthday" };
    // mapping from property to label, needed for column header labels
    Map<String, String> personPropertyToLabelMap = new HashMap<>();
    personPropertyToLabelMap.put("firstName", "Firstname");
    personPropertyToLabelMap.put("lastName", "Lastname");
    personPropertyToLabelMap.put("gender", "Gender");
    personPropertyToLabelMap.put("married", "Married");
    personPropertyToLabelMap.put("birthday", "Birthday");
    ListDataProvider<Person> bodyDataProvider = new ListDataProvider<>(PersonService.getPersons(10), new ReflectiveColumnPropertyAccessor<Person>(personPropertyNames));
    IDataProvider personColumnHeaderDataProvider = new DefaultColumnHeaderDataProvider(personPropertyNames, personPropertyToLabelMap);
    IDataProvider personRowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
    // mapping from property to label, needed for column header labels
    Map<String, String> addressPropertyToLabelMap = new HashMap<>();
    addressPropertyToLabelMap.put("street", "Street");
    addressPropertyToLabelMap.put("housenumber", "Housenumber");
    addressPropertyToLabelMap.put("postalCode", "Postal Code");
    addressPropertyToLabelMap.put("city", "City");
    Composite panel = new Composite(parent, SWT.NONE);
    panel.setLayout(new GridLayout());
    GridDataFactory.fillDefaults().grab(true, true).applyTo(panel);
    Composite gridPanel = new Composite(panel, SWT.NONE);
    gridPanel.setLayout(new GridLayout());
    GridDataFactory.fillDefaults().grab(true, true).applyTo(gridPanel);
    Composite buttonPanel = new Composite(panel, SWT.NONE);
    buttonPanel.setLayout(new GridLayout());
    GridDataFactory.fillDefaults().grab(true, true).applyTo(buttonPanel);
    ConfigRegistry configRegistry = new ConfigRegistry();
    // create the body layer stack
    DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
    bodyDataLayer.setConfigLabelAccumulator(new IConfigLabelAccumulator() {

        @Override
        public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
            switch(columnPosition) {
                case 3:
                    configLabels.addLabel("MARRIED");
                    break;
                case 4:
                    configLabels.addLabel("DATE");
                    break;
            }
        }
    });
    DefaultBodyLayerStack bodyLayerStack = new DefaultBodyLayerStack(bodyDataLayer);
    bodyDataLayer.registerCommandHandler(new DeleteRowCommandHandler<>(bodyDataProvider.getList()));
    bodyDataLayer.registerCommandHandler(new InsertRowCommandHandler<>(bodyDataProvider.getList()));
    // create the column header layer stack
    DataLayer columnHeaderDataLayer = new DataLayer(personColumnHeaderDataProvider);
    ILayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayerStack.getViewportLayer(), bodyLayerStack.getSelectionLayer());
    // create the row header layer stack
    DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(personRowHeaderDataProvider);
    ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, bodyLayerStack.getViewportLayer(), bodyLayerStack.getSelectionLayer());
    // create the corner layer stack
    ILayer cornerLayer = new CornerLayer(new DataLayer(new DefaultCornerDataProvider(personColumnHeaderDataProvider, personRowHeaderDataProvider)), rowHeaderLayer, columnHeaderLayer);
    // create the grid layer composed with the prior created layer stacks
    GridLayer gridLayer = new GridLayer(bodyLayerStack, columnHeaderLayer, rowHeaderLayer, cornerLayer);
    final NatTable natTable = new NatTable(gridPanel, gridLayer, false);
    natTable.setConfigRegistry(configRegistry);
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
    natTable.addConfiguration(new HeaderMenuConfiguration(natTable));
    natTable.addConfiguration(new SingleClickSortConfiguration());
    natTable.addConfiguration(new AbstractRegistryConfiguration() {

        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new CheckBoxPainter(), DisplayMode.NORMAL, "MARRIED");
            configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultDateDisplayConverter("yyyy-MM-dd"), DisplayMode.NORMAL, "DATE");
        }
    });
    natTable.addConfiguration(new AbstractUiBindingConfiguration() {

        private final Menu bodyMenu = new PopupMenuBuilder(natTable).withMenuItemProvider(new IMenuItemProvider() {

            @Override
            public void addMenuItem(NatTable natTable, Menu popupMenu) {
                MenuItem deleteRow = new MenuItem(popupMenu, SWT.PUSH);
                deleteRow.setText("Insert below");
                deleteRow.setEnabled(true);
                deleteRow.addSelectionListener(new SelectionAdapter() {

                    @Override
                    public void widgetSelected(SelectionEvent event) {
                        int rowPosition = MenuItemProviders.getNatEventData(event).getRowPosition();
                        natTable.doCommand(new InsertRowCommand<>(bodyDataLayer, rowPosition, PersonService.getPersons(1).get(0)));
                    }
                });
            }
        }).withMenuItemProvider(new IMenuItemProvider() {

            @Override
            public void addMenuItem(NatTable natTable, Menu popupMenu) {
                MenuItem deleteRow = new MenuItem(popupMenu, SWT.PUSH);
                deleteRow.setText("Delete");
                deleteRow.setEnabled(true);
                deleteRow.addSelectionListener(new SelectionAdapter() {

                    @Override
                    public void widgetSelected(SelectionEvent event) {
                        int rowPosition = MenuItemProviders.getNatEventData(event).getRowPosition();
                        natTable.doCommand(new DeleteRowCommand(natTable, rowPosition));
                    }
                });
            }
        }).build();

        @Override
        public void configureUiBindings(UiBindingRegistry uiBindingRegistry) {
            uiBindingRegistry.registerMouseDownBinding(new MouseEventMatcher(SWT.NONE, GridRegion.BODY, MouseEventMatcher.RIGHT_BUTTON), new PopupMenuAction(this.bodyMenu) {

                @Override
                public void run(NatTable natTable, MouseEvent event) {
                    int columnPosition = natTable.getColumnPositionByX(event.x);
                    int rowPosition = natTable.getRowPositionByY(event.y);
                    if (!bodyLayerStack.getSelectionLayer().isRowPositionFullySelected(rowPosition)) {
                        natTable.doCommand(new SelectRowsCommand(natTable, columnPosition, rowPosition, false, false));
                    }
                    super.run(natTable, event);
                }
            });
        }
    });
    natTable.configure();
    GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
    return panel;
}
Also used : HashMap(java.util.HashMap) AbstractRegistryConfiguration(org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration) HeaderMenuConfiguration(org.eclipse.nebula.widgets.nattable.ui.menu.HeaderMenuConfiguration) DefaultCornerDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider) ConfigRegistry(org.eclipse.nebula.widgets.nattable.config.ConfigRegistry) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) DefaultNatTableStyleConfiguration(org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration) SelectionEvent(org.eclipse.swt.events.SelectionEvent) UiBindingRegistry(org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry) MouseEventMatcher(org.eclipse.nebula.widgets.nattable.ui.matcher.MouseEventMatcher) DefaultRowHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider) AbstractUiBindingConfiguration(org.eclipse.nebula.widgets.nattable.config.AbstractUiBindingConfiguration) GridLayer(org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer) Person(org.eclipse.nebula.widgets.nattable.dataset.person.Person) SelectRowsCommand(org.eclipse.nebula.widgets.nattable.selection.command.SelectRowsCommand) ListDataProvider(org.eclipse.nebula.widgets.nattable.data.ListDataProvider) LabelStack(org.eclipse.nebula.widgets.nattable.layer.LabelStack) ColumnHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer) IConfigLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.IConfigLabelAccumulator) DefaultDateDisplayConverter(org.eclipse.nebula.widgets.nattable.data.convert.DefaultDateDisplayConverter) IDataProvider(org.eclipse.nebula.widgets.nattable.data.IDataProvider) GridLayout(org.eclipse.swt.layout.GridLayout) PopupMenuAction(org.eclipse.nebula.widgets.nattable.ui.menu.PopupMenuAction) CheckBoxPainter(org.eclipse.nebula.widgets.nattable.painter.cell.CheckBoxPainter) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) Menu(org.eclipse.swt.widgets.Menu) MouseEvent(org.eclipse.swt.events.MouseEvent) Composite(org.eclipse.swt.widgets.Composite) ILayer(org.eclipse.nebula.widgets.nattable.layer.ILayer) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) MenuItem(org.eclipse.swt.widgets.MenuItem) RowHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer) CornerLayer(org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer) SingleClickSortConfiguration(org.eclipse.nebula.widgets.nattable.sort.config.SingleClickSortConfiguration) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) IMenuItemProvider(org.eclipse.nebula.widgets.nattable.ui.menu.IMenuItemProvider) DefaultColumnHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider) PopupMenuBuilder(org.eclipse.nebula.widgets.nattable.ui.menu.PopupMenuBuilder) DefaultBodyLayerStack(org.eclipse.nebula.widgets.nattable.layer.stack.DefaultBodyLayerStack)

Aggregations

NatTable (org.eclipse.nebula.widgets.nattable.NatTable)9 DefaultNatTableStyleConfiguration (org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration)9 DataLayer (org.eclipse.nebula.widgets.nattable.layer.DataLayer)9 UiBindingRegistry (org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry)9 IConfigRegistry (org.eclipse.nebula.widgets.nattable.config.IConfigRegistry)8 IDataProvider (org.eclipse.nebula.widgets.nattable.data.IDataProvider)8 DefaultColumnHeaderDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider)8 AbstractRegistryConfiguration (org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration)7 DefaultCornerDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider)7 ColumnHeaderLayer (org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer)7 CornerLayer (org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer)7 GridLayer (org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer)7 RowHeaderLayer (org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer)7 HashMap (java.util.HashMap)6 AbstractUiBindingConfiguration (org.eclipse.nebula.widgets.nattable.config.AbstractUiBindingConfiguration)6 DefaultRowHeaderDataLayer (org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer)6 ILayer (org.eclipse.nebula.widgets.nattable.layer.ILayer)6 ListDataProvider (org.eclipse.nebula.widgets.nattable.data.ListDataProvider)5 DefaultRowHeaderDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider)5 DefaultColumnHeaderDataLayer (org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer)5