Search in sources :

Example 1 with SearchEvent

use of org.eclipse.nebula.widgets.nattable.search.event.SearchEvent in project nebula.widgets.nattable by eclipse.

the class SearchGridCommandHandlerTest method doTest.

private void doTest() 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 GridSearchStrategy gridSearchStrategy = new GridSearchStrategy(this.configRegistry, this.isWrapSearch, 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(1, selectionLayer.getSelectedCellPositions().length);
            assertEquals(this.expected.columnPosition, selectionLayer.getSelectedCellPositions()[0].columnPosition);
            assertEquals(this.expected.rowPosition, selectionLayer.getSelectedCellPositions()[0].rowPosition);
        } else {
            assertNull(searchResultCellCoordinate);
        }
    } finally {
        this.gridLayer.removeLayerListener(listener);
    }
}
Also used : ILayerEvent(org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent) GridSearchStrategy(org.eclipse.nebula.widgets.nattable.search.strategy.GridSearchStrategy) 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)

Example 2 with SearchEvent

use of org.eclipse.nebula.widgets.nattable.search.event.SearchEvent in project nebula.widgets.nattable by eclipse.

the class HierarchicalTreeLayer method handleLayerEvent.

@Override
public void handleLayerEvent(ILayerEvent event) {
    if (event instanceof IStructuralChangeEvent) {
        IStructuralChangeEvent structuralChangeEvent = (IStructuralChangeEvent) event;
        if (structuralChangeEvent.isVerticalStructureChanged()) {
            // recalculate node row indexes
            // build a new collection of nodes to avoid duplication clashes
            // as nodes are equal per column and row index
            int negativeIndex = -1;
            Set<HierarchicalTreeNode> updatedCollapsedNodes = new HashSet<HierarchicalTreeNode>();
            for (HierarchicalTreeNode node : this.collapsedNodes) {
                int newRowIndex = findTopRowIndex(node.columnIndex, node.rowObject);
                // the underlying collection
                if (newRowIndex >= 0) {
                    updatedCollapsedNodes.add(new HierarchicalTreeNode(node.columnIndex, newRowIndex, this.underlyingList.get(newRowIndex)));
                } else if (this.retainRemovedRowObjectNodes) {
                    updatedCollapsedNodes.add(new HierarchicalTreeNode(node.columnIndex, negativeIndex, node.rowObject));
                    negativeIndex--;
                }
            }
            this.collapsedNodes.clear();
            this.collapsedNodes.addAll(updatedCollapsedNodes);
            // recalculate hidden rows based on updated collapsed nodes
            Set<Integer> updatedHiddenRows = new HashSet<Integer>();
            for (HierarchicalTreeNode node : this.collapsedNodes) {
                updatedHiddenRows.addAll(getChildIndexes(node.columnIndex, node.rowIndex));
            }
            getHiddenRowIndexes().clear();
            getHiddenRowIndexes().addAll(updatedHiddenRows);
        }
    } else if (event instanceof SearchEvent) {
        PositionCoordinate coord = ((SearchEvent) event).getCellCoordinate();
        if (coord != null) {
            Integer foundIndex = coord.getLayer().getRowIndexByPosition(coord.rowPosition);
            if (getHiddenRowIndexes().contains(foundIndex)) {
                if (this.expandOnSearch) {
                    // not collapsible
                    for (int level = this.nodeColumnMapping.size() - 2; level >= 0; level--) {
                        ILayerCell nodeCell = coord.getLayer().getCellByPosition(this.nodeColumnMapping.get(level), coord.rowPosition);
                        int colIdx = coord.getLayer().getColumnIndexByPosition(nodeCell.getOriginColumnPosition());
                        int rowIdx = coord.getLayer().getRowIndexByPosition(nodeCell.getOriginRowPosition());
                        if (this.collapsedNodes.contains(new HierarchicalTreeNode(colIdx, rowIdx, null))) {
                            expandOrCollapse(colIdx, rowIdx);
                        }
                    }
                } else {
                    // only make the single row visible again
                    getHiddenRowIndexes().remove(foundIndex);
                }
            }
            invalidateCache();
            fireLayerEvent(new ShowRowPositionsEvent(this, Arrays.asList(foundIndex)));
        }
    }
    super.handleLayerEvent(event);
}
Also used : IStructuralChangeEvent(org.eclipse.nebula.widgets.nattable.layer.event.IStructuralChangeEvent) PositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate) SearchEvent(org.eclipse.nebula.widgets.nattable.search.event.SearchEvent) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell) ShowRowPositionsEvent(org.eclipse.nebula.widgets.nattable.hideshow.event.ShowRowPositionsEvent) HashSet(java.util.HashSet)

Example 3 with SearchEvent

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

use of org.eclipse.nebula.widgets.nattable.search.event.SearchEvent 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)

Aggregations

PositionCoordinate (org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate)4 SearchEvent (org.eclipse.nebula.widgets.nattable.search.event.SearchEvent)4 ILayerListener (org.eclipse.nebula.widgets.nattable.layer.ILayerListener)3 ILayerEvent (org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent)2 SelectionLayer (org.eclipse.nebula.widgets.nattable.selection.SelectionLayer)2 HashSet (java.util.HashSet)1 ShowRowPositionsEvent (org.eclipse.nebula.widgets.nattable.hideshow.event.ShowRowPositionsEvent)1 ILayerCell (org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)1 IStructuralChangeEvent (org.eclipse.nebula.widgets.nattable.layer.event.IStructuralChangeEvent)1 AbstractSearchStrategy (org.eclipse.nebula.widgets.nattable.search.strategy.AbstractSearchStrategy)1 GridSearchStrategy (org.eclipse.nebula.widgets.nattable.search.strategy.GridSearchStrategy)1 SelectionSearchStrategy (org.eclipse.nebula.widgets.nattable.search.strategy.SelectionSearchStrategy)1 SelectCellCommand (org.eclipse.nebula.widgets.nattable.selection.command.SelectCellCommand)1