Search in sources :

Example 6 with ILayerListener

use of org.eclipse.nebula.widgets.nattable.layer.ILayerListener in project nebula.widgets.nattable by eclipse.

the class TickUpdateCommandHandlerTest method shouldNotThrowExceptionOnNoSelection.

@Test
public void shouldNotThrowExceptionOnNoSelection() {
    // if no exception occurs this test succeeds
    // we also check that no update has been triggered
    this.selectionLayer.clear();
    this.selectionLayer.addLayerListener(new ILayerListener() {

        @Override
        public void handleLayerEvent(ILayerEvent event) {
            if (event instanceof CellVisualChangeEvent) {
                fail("update triggered");
            }
        }
    });
    this.commandHandler.doCommand(new TickUpdateCommand(this.testConfigRegistry, true));
}
Also used : ILayerEvent(org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent) ILayerListener(org.eclipse.nebula.widgets.nattable.layer.ILayerListener) CellVisualChangeEvent(org.eclipse.nebula.widgets.nattable.layer.event.CellVisualChangeEvent) Test(org.junit.Test)

Example 7 with ILayerListener

use of org.eclipse.nebula.widgets.nattable.layer.ILayerListener in project nebula.widgets.nattable by eclipse.

the class NatTable method handleLayerEvent.

// Events /////////////////////////////////////////////////////////////////
@Override
public void handleLayerEvent(ILayerEvent event) {
    List<ILayerListener> currentListeners;
    this.eventListenerLock.readLock().lock();
    try {
        currentListeners = this.listeners;
    } finally {
        this.eventListenerLock.readLock().unlock();
    }
    for (ILayerListener layerListener : currentListeners) {
        layerListener.handleLayerEvent(event);
    }
    if (event instanceof CellVisualUpdateEvent) {
        CellVisualUpdateEvent update = (CellVisualUpdateEvent) event;
        repaintCell(update.getColumnPosition(), update.getRowPosition());
        return;
    }
    if (event instanceof ColumnVisualUpdateEvent) {
        ColumnVisualUpdateEvent update = (ColumnVisualUpdateEvent) event;
        // if more than one column has changed repaint the whole table
        Collection<Range> ranges = update.getColumnPositionRanges();
        if (ranges.size() == 1) {
            Range range = ranges.iterator().next();
            if (range.end - range.start == 1) {
                repaintColumn(range.start);
                return;
            }
        }
    }
    if (event instanceof RowVisualUpdateEvent) {
        RowVisualUpdateEvent update = (RowVisualUpdateEvent) event;
        // if more than one row has changed repaint the whole table
        Collection<Range> ranges = update.getRowPositionRanges();
        if (ranges.size() == 1) {
            Range range = ranges.iterator().next();
            if (range.end - range.start == 1) {
                repaintRow(range.start);
                return;
            }
        }
    }
    if (event instanceof ISelectionEvent) {
        if (event instanceof CellSelectionEvent || event instanceof RowSelectionEvent) {
            Event e = new Event();
            e.widget = this;
            try {
                notifyListeners(SWT.Selection, e);
            } catch (RuntimeException re) {
                // $NON-NLS-1$
                log.error("Error on SWT selection processing", re);
            }
        }
        // in case of selections we redraw immediately
        // this is because with Bug 440037 it was reported that
        // NatTable is too lazy in handling selections which
        // was caused by the EventConflaterChain that only performs
        // updates every 100ms to avoid flickering when handling too
        // many refresh operations in a short period
        redraw();
    } else if (event instanceof IVisualChangeEvent) {
        this.conflaterChain.addEvent(event);
    }
    if (event instanceof CellEditorCreatedEvent) {
        CellEditorCreatedEvent editorEvent = (CellEditorCreatedEvent) event;
        this.activeCellEditor = editorEvent.getEditor();
        Control editorControl = this.activeCellEditor.getEditorControl();
        if (editorControl != null && !editorControl.isDisposed()) {
            editorControl.addDisposeListener(new DisposeListener() {

                @Override
                public void widgetDisposed(DisposeEvent e) {
                    NatTable.this.activeCellEditor = null;
                    ActiveCellEditorRegistry.unregisterActiveCellEditor();
                }
            });
        } else {
            this.activeCellEditor = null;
            ActiveCellEditorRegistry.unregisterActiveCellEditor();
        }
        ActiveCellEditorRegistry.registerActiveCellEditor(this.activeCellEditor);
    }
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) IVisualChangeEvent(org.eclipse.nebula.widgets.nattable.layer.event.IVisualChangeEvent) CellVisualUpdateEvent(org.eclipse.nebula.widgets.nattable.layer.event.CellVisualUpdateEvent) RowVisualUpdateEvent(org.eclipse.nebula.widgets.nattable.layer.event.RowVisualUpdateEvent) ILayerListener(org.eclipse.nebula.widgets.nattable.layer.ILayerListener) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) DisposeEvent(org.eclipse.swt.events.DisposeEvent) CellEditorCreatedEvent(org.eclipse.nebula.widgets.nattable.edit.CellEditorCreatedEvent) Control(org.eclipse.swt.widgets.Control) ColumnVisualUpdateEvent(org.eclipse.nebula.widgets.nattable.layer.event.ColumnVisualUpdateEvent) CellSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.CellSelectionEvent) RowSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent) DisposeEvent(org.eclipse.swt.events.DisposeEvent) FocusEvent(org.eclipse.swt.events.FocusEvent) PaintEvent(org.eclipse.swt.events.PaintEvent) CellVisualUpdateEvent(org.eclipse.nebula.widgets.nattable.layer.event.CellVisualUpdateEvent) CellEditorCreatedEvent(org.eclipse.nebula.widgets.nattable.edit.CellEditorCreatedEvent) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) CellSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.CellSelectionEvent) ColumnVisualUpdateEvent(org.eclipse.nebula.widgets.nattable.layer.event.ColumnVisualUpdateEvent) IVisualChangeEvent(org.eclipse.nebula.widgets.nattable.layer.event.IVisualChangeEvent) Event(org.eclipse.swt.widgets.Event) RowVisualUpdateEvent(org.eclipse.nebula.widgets.nattable.layer.event.RowVisualUpdateEvent) RowSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent) ISelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.ISelectionEvent) ILayerEvent(org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent) ISelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.ISelectionEvent)

Example 8 with ILayerListener

use of org.eclipse.nebula.widgets.nattable.layer.ILayerListener in project nebula.widgets.nattable by eclipse.

the class SearchGridCellsCommandHandler method doCommand.

@Override
public boolean doCommand(ILayer targetLayer, SearchCommand searchCommand) throws PatternSyntaxException {
    searchCommand.convertToTargetLayer(targetLayer);
    final ILayerListener searchEventListener = searchCommand.getSearchEventListener();
    if (searchEventListener != null) {
        this.selectionLayer.addLayerListener(searchEventListener);
    }
    try {
        PositionCoordinate anchor = this.selectionLayer.getSelectionAnchor();
        if (anchor.columnPosition < 0 || anchor.rowPosition < 0) {
            anchor = new PositionCoordinate(this.selectionLayer, 0, 0);
        }
        Object dataValueToFind = null;
        if ((dataValueToFind = searchCommand.getSearchText()) == null) {
            dataValueToFind = this.selectionLayer.getDataValueByPosition(anchor.columnPosition, anchor.rowPosition);
        }
        boolean performActionOnResult = true;
        if (searchCommand.getSearchStrategy() instanceof AbstractSearchStrategy) {
            AbstractSearchStrategy searchStrategy = (AbstractSearchStrategy) searchCommand.getSearchStrategy();
            searchStrategy.setContextLayer(targetLayer);
            searchStrategy.setCaseSensitive(searchCommand.isCaseSensitive());
            searchStrategy.setWrapSearch(searchCommand.isWrapSearch());
            searchStrategy.setWholeWord(searchCommand.isWholeWord());
            searchStrategy.setIncremental(searchCommand.isIncremental());
            searchStrategy.setRegex(searchCommand.isRegex());
            searchStrategy.setIncludeCollapsed(searchCommand.isIncludeCollapsed());
            searchStrategy.setSearchDirection(searchCommand.getSearchDirection());
            searchStrategy.setComparator(searchCommand.getComparator());
            performActionOnResult = !searchStrategy.processResultInternally();
        }
        this.searchResultCellCoordinate = searchCommand.getSearchStrategy().executeSearch(dataValueToFind);
        this.selectionLayer.fireLayerEvent(new SearchEvent(this.searchResultCellCoordinate));
        if (performActionOnResult && this.searchResultCellCoordinate != null) {
            final SelectCellCommand command = new SelectCellCommand(this.selectionLayer, this.searchResultCellCoordinate.columnPosition, this.searchResultCellCoordinate.rowPosition, false, false);
            command.setForcingEntireCellIntoViewport(true);
            this.selectionLayer.doCommand(command);
        }
    } finally {
        if (searchEventListener != null) {
            this.selectionLayer.removeLayerListener(searchEventListener);
        }
    }
    return true;
}
Also used : SelectCellCommand(org.eclipse.nebula.widgets.nattable.selection.command.SelectCellCommand) PositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate) ILayerListener(org.eclipse.nebula.widgets.nattable.layer.ILayerListener) SearchEvent(org.eclipse.nebula.widgets.nattable.search.event.SearchEvent) AbstractSearchStrategy(org.eclipse.nebula.widgets.nattable.search.strategy.AbstractSearchStrategy)

Example 9 with ILayerListener

use of org.eclipse.nebula.widgets.nattable.layer.ILayerListener 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 10 with ILayerListener

use of org.eclipse.nebula.widgets.nattable.layer.ILayerListener in project nebula.widgets.nattable by eclipse.

the class GroupByDataLayerSummaryRowConcurrencyTest method shouldCorrectlyCalculateSummaryValues.

// summary value == 55
@Test
public void shouldCorrectlyCalculateSummaryValues() {
    this.summaryRowLayer.addLayerListener(new ILayerListener() {

        @Override
        public synchronized void handleLayerEvent(ILayerEvent event) {
            if (event instanceof CellVisualChangeEvent) {
                GroupByDataLayerSummaryRowConcurrencyTest.this.calcCount++;
            }
        }
    });
    assertNull(this.summaryRowLayer.getDataValueByPosition(0, 0));
    assertNull(this.summaryRowLayer.getDataValueByPosition(1, 0));
    assertNull(this.summaryRowLayer.getDataValueByPosition(2, 0));
    assertNull(this.summaryRowLayer.getDataValueByPosition(3, 0));
    assertNull(this.summaryRowLayer.getDataValueByPosition(4, 0));
    assertNull(this.summaryRowLayer.getDataValueByPosition(5, 0));
    assertNull(this.summaryRowLayer.getDataValueByPosition(6, 0));
    assertNull(this.summaryRowLayer.getDataValueByPosition(7, 0));
    assertNull(this.summaryRowLayer.getDataValueByPosition(8, 0));
    assertNull(this.summaryRowLayer.getDataValueByPosition(9, 0));
    assertNull(this.summaryRowLayer.getDataValueByPosition(10, 0));
    while (this.calcCount < 11) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    assertEquals(55.0, this.summaryRowLayer.getDataValueByPosition(0, 0));
    assertEquals(55.0, this.summaryRowLayer.getDataValueByPosition(1, 0));
    assertEquals(55.0, this.summaryRowLayer.getDataValueByPosition(2, 0));
    assertEquals(55.0, this.summaryRowLayer.getDataValueByPosition(3, 0));
    assertEquals(55.0, this.summaryRowLayer.getDataValueByPosition(4, 0));
    assertEquals(55.0, this.summaryRowLayer.getDataValueByPosition(5, 0));
    assertEquals(55.0, this.summaryRowLayer.getDataValueByPosition(6, 0));
    assertEquals(55.0, this.summaryRowLayer.getDataValueByPosition(7, 0));
    assertEquals(55.0, this.summaryRowLayer.getDataValueByPosition(8, 0));
    assertEquals(55.0, this.summaryRowLayer.getDataValueByPosition(9, 0));
    assertEquals(55.0, this.summaryRowLayer.getDataValueByPosition(10, 0));
}
Also used : ILayerEvent(org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent) ILayerListener(org.eclipse.nebula.widgets.nattable.layer.ILayerListener) CellVisualChangeEvent(org.eclipse.nebula.widgets.nattable.layer.event.CellVisualChangeEvent) Test(org.junit.Test)

Aggregations

ILayerListener (org.eclipse.nebula.widgets.nattable.layer.ILayerListener)15 ILayerEvent (org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent)14 Test (org.junit.Test)8 HashSet (java.util.HashSet)4 SelectionLayer (org.eclipse.nebula.widgets.nattable.selection.SelectionLayer)4 CellSelectionEvent (org.eclipse.nebula.widgets.nattable.selection.event.CellSelectionEvent)4 BaseDataLayerFixture (org.eclipse.nebula.widgets.nattable.test.fixture.layer.BaseDataLayerFixture)4 Rectangle (org.eclipse.swt.graphics.Rectangle)4 PositionCoordinate (org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate)3 Range (org.eclipse.nebula.widgets.nattable.coordinate.Range)3 ColumnReorderEvent (org.eclipse.nebula.widgets.nattable.reorder.event.ColumnReorderEvent)3 SearchEvent (org.eclipse.nebula.widgets.nattable.search.event.SearchEvent)3 RowSelectionEvent (org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent)3 Collection (java.util.Collection)2 ColumnReorderLayer (org.eclipse.nebula.widgets.nattable.reorder.ColumnReorderLayer)2 SelectCellCommand (org.eclipse.nebula.widgets.nattable.selection.command.SelectCellCommand)2 ViewportLayer (org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer)2 HashMap (java.util.HashMap)1 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)1 IDataProvider (org.eclipse.nebula.widgets.nattable.data.IDataProvider)1