Search in sources :

Example 6 with GridCell

use of org.jkiss.dbeaver.ui.controls.lightgrid.GridCell in project dbeaver by serge-rider.

the class SpreadsheetPresentation method copySelectionToString.

@Nullable
public String copySelectionToString(ResultSetCopySettings settings) {
    String columnDelimiter = settings.getColumnDelimiter();
    if (columnDelimiter == null) {
        columnDelimiter = "\t";
    }
    String rowDelimiter = settings.getRowDelimiter();
    if (rowDelimiter == null) {
        rowDelimiter = GeneralUtils.getDefaultLineSeparator();
    }
    List<Object> selectedColumns = spreadsheet.getColumnSelection();
    IGridLabelProvider labelProvider = spreadsheet.getLabelProvider();
    StringBuilder tdt = new StringBuilder();
    if (settings.isCopyHeader()) {
        if (settings.isCopyRowNumbers()) {
            tdt.append("#");
        }
        for (Object column : selectedColumns) {
            if (tdt.length() > 0) {
                tdt.append(columnDelimiter);
            }
            tdt.append(labelProvider.getText(column));
        }
        tdt.append(rowDelimiter);
    }
    List<GridCell> selectedCells = spreadsheet.getCellSelection();
    boolean quoteCells = settings.isQuoteCells() && selectedCells.size() > 1;
    GridCell prevCell = null;
    for (GridCell cell : selectedCells) {
        if (prevCell == null || cell.row != prevCell.row) {
            // Next row
            if (prevCell != null && prevCell.col != cell.col) {
                // Fill empty row tail
                int prevColIndex = selectedColumns.indexOf(prevCell.col);
                for (int i = prevColIndex; i < selectedColumns.size() - 1; i++) {
                    tdt.append(columnDelimiter);
                }
            }
            if (prevCell != null) {
                tdt.append(rowDelimiter);
            }
            if (settings.isCopyRowNumbers()) {
                tdt.append(labelProvider.getText(cell.row)).append(columnDelimiter);
            }
        }
        if (prevCell != null && prevCell.col != cell.col) {
            int prevColIndex = selectedColumns.indexOf(prevCell.col);
            int curColIndex = selectedColumns.indexOf(cell.col);
            for (int i = prevColIndex; i < curColIndex; i++) {
                tdt.append(columnDelimiter);
            }
        }
        boolean recordMode = controller.isRecordMode();
        DBDAttributeBinding column = (DBDAttributeBinding) (!recordMode ? cell.col : cell.row);
        ResultSetRow row = (ResultSetRow) (!recordMode ? cell.row : cell.col);
        Object value = controller.getModel().getCellValue(column, row);
        String cellText = column.getValueRenderer().getValueDisplayString(column.getAttribute(), value, settings.getFormat());
        if (quoteCells && cellText != null) {
            if (cellText.contains(columnDelimiter) || cellText.contains(rowDelimiter)) {
                cellText = '"' + cellText + '"';
            }
        }
        tdt.append(cellText);
        if (settings.isCut()) {
            IValueController valueController = new SpreadsheetValueController(controller, column, row, IValueController.EditType.NONE, null);
            if (!valueController.isReadOnly()) {
                valueController.updateValue(BaseValueManager.makeNullValue(valueController), false);
            }
        }
        prevCell = cell;
    }
    if (settings.isCut()) {
        controller.redrawData(false, false);
        controller.updatePanelsContent(false);
    }
    return tdt.toString();
}
Also used : GridCell(org.jkiss.dbeaver.ui.controls.lightgrid.GridCell) IGridLabelProvider(org.jkiss.dbeaver.ui.controls.lightgrid.IGridLabelProvider) IValueController(org.jkiss.dbeaver.ui.data.IValueController) Nullable(org.jkiss.code.Nullable)

Example 7 with GridCell

use of org.jkiss.dbeaver.ui.controls.lightgrid.GridCell in project dbeaver by serge-rider.

the class SpreadsheetFindReplaceTarget method findAndSelect.

@Override
public int findAndSelect(int offset, String findString, boolean searchForward, boolean caseSensitive, boolean wholeWord, boolean regExSearch) {
    searchPattern = null;
    ResultSetModel model = owner.getController().getModel();
    if (model.isEmpty()) {
        return -1;
    }
    Spreadsheet spreadsheet = owner.getSpreadsheet();
    int rowCount = spreadsheet.getItemCount();
    int columnCount = spreadsheet.getColumnCount();
    Collection<GridPos> selection = spreadsheet.getSelection();
    GridPos startPosition = selection.isEmpty() ? null : selection.iterator().next();
    if (startPosition == null) {
        // From the beginning
        startPosition = new GridPos(0, 0);
    }
    Pattern findPattern;
    if (regExSearch) {
        try {
            findPattern = Pattern.compile(findString, caseSensitive ? 0 : Pattern.CASE_INSENSITIVE);
        } catch (PatternSyntaxException e) {
            log.warn("Bad regex pattern: " + findString);
            return -1;
        }
    } else {
        findPattern = Pattern.compile(Pattern.quote(findString), caseSensitive ? 0 : Pattern.CASE_INSENSITIVE);
    }
    int minColumnNum = owner.getController().isRecordMode() ? -1 : 0;
    for (GridPos curPosition = new GridPos(startPosition); ; ) {
        //Object element = contentProvider.getElement(curPosition);
        if (searchForward) {
            curPosition.col++;
            if (curPosition.col >= columnCount) {
                curPosition.col = minColumnNum;
                curPosition.row++;
            }
        } else {
            curPosition.col--;
            if (curPosition.col < minColumnNum) {
                curPosition.col = columnCount - 1;
                curPosition.row--;
            }
        }
        if (curPosition.row < 0 || curPosition.row >= rowCount) {
            if (offset == -1) {
                // Wrap search - redo search one more time
                offset = 0;
                if (searchForward) {
                    curPosition = new GridPos(0, 0);
                } else {
                    curPosition = new GridPos(columnCount - 1, rowCount - 1);
                }
            } else {
                // Not found
                return -1;
            }
        }
        String cellText;
        if (owner.getController().isRecordMode() && curPosition.col == minColumnNum) {
            // Header
            cellText = spreadsheet.getLabelProvider().getText(spreadsheet.getRowElement(curPosition.row));
        } else {
            GridCell cell = spreadsheet.posToCell(curPosition);
            if (cell != null) {
                cellText = spreadsheet.getContentProvider().getCellText(cell.col, cell.row);
            } else {
                continue;
            }
        }
        Matcher matcher = findPattern.matcher(cellText);
        if (wholeWord ? matcher.matches() : matcher.find()) {
            if (curPosition.col == minColumnNum) {
                curPosition.col = 0;
            }
            spreadsheet.setCellSelection(curPosition);
            spreadsheet.showSelection();
            searchPattern = findPattern;
            return curPosition.row;
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) ResultSetModel(org.jkiss.dbeaver.ui.controls.resultset.ResultSetModel) Matcher(java.util.regex.Matcher) GridPos(org.jkiss.dbeaver.ui.controls.lightgrid.GridPos) Point(org.eclipse.swt.graphics.Point) PatternSyntaxException(java.util.regex.PatternSyntaxException) GridCell(org.jkiss.dbeaver.ui.controls.lightgrid.GridCell)

Example 8 with GridCell

use of org.jkiss.dbeaver.ui.controls.lightgrid.GridCell in project dbeaver by serge-rider.

the class SpreadsheetPresentation method getAdapter.

@Override
public <T> T getAdapter(Class<T> adapter) {
    if (adapter == IPropertySheetPage.class) {
        // Show cell properties
        PropertyPageStandard page = new PropertyPageStandard();
        page.setPropertySourceProvider(new IPropertySourceProvider() {

            @Nullable
            @Override
            public IPropertySource getPropertySource(Object object) {
                if (object instanceof GridCell) {
                    GridCell cell = (GridCell) object;
                    boolean recordMode = controller.isRecordMode();
                    final DBDAttributeBinding attr = (DBDAttributeBinding) (recordMode ? cell.row : cell.col);
                    final ResultSetRow row = (ResultSetRow) (recordMode ? cell.col : cell.row);
                    final SpreadsheetValueController valueController = new SpreadsheetValueController(controller, attr, row, IValueController.EditType.NONE, null);
                    PropertyCollector props = new PropertyCollector(valueController.getBinding().getAttribute(), false);
                    props.collectProperties();
                    valueController.getValueManager().contributeProperties(props, valueController);
                    return new PropertySourceDelegate(props);
                }
                return null;
            }
        });
        return adapter.cast(page);
    } else if (adapter == IFindReplaceTarget.class) {
        return adapter.cast(findReplaceTarget);
    }
    return null;
}
Also used : PropertyPageStandard(org.jkiss.dbeaver.ui.controls.PropertyPageStandard) IPropertySourceProvider(org.eclipse.ui.views.properties.IPropertySourceProvider) PropertySourceDelegate(org.jkiss.dbeaver.ui.properties.PropertySourceDelegate) PropertyCollector(org.jkiss.dbeaver.runtime.properties.PropertyCollector) IPropertySource(org.eclipse.ui.views.properties.IPropertySource) GridCell(org.jkiss.dbeaver.ui.controls.lightgrid.GridCell) IFindReplaceTarget(org.eclipse.jface.text.IFindReplaceTarget) Nullable(org.jkiss.code.Nullable)

Example 9 with GridCell

use of org.jkiss.dbeaver.ui.controls.lightgrid.GridCell in project dbeaver by serge-rider.

the class SpreadsheetPresentation method scrollToRow.

public void scrollToRow(@NotNull RowPosition position) {
    boolean recordMode = controller.isRecordMode();
    ResultSetRow curRow = controller.getCurrentRow();
    ResultSetModel model = controller.getModel();
    switch(position) {
        case FIRST:
            if (recordMode) {
                if (model.getRowCount() > 0) {
                    controller.setCurrentRow(model.getRow(0));
                } else {
                    controller.setCurrentRow(null);
                }
            } else {
                spreadsheet.shiftCursor(0, -spreadsheet.getItemCount(), false);
            }
            break;
        case PREVIOUS:
            if (recordMode && curRow != null && curRow.getVisualNumber() > 0) {
                controller.setCurrentRow(model.getRow(curRow.getVisualNumber() - 1));
            } else {
                spreadsheet.shiftCursor(0, -1, false);
            }
            break;
        case NEXT:
            if (recordMode && curRow != null && curRow.getVisualNumber() < model.getRowCount() - 1) {
                controller.setCurrentRow(model.getRow(curRow.getVisualNumber() + 1));
            } else {
                spreadsheet.shiftCursor(0, 1, false);
            }
            break;
        case LAST:
            if (recordMode && model.getRowCount() > 0) {
                controller.setCurrentRow(model.getRow(model.getRowCount() - 1));
            } else {
                spreadsheet.shiftCursor(0, spreadsheet.getItemCount(), false);
            }
            break;
        case CURRENT:
            if (curRow != null) {
                GridPos curPos = spreadsheet.getCursorPosition();
                GridCell newCell = spreadsheet.posToCell(new GridPos(curPos.col, curRow.getVisualNumber()));
                if (newCell != null) {
                    spreadsheet.setCursor(newCell, false);
                }
            }
            break;
    }
    if (controller.isRecordMode()) {
        // Update focus cell
        restoreState(curAttribute);
    }
    // Update controls
    controller.updateEditControls();
    controller.updateStatusMessage();
    if (recordMode) {
        // Refresh meta if we are in record mode
        refreshData(true, false, true);
    }
}
Also used : GridPos(org.jkiss.dbeaver.ui.controls.lightgrid.GridPos) GridCell(org.jkiss.dbeaver.ui.controls.lightgrid.GridCell)

Example 10 with GridCell

use of org.jkiss.dbeaver.ui.controls.lightgrid.GridCell in project dbeaver by serge-rider.

the class SpreadsheetPresentation method restoreState.

@Override
public void restoreState(Object state) {
    this.curAttribute = controller.getModel().getAttributeBinding((DBDAttributeBinding) state);
    ResultSetRow curRow = controller.getCurrentRow();
    if (curRow != null && this.curAttribute != null) {
        GridCell cell = controller.isRecordMode() ? new GridCell(curRow, this.curAttribute) : new GridCell(this.curAttribute, curRow);
        //spreadsheet.selectCell(cell);
        spreadsheet.setCursor(cell, false);
    }
}
Also used : GridCell(org.jkiss.dbeaver.ui.controls.lightgrid.GridCell)

Aggregations

GridCell (org.jkiss.dbeaver.ui.controls.lightgrid.GridCell)10 GridPos (org.jkiss.dbeaver.ui.controls.lightgrid.GridPos)4 Nullable (org.jkiss.code.Nullable)2 IGridLabelProvider (org.jkiss.dbeaver.ui.controls.lightgrid.IGridLabelProvider)2 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 IFindReplaceTarget (org.eclipse.jface.text.IFindReplaceTarget)1 IPropertyChangeListener (org.eclipse.jface.util.IPropertyChangeListener)1 PropertyChangeEvent (org.eclipse.jface.util.PropertyChangeEvent)1 Point (org.eclipse.swt.graphics.Point)1 GridData (org.eclipse.swt.layout.GridData)1 IPropertySource (org.eclipse.ui.views.properties.IPropertySource)1 IPropertySourceProvider (org.eclipse.ui.views.properties.IPropertySourceProvider)1 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)1 PropertyCollector (org.jkiss.dbeaver.runtime.properties.PropertyCollector)1 PropertyPageStandard (org.jkiss.dbeaver.ui.controls.PropertyPageStandard)1 IGridContentProvider (org.jkiss.dbeaver.ui.controls.lightgrid.IGridContentProvider)1 ResultSetModel (org.jkiss.dbeaver.ui.controls.resultset.ResultSetModel)1 ResultSetRow (org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow)1