Search in sources :

Example 1 with ISearchStrategy

use of net.heartsome.cat.ts.ui.xliffeditor.nattable.search.strategy.ISearchStrategy in project translationstudio8 by heartsome.

the class FindReplaceCommandHandler method doCommand.

public boolean doCommand(ILayer targetLayer, FindReplaceCommand findReplaceCommand) {
    findReplaceCommand.convertToTargetLayer(targetLayer);
    ISearchStrategy searchStrategy = findReplaceCommand.getSearchStrategy();
    if (findReplaceCommand.getSearchEventListener() != null) {
        selectionLayer.addLayerListener(findReplaceCommand.getSearchEventListener());
    }
    PositionCoordinate anchor = selectionLayer.getSelectionAnchor();
    if (anchor.columnPosition < 0 || anchor.rowPosition < 0) {
        anchor = new PositionCoordinate(selectionLayer, 0, 0);
    }
    searchStrategy.setContextLayer(targetLayer);
    Object dataValueToFind = null;
    if ((dataValueToFind = findReplaceCommand.getSearchText()) == null) {
        dataValueToFind = selectionLayer.getDataValueByPosition(anchor.columnPosition, anchor.rowPosition);
    }
    ICellSearchStrategy cellSearchStrategy = findReplaceCommand.getCellSearchStrategy();
    searchStrategy.setCellSearchStrategy(cellSearchStrategy);
    DefaultCellSearchStrategy defaultCellSearchStrategy = null;
    if (cellSearchStrategy instanceof DefaultCellSearchStrategy) {
        defaultCellSearchStrategy = (DefaultCellSearchStrategy) cellSearchStrategy;
    }
    searchResultCellRegion = searchStrategy.executeSearch(dataValueToFind);
    if (searchResultCellRegion != null) {
        PositionCoordinate searchResultCellCoordinate = searchResultCellRegion.getPositionCoordinate();
        int rowPosition = searchResultCellCoordinate.rowPosition;
        XLIFFEditorImplWithNatTable editor = XLIFFEditorImplWithNatTable.getCurrent();
        ViewportLayer viewportLayer = LayerUtil.getLayer(editor.getTable(), ViewportLayer.class);
        HsMultiActiveCellEditor.commit(true);
        if (!editor.isHorizontalLayout()) {
            viewportLayer.doCommand(new SelectCellCommand(selectionLayer, searchResultCellCoordinate.columnPosition, rowPosition / 2 * 2, false, false));
        } else {
            viewportLayer.doCommand(new SelectCellCommand(selectionLayer, searchResultCellCoordinate.columnPosition, rowPosition, false, false));
        }
        HsMultiCellEditorControl.activeSourceAndTargetCell(editor);
        HsMultiActiveCellEditor.setCellEditorForceFocusByIndex(searchResultCellCoordinate.columnPosition, rowPosition);
        StyledTextCellEditor cellEditor = HsMultiActiveCellEditor.getFocusCellEditor();
        if (cellEditor != null) {
            String dataValue = cellEditor.getSegmentViewer().getDocument().get();
            if (dataValue != null) {
                int startOffset = -1;
                if (defaultCellSearchStrategy != null) {
                    startOffset = defaultCellSearchStrategy.getStartOffset();
                }
                defaultCellSearchStrategy.setStartOffset(startOffset);
                String findString = dataValueToFind.toString();
                if (XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) {
                    findString = findString.replaceAll("\\n", Constants.LINE_SEPARATOR_CHARACTER + "\n");
                    findString = findString.replaceAll("\\t", Constants.TAB_CHARACTER + "​");
                    findString = findString.replaceAll(" ", Constants.SPACE_CHARACTER + "​");
                }
                IRegion region = defaultCellSearchStrategy.executeSearch(findString, dataValue);
                if (region != null) {
                    HsMultiActiveCellEditor.setSelectionText(cellEditor, region.getOffset(), region.getLength());
                }
                defaultCellSearchStrategy.setStartOffset(-1);
            }
        }
    }
    selectionLayer.fireLayerEvent(new FindReplaceEvent(searchResultCellRegion));
    if (findReplaceCommand.getSearchEventListener() != null) {
        selectionLayer.removeLayerListener(findReplaceCommand.getSearchEventListener());
    }
    return true;
}
Also used : XLIFFEditorImplWithNatTable(net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable) ICellSearchStrategy(net.heartsome.cat.ts.ui.xliffeditor.nattable.search.strategy.ICellSearchStrategy) SelectCellCommand(net.sourceforge.nattable.selection.command.SelectCellCommand) FindReplaceEvent(net.heartsome.cat.ts.ui.xliffeditor.nattable.search.event.FindReplaceEvent) ViewportLayer(net.sourceforge.nattable.viewport.ViewportLayer) IRegion(org.eclipse.jface.text.IRegion) DefaultCellSearchStrategy(net.heartsome.cat.ts.ui.xliffeditor.nattable.search.strategy.DefaultCellSearchStrategy) ISearchStrategy(net.heartsome.cat.ts.ui.xliffeditor.nattable.search.strategy.ISearchStrategy) PositionCoordinate(net.sourceforge.nattable.coordinate.PositionCoordinate) StyledTextCellEditor(net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.StyledTextCellEditor)

Aggregations

StyledTextCellEditor (net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.StyledTextCellEditor)1 XLIFFEditorImplWithNatTable (net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable)1 FindReplaceEvent (net.heartsome.cat.ts.ui.xliffeditor.nattable.search.event.FindReplaceEvent)1 DefaultCellSearchStrategy (net.heartsome.cat.ts.ui.xliffeditor.nattable.search.strategy.DefaultCellSearchStrategy)1 ICellSearchStrategy (net.heartsome.cat.ts.ui.xliffeditor.nattable.search.strategy.ICellSearchStrategy)1 ISearchStrategy (net.heartsome.cat.ts.ui.xliffeditor.nattable.search.strategy.ISearchStrategy)1 PositionCoordinate (net.sourceforge.nattable.coordinate.PositionCoordinate)1 SelectCellCommand (net.sourceforge.nattable.selection.command.SelectCellCommand)1 ViewportLayer (net.sourceforge.nattable.viewport.ViewportLayer)1 IRegion (org.eclipse.jface.text.IRegion)1