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();
}
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;
}
}
}
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;
}
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);
}
}
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);
}
}
Aggregations