Search in sources :

Example 11 with ILayerListener

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

the class RowSelectionIntegrationTest method onlySelectRowEventsFired.

@Test
public void onlySelectRowEventsFired() {
    this.nattable.addLayerListener(new ILayerListener() {

        @Override
        public void handleLayerEvent(ILayerEvent event) {
            if (event instanceof CellSelectionEvent) {
                fail("CellSelectionEvent fired for row selection");
            }
        }
    });
    this.nattable.doCommand(new SelectRowsCommand(this.selectionLayer, 0, 0, false, false));
    // the second call first clears the selection and then applies the new
    // one clearing by default also fires a CellSelectionEvent with negative
    // values
    this.nattable.doCommand(new SelectRowsCommand(this.selectionLayer, 0, 3, false, false));
}
Also used : ILayerEvent(org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent) CellSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.CellSelectionEvent) ILayerListener(org.eclipse.nebula.widgets.nattable.layer.ILayerListener) SelectRowsCommand(org.eclipse.nebula.widgets.nattable.selection.command.SelectRowsCommand) Test(org.junit.Test)

Example 12 with ILayerListener

use of org.eclipse.nebula.widgets.nattable.layer.ILayerListener in project tdq-studio-se by Talend.

the class DataSampleTable method addCustomSelectionBehaviour.

private void addCustomSelectionBehaviour() {
    natTable.addLayerListener(new ILayerListener() {

        @Override
        public void handleLayerEvent(ILayerEvent event) {
            if (event instanceof ColumnHeaderSelectionEvent) {
                ColumnHeaderSelectionEvent columnEvent = (ColumnHeaderSelectionEvent) event;
                Collection<Range> ranges = columnEvent.getColumnPositionRanges();
                if (ranges.size() > 0) {
                    Range range = ranges.iterator().next();
                    handleColumnSelectionChange(range.start);
                }
            } else if (event instanceof ColumnReorderEvent) {
                if (ColumnIndexMap == null) {
                    ColumnIndexMap = new HashMap<String, Integer>();
                } else {
                    ColumnIndexMap.clear();
                }
                // save propertyNames oder into lastTimePropertyNameOrder
                initPropertyNameOrder();
                // Fill elements into ColumnIndexMap before the order change ones.
                fillPreElement();
                // Fill the oder all of elements which display on the table into ColumnIndexMap
                fillElementWhichDisplayOnTable();
                // Fill the oder all of elements which after the order change ones.
                fillEndElement();
                // update newest order state on the lastTimePropertyNameOrder list
                savePropertyNameState();
                notifyObservers();
            }
        }

        /**
         * DOC talend Comment method "fillElementWhichDisplayOnTable". copy by #fillElementWhichDisplayOnTable
         */
        private void fillElementWhichDisplayOnTable() {
            for (int index = 1; index < natTable.getColumnCount(); index++) {
                int columnIndexByPosition = natTable.getColumnIndexByPosition(index);
                ColumnIndexMap.put(propertyNames[columnIndexByPosition], ColumnIndexMap.size());
            }
        }

        private void fillEndElement() {
            for (int index = ColumnIndexMap.size(); index < propertyNames.length; index++) {
                ColumnIndexMap.put(lastTimePropertyNameOrder.get(index), index);
            }
        }

        private void fillPreElement() {
            int disColumnCount = natTable.getColumnCount() - 1;
            // display all case so that no preElement one need to be filled
            if (disColumnCount >= lastTimePropertyNameOrder.size()) {
                return;
            }
            // not all display
            String endName = propertyNames[natTable.getColumnIndexByPosition(1)];
            int firstOneLastPosition = lastTimePropertyNameOrder.indexOf(endName);
            endName = propertyNames[natTable.getColumnIndexByPosition(2)];
            int secondOneLastPosition = lastTimePropertyNameOrder.indexOf(endName);
            if (secondOneLastPosition - firstOneLastPosition > 0) {
                // 1 is not be change order case
                endName = lastTimePropertyNameOrder.get(firstOneLastPosition);
                // For example 1->2+
                if (secondOneLastPosition - firstOneLastPosition == 1) {
                    String moveColName = findMoveColumnName(firstOneLastPosition - 1);
                    if (moveColName != null) {
                        endName = moveColName;
                    }
                }
            // For example 1->2,2+->1
            } else if (secondOneLastPosition - firstOneLastPosition < 0) {
                // in fact the value shoud be
                // firstOneLastPosition+secondOneLastPosition-firstOneLastPosition
                endName = lastTimePropertyNameOrder.get(secondOneLastPosition);
            }
            // ~ not all display
            int index = 0;
            for (String propertyName : lastTimePropertyNameOrder) {
                if (endName.equals(propertyName)) {
                    break;
                }
                ColumnIndexMap.put(propertyName, index++);
            }
        }

        /**
         * Find the column name which be moved on the ui
         *
         * @param spacing
         * @return
         */
        private String findMoveColumnName(int spacing) {
            for (int index = 3; index < natTable.getColumnCount() - 1; index++) {
                String propertyName = propertyNames[natTable.getColumnIndexByPosition(index)];
                int lastPosition = lastTimePropertyNameOrder.indexOf(propertyName);
                if (spacing > lastPosition - index) {
                    return propertyName;
                } else if (spacing == lastPosition - index) {
                    continue;
                } else {
                    break;
                }
            }
            return null;
        }
    });
}
Also used : ILayerEvent(org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent) ColumnHeaderSelectionEvent(org.eclipse.nebula.widgets.nattable.grid.layer.event.ColumnHeaderSelectionEvent) Collection(java.util.Collection) ColumnReorderEvent(org.eclipse.nebula.widgets.nattable.reorder.event.ColumnReorderEvent) ILayerListener(org.eclipse.nebula.widgets.nattable.layer.ILayerListener) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range)

Example 13 with ILayerListener

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

the class SearchGridCommandHandlerTest method doTestOnSelection.

private void doTestOnSelection() throws PatternSyntaxException {
    // Register call back
    final ILayerListener listener = new ILayerListener() {

        @Override
        public void handleLayerEvent(ILayerEvent event) {
            if (event instanceof SearchEvent) {
                // Check event, coordinate should be in composite layer
                // coordinates
                SearchEvent searchEvent = (SearchEvent) event;
                if (SearchGridCommandHandlerTest.this.expected != null) {
                    assertEquals(SearchGridCommandHandlerTest.this.expected.columnPosition, searchEvent.getCellCoordinate().getColumnPosition());
                    assertEquals(SearchGridCommandHandlerTest.this.expected.rowPosition, searchEvent.getCellCoordinate().getRowPosition());
                } else {
                    assertNull(searchEvent.getCellCoordinate());
                }
            }
        }
    };
    this.gridLayer.addLayerListener(listener);
    try {
        SelectionLayer selectionLayer = this.gridLayer.getBodyLayer().getSelectionLayer();
        final SelectionSearchStrategy gridSearchStrategy = new SelectionSearchStrategy(this.configRegistry, this.isColumnFirst);
        final SearchCommand searchCommand = new SearchCommand(this.searchText, selectionLayer, gridSearchStrategy, this.isForward ? ISearchDirection.SEARCH_FORWARD : ISearchDirection.SEARCH_BACKWARDS, this.isWrapSearch, this.isCaseSensitive, this.isWholeWord, this.isIncremental, this.isRegex, this.isIncludeCollapsed, new CellValueAsStringComparator<>());
        this.commandHandler.doCommand(selectionLayer, searchCommand);
        final PositionCoordinate searchResultCellCoordinate = this.commandHandler.getSearchResultCellCoordinate();
        if (this.expected != null) {
            assertEquals(this.expected.columnPosition, searchResultCellCoordinate.columnPosition);
            assertEquals(this.expected.rowPosition, searchResultCellCoordinate.rowPosition);
            assertEquals(50, selectionLayer.getSelectedCellPositions().length);
            assertEquals(this.expected.columnPosition, selectionLayer.getSelectionAnchor().getColumnPosition());
            assertEquals(this.expected.rowPosition, selectionLayer.getSelectionAnchor().getRowPosition());
        } else {
            assertNull(searchResultCellCoordinate);
        }
    } finally {
        this.gridLayer.removeLayerListener(listener);
    }
}
Also used : ILayerEvent(org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent) SelectionLayer(org.eclipse.nebula.widgets.nattable.selection.SelectionLayer) 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) SelectionSearchStrategy(org.eclipse.nebula.widgets.nattable.search.strategy.SelectionSearchStrategy)

Example 14 with ILayerListener

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

the class RowReorderLayerVisibleChangeTest method returnsCorrectPositionRectangleForMultiColumnReorderRightCase.

@SuppressWarnings("boxing")
@Test
public /**
 * 	Index		2	3	0	1 ... 20
 *          --------------------
 *  Position 	0 	1	2	3 ... 20
 */
void returnsCorrectPositionRectangleForMultiColumnReorderRightCase() {
    RowReorderLayer reorderLayer = new RowReorderLayer(new BaseDataLayerFixture(20, 20));
    // Build expected cell positions to redraw
    final Set<Rectangle> expectedPositions = new HashSet<Rectangle>();
    expectedPositions.add(new Rectangle(0, 0, 20, 20));
    reorderLayer.addLayerListener(new ILayerListener() {

        @Override
        public void handleLayerEvent(ILayerEvent event) {
            RowReorderEvent multiReorder = (RowReorderEvent) event;
            assertTrue(multiReorder.getChangedPositionRectangles().containsAll(expectedPositions));
        }
    });
    // Reorder from beginning of grid
    List<Integer> fromRowPositions = Arrays.asList(new Integer[] { 0, 1 });
    reorderLayer.reorderMultipleRowPositions(fromRowPositions, 2);
    // Reorder to middle of grid
    expectedPositions.clear();
    expectedPositions.add(new Rectangle(0, 5, 20, 15));
    fromRowPositions = Arrays.asList(new Integer[] { 5, 6, 7, 8 });
    reorderLayer.reorderMultipleRowPositions(fromRowPositions, 10);
    // Reorder to end of grid
    expectedPositions.clear();
    expectedPositions.add(new Rectangle(0, 10, 20, 10));
    fromRowPositions = Arrays.asList(new Integer[] { 10, 11, 12, 13 });
    reorderLayer.reorderMultipleRowPositions(fromRowPositions, 19);
}
Also used : ILayerEvent(org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent) BaseDataLayerFixture(org.eclipse.nebula.widgets.nattable.test.fixture.layer.BaseDataLayerFixture) Rectangle(org.eclipse.swt.graphics.Rectangle) ILayerListener(org.eclipse.nebula.widgets.nattable.layer.ILayerListener) RowReorderEvent(org.eclipse.nebula.widgets.nattable.reorder.event.RowReorderEvent) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with ILayerListener

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

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