Search in sources :

Example 16 with SmartTextContentAdapter

use of org.jkiss.dbeaver.ui.contentassist.SmartTextContentAdapter in project dbeaver by dbeaver.

the class StringEditorTable method createEditableList.

public static Table createEditableList(@NotNull Composite parent, @NotNull String name, @Nullable List<String> values, @Nullable DBPImage icon, @Nullable IContentProposalProvider proposalProvider) {
    Group group = UIUtils.createControlGroup(parent, name, 2, GridData.FILL_BOTH, 0);
    final Table valueTable = new Table(group, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    final GridData gd = new GridData(GridData.FILL_BOTH);
    gd.widthHint = 300;
    gd.heightHint = 100;
    valueTable.setLayoutData(gd);
    // valueTable.setHeaderVisible(true);
    valueTable.setLinesVisible(true);
    final TableColumn valueColumn = UIUtils.createTableColumn(valueTable, SWT.LEFT, UIMessages.properties_value);
    valueTable.addControlListener(new ControlAdapter() {

        @Override
        public void controlResized(ControlEvent e) {
            valueColumn.setWidth(valueTable.getClientArea().width);
        }
    });
    fillFilterValues(valueTable, values, icon);
    final CustomTableEditor tableEditor = new CustomTableEditor(valueTable) {

        {
            firstTraverseIndex = 0;
            lastTraverseIndex = 0;
        // editOnEnter = false;
        }

        @Override
        protected Control createEditor(Table table, int index, TableItem item) {
            Text editor = new Text(table, SWT.BORDER);
            editor.setText(item.getText());
            editor.addModifyListener(e -> {
                // Save value immediately. This solves MacOS problems with focus events.
                saveEditorValue(editor, index, item);
            });
            if (proposalProvider != null) {
                setProposalAdapter(ContentAssistUtils.installContentProposal(editor, new SmartTextContentAdapter(), proposalProvider));
            }
            return editor;
        }

        @Override
        protected void saveEditorValue(Control control, int index, TableItem item) {
            item.setText(((Text) control).getText().trim());
        }
    };
    Composite buttonsGroup = UIUtils.createPlaceholder(group, 1, 5);
    buttonsGroup.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
    final Button addButton = new Button(buttonsGroup, SWT.PUSH);
    addButton.setText(UIMessages.button_add);
    addButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Button removeButton = new Button(buttonsGroup, SWT.PUSH);
    removeButton.setText(UIMessages.button_remove);
    removeButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    removeButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            int selectionIndex = valueTable.getSelectionIndex();
            if (selectionIndex >= 0) {
                tableEditor.closeEditor();
                valueTable.remove(selectionIndex);
                removeButton.setEnabled(valueTable.getSelectionIndex() >= 0);
            }
        }
    });
    removeButton.setEnabled(false);
    final Button clearButton = new Button(buttonsGroup, SWT.PUSH);
    clearButton.setText(UIMessages.button_clear);
    clearButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    clearButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            tableEditor.closeEditor();
            valueTable.removeAll();
            removeButton.setEnabled(false);
        }
    });
    addButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            TableItem newItem = new TableItem(valueTable, SWT.LEFT);
            if (icon != null) {
                newItem.setImage(DBeaverIcons.getImage(icon));
            }
            valueTable.setSelection(newItem);
            tableEditor.closeEditor();
            tableEditor.showEditor(newItem);
            removeButton.setEnabled(true);
        }
    });
    valueTable.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            int selectionIndex = valueTable.getSelectionIndex();
            removeButton.setEnabled(selectionIndex >= 0);
        }
    });
    return valueTable;
}
Also used : SmartTextContentAdapter(org.jkiss.dbeaver.ui.contentassist.SmartTextContentAdapter) ControlAdapter(org.eclipse.swt.events.ControlAdapter) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ControlEvent(org.eclipse.swt.events.ControlEvent)

Aggregations

SmartTextContentAdapter (org.jkiss.dbeaver.ui.contentassist.SmartTextContentAdapter)16 GridData (org.eclipse.swt.layout.GridData)14 StringContentProposalProvider (org.jkiss.dbeaver.ui.contentassist.StringContentProposalProvider)14 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)10 SelectionEvent (org.eclipse.swt.events.SelectionEvent)10 SWT (org.eclipse.swt.SWT)8 UIUtils (org.jkiss.dbeaver.ui.UIUtils)8 ContentAssistUtils (org.jkiss.dbeaver.ui.contentassist.ContentAssistUtils)8 GeneralUtils (org.jkiss.dbeaver.utils.GeneralUtils)8 CommonUtils (org.jkiss.utils.CommonUtils)8 org.eclipse.swt.widgets (org.eclipse.swt.widgets)6 DialogUtils (org.jkiss.dbeaver.ui.dialogs.DialogUtils)6 File (java.io.File)4 SelectionListener (org.eclipse.swt.events.SelectionListener)4 NativeToolUtils (org.jkiss.dbeaver.tasks.nativetool.NativeToolUtils)4 ArrayList (java.util.ArrayList)2 Comparator (java.util.Comparator)2 List (java.util.List)2 Locale (java.util.Locale)2 JexlExpression (org.apache.commons.jexl3.JexlExpression)2