Search in sources :

Example 1 with CustomLineBorderDecorator

use of org.eclipse.nebula.widgets.nattable.painter.cell.decorator.CustomLineBorderDecorator in project nebula.widgets.nattable by eclipse.

the class _001_Custom_styling_of_specific_cells method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    List<SimplePerson> myList = new ArrayList<>();
    for (int i = 1; i <= 100; i++) {
        myList.add(new SimplePerson(i, "Joe" + i, new Date()));
    }
    String[] propertyNames = { "id", "name", "birthDate" };
    IColumnPropertyAccessor<SimplePerson> columnPropertyAccessor = new ReflectiveColumnPropertyAccessor<>(propertyNames);
    ListDataProvider<SimplePerson> listDataProvider = new ListDataProvider<>(myList, columnPropertyAccessor);
    DefaultGridLayer gridLayer = new DefaultGridLayer(listDataProvider, new DummyColumnHeaderDataProvider(listDataProvider));
    final DefaultBodyLayerStack bodyLayer = gridLayer.getBodyLayer();
    // Custom label "FOO" for cell at column, row index (1, 5)
    IConfigLabelAccumulator cellLabelAccumulator = new IConfigLabelAccumulator() {

        @Override
        public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
            int columnIndex = bodyLayer.getColumnIndexByPosition(columnPosition);
            int rowIndex = bodyLayer.getRowIndexByPosition(rowPosition);
            if (columnIndex == 1 && rowIndex == 5) {
                configLabels.addLabel(FOO_LABEL);
            }
            if (columnIndex == 1 && rowIndex == 10) {
                configLabels.addLabel(BAR_LABEL);
            }
            // add labels for surrounding borders
            if (rowIndex == 13) {
                configLabels.addLabel(CustomLineBorderDecorator.TOP_LINE_BORDER_LABEL);
                configLabels.addLabel(CustomLineBorderDecorator.BOTTOM_LINE_BORDER_LABEL);
                if (columnIndex == 0) {
                    configLabels.addLabel(CustomLineBorderDecorator.LEFT_LINE_BORDER_LABEL);
                }
                if (columnIndex == 2) {
                    configLabels.addLabel(CustomLineBorderDecorator.RIGHT_LINE_BORDER_LABEL);
                }
            }
        }
    };
    bodyLayer.setConfigLabelAccumulator(cellLabelAccumulator);
    NatTable natTable = new NatTable(parent, gridLayer, false);
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration() {

        {
            // override the LineBorderDecorator here to show how to paint
            // borders on single sides of a cell
            this.cellPainter = new CustomLineBorderDecorator(new TextPainter());
            // set a border style
            this.borderStyle = new BorderStyle(2, GUIHelper.COLOR_BLUE, LineStyleEnum.DASHDOT);
        }
    });
    // Custom style for label "FOO"
    natTable.addConfiguration(new AbstractRegistryConfiguration() {

        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            Style cellStyle = new Style();
            cellStyle.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR, GUIHelper.COLOR_GREEN);
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, cellStyle, DisplayMode.NORMAL, FOO_LABEL);
            cellStyle = new Style();
            cellStyle.setAttributeValue(CellStyleAttributes.TEXT_DECORATION, TextDecorationEnum.UNDERLINE_STRIKETHROUGH);
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, cellStyle, DisplayMode.NORMAL, BAR_LABEL);
        }
    });
    natTable.configure();
    return natTable;
}
Also used : ListDataProvider(org.eclipse.nebula.widgets.nattable.data.ListDataProvider) LabelStack(org.eclipse.nebula.widgets.nattable.layer.LabelStack) AbstractRegistryConfiguration(org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration) ArrayList(java.util.ArrayList) IConfigLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.IConfigLabelAccumulator) DefaultNatTableStyleConfiguration(org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration) Style(org.eclipse.nebula.widgets.nattable.style.Style) BorderStyle(org.eclipse.nebula.widgets.nattable.style.BorderStyle) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) BorderStyle(org.eclipse.nebula.widgets.nattable.style.BorderStyle) TextPainter(org.eclipse.nebula.widgets.nattable.painter.cell.TextPainter) Date(java.util.Date) DummyColumnHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DummyColumnHeaderDataProvider) ReflectiveColumnPropertyAccessor(org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor) CustomLineBorderDecorator(org.eclipse.nebula.widgets.nattable.painter.cell.decorator.CustomLineBorderDecorator) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) DefaultGridLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultGridLayer) DefaultBodyLayerStack(org.eclipse.nebula.widgets.nattable.layer.stack.DefaultBodyLayerStack) SimplePerson(org.eclipse.nebula.widgets.nattable.dataset.person.SimplePerson)

Aggregations

ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)1 AbstractRegistryConfiguration (org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration)1 DefaultNatTableStyleConfiguration (org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration)1 IConfigRegistry (org.eclipse.nebula.widgets.nattable.config.IConfigRegistry)1 ListDataProvider (org.eclipse.nebula.widgets.nattable.data.ListDataProvider)1 ReflectiveColumnPropertyAccessor (org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor)1 SimplePerson (org.eclipse.nebula.widgets.nattable.dataset.person.SimplePerson)1 DummyColumnHeaderDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DummyColumnHeaderDataProvider)1 DefaultGridLayer (org.eclipse.nebula.widgets.nattable.grid.layer.DefaultGridLayer)1 LabelStack (org.eclipse.nebula.widgets.nattable.layer.LabelStack)1 IConfigLabelAccumulator (org.eclipse.nebula.widgets.nattable.layer.cell.IConfigLabelAccumulator)1 DefaultBodyLayerStack (org.eclipse.nebula.widgets.nattable.layer.stack.DefaultBodyLayerStack)1 TextPainter (org.eclipse.nebula.widgets.nattable.painter.cell.TextPainter)1 CustomLineBorderDecorator (org.eclipse.nebula.widgets.nattable.painter.cell.decorator.CustomLineBorderDecorator)1 BorderStyle (org.eclipse.nebula.widgets.nattable.style.BorderStyle)1 Style (org.eclipse.nebula.widgets.nattable.style.Style)1