Search in sources :

Example 11 with DBPDataKind

use of org.jkiss.dbeaver.model.DBPDataKind in project dbeaver by serge-rider.

the class PlainTextPresentation method printGrid.

private void printGrid(boolean append) {
    DBPPreferenceStore prefs = getController().getPreferenceStore();
    int maxColumnSize = prefs.getInt(ResultSetPreferences.RESULT_TEXT_MAX_COLUMN_SIZE);
    boolean delimLeading = prefs.getBoolean(ResultSetPreferences.RESULT_TEXT_DELIMITER_LEADING);
    boolean delimTrailing = prefs.getBoolean(ResultSetPreferences.RESULT_TEXT_DELIMITER_TRAILING);
    boolean extraSpaces = prefs.getBoolean(ResultSetPreferences.RESULT_TEXT_EXTRA_SPACES);
    this.showNulls = getController().getPreferenceStore().getBoolean(ResultSetPreferences.RESULT_TEXT_SHOW_NULLS);
    DBDDisplayFormat displayFormat = DBDDisplayFormat.safeValueOf(prefs.getString(ResultSetPreferences.RESULT_TEXT_VALUE_FORMAT));
    StringBuilder grid = new StringBuilder(512);
    ResultSetModel model = controller.getModel();
    List<DBDAttributeBinding> attrs = model.getVisibleAttributes();
    List<ResultSetRow> allRows = model.getAllRows();
    int extraSpacesNum = extraSpaces ? 2 : 0;
    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() + extraSpacesNum;
            if (showNulls && !attr.isRequired()) {
                colWidths[i] = Math.max(colWidths[i], DBConstants.NULL_VALUE_LABEL.length());
            }
            for (ResultSetRow row : allRows) {
                String displayString = getCellString(model, attr, row, displayFormat);
                colWidths[i] = Math.max(colWidths[i], getStringWidth(displayString) + extraSpacesNum);
            }
        }
        for (int i = 0; i < colWidths.length; i++) {
            if (colWidths[i] > maxColumnSize) {
                colWidths[i] = maxColumnSize;
            }
        }
    }
    if (!append) {
        // Print header
        if (delimLeading)
            grid.append("|");
        for (int i = 0; i < attrs.size(); i++) {
            if (i > 0)
                grid.append("|");
            if (extraSpaces)
                grid.append(" ");
            DBDAttributeBinding attr = attrs.get(i);
            String attrName = getAttributeName(attr);
            grid.append(attrName);
            for (int k = colWidths[i] - attrName.length() - extraSpacesNum; k > 0; k--) {
                grid.append(" ");
            }
            if (extraSpaces)
                grid.append(" ");
        }
        if (delimTrailing)
            grid.append("|");
        grid.append("\n");
        // Print header
        if (delimLeading)
            grid.append("|");
        for (int i = 0; i < attrs.size(); i++) {
            if (i > 0)
                grid.append("|");
            for (int k = colWidths[i]; k > 0; k--) {
                grid.append("-");
            }
        }
        if (delimTrailing)
            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);
        if (delimLeading)
            grid.append("|");
        for (int k = 0; k < attrs.size(); k++) {
            if (k > 0)
                grid.append("|");
            DBDAttributeBinding attr = attrs.get(k);
            String displayString = getCellString(model, attr, row, displayFormat);
            if (displayString.length() >= colWidths[k]) {
                displayString = CommonUtils.truncateString(displayString, colWidths[k]);
            }
            int stringWidth = getStringWidth(displayString);
            if (extraSpaces)
                grid.append(" ");
            DBPDataKind dataKind = attr.getDataKind();
            if ((dataKind == DBPDataKind.NUMERIC && rightJustifyNumbers) || (dataKind == DBPDataKind.DATETIME && rightJustifyDateTime)) {
                // Right justify value
                for (int j = colWidths[k] - stringWidth - extraSpacesNum; j > 0; j--) {
                    grid.append(" ");
                }
                grid.append(displayString);
            } else {
                grid.append(displayString);
                for (int j = colWidths[k] - stringWidth - extraSpacesNum; j > 0; j--) {
                    grid.append(" ");
                }
            }
            if (extraSpaces)
                grid.append(" ");
        }
        if (delimTrailing)
            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();
}
Also used : DBDDisplayFormat(org.jkiss.dbeaver.model.data.DBDDisplayFormat) DBPDataKind(org.jkiss.dbeaver.model.DBPDataKind) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBPPreferenceStore(org.jkiss.dbeaver.model.preferences.DBPPreferenceStore)

Aggregations

DBPDataKind (org.jkiss.dbeaver.model.DBPDataKind)11 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)4 DBDDisplayFormat (org.jkiss.dbeaver.model.data.DBDDisplayFormat)4 DBPPreferenceStore (org.jkiss.dbeaver.model.preferences.DBPPreferenceStore)4 DBException (org.jkiss.dbeaver.DBException)3 CSVReader (au.com.bytecode.opencsv.CSVReader)2 ArrayList (java.util.ArrayList)2 NotNull (org.jkiss.code.NotNull)2