Search in sources :

Example 31 with SelectRowsCommand

use of org.eclipse.nebula.widgets.nattable.selection.command.SelectRowsCommand in project nebula.widgets.nattable by eclipse.

the class RowSelectionTest method shouldExtendFromAnchorWithShiftKeyPressed.

@Test
public void shouldExtendFromAnchorWithShiftKeyPressed() {
    // start from a clear selection state
    this.selectionLayer.clear();
    this.selectionLayer.doCommand(new SelectRowsCommand(this.selectionLayer, 0, 4, true, false));
    // select rows to the top
    this.selectionLayer.doCommand(new SelectRowsCommand(this.selectionLayer, 0, 2, true, false));
    for (int row = 2; row <= 4; row++) {
        assertTrue("row " + row + " not fully selected", this.selectionLayer.isRowPositionFullySelected(row));
    }
    assertFalse("row 5 fully selected", this.selectionLayer.isRowPositionFullySelected(5));
    // select rows to the bottom
    this.selectionLayer.doCommand(new SelectRowsCommand(this.selectionLayer, 0, 6, true, false));
    assertFalse("row 2 fully selected", this.selectionLayer.isRowPositionFullySelected(2));
    assertFalse("row 3 fully selected", this.selectionLayer.isRowPositionFullySelected(3));
    for (int row = 4; row <= 6; row++) {
        assertTrue("row " + row + " not fully selected", this.selectionLayer.isRowPositionFullySelected(row));
    }
}
Also used : SelectRowsCommand(org.eclipse.nebula.widgets.nattable.selection.command.SelectRowsCommand) Test(org.junit.Test)

Example 32 with SelectRowsCommand

use of org.eclipse.nebula.widgets.nattable.selection.command.SelectRowsCommand in project nebula.widgets.nattable by eclipse.

the class RowSelectionTest method testMultiSelectionRestore.

@Test
public void testMultiSelectionRestore() {
    this.selectionLayer.clear();
    this.selectionLayer.doCommand(new SelectRowsCommand(this.selectionLayer, 1, 0, false, false));
    assertEquals(1, this.selectionLayer.getSelectedRowPositions().size());
    assertEquals(1, this.selectionLayer.getSelectedRowCount());
    this.selectionLayer.doCommand(new SelectRowsCommand(this.selectionLayer, 1, 2, true, false));
    assertEquals(1, this.selectionLayer.getSelectedRowPositions().size());
    assertEquals(3, this.selectionLayer.getSelectedRowCount());
    this.selectionLayer.doCommand(new SelectRowsCommand(this.selectionLayer, 1, 2, false, true));
    assertEquals(1, this.selectionLayer.getSelectedRowPositions().size());
    assertEquals(2, this.selectionLayer.getSelectedRowCount());
    this.selectionLayer.doCommand(new SelectRowsCommand(this.selectionLayer, 1, 1, false, true));
    assertEquals(1, this.selectionLayer.getSelectedRowPositions().size());
    assertEquals(1, this.selectionLayer.getSelectedRowCount());
    this.selectionLayer.doCommand(new SelectRowsCommand(this.selectionLayer, 1, 0, false, true));
    assertEquals(0, this.selectionLayer.getSelectedRowPositions().size());
    assertEquals(0, this.selectionLayer.getSelectedRowCount());
    this.selectionLayer.doCommand(new SelectRowsCommand(this.selectionLayer, 1, 0, false, true));
    assertEquals(1, this.selectionLayer.getSelectedRowPositions().size());
    assertEquals(1, this.selectionLayer.getSelectedRowCount());
}
Also used : SelectRowsCommand(org.eclipse.nebula.widgets.nattable.selection.command.SelectRowsCommand) Test(org.junit.Test)

Example 33 with SelectRowsCommand

use of org.eclipse.nebula.widgets.nattable.selection.command.SelectRowsCommand in project nebula.widgets.nattable by eclipse.

the class RowSelectionProvider method setSelection.

@SuppressWarnings("unchecked")
@Override
public void setSelection(ISelection selection) {
    if (this.selectionLayer != null && selection instanceof IStructuredSelection) {
        if (!this.addSelectionOnSet || selection.isEmpty()) {
            this.selectionLayer.clear(false);
        }
        if (!selection.isEmpty()) {
            List<T> rowObjects = ((IStructuredSelection) selection).toList();
            Set<Integer> rowPositions = new HashSet<Integer>();
            for (T rowObject : rowObjects) {
                int rowIndex = this.rowDataProvider.indexOfRowObject(rowObject);
                int rowPosition = this.selectionLayer.getRowPositionByIndex(rowIndex);
                rowPositions.add(Integer.valueOf(rowPosition));
            }
            int intValue = -1;
            if (!rowPositions.isEmpty()) {
                Integer max = Collections.max(rowPositions);
                intValue = max.intValue();
            }
            if (intValue >= 0) {
                this.selectionLayer.doCommand(new SelectRowsCommand(this.selectionLayer, 0, ObjectUtils.asIntArray(rowPositions), false, true, intValue));
            }
        } else {
            this.selectionLayer.fireCellSelectionEvent(this.selectionLayer.getLastSelectedCell().columnPosition, this.selectionLayer.getLastSelectedCell().rowPosition, false, false, false);
        }
    }
}
Also used : IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) HashSet(java.util.HashSet) SelectRowsCommand(org.eclipse.nebula.widgets.nattable.selection.command.SelectRowsCommand)

Example 34 with SelectRowsCommand

use of org.eclipse.nebula.widgets.nattable.selection.command.SelectRowsCommand in project nebula.widgets.nattable by eclipse.

the class SelectRowAction method run.

@Override
public void run(NatTable natTable, MouseEvent event) {
    super.run(natTable, event);
    natTable.doCommand(new SelectRowsCommand(natTable, getGridColumnPosition(), getGridRowPosition(), isWithShiftMask(), isWithControlMask()));
}
Also used : SelectRowsCommand(org.eclipse.nebula.widgets.nattable.selection.command.SelectRowsCommand)

Example 35 with SelectRowsCommand

use of org.eclipse.nebula.widgets.nattable.selection.command.SelectRowsCommand 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

SelectRowsCommand (org.eclipse.nebula.widgets.nattable.selection.command.SelectRowsCommand)35 Test (org.junit.Test)32 RowStructuralRefreshEvent (org.eclipse.nebula.widgets.nattable.layer.event.RowStructuralRefreshEvent)12 StructuralRefreshEvent (org.eclipse.nebula.widgets.nattable.layer.event.StructuralRefreshEvent)9 Range (org.eclipse.nebula.widgets.nattable.coordinate.Range)5 RowDataFixture (org.eclipse.nebula.widgets.nattable.dataset.fixture.data.RowDataFixture)5 RowDeleteEvent (org.eclipse.nebula.widgets.nattable.layer.event.RowDeleteEvent)5 RowSelectionEvent (org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent)5 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)4 ListDataProvider (org.eclipse.nebula.widgets.nattable.data.ListDataProvider)4 DataLayer (org.eclipse.nebula.widgets.nattable.layer.DataLayer)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 GridLayer (org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer)2 AbstractDpiConverter (org.eclipse.nebula.widgets.nattable.layer.AbstractDpiConverter)2 IDpiConverter (org.eclipse.nebula.widgets.nattable.layer.IDpiConverter)2 NatTableFixture (org.eclipse.nebula.widgets.nattable.test.fixture.NatTableFixture)2 GridLayerFixture (org.eclipse.nebula.widgets.nattable.test.fixture.layer.GridLayerFixture)2 EventList (ca.odell.glazedlists.EventList)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1