use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by serge-rider.
the class Spreadsheet method handleEvent.
@Override
public void handleEvent(final Event event) {
switch(event.type) {
// case SWT.KeyUp:
case SWT.KeyDown:
boolean ctrlPressed = ((event.stateMask & SWT.CTRL) != 0);
if (!ctrlPressed && (event.keyCode == SWT.CR || (event.keyCode >= SWT.KEYPAD_0 && event.keyCode <= SWT.KEYPAD_9) || (event.keyCode == '-' || event.keyCode == '+' || event.keyCode == SWT.KEYPAD_ADD || event.keyCode == SWT.KEYPAD_SUBTRACT) || (event.keyCode >= 'a' && event.keyCode <= 'z') || (event.keyCode >= '0' && event.keyCode <= '9')) || Character.isLetterOrDigit(event.character)) {
Control editorControl = tableEditor.getEditor();
if (editorControl == null || editorControl.isDisposed()) {
editorControl = presentation.openValueEditor(true);
}
final SpreadsheetPresentation presentation = getPresentation();
final DBDAttributeBinding attribute = presentation.getCurrentAttribute();
if (editorControl != null && attribute != null && presentation.getController().getAttributeReadOnlyStatus(attribute) == null && event.keyCode != SWT.CR) {
if (!editorControl.isDisposed()) {
// We used to forward key even to control but it worked poorly.
// So let's just insert first letter (it will remove old value which must be selected for inline controls)
String strValue = String.valueOf(event.character);
if (editorControl instanceof Text) {
((Text) editorControl).insert(strValue);
} else if (editorControl instanceof StyledText) {
((StyledText) editorControl).insert(strValue);
}
/*
// Set editor value
// Forward the same key event to just created control
final Event kdEvent = new Event();
kdEvent.type = event.type;
kdEvent.character = event.character;
kdEvent.keyCode = event.keyCode;
editorControl.setFocus();
editorControl.getDisplay().post(kdEvent);
*/
}
}
}
break;
case SWT.MouseDoubleClick:
if (event.button != 1) {
return;
}
GridPos pos = super.getCell(new Point(event.x, event.y));
GridPos focusPos = super.getFocusPos();
if (pos != null && focusPos != null && pos.equals(super.getFocusPos())) {
DoubleClickBehavior doubleClickBehavior = CommonUtils.valueOf(DoubleClickBehavior.class, presentation.getPreferenceStore().getString(ResultSetPreferences.RESULT_SET_DOUBLE_CLICK), DoubleClickBehavior.NONE);
switch(doubleClickBehavior) {
case NONE:
return;
case EDITOR:
presentation.openValueEditor(false);
break;
case INLINE_EDITOR:
presentation.openValueEditor(true);
break;
case COPY_VALUE:
{
ResultSetCopySettings copySettings = new ResultSetCopySettings();
copySettings.setFormat(DBDDisplayFormat.EDIT);
ResultSetUtils.copyToClipboard(presentation.copySelection(copySettings));
break;
}
case COPY_PASTE_VALUE:
{
IResultSetValueReflector valueReflector = GeneralUtils.adapt(presentation.getController().getContainer(), IResultSetValueReflector.class);
if (valueReflector != null) {
DBDAttributeBinding currentAttribute = presentation.getCurrentAttribute();
ResultSetRow currentRow = presentation.getController().getCurrentRow();
if (currentAttribute != null && currentRow != null) {
Object cellValue = presentation.getController().getModel().getCellValue(currentAttribute, currentRow);
ResultSetCopySettings copySettings = new ResultSetCopySettings();
Map<Transfer, Object> selFormats = presentation.copySelection(copySettings);
Object textValue = selFormats.get(TextTransfer.getInstance());
if (textValue != null) {
valueReflector.insertCurrentCellValue(currentAttribute, cellValue, CommonUtils.toString(textValue));
}
}
} else {
// No value reflector - open inline editor then
presentation.openValueEditor(true);
}
break;
}
}
}
break;
case SWT.MouseDown:
if (event.button == 2) {
// presentation.openValueEditor(true);
}
break;
case LightGrid.Event_ChangeSort:
presentation.changeSorting(event.data, event.stateMask);
break;
case LightGrid.Event_FilterColumn:
// showFiltersMenu
presentation.showFiltering(event.data);
break;
case LightGrid.Event_NavigateLink:
// Perform navigation async because it may change grid content and
// we don't want to mess current grid state
UIUtils.asyncExec(() -> presentation.navigateLink((GridCell) event.data, event.stateMask));
break;
}
}
use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by serge-rider.
the class SpreadsheetFindReplaceTarget method replaceSelection.
@Override
public void replaceSelection(String text, boolean regExReplace) {
GridPos selection = (GridPos) owner.getSelection().getFirstElement();
if (selection == null) {
return;
}
GridCell cell = owner.getSpreadsheet().posToCell(selection);
if (cell == null) {
return;
}
String oldValue = CommonUtils.toString(owner.getSpreadsheet().getContentProvider().getCellValue(cell.col, cell.row, true, true));
String newValue = text;
if (searchPattern != null) {
newValue = searchPattern.matcher(oldValue).replaceAll(newValue);
}
boolean recordMode = owner.getController().isRecordMode();
final DBDAttributeBinding attr = (DBDAttributeBinding) (recordMode ? cell.row : cell.col);
final ResultSetRow row = (ResultSetRow) (recordMode ? cell.col : cell.row);
owner.getController().getModel().updateCellValue(attr, row, newValue);
owner.getController().updatePanelsContent(false);
}
use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by serge-rider.
the class DataExporterDbUnit method exportRow.
@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) throws DBException, IOException {
PrintWriter out = getWriter();
out.write(" <" + tableName);
for (int i = 0; i < row.length; i++) {
if (DBUtils.isNullValue(row[i]) && !includeNullValues) {
continue;
}
DBDAttributeBinding column = columns[i];
String columnName = escapeXmlElementName(column.getName());
if (columnName != null && upperCaseColumnNames) {
columnName = columnName.toUpperCase();
}
out.write(" " + columnName + "=\"");
Object columnValue = row[i];
if (DBUtils.isNullValue(columnValue)) {
writeTextCell("" + getSite().getProperties().get(PROP_NULL_VALUE_STRING));
} else if (columnValue instanceof Float || columnValue instanceof Double || columnValue instanceof BigDecimal) {
int scale = column.getMetaAttribute().getScale() != null && column.getMetaAttribute().getScale() > 0 ? column.getMetaAttribute().getScale() : 1;
try {
out.write(String.format(Locale.ROOT, "%." + scale + "f", columnValue));
} catch (Exception e) {
out.write(columnValue.toString());
}
} else if (columnValue instanceof Boolean) {
out.write(columnValue.toString());
} else if (columnValue instanceof Number) {
out.write(columnValue.toString());
} else if (columnValue instanceof Timestamp) {
try {
int nanoseconds = ((Timestamp) columnValue).getNanos();
out.write(String.format(Locale.ROOT, "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%2$d", columnValue, nanoseconds));
} catch (Exception e) {
out.write(columnValue.toString());
}
} else if (columnValue instanceof Time) {
try {
out.write(String.format(Locale.ROOT, "%1$tH:%1$tM:%1$tS", columnValue));
} catch (Exception e) {
out.write(columnValue.toString());
}
} else if (columnValue instanceof Date) {
try {
out.write(String.format(Locale.ROOT, "%1$tY-%1$tm-%1$td", columnValue));
} catch (Exception e) {
out.write(columnValue.toString());
}
} else if (columnValue instanceof DBDContent) {
// Content
// Inline textual content and handle binaries in some special way
DBDContent content = (DBDContent) columnValue;
try {
DBDContentStorage cs = content.getContents(session.getProgressMonitor());
if (cs != null) {
if (ContentUtils.isTextContent(content)) {
try (Reader reader = cs.getContentReader()) {
writeCellValue(reader);
}
} else {
try (final InputStream stream = cs.getContentStream()) {
Base64.encode(stream, cs.getContentLength(), getSite().getWriter());
}
}
}
} finally {
content.release();
}
} else {
writeTextCell(super.getValueDisplayString(column, columnValue));
}
out.write("\"");
}
out.write("/>" + CommonUtils.getLineSeparator());
}
use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by serge-rider.
the class DataExporterJSON method exportRow.
@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) throws DBException, IOException {
PrintWriter out = getWriter();
if (rowNum > 0) {
out.write(",\n");
}
rowNum++;
if (isJsonDocumentResults(session.getProgressMonitor(), row)) {
DBDDocument document = (DBDDocument) row[0];
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
document.serializeDocument(session.getProgressMonitor(), buffer, StandardCharsets.UTF_8);
String jsonText = buffer.toString(StandardCharsets.UTF_8.name());
out.write(jsonText);
} else {
out.write("\t{\n");
for (int i = 0; i < columns.length; i++) {
DBDAttributeBinding column = columns[i];
String columnName = column.getLabel();
if (CommonUtils.isEmpty(columnName)) {
columnName = column.getName();
}
out.write("\t\t\"" + JSONUtils.escapeJsonString(columnName) + "\" : ");
Object cellValue = row[column.getOrdinalPosition()];
if (DBUtils.isNullValue(cellValue)) {
writeTextCell(null);
} else if (cellValue instanceof DBDContent) {
// Content
// Inline textual content and handle binaries in some special way
DBDContent content = (DBDContent) cellValue;
try {
DBDContentStorage cs = content.getContents(session.getProgressMonitor());
if (cs != null) {
if (ContentUtils.isTextContent(content)) {
try (Reader in = cs.getContentReader()) {
out.write("\"");
writeCellValue(in);
out.write("\"");
}
} else {
getSite().writeBinaryData(cs);
}
}
} finally {
content.release();
}
} else {
if (cellValue instanceof Number || cellValue instanceof Boolean) {
out.write(cellValue.toString());
} else if (cellValue instanceof Date && formatDateISO) {
writeTextCell(JSONUtils.formatDate((Date) cellValue));
} else {
writeTextCell(super.getValueDisplayString(column, cellValue));
}
}
if (i < columns.length - 1) {
out.write(",");
}
out.write("\n");
}
out.write("\t}");
}
}
use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by serge-rider.
the class DataExporterTXT method exportRow.
@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) {
StringBuilder txt = new StringBuilder();
if (delimLeading)
txt.append("|");
for (int k = 0; k < columns.length; k++) {
if (k > 0)
txt.append("|");
DBDAttributeBinding attr = columns[k];
String displayString = getCellString(attr, row[k], DBDDisplayFormat.EDIT);
if (displayString.length() > colWidths[k]) {
displayString = CommonUtils.truncateString(displayString, colWidths[k]);
}
txt.append(displayString);
for (int j = colWidths[k] - displayString.length(); j > 0; j--) {
txt.append(" ");
}
}
if (delimTrailing)
txt.append("|");
txt.append("\n");
getWriter().print(txt);
}
Aggregations