Search in sources :

Example 46 with Person

use of org.eclipse.nebula.widgets.nattable.dataset.person.Person in project nebula.widgets.nattable by eclipse.

the class _424_NebulaRichTextIntegrationExample method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    // set the directory to which the richtext resources should be unpacked
    System.setProperty(RichTextEditor.JAR_UNPACK_LOCATION_PROPERTY, System.getProperty("user.dir") + File.separator + RichTextEditor.class.getPackage().getName());
    String[] propertyNames = new String[] { "firstName", "lastName", "gender", "married", "description" };
    Map<String, String> propertyToLabelMap = new HashMap<>();
    propertyToLabelMap.put("firstName", "Firstname");
    propertyToLabelMap.put("lastName", "Lastname");
    propertyToLabelMap.put("gender", "Gender");
    propertyToLabelMap.put("married", "Married");
    propertyToLabelMap.put("description", "Description");
    IColumnAccessor<Person> columnAccessor = new ReflectiveColumnPropertyAccessor<>(propertyNames);
    List<Person> persons = PersonService.getPersons(10);
    IDataProvider bodyDataProvider = new ListDataProvider<>(persons, columnAccessor);
    DefaultColumnHeaderDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
    DefaultGridLayer gridLayer = new DefaultGridLayer(bodyDataProvider, columnHeaderDataProvider);
    ((AbstractLayer) gridLayer.getBodyDataLayer()).setConfigLabelAccumulator(new ColumnLabelAccumulator());
    NatTable natTable = new NatTable(parent, gridLayer, false);
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
    // add custom painter and editor configuration
    natTable.addConfiguration(new AbstractRegistryConfiguration() {

        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            // configure converter
            MarkupDisplayConverter markupConverter = new MarkupDisplayConverter();
            markupConverter.registerMarkup("Simpson", "<em>", "</em>");
            markupConverter.registerMarkup("Smithers", "<span style=\"background-color:rgb(255, 0, 0)\"><strong><s><u>", "</u></s></strong></span>");
            // register markup display converter for normal displaymode
            configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, markupConverter, DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 1);
            // register default display converter for editing, so there is
            // no markup in the editor
            configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultDisplayConverter(), DisplayMode.EDIT, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 1);
            configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultBooleanDisplayConverter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
            // configure cell painter
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new BackgroundPainter(new PaddingDecorator(new RichTextCellPainter(), 2, 5, 2, 5)), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 1);
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new CheckBoxPainter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new BackgroundPainter(new PaddingDecorator(new RichTextCellPainter(), 2, 5, 2, 5)), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 4);
            // configure editing
            configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, EditableRule.ALWAYS_EDITABLE);
            configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new CheckBoxCellEditor(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
            configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new RichTextCellEditor(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 4);
        }
    });
    natTable.configure();
    natTable.setTheme(new ModernNatTableThemeConfiguration());
    return natTable;
}
Also used : ListDataProvider(org.eclipse.nebula.widgets.nattable.data.ListDataProvider) RichTextCellEditor(org.eclipse.nebula.widgets.nattable.extension.nebula.richtext.RichTextCellEditor) AbstractLayer(org.eclipse.nebula.widgets.nattable.layer.AbstractLayer) HashMap(java.util.HashMap) AbstractRegistryConfiguration(org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration) CheckBoxCellEditor(org.eclipse.nebula.widgets.nattable.edit.editor.CheckBoxCellEditor) IDataProvider(org.eclipse.nebula.widgets.nattable.data.IDataProvider) DefaultDisplayConverter(org.eclipse.nebula.widgets.nattable.data.convert.DefaultDisplayConverter) DefaultNatTableStyleConfiguration(org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration) CheckBoxPainter(org.eclipse.nebula.widgets.nattable.painter.cell.CheckBoxPainter) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) RichTextEditor(org.eclipse.nebula.widgets.richtext.RichTextEditor) ModernNatTableThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.ModernNatTableThemeConfiguration) BackgroundPainter(org.eclipse.nebula.widgets.nattable.painter.cell.BackgroundPainter) DefaultBooleanDisplayConverter(org.eclipse.nebula.widgets.nattable.data.convert.DefaultBooleanDisplayConverter) MarkupDisplayConverter(org.eclipse.nebula.widgets.nattable.extension.nebula.richtext.MarkupDisplayConverter) PaddingDecorator(org.eclipse.nebula.widgets.nattable.painter.cell.decorator.PaddingDecorator) RichTextCellPainter(org.eclipse.nebula.widgets.nattable.extension.nebula.richtext.RichTextCellPainter) ReflectiveColumnPropertyAccessor(org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor) 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) Person(org.eclipse.nebula.widgets.nattable.dataset.person.Person) DefaultGridLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultGridLayer)

Example 47 with Person

use of org.eclipse.nebula.widgets.nattable.dataset.person.Person in project nebula.widgets.nattable by eclipse.

the class _5011_DataLayerExample method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    parent.setLayout(new GridLayout());
    // property names of the Person class
    String[] propertyNames = { "firstName", "lastName", "gender", "married", "birthday" };
    IColumnPropertyAccessor<Person> columnPropertyAccessor = new ReflectiveColumnPropertyAccessor<>(propertyNames);
    IDataProvider bodyDataProvider = new ListDataProvider<>(PersonService.getPersons(10), columnPropertyAccessor);
    final DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
    // use different style bits to avoid rendering of inactive scrollbars
    // for small table
    // Note: The enabling/disabling and showing of the scrollbars is handled
    // by the ViewportLayer.
    // Without the ViewportLayer the scrollbars will always be visible with
    // the default
    // style bits of NatTable.
    final NatTable natTable = new NatTable(parent, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.DOUBLE_BUFFERED, bodyDataLayer);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
    Button b1 = new Button(parent, SWT.PUSH);
    b1.setText("Toggle column width");
    b1.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            _5011_DataLayerExample.this.showDefaultColumnWidth = !_5011_DataLayerExample.this.showDefaultColumnWidth;
            if (_5011_DataLayerExample.this.showDefaultColumnWidth) {
                // reset to default
                bodyDataLayer.setColumnWidthByPosition(0, DataLayer.DEFAULT_COLUMN_WIDTH, false);
                bodyDataLayer.setColumnWidthByPosition(1, DataLayer.DEFAULT_COLUMN_WIDTH, false);
                bodyDataLayer.setColumnWidthByPosition(2, DataLayer.DEFAULT_COLUMN_WIDTH, false);
                bodyDataLayer.setColumnWidthByPosition(3, DataLayer.DEFAULT_COLUMN_WIDTH, false);
                // this one will trigger the refresh
                bodyDataLayer.setColumnWidthByPosition(4, DataLayer.DEFAULT_COLUMN_WIDTH, true);
            } else {
                bodyDataLayer.setColumnWidthByPosition(0, 70, false);
                bodyDataLayer.setColumnWidthByPosition(1, 70, false);
                bodyDataLayer.setColumnWidthByPosition(2, 50, false);
                bodyDataLayer.setColumnWidthByPosition(3, 30, false);
                // this one will trigger the refresh
                bodyDataLayer.setColumnWidthByPosition(4, 200, true);
            }
        }
    });
    Button b2 = new Button(parent, SWT.PUSH);
    b2.setText("Toggle row height");
    b2.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            _5011_DataLayerExample.this.showDefaultRowHeight = !_5011_DataLayerExample.this.showDefaultRowHeight;
            if (_5011_DataLayerExample.this.showDefaultRowHeight) {
                // reset to default
                bodyDataLayer.setDefaultRowHeight(DataLayer.DEFAULT_ROW_HEIGHT);
            } else {
                bodyDataLayer.setDefaultRowHeight(50);
            }
            // repaint the table, as setting the default height is not
            // triggering a refresh automatically
            // this is because setting the default usually should be done
            // prior rendering
            natTable.refresh(false);
        }
    });
    return natTable;
}
Also used : ListDataProvider(org.eclipse.nebula.widgets.nattable.data.ListDataProvider) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) IDataProvider(org.eclipse.nebula.widgets.nattable.data.IDataProvider) ReflectiveColumnPropertyAccessor(org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor) GridLayout(org.eclipse.swt.layout.GridLayout) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) Person(org.eclipse.nebula.widgets.nattable.dataset.person.Person)

Example 48 with Person

use of org.eclipse.nebula.widgets.nattable.dataset.person.Person in project nebula.widgets.nattable by eclipse.

the class _5052_RowSelectionExample 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
    IRowDataProvider<Person> bodyDataProvider = new ListDataProvider<>(data, columnPropertyAccessor);
    final DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
    // create a SelectionLayer without using the default configuration
    // this enables us to add the row selection configuration cleanly
    // afterwards
    final SelectionLayer selectionLayer = new SelectionLayer(bodyDataLayer, false);
    ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
    // use a RowSelectionModel that will perform row selections and is able
    // to identify a row via unique ID
    selectionLayer.setSelectionModel(new RowSelectionModel<>(selectionLayer, bodyDataProvider, new IRowIdAccessor<Person>() {

        @Override
        public Serializable getRowId(Person rowObject) {
            return rowObject.getId();
        }
    }));
    // register the DefaultRowSelectionLayerConfiguration that contains the
    // default styling and functionality bindings (search, tick update)
    // and different configurations for a move command handler that always
    // moves by a row and row only selection bindings
    selectionLayer.addConfiguration(new DefaultRowSelectionLayerConfiguration());
    // 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);
    return new NatTable(parent, gridLayer);
}
Also used : ListDataProvider(org.eclipse.nebula.widgets.nattable.data.ListDataProvider) HashMap(java.util.HashMap) ColumnHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer) 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) IRowIdAccessor(org.eclipse.nebula.widgets.nattable.data.IRowIdAccessor) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) ILayer(org.eclipse.nebula.widgets.nattable.layer.ILayer) DefaultRowHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider) ReflectiveColumnPropertyAccessor(org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor) 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) 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) DefaultRowSelectionLayerConfiguration(org.eclipse.nebula.widgets.nattable.selection.config.DefaultRowSelectionLayerConfiguration)

Example 49 with Person

use of org.eclipse.nebula.widgets.nattable.dataset.person.Person in project nebula.widgets.nattable by eclipse.

the class _5054_SelectionProviderExample method createExampleControl.

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

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

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

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

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

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

Example 50 with Person

use of org.eclipse.nebula.widgets.nattable.dataset.person.Person 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

Person (org.eclipse.nebula.widgets.nattable.dataset.person.Person)55 DataLayer (org.eclipse.nebula.widgets.nattable.layer.DataLayer)31 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)29 HashMap (java.util.HashMap)25 IDataProvider (org.eclipse.nebula.widgets.nattable.data.IDataProvider)25 DefaultColumnHeaderDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider)25 ColumnHeaderLayer (org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer)25 ILayer (org.eclipse.nebula.widgets.nattable.layer.ILayer)23 SelectionLayer (org.eclipse.nebula.widgets.nattable.selection.SelectionLayer)23 GridLayer (org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer)22 ListDataProvider (org.eclipse.nebula.widgets.nattable.data.ListDataProvider)21 RowHeaderLayer (org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer)21 ReflectiveColumnPropertyAccessor (org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor)19 DefaultCornerDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider)19 CornerLayer (org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer)19 ViewportLayer (org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer)19 DefaultNatTableStyleConfiguration (org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration)18 DefaultRowHeaderDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider)18 DefaultRowHeaderDataLayer (org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer)17 GridLayout (org.eclipse.swt.layout.GridLayout)16