Search in sources :

Example 6 with RowSelectionEvent

use of org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent in project nebula.widgets.nattable by eclipse.

the class RowSelectionModel method handleLayerEvent.

@Override
public void handleLayerEvent(IStructuralChangeEvent event) {
    // handling for deleting rows
    if (event.isVerticalStructureChanged()) {
        // the change is already done and we don't know about indexes, so we
        // need to check if the selected objects still exist
        Collection<Serializable> keysToRemove = new ArrayList<Serializable>();
        for (Map.Entry<Serializable, R> entry : this.selectedRows.entrySet()) {
            int rowIndex = this.rowDataProvider.indexOfRowObject(entry.getValue());
            if (rowIndex == -1) {
                keysToRemove.add(entry.getKey());
            }
        }
        this.selectionsLock.readLock().lock();
        try {
            for (Serializable toRemove : keysToRemove) {
                this.selectedRows.remove(toRemove);
            }
        } finally {
            this.selectionsLock.readLock().unlock();
        }
        // change for all deleted rows
        if (!keysToRemove.isEmpty()) {
            Collection<Integer> rowPositions = new HashSet<Integer>();
            Collection<StructuralDiff> diffs = event.getRowDiffs();
            if (diffs != null) {
                for (StructuralDiff rowDiff : diffs) {
                    if (rowDiff.getDiffType() != null && rowDiff.getDiffType().equals(DiffTypeEnum.DELETE)) {
                        Range beforePositionRange = rowDiff.getBeforePositionRange();
                        for (int i = beforePositionRange.start; i < beforePositionRange.end; i++) {
                            rowPositions.add(i);
                        }
                    }
                }
            }
            // if there is no diff in the event we assume everything has
            // changed, in such a case we are not able to fire an
            // appropriate event the layer stack upwards since it will be
            // stopped while converting it to the target layer
            // for the RowSelectionProvider this is sufficient because it
            // registers itself as a listener to the SelectionLayer and
            // therefore gets informed about the selection change
            this.selectionLayer.fireLayerEvent(new RowSelectionEvent(this.selectionLayer, rowPositions, -1, false, false));
        }
    }
}
Also used : Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) RowSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent) StructuralDiff(org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 7 with RowSelectionEvent

use of org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent in project nebula.widgets.nattable by eclipse.

the class _5053_SelectionEventsExample 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
    final IRowDataProvider<Person> bodyDataProvider = new ListDataProvider<>(data, columnPropertyAccessor);
    final DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
    final SelectionLayer selectionLayer = new SelectionLayer(bodyDataLayer);
    ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
    // 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);
    final NatTable natTable = new NatTable(parent, gridLayer);
    // Events are fired whenever selection occurs. These can be use to
    // trigger external actions as required.
    // 
    // This adds a custom ILayerListener that will listen and handle
    // selection events on NatTable level
    natTable.addLayerListener(new ILayerListener() {

        // Default selection behavior selects cells by default.
        @Override
        public void handleLayerEvent(ILayerEvent event) {
            if (event instanceof CellSelectionEvent) {
                CellSelectionEvent cellEvent = (CellSelectionEvent) event;
                log("Selected cell: [" + cellEvent.getRowPosition() + ", " + cellEvent.getColumnPosition() + "], " + natTable.getDataValueByPosition(cellEvent.getColumnPosition(), cellEvent.getRowPosition()));
            } else if (event instanceof ColumnSelectionEvent) {
                ColumnSelectionEvent columnEvent = (ColumnSelectionEvent) event;
                log("Selected Column: " + columnEvent.getColumnPositionRanges());
            } else if (event instanceof RowSelectionEvent) {
                // directly ask the SelectionLayer about the selected rows
                // and access the data via IRowDataProvider
                Collection<Range> selections = selectionLayer.getSelectedRowPositions();
                StringBuilder builder = new StringBuilder("Selected Persons: ").append(selectionLayer.getSelectedRowPositions()).append("[");
                for (Range r : selections) {
                    for (int i = r.start; i < r.end; i++) {
                        Person p = bodyDataProvider.getRowObject(i);
                        if (p != null) {
                            if (!builder.toString().endsWith("[")) {
                                builder.append(", ");
                            }
                            builder.append(p.getFirstName()).append(" ").append(p.getLastName());
                        }
                    }
                }
                builder.append("]");
                log(builder.toString());
            }
        }
    });
    // Layout widgets
    parent.setLayout(new GridLayout(1, true));
    natTable.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
    // add a log area to the example to show the log entries
    setupTextArea(parent);
    return natTable;
}
Also used : ListDataProvider(org.eclipse.nebula.widgets.nattable.data.ListDataProvider) HashMap(java.util.HashMap) ColumnHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer) ILayerListener(org.eclipse.nebula.widgets.nattable.layer.ILayerListener) 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) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) GridLayout(org.eclipse.swt.layout.GridLayout) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) ILayer(org.eclipse.nebula.widgets.nattable.layer.ILayer) ColumnSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.ColumnSelectionEvent) DefaultRowHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) ReflectiveColumnPropertyAccessor(org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor) ILayerEvent(org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent) RowHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer) CornerLayer(org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer) CellSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.CellSelectionEvent) SelectionLayer(org.eclipse.nebula.widgets.nattable.selection.SelectionLayer) RowSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent) GridData(org.eclipse.swt.layout.GridData) Collection(java.util.Collection) 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)

Example 8 with RowSelectionEvent

use of org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent in project nebula.widgets.nattable by eclipse.

the class ColumnGroupHeaderLayerSelectionTest method shouldSelectAllCellsInGroupWithCtrl.

@Test
public void shouldSelectAllCellsInGroupWithCtrl() {
    this.gridLayer.doCommand(new ViewportSelectColumnGroupCommand(this.gridLayer, 2, 0, false, false));
    assertEquals(1, this.layerListener.getEventsCount());
    assertTrue(this.layerListener.containsInstanceOf(RowSelectionEvent.class));
    RowSelectionEvent event = (RowSelectionEvent) this.layerListener.getReceivedEvent(RowSelectionEvent.class);
    Collection<Range> rowPositionRanges = event.getRowPositionRanges();
    assertEquals(1, rowPositionRanges.size());
    assertEquals(new Range(0, this.gridLayer.getBodyLayer().getSelectionLayer().getRowCount()), rowPositionRanges.iterator().next());
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(0));
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(1));
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(2));
    this.gridLayer.doCommand(new ViewportSelectColumnGroupCommand(this.gridLayer, 6, 0, false, true));
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(0));
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(1));
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(2));
    assertFalse(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(3));
    assertFalse(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(4));
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(5));
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(6));
}
Also used : ViewportSelectColumnGroupCommand(org.eclipse.nebula.widgets.nattable.group.command.ViewportSelectColumnGroupCommand) RowSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) Test(org.junit.Test)

Example 9 with RowSelectionEvent

use of org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent in project nebula.widgets.nattable by eclipse.

the class ColumnGroupHeaderLayerSelectionTest method shouldSelectAllCellsInGroupsToRightWithShift.

@Test
public void shouldSelectAllCellsInGroupsToRightWithShift() {
    this.gridLayer.doCommand(new ViewportSelectColumnGroupCommand(this.gridLayer, 2, 0, false, false));
    assertEquals(1, this.layerListener.getEventsCount());
    assertTrue(this.layerListener.containsInstanceOf(RowSelectionEvent.class));
    RowSelectionEvent event = (RowSelectionEvent) this.layerListener.getReceivedEvent(RowSelectionEvent.class);
    Collection<Range> rowPositionRanges = event.getRowPositionRanges();
    assertEquals(1, rowPositionRanges.size());
    assertEquals(new Range(0, this.gridLayer.getBodyLayer().getSelectionLayer().getRowCount()), rowPositionRanges.iterator().next());
    this.layerListener.clearReceivedEvents();
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(0));
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(1));
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(2));
    assertFalse(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(3));
    assertFalse(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(4));
    assertFalse(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(5));
    assertFalse(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(6));
    this.gridLayer.doCommand(new ViewportSelectColumnGroupCommand(this.gridLayer, 6, 0, true, false));
    assertEquals(1, this.layerListener.getEventsCount());
    assertTrue(this.layerListener.containsInstanceOf(RowSelectionEvent.class));
    event = (RowSelectionEvent) this.layerListener.getReceivedEvent(RowSelectionEvent.class);
    rowPositionRanges = event.getRowPositionRanges();
    assertEquals(1, rowPositionRanges.size());
    assertEquals(new Range(0, this.gridLayer.getBodyLayer().getSelectionLayer().getRowCount()), rowPositionRanges.iterator().next());
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(0));
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(1));
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(2));
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(3));
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(4));
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(5));
    assertTrue(this.gridLayer.getBodyLayer().getSelectionLayer().isColumnPositionFullySelected(6));
}
Also used : ViewportSelectColumnGroupCommand(org.eclipse.nebula.widgets.nattable.group.command.ViewportSelectColumnGroupCommand) RowSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) Test(org.junit.Test)

Example 10 with RowSelectionEvent

use of org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent in project nebula.widgets.nattable by eclipse.

the class SelectionEventsTest method shouldFireRowSelectionEvent.

@Test
public void shouldFireRowSelectionEvent() throws Exception {
    // Select single row
    this.nattable.doCommand(new SelectRowsCommand(this.nattable, 5, 5, NO_SHIFT, NO_CTRL));
    assertEquals(1, this.listener.getEventsCount());
    assertTrue(this.listener.containsInstanceOf(RowSelectionEvent.class));
    RowSelectionEvent event = (RowSelectionEvent) this.listener.getReceivedEvents().get(0);
    assertEquals(5, event.getRowPositionRanges().iterator().next().start);
    assertEquals(6, event.getRowPositionRanges().iterator().next().end);
    // Select additional rows with shift
    this.nattable.doCommand(new SelectRowsCommand(this.nattable, 5, 7, WITH_SHIFT, NO_CTRL));
    assertEquals(2, this.listener.getEventsCount());
    assertTrue(this.listener.containsInstanceOf(RowSelectionEvent.class));
    event = (RowSelectionEvent) this.listener.getReceivedEvents().get(1);
    assertEquals(1, event.getRowPositionRanges().size());
    assertEquals(5, event.getRowPositionRanges().iterator().next().start);
    assertEquals(8, event.getRowPositionRanges().iterator().next().end);
}
Also used : RowSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent) SelectRowsCommand(org.eclipse.nebula.widgets.nattable.selection.command.SelectRowsCommand) Test(org.junit.Test)

Aggregations

RowSelectionEvent (org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent)15 Range (org.eclipse.nebula.widgets.nattable.coordinate.Range)9 Test (org.junit.Test)7 SelectRowsCommand (org.eclipse.nebula.widgets.nattable.selection.command.SelectRowsCommand)5 HashSet (java.util.HashSet)3 ViewportSelectColumnGroupCommand (org.eclipse.nebula.widgets.nattable.group.command.ViewportSelectColumnGroupCommand)3 ILayerListener (org.eclipse.nebula.widgets.nattable.layer.ILayerListener)3 ILayerEvent (org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent)3 CellSelectionEvent (org.eclipse.nebula.widgets.nattable.selection.event.CellSelectionEvent)3 HashMap (java.util.HashMap)2 ColumnSelectionEvent (org.eclipse.nebula.widgets.nattable.selection.event.ColumnSelectionEvent)2 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Map (java.util.Map)1 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)1 IDataProvider (org.eclipse.nebula.widgets.nattable.data.IDataProvider)1 ListDataProvider (org.eclipse.nebula.widgets.nattable.data.ListDataProvider)1 ReflectiveColumnPropertyAccessor (org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor)1 Person (org.eclipse.nebula.widgets.nattable.dataset.person.Person)1