Search in sources :

Example 1 with ColumnSelectionEvent

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

the class ColumnHeaderSelectionListener method handleLayerEvent.

@Override
public void handleLayerEvent(ILayerEvent event) {
    if (event instanceof ColumnSelectionEvent) {
        ColumnSelectionEvent selectionEvent = (ColumnSelectionEvent) event;
        ColumnHeaderSelectionEvent colHeaderSelectionEvent = new ColumnHeaderSelectionEvent(this.columnHeaderLayer, selectionEvent.getColumnPositionRanges());
        this.columnHeaderLayer.fireLayerEvent(colHeaderSelectionEvent);
    }
}
Also used : ColumnSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.ColumnSelectionEvent) ColumnHeaderSelectionEvent(org.eclipse.nebula.widgets.nattable.grid.layer.event.ColumnHeaderSelectionEvent)

Example 2 with ColumnSelectionEvent

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

the class SelectColumnCommandHandler method selectColumn.

protected void selectColumn(int columnPosition, int rowPosition, boolean withShiftMask, boolean withControlMask) {
    if (noShiftOrControl(withShiftMask, withControlMask)) {
        this.selectionLayer.clear(false);
        this.selectionLayer.selectCell(columnPosition, 0, false, false);
        this.selectionLayer.selectRegion(columnPosition, 0, 1, Integer.MAX_VALUE);
        this.selectionLayer.moveSelectionAnchor(columnPosition, rowPosition);
    } else if (bothShiftAndControl(withShiftMask, withControlMask)) {
        selectColumnWithShiftKey(columnPosition);
    } else if (isShiftOnly(withShiftMask, withControlMask)) {
        selectColumnWithShiftKey(columnPosition);
    } else if (isControlOnly(withShiftMask, withControlMask)) {
        selectColumnWithCtrlKey(columnPosition, rowPosition);
    }
    // Set last selected column position to the recently clicked column
    this.selectionLayer.setLastSelectedCell(columnPosition, rowPosition);
    this.selectionLayer.fireLayerEvent(new ColumnSelectionEvent(this.selectionLayer, columnPosition, withShiftMask, withControlMask));
}
Also used : ColumnSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.ColumnSelectionEvent)

Example 3 with ColumnSelectionEvent

use of org.eclipse.nebula.widgets.nattable.selection.event.ColumnSelectionEvent 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 4 with ColumnSelectionEvent

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

the class SelectionEventsTest method shouldFireColumnSelectionEvent.

@Test
public void shouldFireColumnSelectionEvent() throws Exception {
    this.nattable.doCommand(new SelectColumnCommand(this.nattable, 5, 5, NO_SHIFT, NO_CTRL));
    assertEquals(2, this.listener.getEventsCount());
    assertTrue(this.listener.containsInstanceOf(ColumnSelectionEvent.class));
    assertTrue(this.listener.containsInstanceOf(ColumnHeaderSelectionEvent.class));
    ColumnSelectionEvent event = (ColumnSelectionEvent) this.listener.getReceivedEvents().get(0);
    assertEquals(5, event.getColumnPositionRanges().iterator().next().start);
    assertEquals(6, event.getColumnPositionRanges().iterator().next().end);
}
Also used : SelectColumnCommand(org.eclipse.nebula.widgets.nattable.selection.command.SelectColumnCommand) ColumnSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.ColumnSelectionEvent) ColumnHeaderSelectionEvent(org.eclipse.nebula.widgets.nattable.grid.layer.event.ColumnHeaderSelectionEvent) Test(org.junit.Test)

Example 5 with ColumnSelectionEvent

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

the class Selection_events method addCustomSelectionBehaviour.

private void addCustomSelectionBehaviour() {
    this.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() + "], " + Selection_events.this.nattable.getDataValueByPosition(cellEvent.getColumnPosition(), cellEvent.getRowPosition()));
            }
        }
    });
    // Events are fired whenever selection occurs. These can be use to
    // trigger
    // external actions as required. Also you can use this data to pull out
    // the backing data from the IRowDataProvider. Example:
    // rowDataProvider.getRowObject(natTable.getRowIndexByPosition(selectedRowPosition));
    this.nattable.addLayerListener(new ILayerListener() {

        @Override
        public void handleLayerEvent(ILayerEvent event) {
            if (event instanceof RowSelectionEvent) {
                RowSelectionEvent rowEvent = (RowSelectionEvent) event;
                log("Selected Row: " + ObjectUtils.toString(rowEvent.getRowPositionRanges()));
            }
        }
    });
    this.nattable.addLayerListener(new ILayerListener() {

        @Override
        public void handleLayerEvent(ILayerEvent event) {
            if (event instanceof ColumnSelectionEvent) {
                ColumnSelectionEvent columnEvent = (ColumnSelectionEvent) event;
                log("Selected Column: " + columnEvent.getColumnPositionRanges());
            }
        }
    });
}
Also used : ILayerEvent(org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent) CellSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.CellSelectionEvent) RowSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent) ColumnSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.ColumnSelectionEvent) ILayerListener(org.eclipse.nebula.widgets.nattable.layer.ILayerListener)

Aggregations

ColumnSelectionEvent (org.eclipse.nebula.widgets.nattable.selection.event.ColumnSelectionEvent)5 ColumnHeaderSelectionEvent (org.eclipse.nebula.widgets.nattable.grid.layer.event.ColumnHeaderSelectionEvent)2 ILayerListener (org.eclipse.nebula.widgets.nattable.layer.ILayerListener)2 ILayerEvent (org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent)2 CellSelectionEvent (org.eclipse.nebula.widgets.nattable.selection.event.CellSelectionEvent)2 RowSelectionEvent (org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)1 Range (org.eclipse.nebula.widgets.nattable.coordinate.Range)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 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