Search in sources :

Example 11 with DBDLabelValuePair

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

the class GenericFilterValueEdit method setupTable.

void setupTable(Composite composite, int style, boolean visibleLines, boolean visibleHeader, Object layoutData) {
    tableViewer = new TableViewer(composite, style);
    Table table = this.tableViewer.getTable();
    table.setLinesVisible(false);
    table.setHeaderVisible(visibleHeader);
    table.setLayoutData(layoutData);
    this.tableViewer.setContentProvider(new ListContentProvider());
    isCheckedTable = (style & SWT.CHECK) == SWT.CHECK;
    if (isCheckedTable) {
        buttonsPanel = UIUtils.createComposite(composite, 2);
        buttonsPanel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        toggleButton = UIUtils.createDialogButton(buttonsPanel, "&Select All", new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                TableItem[] items = tableViewer.getTable().getItems();
                if (Boolean.FALSE.equals(toggleButton.getData())) {
                    // Clear all checked
                    for (TableItem item : items) {
                        item.setChecked(false);
                    }
                    toggleButton.setData(false);
                    savedValues.clear();
                } else {
                    for (TableItem item : items) {
                        item.setChecked(true);
                        savedValues.add((((DBDLabelValuePair) item.getData())).getValue());
                    }
                    toggleButton.setData(true);
                }
                updateToggleButton(toggleButton);
            }
        });
        toggleButton.setData(true);
        GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
        gd.widthHint = 120;
        toggleButton.setLayoutData(gd);
        UIUtils.createEmptyLabel(buttonsPanel, 1, 1).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        tableViewer.getTable().addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                if (e.detail == SWT.CHECK) {
                    DBDLabelValuePair value = (DBDLabelValuePair) e.item.getData();
                    if (((TableItem) e.item).getChecked()) {
                        savedValues.add(value.getValue());
                    } else {
                        savedValues.remove(value.getValue());
                    }
                    updateToggleButton(toggleButton);
                }
            }
        });
    }
}
Also used : ListContentProvider(org.jkiss.dbeaver.ui.controls.ListContentProvider) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) TableViewer(org.eclipse.jface.viewers.TableViewer) DBDLabelValuePair(org.jkiss.dbeaver.model.data.DBDLabelValuePair)

Example 12 with DBDLabelValuePair

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

the class GenericFilterValueEdit method loadMultiValueList.

private void loadMultiValueList(@NotNull Collection<DBDLabelValuePair> values, boolean mergeResultsWithData) {
    if (tableViewer == null || tableViewer.getControl() == null || tableViewer.getControl().isDisposed()) {
        return;
    }
    Pattern pattern = null;
    if (!CommonUtils.isEmpty(filterPattern) && attribute.getDataKind() == DBPDataKind.STRING) {
        pattern = Pattern.compile(SQLUtils.makeLikePattern("%" + filterPattern + "%"), Pattern.CASE_INSENSITIVE);
    }
    // Get all values from actual RSV data
    boolean hasNulls = false;
    Map<Object, DBDLabelValuePair> rowData = new HashMap<>();
    for (DBDLabelValuePair pair : values) {
        final DBDLabelValuePair oldLabel = rowData.get(pair.getValue());
        if (oldLabel != null) {
            // Duplicate label for single key - may happen in case of composite foreign keys
            String multiLabel = oldLabel.getLabel() + "," + pair.getLabel();
            if (multiLabel.length() > 200) {
                multiLabel = multiLabel.substring(0, 200) + MULTI_KEY_LABEL;
            }
            rowData.put(pair.getValue(), new DBDLabelValuePair(multiLabel, pair.getValue()));
        } else {
            rowData.put(pair.getValue(), pair);
        }
    }
    if (mergeResultsWithData) {
        // Add values from fetched rows
        for (ResultSetRow row : viewer.getModel().getAllRows()) {
            Object cellValue = viewer.getModel().getCellValue(attribute, row);
            if (DBUtils.isNullValue(cellValue)) {
                hasNulls = true;
                continue;
            }
            if (!keyPresents(rowData, cellValue)) {
                String itemString = attribute.getValueHandler().getValueDisplayString(attribute, cellValue, DBDDisplayFormat.UI);
                rowData.put(cellValue, new DBDLabelValuePair(itemString, cellValue));
            }
        }
    }
    List<DBDLabelValuePair> sortedList = new ArrayList<>(rowData.values());
    if (pattern != null) {
        for (Iterator<DBDLabelValuePair> iter = sortedList.iterator(); iter.hasNext(); ) {
            final DBDLabelValuePair valuePair = iter.next();
            String itemString = attribute.getValueHandler().getValueDisplayString(attribute, valuePair.getValue(), DBDDisplayFormat.UI);
            if (!pattern.matcher(itemString).matches() && (valuePair.getLabel() == null || !pattern.matcher(valuePair.getLabel()).matches())) {
                iter.remove();
            }
        }
    } else if (filterPattern != null && attribute.getDataKind() == DBPDataKind.NUMERIC) {
        // Filter numeric values
        double minValue = CommonUtils.toDouble(filterPattern);
        for (Iterator<DBDLabelValuePair> iter = sortedList.iterator(); iter.hasNext(); ) {
            final DBDLabelValuePair valuePair = iter.next();
            String itemString = attribute.getValueHandler().getValueDisplayString(attribute, valuePair.getValue(), DBDDisplayFormat.EDIT);
            double itemValue = CommonUtils.toDouble(itemString);
            if (itemValue < minValue) {
                iter.remove();
            }
        }
    }
    try {
        Collections.sort(sortedList);
    } catch (Exception e) {
        // FIXME: This may happen in some crazy cases -
        // FIXME: error "Comparison method violates its general contract!" happens in case of long strings sorting
        // FIXME: Test on sakila.film.description
        log.error("Error sorting value collection", e);
    }
    if (hasNulls) {
        boolean nullPresents = false;
        for (DBDLabelValuePair val : rowData.values()) {
            if (DBUtils.isNullValue(val.getValue())) {
                nullPresents = true;
                break;
            }
        }
        if (!nullPresents) {
            sortedList.add(0, new DBDLabelValuePair(DBValueFormatting.getDefaultValueDisplayString(null, DBDDisplayFormat.UI), null));
        }
    }
    Set<Object> checkedValues = new HashSet<>();
    for (ResultSetRow row : rows) {
        Object value = viewer.getModel().getCellValue(attribute, row);
        checkedValues.add(value);
    }
    DBDAttributeConstraint constraint = viewer.getModel().getDataFilter().getConstraint(attribute);
    if (constraint != null && constraint.getOperator() == DBCLogicalOperator.IN) {
        // checkedValues.add(constraint.getValue());
        if (constraint.getValue() instanceof Object[]) {
            Collections.addAll(checkedValues, (Object[]) constraint.getValue());
        }
    }
    checkedValues.addAll(savedValues);
    tableViewer.setInput(sortedList);
    DBDLabelValuePair firstVisibleItem = null;
    if (isCheckedTable)
        for (DBDLabelValuePair row : sortedList) {
            Object cellValue = row.getValue();
            if (checkedValues.contains(cellValue)) {
                TableItem t = (TableItem) tableViewer.testFindItem(row);
                t.setChecked(true);
                // ((CheckboxTableViewer) tableViewer).setChecked(row, true);
                if (firstVisibleItem == null) {
                    firstVisibleItem = row;
                }
            }
        }
    ViewerColumnController vcc = ViewerColumnController.getFromControl(tableViewer.getTable());
    if (vcc != null) {
        vcc.repackColumns();
    } else {
        UIUtils.packColumns(tableViewer.getTable(), true);
    }
    if (firstVisibleItem != null) {
        final Widget item = tableViewer.testFindItem(firstVisibleItem);
        if (item != null) {
            tableViewer.getTable().setSelection((TableItem) item);
            tableViewer.getTable().showItem((TableItem) item);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) ViewerColumnController(org.jkiss.dbeaver.ui.controls.ViewerColumnController) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) DBDLabelValuePair(org.jkiss.dbeaver.model.data.DBDLabelValuePair)

Example 13 with DBDLabelValuePair

use of org.jkiss.dbeaver.model.data.DBDLabelValuePair in project dbeaver by dbeaver.

the class SQLCompletionAnalyzer method makeProposalsFromAttributeValues.

private void makeProposalsFromAttributeValues(DBPDataSource dataSource, SQLWordPartDetector wordDetector, DBSEntity entity) throws DBException {
    List<String> prevWords = wordDetector.getPrevWords();
    if (!prevWords.isEmpty()) {
        // Column name?
        String columnName = prevWords.get(prevWords.size() - 1);
        if (!DBUtils.isQuotedIdentifier(dataSource, columnName)) {
            int divPos = columnName.indexOf(request.getContext().getSyntaxManager().getStructSeparator());
            if (divPos != -1) {
                columnName = columnName.substring(divPos + 1);
            }
        }
        columnName = DBUtils.getUnQuotedIdentifier(dataSource, columnName);
        DBSEntityAttribute attribute = entity.getAttribute(monitor, columnName);
        if (attribute instanceof DBSAttributeEnumerable) {
            try (DBCSession session = request.getContext().getExecutionContext().openSession(monitor, DBCExecutionPurpose.META, "Read attribute values")) {
                List<DBDLabelValuePair> valueEnumeration = ((DBSAttributeEnumerable) attribute).getValueEnumeration(session, null, MAX_ATTRIBUTE_VALUE_PROPOSALS, false);
                if (!valueEnumeration.isEmpty()) {
                    DBPImage attrImage = null;
                    for (DBDLabelValuePair valuePair : valueEnumeration) {
                        String sqlValue = SQLUtils.convertValueToSQL(dataSource, attribute, valuePair.getValue());
                        proposals.add(request.getContext().createProposal(request, CommonUtils.toString(valuePair.getValue()), sqlValue, sqlValue.length(), attrImage, DBPKeywordType.LITERAL, null, null, Collections.emptyMap()));
                    }
                }
            }
        }
    }
}
Also used : DBDLabelValuePair(org.jkiss.dbeaver.model.data.DBDLabelValuePair) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession)

Example 14 with DBDLabelValuePair

use of org.jkiss.dbeaver.model.data.DBDLabelValuePair in project dbeaver by dbeaver.

the class GenericFilterValueEdit method setupTable.

void setupTable(Composite composite, int style, boolean visibleLines, boolean visibleHeader, Object layoutData) {
    tableViewer = new TableViewer(composite, style);
    Table table = this.tableViewer.getTable();
    table.setLinesVisible(false);
    table.setHeaderVisible(visibleHeader);
    table.setLayoutData(layoutData);
    this.tableViewer.setContentProvider(new ListContentProvider());
    isCheckedTable = (style & SWT.CHECK) == SWT.CHECK;
    if (isCheckedTable) {
        buttonsPanel = UIUtils.createComposite(composite, 2);
        buttonsPanel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        toggleButton = UIUtils.createDialogButton(buttonsPanel, "&Select All", new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                TableItem[] items = tableViewer.getTable().getItems();
                if (Boolean.FALSE.equals(toggleButton.getData())) {
                    // Clear all checked
                    for (TableItem item : items) {
                        item.setChecked(false);
                    }
                    toggleButton.setData(false);
                    savedValues.clear();
                } else {
                    for (TableItem item : items) {
                        item.setChecked(true);
                        savedValues.add((((DBDLabelValuePair) item.getData())).getValue());
                    }
                    toggleButton.setData(true);
                }
                updateToggleButton(toggleButton);
            }
        });
        toggleButton.setData(true);
        GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
        gd.widthHint = 120;
        toggleButton.setLayoutData(gd);
        UIUtils.createEmptyLabel(buttonsPanel, 1, 1).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        tableViewer.getTable().addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                if (e.detail == SWT.CHECK) {
                    DBDLabelValuePair value = (DBDLabelValuePair) e.item.getData();
                    if (((TableItem) e.item).getChecked()) {
                        savedValues.add(value.getValue());
                    } else {
                        savedValues.remove(value.getValue());
                    }
                    updateToggleButton(toggleButton);
                }
            }
        });
    }
}
Also used : ListContentProvider(org.jkiss.dbeaver.ui.controls.ListContentProvider) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) TableViewer(org.eclipse.jface.viewers.TableViewer) DBDLabelValuePair(org.jkiss.dbeaver.model.data.DBDLabelValuePair)

Example 15 with DBDLabelValuePair

use of org.jkiss.dbeaver.model.data.DBDLabelValuePair in project dbeaver by dbeaver.

the class GenericFilterValueEdit method loadMultiValueList.

private void loadMultiValueList(@NotNull Collection<DBDLabelValuePair> values, boolean mergeResultsWithData) {
    if (tableViewer == null || tableViewer.getControl() == null || tableViewer.getControl().isDisposed()) {
        return;
    }
    Pattern pattern = null;
    if (!CommonUtils.isEmpty(filterPattern) && attribute.getDataKind() == DBPDataKind.STRING) {
        pattern = Pattern.compile(SQLUtils.makeLikePattern("%" + filterPattern + "%"), Pattern.CASE_INSENSITIVE);
    }
    // Get all values from actual RSV data
    boolean hasNulls = false;
    Map<Object, DBDLabelValuePair> rowData = new HashMap<>();
    for (DBDLabelValuePair pair : values) {
        final DBDLabelValuePair oldLabel = rowData.get(pair.getValue());
        if (oldLabel != null) {
            // Duplicate label for single key - may happen in case of composite foreign keys
            String multiLabel = oldLabel.getLabel() + "," + pair.getLabel();
            if (multiLabel.length() > 200) {
                multiLabel = multiLabel.substring(0, 200) + MULTI_KEY_LABEL;
            }
            rowData.put(pair.getValue(), new DBDLabelValuePair(multiLabel, pair.getValue()));
        } else {
            rowData.put(pair.getValue(), pair);
        }
    }
    if (mergeResultsWithData) {
        // Add values from fetched rows
        for (ResultSetRow row : viewer.getModel().getAllRows()) {
            Object cellValue = viewer.getModel().getCellValue(attribute, row);
            if (DBUtils.isNullValue(cellValue)) {
                hasNulls = true;
                continue;
            }
            if (!keyPresents(rowData, cellValue)) {
                String itemString = attribute.getValueHandler().getValueDisplayString(attribute, cellValue, DBDDisplayFormat.UI);
                rowData.put(cellValue, new DBDLabelValuePair(itemString, cellValue));
            }
        }
    }
    List<DBDLabelValuePair> sortedList = new ArrayList<>(rowData.values());
    if (pattern != null) {
        for (Iterator<DBDLabelValuePair> iter = sortedList.iterator(); iter.hasNext(); ) {
            final DBDLabelValuePair valuePair = iter.next();
            String itemString = attribute.getValueHandler().getValueDisplayString(attribute, valuePair.getValue(), DBDDisplayFormat.UI);
            if (!pattern.matcher(itemString).matches() && (valuePair.getLabel() == null || !pattern.matcher(valuePair.getLabel()).matches())) {
                iter.remove();
            }
        }
    } else if (filterPattern != null && attribute.getDataKind() == DBPDataKind.NUMERIC) {
        // Filter numeric values
        double minValue = CommonUtils.toDouble(filterPattern);
        for (Iterator<DBDLabelValuePair> iter = sortedList.iterator(); iter.hasNext(); ) {
            final DBDLabelValuePair valuePair = iter.next();
            String itemString = attribute.getValueHandler().getValueDisplayString(attribute, valuePair.getValue(), DBDDisplayFormat.EDIT);
            double itemValue = CommonUtils.toDouble(itemString);
            if (itemValue < minValue) {
                iter.remove();
            }
        }
    }
    try {
        Collections.sort(sortedList);
    } catch (Exception e) {
        // FIXME: This may happen in some crazy cases -
        // FIXME: error "Comparison method violates its general contract!" happens in case of long strings sorting
        // FIXME: Test on sakila.film.description
        log.error("Error sorting value collection", e);
    }
    if (hasNulls) {
        boolean nullPresents = false;
        for (DBDLabelValuePair val : rowData.values()) {
            if (DBUtils.isNullValue(val.getValue())) {
                nullPresents = true;
                break;
            }
        }
        if (!nullPresents) {
            sortedList.add(0, new DBDLabelValuePair(DBValueFormatting.getDefaultValueDisplayString(null, DBDDisplayFormat.UI), null));
        }
    }
    Set<Object> checkedValues = new HashSet<>();
    for (ResultSetRow row : rows) {
        Object value = viewer.getModel().getCellValue(attribute, row);
        checkedValues.add(value);
    }
    DBDAttributeConstraint constraint = viewer.getModel().getDataFilter().getConstraint(attribute);
    if (constraint != null && constraint.getOperator() == DBCLogicalOperator.IN) {
        // checkedValues.add(constraint.getValue());
        if (constraint.getValue() instanceof Object[]) {
            Collections.addAll(checkedValues, (Object[]) constraint.getValue());
        }
    }
    checkedValues.addAll(savedValues);
    tableViewer.setInput(sortedList);
    DBDLabelValuePair firstVisibleItem = null;
    if (isCheckedTable)
        for (DBDLabelValuePair row : sortedList) {
            Object cellValue = row.getValue();
            if (checkedValues.contains(cellValue)) {
                TableItem t = (TableItem) tableViewer.testFindItem(row);
                t.setChecked(true);
                // ((CheckboxTableViewer) tableViewer).setChecked(row, true);
                if (firstVisibleItem == null) {
                    firstVisibleItem = row;
                }
            }
        }
    ViewerColumnController vcc = ViewerColumnController.getFromControl(tableViewer.getTable());
    if (vcc != null) {
        vcc.repackColumns();
    } else {
        UIUtils.packColumns(tableViewer.getTable(), true);
    }
    if (firstVisibleItem != null) {
        final Widget item = tableViewer.testFindItem(firstVisibleItem);
        if (item != null) {
            tableViewer.getTable().setSelection((TableItem) item);
            tableViewer.getTable().showItem((TableItem) item);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) ViewerColumnController(org.jkiss.dbeaver.ui.controls.ViewerColumnController) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) DBDLabelValuePair(org.jkiss.dbeaver.model.data.DBDLabelValuePair)

Aggregations

DBDLabelValuePair (org.jkiss.dbeaver.model.data.DBDLabelValuePair)19 DBException (org.jkiss.dbeaver.DBException)8 GridData (org.eclipse.swt.layout.GridData)7 ViewerColumnController (org.jkiss.dbeaver.ui.controls.ViewerColumnController)6 ColumnLabelProvider (org.eclipse.jface.viewers.ColumnLabelProvider)5 Pattern (java.util.regex.Pattern)4 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)4 SelectionEvent (org.eclipse.swt.events.SelectionEvent)4 TableItem (org.eclipse.swt.widgets.TableItem)4 java.util (java.util)3 ListContentProvider (org.jkiss.dbeaver.ui.controls.ListContentProvider)3 ResultSetRow (org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Action (org.eclipse.jface.action.Action)2 TableViewer (org.eclipse.jface.viewers.TableViewer)2 TableViewerColumn (org.eclipse.jface.viewers.TableViewerColumn)2 Nullable (org.jkiss.code.Nullable)2 DBDAttributeConstraint (org.jkiss.dbeaver.model.data.DBDAttributeConstraint)2 DBCSession (org.jkiss.dbeaver.model.exec.DBCSession)2 DBSEntityAssociation (org.jkiss.dbeaver.model.struct.DBSEntityAssociation)2