use of org.jkiss.dbeaver.model.data.DBDDisplayFormat in project dbeaver by serge-rider.
the class PlainTextPresentation method printGrid.
private void printGrid(boolean append) {
int maxColumnSize = getController().getPreferenceStore().getInt(DBeaverPreferences.RESULT_TEXT_MAX_COLUMN_SIZE);
DBDDisplayFormat displayFormat = DBDDisplayFormat.safeValueOf(getController().getPreferenceStore().getString(DBeaverPreferences.RESULT_TEXT_VALUE_FORMAT));
StringBuilder grid = new StringBuilder(512);
ResultSetModel model = controller.getModel();
List<DBDAttributeBinding> attrs = model.getVisibleAttributes();
List<ResultSetRow> allRows = model.getAllRows();
if (colWidths == null) {
// Calculate column widths
colWidths = new int[attrs.size()];
for (int i = 0; i < attrs.size(); i++) {
DBDAttributeBinding attr = attrs.get(i);
colWidths[i] = getAttributeName(attr).length();
for (ResultSetRow row : allRows) {
String displayString = getCellString(model, attr, row, displayFormat);
colWidths[i] = Math.max(colWidths[i], displayString.length());
}
}
for (int i = 0; i < colWidths.length; i++) {
colWidths[i]++;
if (colWidths[i] > maxColumnSize) {
colWidths[i] = maxColumnSize;
}
}
}
if (!append) {
// Print header
for (int i = 0; i < attrs.size(); i++) {
DBDAttributeBinding attr = attrs.get(i);
String attrName = getAttributeName(attr);
grid.append(attrName);
for (int k = colWidths[i] - attrName.length(); k > 0; k--) {
grid.append(" ");
}
grid.append("|");
}
grid.append("\n");
// Print header
for (int i = 0; i < attrs.size(); i++) {
for (int k = colWidths[i]; k > 0; k--) {
grid.append("-");
}
grid.append("|");
}
grid.append("\n");
}
// Print rows
int firstRow = append ? totalRows : 0;
if (append) {
grid.append("\n");
}
for (int i = firstRow; i < allRows.size(); i++) {
ResultSetRow row = allRows.get(i);
for (int k = 0; k < attrs.size(); k++) {
DBDAttributeBinding attr = attrs.get(k);
String displayString = getCellString(model, attr, row, displayFormat);
if (displayString.length() >= colWidths[k] - 1) {
displayString = CommonUtils.truncateString(displayString, colWidths[k] - 1);
}
grid.append(displayString);
for (int j = colWidths[k] - displayString.length(); j > 0; j--) {
grid.append(" ");
}
grid.append("|");
}
grid.append("\n");
}
// cut last line feed
grid.setLength(grid.length() - 1);
if (append) {
text.append(grid.toString());
} else {
text.setText(grid.toString());
}
totalRows = allRows.size();
}
use of org.jkiss.dbeaver.model.data.DBDDisplayFormat in project dbeaver by serge-rider.
the class PlainTextPresentation method printRecord.
private void printRecord() {
DBDDisplayFormat displayFormat = DBDDisplayFormat.safeValueOf(getController().getPreferenceStore().getString(DBeaverPreferences.RESULT_TEXT_VALUE_FORMAT));
StringBuilder grid = new StringBuilder(512);
ResultSetModel model = controller.getModel();
List<DBDAttributeBinding> attrs = model.getVisibleAttributes();
String[] values = new String[attrs.size()];
ResultSetRow currentRow = controller.getCurrentRow();
// Calculate column widths
int nameWidth = 4, valueWidth = 5;
for (int i = 0; i < attrs.size(); i++) {
DBDAttributeBinding attr = attrs.get(i);
nameWidth = Math.max(nameWidth, getAttributeName(attr).length());
values[i] = attr.getValueHandler().getValueDisplayString(attr, model.getCellValue(attr, currentRow), displayFormat);
valueWidth = Math.max(valueWidth, values[i].length());
}
// Header
grid.append("Name");
for (int j = nameWidth - 4; j > 0; j--) {
grid.append(" ");
}
grid.append("|Value\n");
for (int j = 0; j < nameWidth; j++) grid.append("-");
grid.append("|");
for (int j = 0; j < valueWidth; j++) grid.append("-");
grid.append("\n");
// Values
for (int i = 0; i < attrs.size(); i++) {
DBDAttributeBinding attr = attrs.get(i);
String name = getAttributeName(attr);
grid.append(name);
for (int j = nameWidth - name.length(); j > 0; j--) {
grid.append(" ");
}
grid.append("|");
grid.append(values[i]);
grid.append("\n");
}
// cut last line feed
grid.setLength(grid.length() - 1);
text.setText(grid.toString());
}
Aggregations