Search in sources :

Example 1 with ColumnWidthResetCommand

use of org.eclipse.nebula.widgets.nattable.resize.command.ColumnWidthResetCommand in project nebula.widgets.nattable by eclipse.

the class _307_ChangeDataProviderExample 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");
    this.personBodyDataProvider = new ListDataProvider<>(PersonService.getPersons(10), new ReflectiveColumnPropertyAccessor<Person>(personPropertyNames));
    this.personColumnHeaderDataProvider = new DefaultColumnHeaderDataProvider(personPropertyNames, personPropertyToLabelMap);
    this.personRowHeaderDataProvider = new DefaultRowHeaderDataProvider(this.personBodyDataProvider);
    // property names of the Address class
    String[] addressPropertyNames = { "street", "housenumber", "postalCode", "city" };
    // 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");
    this.addressBodyDataProvider = new ListDataProvider<>(PersonService.getAddress(20), new ReflectiveColumnPropertyAccessor<>(addressPropertyNames));
    this.addressColumnHeaderDataProvider = new DefaultColumnHeaderDataProvider(addressPropertyNames, addressPropertyToLabelMap);
    this.addressRowHeaderDataProvider = new DefaultRowHeaderDataProvider(this.addressBodyDataProvider);
    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(this.personBodyDataProvider);
    bodyDataLayer.setConfigLabelAccumulator(this.personAccumulator);
    DefaultBodyLayerStack bodyLayerStack = new DefaultBodyLayerStack(bodyDataLayer);
    // create the column header layer stack
    DataLayer columnHeaderDataLayer = new DataLayer(this.personColumnHeaderDataProvider);
    ILayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayerStack.getViewportLayer(), bodyLayerStack.getSelectionLayer());
    // create the row header layer stack
    DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(this.personRowHeaderDataProvider);
    ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, bodyLayerStack.getViewportLayer(), bodyLayerStack.getSelectionLayer());
    // create the corner layer stack
    ILayer cornerLayer = new CornerLayer(new DataLayer(new DefaultCornerDataProvider(this.personColumnHeaderDataProvider, this.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.configure();
    GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
    Button showPersonsButton = new Button(buttonPanel, SWT.PUSH);
    showPersonsButton.setText("Show Persons");
    showPersonsButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            bodyDataLayer.setDataProvider(_307_ChangeDataProviderExample.this.personBodyDataProvider);
            columnHeaderDataLayer.setDataProvider(_307_ChangeDataProviderExample.this.personColumnHeaderDataProvider);
            rowHeaderDataLayer.setDataProvider(_307_ChangeDataProviderExample.this.personRowHeaderDataProvider);
            bodyDataLayer.setConfigLabelAccumulator(_307_ChangeDataProviderExample.this.personAccumulator);
            natTable.doCommand(new RowHeightResetCommand(false));
            natTable.doCommand(new ColumnWidthResetCommand(false));
            natTable.refresh();
            natTable.getHorizontalBar().setVisible(false);
            natTable.getVerticalBar().setVisible(false);
        }
    });
    Button showAddressButton = new Button(buttonPanel, SWT.PUSH);
    showAddressButton.setText("Show Address");
    showAddressButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            bodyDataLayer.setDataProvider(_307_ChangeDataProviderExample.this.addressBodyDataProvider);
            columnHeaderDataLayer.setDataProvider(_307_ChangeDataProviderExample.this.addressColumnHeaderDataProvider);
            rowHeaderDataLayer.setDataProvider(_307_ChangeDataProviderExample.this.addressRowHeaderDataProvider);
            bodyDataLayer.setConfigLabelAccumulator(null);
            natTable.doCommand(new RowHeightResetCommand(false));
            natTable.doCommand(new ColumnWidthResetCommand(false));
            natTable.refresh();
        }
    });
    return panel;
}
Also used : HashMap(java.util.HashMap) ColumnHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer) AbstractRegistryConfiguration(org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration) HeaderMenuConfiguration(org.eclipse.nebula.widgets.nattable.ui.menu.HeaderMenuConfiguration) DefaultDateDisplayConverter(org.eclipse.nebula.widgets.nattable.data.convert.DefaultDateDisplayConverter) 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) GridLayout(org.eclipse.swt.layout.GridLayout) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) Button(org.eclipse.swt.widgets.Button) DefaultNatTableStyleConfiguration(org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration) CheckBoxPainter(org.eclipse.nebula.widgets.nattable.painter.cell.CheckBoxPainter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) RowHeightResetCommand(org.eclipse.nebula.widgets.nattable.resize.command.RowHeightResetCommand) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) Composite(org.eclipse.swt.widgets.Composite) ILayer(org.eclipse.nebula.widgets.nattable.layer.ILayer) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) 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) SingleClickSortConfiguration(org.eclipse.nebula.widgets.nattable.sort.config.SingleClickSortConfiguration) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) ColumnWidthResetCommand(org.eclipse.nebula.widgets.nattable.resize.command.ColumnWidthResetCommand) GridLayer(org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer) DefaultColumnHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider) DefaultBodyLayerStack(org.eclipse.nebula.widgets.nattable.layer.stack.DefaultBodyLayerStack)

Aggregations

HashMap (java.util.HashMap)1 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)1 AbstractRegistryConfiguration (org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration)1 ConfigRegistry (org.eclipse.nebula.widgets.nattable.config.ConfigRegistry)1 DefaultNatTableStyleConfiguration (org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration)1 IConfigRegistry (org.eclipse.nebula.widgets.nattable.config.IConfigRegistry)1 ReflectiveColumnPropertyAccessor (org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor)1 DefaultDateDisplayConverter (org.eclipse.nebula.widgets.nattable.data.convert.DefaultDateDisplayConverter)1 DefaultColumnHeaderDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider)1 DefaultCornerDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider)1 DefaultRowHeaderDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider)1 ColumnHeaderLayer (org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer)1 CornerLayer (org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer)1 DefaultRowHeaderDataLayer (org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer)1 GridLayer (org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer)1 RowHeaderLayer (org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer)1 DataLayer (org.eclipse.nebula.widgets.nattable.layer.DataLayer)1 ILayer (org.eclipse.nebula.widgets.nattable.layer.ILayer)1 DefaultBodyLayerStack (org.eclipse.nebula.widgets.nattable.layer.stack.DefaultBodyLayerStack)1 CheckBoxPainter (org.eclipse.nebula.widgets.nattable.painter.cell.CheckBoxPainter)1