Search in sources :

Example 6 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) {
    Pattern pattern = null;
    if (!CommonUtils.isEmpty(filterPattern)) {
        pattern = Pattern.compile(SQLUtils.makeLikePattern("%" + filterPattern + "%"), Pattern.CASE_INSENSITIVE);
    }
    // Get all values from actual RSV data
    boolean hasNulls = false;
    java.util.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);
        }
    }
    // Add values from fetched rows
    for (ResultSetRow row : viewer.getModel().getAllRows()) {
        Object cellValue = viewer.getModel().getCellValue(attr, row);
        if (DBUtils.isNullValue(cellValue)) {
            hasNulls = true;
            continue;
        }
        if (!rowData.containsKey(cellValue)) {
            String itemString = attr.getValueHandler().getValueDisplayString(attr, cellValue, DBDDisplayFormat.UI);
            rowData.put(cellValue, new DBDLabelValuePair(itemString, cellValue));
        }
    }
    java.util.List<DBDLabelValuePair> sortedList = new ArrayList<>(rowData.values());
    Collections.sort(sortedList);
    if (pattern != null) {
        for (Iterator<DBDLabelValuePair> iter = sortedList.iterator(); iter.hasNext(); ) {
            final DBDLabelValuePair valuePair = iter.next();
            String itemString = attr.getValueHandler().getValueDisplayString(attr, valuePair.getValue(), DBDDisplayFormat.UI);
            if (!pattern.matcher(itemString).matches() && (valuePair.getLabel() == null || !pattern.matcher(valuePair.getLabel()).matches())) {
                iter.remove();
            }
        }
    }
    Collections.sort(sortedList);
    if (hasNulls) {
        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(attr, row);
        checkedValues.add(value);
    }
    table.setInput(sortedList);
    DBDLabelValuePair firstVisibleItem = null;
    if (isCheckedTable)
        for (DBDLabelValuePair row : sortedList) {
            Object cellValue = row.getValue();
            if (checkedValues.contains(cellValue)) {
                TableItem t = (TableItem) table.testFindItem(row);
                t.setChecked(true);
                // ((CheckboxTableViewer) table).setChecked(row, true);
                if (firstVisibleItem == null) {
                    firstVisibleItem = row;
                }
            }
        }
    ViewerColumnController vcc = ViewerColumnController.getFromControl(table.getTable());
    if (vcc != null)
        vcc.repackColumns();
    if (firstVisibleItem != null) {
        final Widget item = table.testFindItem(firstVisibleItem);
        if (item != null) {
            table.getTable().setSelection((TableItem) item);
            table.getTable().showItem((TableItem) item);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) ViewerColumnController(org.jkiss.dbeaver.ui.controls.ViewerColumnController) TableItem(org.eclipse.swt.widgets.TableItem) Widget(org.eclipse.swt.widgets.Widget) ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) java.util(java.util) DBDLabelValuePair(org.jkiss.dbeaver.model.data.DBDLabelValuePair)

Example 7 with DBDLabelValuePair

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

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 8 with DBDLabelValuePair

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

the class FilterValueEditPopup method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    DBSEntityReferrer descReferrer = ResultSetUtils.getEnumerableConstraint(filter.getAttribute());
    Composite group = (Composite) super.createDialogArea(parent);
    {
        Composite labelComposite = UIUtils.createComposite(group, 2);
        labelComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        Label controlLabel = UIUtils.createControlLabel(labelComposite, "Choose value(s) to filter by");
        controlLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        if (descReferrer instanceof DBSEntityAssociation) {
            Link hintLabel = UIUtils.createLink(labelComposite, "(<a>Define Description</a>)", new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    EditDictionaryPage editDictionaryPage = new EditDictionaryPage(((DBSEntityAssociation) descReferrer).getAssociatedEntity());
                    if (editDictionaryPage.edit(parent.getShell())) {
                        filter.loadValues(null);
                    }
                }
            });
            hintLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
        } else {
            UIUtils.createEmptyLabel(labelComposite, 1, 1);
        }
    }
    Text filterTextbox = filter.addFilterTextbox(group);
    filterTextbox.setFocus();
    filterTextbox.addTraverseListener(e -> {
        Table table = filter.getTableViewer().getTable();
        if (e.detail == SWT.TRAVERSE_ARROW_PREVIOUS || e.detail == SWT.TRAVERSE_ARROW_NEXT) {
            if (table.getSelectionIndex() < 0 && table.getItemCount() > 0) {
                table.setSelection(0);
            }
            table.setFocus();
        } else if (e.detail == SWT.TRAVERSE_RETURN) {
            applyFilterValue();
        }
    });
    UIUtils.addEmptyTextHint(filterTextbox, text -> "Type partial value to search");
    Composite tableComposite = UIUtils.createComposite(group, 1);
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.widthHint = 400;
    gd.heightHint = 300;
    tableComposite.setLayoutData(gd);
    filter.setupTable(tableComposite, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL | (filter.getOperator() == DBCLogicalOperator.IN ? SWT.CHECK : SWT.NONE), true, descReferrer != null, new GridData(GridData.FILL_BOTH));
    Table table = filter.getTableViewer().getTable();
    TableViewerColumn resultsetColumn = new TableViewerColumn(filter.getTableViewer(), UIUtils.createTableColumn(table, SWT.NONE, "Value"));
    resultsetColumn.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            return filter.getAttribute().getValueHandler().getValueDisplayString(filter.getAttribute(), ((DBDLabelValuePair) element).getValue(), DBDDisplayFormat.UI);
        }
    });
    TableViewerColumn descColumn;
    if (descReferrer != null) {
        descColumn = new TableViewerColumn(filter.getTableViewer(), UIUtils.createTableColumn(table, SWT.NONE, "Description"));
        descColumn.setLabelProvider(new ColumnLabelProvider() {

            @Override
            public String getText(Object element) {
                return ((DBDLabelValuePair) element).getLabel();
            }
        });
    }
    filter.getTableViewer().addSelectionChangedListener(event -> {
        value = filter.getFilterValue();
    // okPressed();
    });
    filter.getTableViewer().addDoubleClickListener(event -> applyFilterValue());
    filter.setFilterPattern(null);
    filter.loadValues(() -> UIUtils.asyncExec(() -> UIUtils.packColumns(table, false)));
    filter.createFilterButton(ResultSetMessages.sql_editor_resultset_filter_panel_btn_apply, new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            applyFilterValue();
        }
    });
    closeOnFocusLost(filterTextbox, table);
    return tableComposite;
}
Also used : SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) EditDictionaryPage(org.jkiss.dbeaver.ui.editors.object.struct.EditDictionaryPage) DBSEntityAssociation(org.jkiss.dbeaver.model.struct.DBSEntityAssociation) DBSEntityReferrer(org.jkiss.dbeaver.model.struct.DBSEntityReferrer) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) TableViewerColumn(org.eclipse.jface.viewers.TableViewerColumn) DBDLabelValuePair(org.jkiss.dbeaver.model.data.DBDLabelValuePair)

Example 9 with DBDLabelValuePair

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

the class FilterValueEditDialog method createMultiValueSelector.

private void createMultiValueSelector(Composite composite) {
    GridData layoutData = new GridData(GridData.FILL_BOTH);
    layoutData.widthHint = 400;
    layoutData.heightHint = 300;
    handler.setupTable(composite, SWT.BORDER | SWT.MULTI | SWT.CHECK | SWT.FULL_SELECTION, true, true, layoutData);
    ViewerColumnController columnController = new ViewerColumnController(getClass().getName(), handler.getTableViewer());
    columnController.addColumn("Value", "Value", SWT.LEFT, true, true, new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            return handler.getAttribute().getValueHandler().getValueDisplayString(handler.getAttribute(), ((DBDLabelValuePair) element).getValue(), DBDDisplayFormat.UI);
        }
    });
    columnController.addColumn("Description", "Row description (composed from dictionary columns)", SWT.LEFT, true, true, new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            return ((DBDLabelValuePair) element).getLabel();
        }
    });
    columnController.createColumns(true);
    Action[] elements = new Action[] { new Action("Select &All") {

        @Override
        public void run() {
            for (TableItem item : handler.getTableViewer().getTable().getItems()) {
                item.setChecked(true);
            }
        }
    }, new Action("Select &None") {

        @Override
        public void run() {
            for (TableItem item : handler.getTableViewer().getTable().getItems()) {
                item.setChecked(false);
            }
        }
    } };
    handler.addContextMenu(elements);
    handler.addFilterTextbox(composite);
    handler.setFilterPattern(null);
    handler.loadValues(null);
    columnController.createColumns(true);
}
Also used : ViewerColumnController(org.jkiss.dbeaver.ui.controls.ViewerColumnController) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) Action(org.eclipse.jface.action.Action) TableItem(org.eclipse.swt.widgets.TableItem) GridData(org.eclipse.swt.layout.GridData) DBDLabelValuePair(org.jkiss.dbeaver.model.data.DBDLabelValuePair)

Example 10 with DBDLabelValuePair

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

the class GenericFilterValueEdit method getFilterValue.

@Nullable
public Object getFilterValue() {
    if (tableViewer != null) {
        Set<Object> values = new LinkedHashSet<>();
        for (DBDLabelValuePair item : getMultiValues()) {
            if (((TableItem) tableViewer.testFindItem(item)).getChecked()) {
                values.add(item.getValue());
            }
        }
        values.addAll(savedValues);
        return values.toArray();
    } else if (editor != null) {
        try {
            return editor.extractEditorValue();
        } catch (DBException e) {
            log.error("Can't get editor value", e);
        }
    }
    return null;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDLabelValuePair(org.jkiss.dbeaver.model.data.DBDLabelValuePair) Nullable(org.jkiss.code.Nullable)

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