Search in sources :

Example 1 with IGridLabelProvider

use of org.jkiss.dbeaver.ui.controls.lightgrid.IGridLabelProvider 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)

Aggregations

Nullable (org.jkiss.code.Nullable)1 GridCell (org.jkiss.dbeaver.ui.controls.lightgrid.GridCell)1 IGridLabelProvider (org.jkiss.dbeaver.ui.controls.lightgrid.IGridLabelProvider)1 IValueController (org.jkiss.dbeaver.ui.data.IValueController)1