Search in sources :

Example 11 with ListContentProvider

use of org.jkiss.dbeaver.ui.controls.ListContentProvider in project dbeaver by serge-rider.

the class NodeListControl method createContentProvider.

private static IContentProvider createContentProvider(DBNNode node, DBXTreeNode metaNode) {
    if (node instanceof DBNDatabaseNode) {
        final DBNDatabaseNode dbNode = (DBNDatabaseNode) node;
        if (metaNode == null) {
            metaNode = dbNode.getMeta();
        }
        final List<DBXTreeNode> inlineMetas = collectInlineMetas(dbNode, metaNode);
        if (!inlineMetas.isEmpty()) {
            return new TreeContentProvider() {

                @Override
                public boolean hasChildren(Object parentElement) {
                    return parentElement instanceof DBNDatabaseNode && ((DBNDatabaseNode) parentElement).hasChildren(false);
                }

                @Override
                public Object[] getChildren(Object parentElement) {
                    if (parentElement instanceof DBNDatabaseNode) {
                        try {
                            // Read children with void progress monitor because inline children SHOULD be already cached
                            DBNNode[] children = DBNUtils.getNodeChildrenFiltered(new VoidProgressMonitor(), (DBNDatabaseNode) parentElement, false);
                            if (ArrayUtils.isEmpty(children)) {
                                return null;
                            } else {
                                return children;
                            }
                        } catch (DBException e) {
                            log.error(e);
                        }
                    }
                    return null;
                }
            };
        }
    }
    return new ListContentProvider();
}
Also used : TreeContentProvider(org.jkiss.dbeaver.ui.controls.TreeContentProvider) DBXTreeNode(org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode) DBException(org.jkiss.dbeaver.DBException) ListContentProvider(org.jkiss.dbeaver.ui.controls.ListContentProvider) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPObject(org.jkiss.dbeaver.model.DBPObject) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)

Example 12 with ListContentProvider

use of org.jkiss.dbeaver.ui.controls.ListContentProvider in project dbeaver by serge-rider.

the class PostgreCreateExtensionDialog method createDialogArea.

@Override
protected Composite createDialogArea(Composite parent) {
    final Composite composite = super.createDialogArea(parent);
    final Composite group = new Composite(composite, SWT.NONE);
    group.setLayout(new GridLayout(2, false));
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.widthHint = 600;
    gd.heightHint = 200;
    gd.verticalIndent = 0;
    gd.horizontalIndent = 0;
    group.setLayoutData(gd);
    // $NON-NLS-2$
    final Text databaseText = UIUtils.createLabelText(group, PostgreMessages.dialog_create_extension_database, newextension.getDatabase().getName(), SWT.BORDER | SWT.READ_ONLY);
    final Combo schemaCombo = UIUtils.createLabelCombo(group, PostgreMessages.dialog_create_extension_schema, SWT.BORDER | SWT.DROP_DOWN | SWT.READ_ONLY);
    final Label lblExtension = UIUtils.createLabel(group, PostgreMessages.dialog_create_extension_name);
    extTable = new TableViewer(group, SWT.BORDER | SWT.UNDERLINE_SINGLE | SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);
    {
        final Table table = extTable.getTable();
        table.setLayoutData(new GridData(GridData.FILL_BOTH));
        table.setLinesVisible(true);
        table.setHeaderVisible(true);
        table.addControlListener(new ControlAdapter() {

            @Override
            public void controlResized(ControlEvent e) {
                UIUtils.packColumns(table);
                UIUtils.maxTableColumnsWidth(table);
                table.removeControlListener(this);
            }
        });
    }
    ViewerColumnController columnController = new ViewerColumnController("AvailabelExtensionDialog", extTable);
    columnController.addColumn(PostgreMessages.dialog_create_extension_column_name, null, SWT.NONE, true, true, new CellLabelProvider() {

        @Override
        public void update(ViewerCell cell) {
            cell.setText(((PostgreAvailableExtension) cell.getElement()).getName());
        }
    });
    columnController.addColumn(PostgreMessages.dialog_create_extension_column_version, null, SWT.NONE, true, true, new CellLabelProvider() {

        @Override
        public void update(ViewerCell cell) {
            cell.setText(((PostgreAvailableExtension) cell.getElement()).getVersion());
        }
    });
    columnController.addColumn(PostgreMessages.dialog_create_extension_column_description, null, SWT.NONE, true, true, new CellLabelProvider() {

        @Override
        public void update(ViewerCell cell) {
            cell.setText(((PostgreAvailableExtension) cell.getElement()).getDescription());
        }
    });
    columnController.createColumns();
    extTable.addSelectionChangedListener(event -> {
        IStructuredSelection selection = (IStructuredSelection) event.getSelection();
        if (!selection.isEmpty()) {
            // installed.get(extensionCombo.getSelectionIndex());
            extension = (PostgreAvailableExtension) selection.getFirstElement();
            checkEnabled();
        }
    });
    extTable.setContentProvider(new ListContentProvider());
    schemaCombo.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            schema = allSchemas.get(schemaCombo.getSelectionIndex());
            checkEnabled();
        }
    });
    new AbstractJob("Load schemas") {

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            try {
                allSchemas = new ArrayList<>(newextension.getDatabase().getSchemas(monitor));
                UIUtils.syncExec(() -> {
                    for (PostgreSchema schema : allSchemas) {
                        schemaCombo.add(schema.getName());
                    }
                    schema = DBUtils.findObject(allSchemas, PostgreConstants.PUBLIC_SCHEMA_NAME);
                    if (schema != null) {
                        schemaCombo.setText(schema.getName());
                    }
                });
            } catch (DBException e) {
                return GeneralUtils.makeExceptionStatus(e);
            }
            return Status.OK_STATUS;
        }
    }.schedule();
    new AbstractJob("Load available extensions") {

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            try {
                final List<PostgreAvailableExtension> installed = new ArrayList<>(newextension.getDatabase().getAvailableExtensions(monitor));
                UIUtils.syncExec(() -> {
                    extTable.setInput(installed);
                });
            } catch (DBException e) {
                return GeneralUtils.makeExceptionStatus(e);
            }
            return Status.OK_STATUS;
        }
    }.schedule();
    return composite;
}
Also used : PostgreAvailableExtension(org.jkiss.dbeaver.ext.postgresql.model.PostgreAvailableExtension) ViewerColumnController(org.jkiss.dbeaver.ui.controls.ViewerColumnController) DBException(org.jkiss.dbeaver.DBException) IStatus(org.eclipse.core.runtime.IStatus) ControlAdapter(org.eclipse.swt.events.ControlAdapter) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) GridLayout(org.eclipse.swt.layout.GridLayout) ListContentProvider(org.jkiss.dbeaver.ui.controls.ListContentProvider) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ArrayList(java.util.ArrayList) List(java.util.List) CellLabelProvider(org.eclipse.jface.viewers.CellLabelProvider) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ViewerCell(org.eclipse.jface.viewers.ViewerCell) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) GridData(org.eclipse.swt.layout.GridData) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) ControlEvent(org.eclipse.swt.events.ControlEvent) TableViewer(org.eclipse.jface.viewers.TableViewer) PostgreSchema(org.jkiss.dbeaver.ext.postgresql.model.PostgreSchema)

Example 13 with ListContentProvider

use of org.jkiss.dbeaver.ui.controls.ListContentProvider in project dbeaver by serge-rider.

the class DashboardAddDialog method createDialogArea.

@Override
protected Composite createDialogArea(Composite parent) {
    Composite dialogArea = super.createDialogArea(parent);
    // AdvancedListViewer listViewer = new AdvancedListViewer(dialogArea, SWT.NONE);
    // listViewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
    TableViewer dashboardTable = new TableViewer(dialogArea, SWT.BORDER | SWT.FULL_SELECTION);
    dashboardTable.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
    Table table = dashboardTable.getTable();
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.widthHint = 400;
    gd.heightHint = 200;
    table.setLayoutData(gd);
    table.setHeaderVisible(true);
    UIUtils.createTableColumn(table, SWT.LEFT, UIDashboardMessages.dialog_add_dashboard_column_name);
    UIUtils.createTableColumn(table, SWT.LEFT, UIDashboardMessages.dialog_add_dashboard_column_description);
    dashboardTable.setLabelProvider(new CellLabelProvider() {

        @Override
        public void update(ViewerCell cell) {
            DashboardDescriptor dashboardDescriptor = (DashboardDescriptor) cell.getElement();
            if (cell.getColumnIndex() == 0) {
                cell.setText(dashboardDescriptor.getName());
            } else {
                cell.setText(CommonUtils.notEmpty(dashboardDescriptor.getDescription()));
            }
        }
    });
    dashboardTable.addDoubleClickListener(event -> {
        if (!dashboardTable.getSelection().isEmpty()) {
            okPressed();
        }
    });
    dashboardTable.addSelectionChangedListener(event -> {
        ISelection selection = dashboardTable.getSelection();
        getButton(IDialogConstants.OK_ID).setEnabled(!selection.isEmpty());
        if (selection instanceof IStructuredSelection) {
            selectedDashboard = (DashboardDescriptor) ((IStructuredSelection) selection).getFirstElement();
        }
        getButton(IDialogConstants.OK_ID).setEnabled(selectedDashboard != null);
    });
    table.addPaintListener(e -> {
        if (table.getItemCount() == 0) {
            final String dbmsName = viewConfiguration.getDataSourceContainer().getDriver().getName();
            final String msg = NLS.bind(UIDashboardMessages.dialog_add_dashboard_message_no_more_dashboards_for, dbmsName);
            UIUtils.drawMessageOverControl(table, e, msg, 0);
        }
    });
    dashboardTable.setContentProvider(new ListContentProvider());
    java.util.List<DashboardDescriptor> dashboards = new ArrayList<>(DashboardRegistry.getInstance().getDashboards(viewConfiguration.getDataSourceContainer(), false));
    dashboards.removeIf(descriptor -> viewConfiguration.getDashboardConfig(descriptor.getId()) != null);
    dashboardTable.setInput(dashboards);
    UIUtils.asyncExec(() -> UIUtils.packColumns(table, true));
    return dialogArea;
}
Also used : Table(org.eclipse.swt.widgets.Table) Composite(org.eclipse.swt.widgets.Composite) ArrayList(java.util.ArrayList) ListContentProvider(org.jkiss.dbeaver.ui.controls.ListContentProvider) DashboardDescriptor(org.jkiss.dbeaver.ui.dashboard.registry.DashboardDescriptor) GridData(org.eclipse.swt.layout.GridData)

Example 14 with ListContentProvider

use of org.jkiss.dbeaver.ui.controls.ListContentProvider in project dbeaver by dbeaver.

the class SQLScriptTaskPageSettings method createControl.

@Override
public void createControl(Composite parent) {
    initializeDialogUnits(parent);
    Composite composite = UIUtils.createComposite(parent, 1);
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));
    SQLScriptExecuteSettings dtSettings = getWizard().getSettings();
    SashForm mainGroup = new SashForm(composite, SWT.NONE);
    mainGroup.setSashWidth(5);
    mainGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
    DBNProject projectNode = DBWorkbench.getPlatform().getNavigatorModel().getRoot().getProjectNode(sqlWizard.getProject());
    {
        Composite filesGroup = UIUtils.createControlGroup(mainGroup, DTMessages.sql_script_task_page_settings_group_files, 2, GridData.FILL_BOTH, 0);
        scriptsViewer = new TableViewer(filesGroup, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
        scriptsViewer.setContentProvider(new ListContentProvider());
        scriptsViewer.getTable().setHeaderVisible(true);
        scriptsViewer.setLabelProvider(new ColumnLabelProvider() {

            @Override
            public String getText(Object element) {
                return ((DBNResource) element).getResource().getProjectRelativePath().toString();
            }

            @Override
            public Image getImage(Object element) {
                return DBeaverIcons.getImage(((DBNResource) element).getNodeIconDefault());
            }
        });
        scriptsViewer.addDoubleClickListener(event -> {
            StructuredSelection selection = (StructuredSelection) event.getSelection();
            IResource resource = ((DBNResource) selection.getFirstElement()).getResource();
            if (resource != null) {
                DBPResourceHandler handler = DBWorkbench.getPlatform().getWorkspace().getResourceHandler(resource);
                if (handler != null) {
                    try {
                        handler.openResource(resource);
                    } catch (Exception e) {
                        log.error("Failed to open resource " + resource, e);
                    }
                }
            }
        });
        // GridData gd = new GridData(GridData.FILL_BOTH);
        // gd.heightHint = 300;
        // gd.widthHint = 400;
        // scriptsViewer.getTable().setLayoutData(gd);
        SQLScriptTaskScriptSelectorDialog.createScriptColumns(scriptsViewer);
        final Table scriptTable = scriptsViewer.getTable();
        scriptTable.setLayoutData(new GridData(GridData.FILL_BOTH));
        ToolBar buttonsToolbar = new ToolBar(filesGroup, SWT.VERTICAL);
        buttonsToolbar.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
        UIUtils.createToolItem(buttonsToolbar, DTUIMessages.sql_script_task_page_settings_tool_item_text_add_script, UIIcon.ROW_ADD, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                SQLScriptTaskScriptSelectorDialog dialog = new SQLScriptTaskScriptSelectorDialog(getShell(), projectNode);
                if (dialog.open() == IDialogConstants.OK_ID) {
                    for (DBNResource script : dialog.getSelectedScripts()) {
                        if (!selectedScripts.contains(script)) {
                            selectedScripts.add(script);
                        }
                    }
                    refreshScripts();
                }
            }
        });
        ToolItem deleteItem = UIUtils.createToolItem(buttonsToolbar, DTUIMessages.sql_script_task_page_settings_tool_item_text_remove_script, UIIcon.ROW_DELETE, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                ISelection selection = scriptsViewer.getSelection();
                if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
                    for (Object element : ((IStructuredSelection) selection).toArray()) {
                        if (element instanceof DBNResource) {
                            selectedScripts.remove(element);
                        }
                    }
                    refreshScripts();
                }
            }
        });
        UIUtils.createToolBarSeparator(buttonsToolbar, SWT.HORIZONTAL);
        ToolItem moveUpItem = UIUtils.createToolItem(buttonsToolbar, DTUIMessages.sql_script_task_page_settings_tool_item_text_move_script_up, UIIcon.ARROW_UP, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                int selectionIndex = scriptTable.getSelectionIndex();
                if (selectionIndex > 0) {
                    DBNResource prevScript = selectedScripts.get(selectionIndex - 1);
                    selectedScripts.set(selectionIndex - 1, selectedScripts.get(selectionIndex));
                    selectedScripts.set(selectionIndex, prevScript);
                    refreshScripts();
                }
            }
        });
        ToolItem moveDownItem = UIUtils.createToolItem(buttonsToolbar, DTUIMessages.sql_script_task_page_settings_tool_item_text_move_script_down, UIIcon.ARROW_DOWN, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                int selectionIndex = scriptTable.getSelectionIndex();
                if (selectionIndex < scriptTable.getItemCount() - 1) {
                    DBNResource nextScript = selectedScripts.get(selectionIndex + 1);
                    selectedScripts.set(selectionIndex + 1, selectedScripts.get(selectionIndex));
                    selectedScripts.set(selectionIndex, nextScript);
                    refreshScripts();
                }
            }
        });
        scriptsViewer.addSelectionChangedListener(event -> {
            int selectionIndex = scriptTable.getSelectionIndex();
            deleteItem.setEnabled(selectionIndex >= 0);
            moveUpItem.setEnabled(selectionIndex > 0);
            moveDownItem.setEnabled(selectionIndex < scriptTable.getItemCount() - 1);
        });
        deleteItem.setEnabled(false);
    }
    {
        Composite connectionsGroup = UIUtils.createControlGroup(mainGroup, DTMessages.sql_script_task_page_settings_group_connections, 2, GridData.FILL_BOTH, 0);
        dataSourceViewer = new TableViewer(connectionsGroup, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
        dataSourceViewer.setContentProvider(new ListContentProvider());
        // dataSourceViewer.getTable().setHeaderVisible(true);
        dataSourceViewer.setLabelProvider(new ColumnLabelProvider() {

            @Override
            public String getText(Object element) {
                return ((DBNDataSource) element).getNodeName();
            }

            @Override
            public Image getImage(Object element) {
                return DBeaverIcons.getImage(((DBNDataSource) element).getNodeIcon());
            }
        });
        GridData gd = new GridData(GridData.FILL_BOTH);
        gd.heightHint = 300;
        gd.widthHint = 400;
        dataSourceViewer.getTable().setLayoutData(gd);
        final Table dsTable = dataSourceViewer.getTable();
        dsTable.setLayoutData(new GridData(GridData.FILL_BOTH));
        ToolBar buttonsToolbar = new ToolBar(connectionsGroup, SWT.VERTICAL);
        buttonsToolbar.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
        UIUtils.createToolItem(buttonsToolbar, DTUIMessages.sql_script_task_page_settings_tool_item_text_add_data_source, UIIcon.ROW_ADD, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                SQLScriptTaskDataSourceSelectorDialog dialog = new SQLScriptTaskDataSourceSelectorDialog(getShell(), projectNode);
                if (dialog.open() == IDialogConstants.OK_ID) {
                    for (DBNDataSource ds : dialog.getSelectedDataSources()) {
                        if (!selectedDataSources.contains(ds)) {
                            selectedDataSources.add(ds);
                        }
                    }
                    refreshDataSources();
                    updatePageCompletion();
                }
            }
        });
        ToolItem deleteItem = UIUtils.createToolItem(buttonsToolbar, DTUIMessages.sql_script_task_page_settings_tool_item_text_remove_data_source, UIIcon.ROW_DELETE, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                ISelection selection = dataSourceViewer.getSelection();
                if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
                    for (Object element : ((IStructuredSelection) selection).toArray()) {
                        if (element instanceof DBNDataSource) {
                            selectedDataSources.remove(element);
                        }
                    }
                    refreshDataSources();
                    updatePageCompletion();
                }
            }
        });
        UIUtils.createToolBarSeparator(buttonsToolbar, SWT.HORIZONTAL);
        ToolItem moveUpItem = UIUtils.createToolItem(buttonsToolbar, DTUIMessages.sql_script_task_page_settings_tool_item_text_move_data_source_up, UIIcon.ARROW_UP, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                int selectionIndex = dsTable.getSelectionIndex();
                if (selectionIndex > 0) {
                    DBNDataSource prevScript = selectedDataSources.get(selectionIndex - 1);
                    selectedDataSources.set(selectionIndex - 1, selectedDataSources.get(selectionIndex));
                    selectedDataSources.set(selectionIndex, prevScript);
                    refreshDataSources();
                }
            }
        });
        ToolItem moveDownItem = UIUtils.createToolItem(buttonsToolbar, DTUIMessages.sql_script_task_page_settings_tool_item_text_move_data_source_down, UIIcon.ARROW_DOWN, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                int selectionIndex = dsTable.getSelectionIndex();
                if (selectionIndex < dsTable.getItemCount() - 1) {
                    DBNDataSource nextScript = selectedDataSources.get(selectionIndex + 1);
                    selectedDataSources.set(selectionIndex + 1, selectedDataSources.get(selectionIndex));
                    selectedDataSources.set(selectionIndex, nextScript);
                    refreshScripts();
                }
            }
        });
        dataSourceViewer.addSelectionChangedListener(event -> {
            int selectionIndex = dsTable.getSelectionIndex();
            deleteItem.setEnabled(selectionIndex >= 0);
            moveUpItem.setEnabled(selectionIndex > 0);
            moveDownItem.setEnabled(selectionIndex < dsTable.getItemCount() - 1);
        });
        deleteItem.setEnabled(false);
    }
    {
        Composite settingsGroup = UIUtils.createControlGroup(composite, DTMessages.sql_script_task_page_settings_group_script, 3, GridData.HORIZONTAL_ALIGN_BEGINNING, 0);
        ignoreErrorsCheck = UIUtils.createCheckbox(settingsGroup, DTMessages.sql_script_task_page_settings_option_ignore_errors, "", dtSettings.isIgnoreErrors(), 1);
        dumpQueryCheck = UIUtils.createCheckbox(settingsGroup, DTMessages.sql_script_task_page_settings_option_dump_results, "", dtSettings.isDumpQueryResultsToLog(), 1);
        dumpQueryCheck.setEnabled(false);
        autoCommitCheck = UIUtils.createCheckbox(settingsGroup, DTMessages.sql_script_task_page_settings_option_auto_commit, "", dtSettings.isAutoCommit(), 1);
    }
    getWizard().createTaskSaveButtons(composite, true, 1);
    loadSettings();
    setControl(composite);
}
Also used : DBWorkbench(org.jkiss.dbeaver.runtime.DBWorkbench) ActiveWizardPage(org.jkiss.dbeaver.ui.dialogs.ActiveWizardPage) ListContentProvider(org.jkiss.dbeaver.ui.controls.ListContentProvider) java.util(java.util) SashForm(org.eclipse.swt.custom.SashForm) Image(org.eclipse.swt.graphics.Image) IDialogConstants(org.eclipse.jface.dialogs.IDialogConstants) DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) DBNResource(org.jkiss.dbeaver.model.navigator.DBNResource) DBeaverIcons(org.jkiss.dbeaver.ui.DBeaverIcons) SQLScriptExecuteSettings(org.jkiss.dbeaver.tools.sql.SQLScriptExecuteSettings) UIUtils(org.jkiss.dbeaver.ui.UIUtils) Log(org.jkiss.dbeaver.Log) DBNProject(org.jkiss.dbeaver.model.navigator.DBNProject) IFile(org.eclipse.core.resources.IFile) UIIcon(org.jkiss.dbeaver.ui.UIIcon) GridData(org.eclipse.swt.layout.GridData) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) DTMessages(org.jkiss.dbeaver.tools.transfer.internal.DTMessages) DTUIMessages(org.jkiss.dbeaver.tools.transfer.ui.internal.DTUIMessages) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) CommonUtils(org.jkiss.utils.CommonUtils) org.eclipse.swt.widgets(org.eclipse.swt.widgets) org.eclipse.jface.viewers(org.eclipse.jface.viewers) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) List(java.util.List) IResource(org.eclipse.core.resources.IResource) SWT(org.eclipse.swt.SWT) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DBPResourceHandler(org.jkiss.dbeaver.model.app.DBPResourceHandler) SQLScriptExecuteSettings(org.jkiss.dbeaver.tools.sql.SQLScriptExecuteSettings) ListContentProvider(org.jkiss.dbeaver.ui.controls.ListContentProvider) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DBNProject(org.jkiss.dbeaver.model.navigator.DBNProject) DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) DBNResource(org.jkiss.dbeaver.model.navigator.DBNResource) SashForm(org.eclipse.swt.custom.SashForm) GridData(org.eclipse.swt.layout.GridData) DBPResourceHandler(org.jkiss.dbeaver.model.app.DBPResourceHandler) IResource(org.eclipse.core.resources.IResource)

Example 15 with ListContentProvider

use of org.jkiss.dbeaver.ui.controls.ListContentProvider in project dbeaver by dbeaver.

the class ColumnsMappingDialog method createDialogArea.

@Override
protected Composite createDialogArea(Composite parent) {
    DBPDataSource targetDataSource = settings.getTargetDataSource(mapping);
    boldFont = UIUtils.makeBoldFont(parent.getFont());
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(2, false));
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));
    DBPDataSource sourceDataSource = mapping.getSource().getDataSource();
    UIUtils.createLabelText(composite, DTUIMessages.columns_mapping_dialog_composite_label_text_source_container, sourceDataSource == null ? "" : sourceDataSource.getContainer().getName(), SWT.BORDER | SWT.READ_ONLY);
    Text sourceEntity = UIUtils.createLabelText(composite, DTUIMessages.columns_mapping_dialog_composite_label_text_source_entity, DBUtils.getObjectFullName(mapping.getSource(), DBPEvaluationContext.UI), SWT.BORDER | SWT.READ_ONLY | SWT.MULTI | SWT.V_SCROLL);
    ((GridData) sourceEntity.getLayoutData()).widthHint = 600;
    ((GridData) sourceEntity.getLayoutData()).heightHint = UIUtils.getFontHeight(sourceEntity) * 3;
    UIUtils.createLabelText(composite, DTUIMessages.columns_mapping_dialog_composite_label_text_target_container, (targetDataSource == null ? "?" : targetDataSource.getContainer().getName()), SWT.BORDER | SWT.READ_ONLY);
    Text targetEntity = UIUtils.createLabelText(composite, DTUIMessages.columns_mapping_dialog_composite_label_text_target_entity, mapping.getTargetName(), SWT.BORDER | SWT.READ_ONLY);
    ((GridData) targetEntity.getLayoutData()).widthHint = 600;
    ((GridData) targetEntity.getLayoutData()).heightHint = UIUtils.getFontHeight(sourceEntity) * 3;
    mappingViewer = new TableViewer(composite, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.widthHint = 600;
    gd.heightHint = 300;
    gd.horizontalSpan = 2;
    mappingViewer.getTable().setLayoutData(gd);
    mappingViewer.getTable().setLinesVisible(true);
    mappingViewer.getTable().setHeaderVisible(true);
    mappingViewer.setContentProvider(new ListContentProvider());
    mappingViewer.getTable().addKeyListener(new KeyAdapter() {

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.character == SWT.DEL) {
                for (TableItem item : mappingViewer.getTable().getSelection()) {
                    DatabaseMappingAttribute attribute = (DatabaseMappingAttribute) item.getData();
                    attribute.setMappingType(DatabaseMappingType.skip);
                }
                mappingViewer.refresh();
            } else if (e.character == SWT.SPACE) {
                for (TableItem item : mappingViewer.getTable().getSelection()) {
                    DatabaseMappingAttribute attribute = (DatabaseMappingAttribute) item.getData();
                    attribute.setMappingType(DatabaseMappingType.existing);
                    try {
                        attribute.updateMappingType(new VoidProgressMonitor());
                    } catch (DBException e1) {
                        DBWorkbench.getPlatformUI().showError("Bad mapping", "Invalid column mapping", e1);
                    }
                }
                mappingViewer.refresh();
            }
        }
    });
    {
        TableViewerColumn columnSource = new TableViewerColumn(mappingViewer, SWT.LEFT);
        columnSource.setLabelProvider(new CellLabelProvider() {

            @Override
            public void update(ViewerCell cell) {
                DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) cell.getElement();
                cell.setText(DBUtils.getObjectFullName(attrMapping.getSource(), DBPEvaluationContext.UI));
                if (attrMapping.getIcon() != null) {
                    cell.setImage(DBeaverIcons.getImage(attrMapping.getIcon()));
                }
            }
        });
        columnSource.getColumn().setText(DTUIMessages.columns_mapping_dialog_column_source_text);
        columnSource.getColumn().setWidth(170);
    }
    {
        TableViewerColumn columnSourceType = new TableViewerColumn(mappingViewer, SWT.LEFT);
        columnSourceType.setLabelProvider(new CellLabelProvider() {

            @Override
            public void update(ViewerCell cell) {
                cell.setText(((DatabaseMappingAttribute) cell.getElement()).getSourceType());
            }
        });
        columnSourceType.getColumn().setText(DTUIMessages.columns_mapping_dialog_column_source_type_text);
        columnSourceType.getColumn().setWidth(100);
    }
    {
        TableViewerColumn columnTarget = new TableViewerColumn(mappingViewer, SWT.LEFT);
        columnTarget.setLabelProvider(new CellLabelProvider() {

            @Override
            public void update(ViewerCell cell) {
                DatabaseMappingAttribute mapping = (DatabaseMappingAttribute) cell.getElement();
                cell.setText(mapping.getTargetName());
                if (mapping.getMappingType() == DatabaseMappingType.unspecified) {
                    cell.setBackground(UIUtils.getSharedTextColors().getColor(SharedTextColors.COLOR_WARNING));
                } else {
                    cell.setBackground(null);
                }
                cell.setFont(boldFont);
            }
        });
        columnTarget.getColumn().setText(DTUIMessages.columns_mapping_dialog_column_target_text);
        columnTarget.getColumn().setWidth(170);
        columnTarget.setEditingSupport(new EditingSupport(mappingViewer) {

            @Override
            protected CellEditor getCellEditor(Object element) {
                try {
                    java.util.List<String> items = new ArrayList<>();
                    DatabaseMappingAttribute mapping = (DatabaseMappingAttribute) element;
                    if (mapping.getParent().getMappingType() == DatabaseMappingType.existing && mapping.getParent().getTarget() instanceof DBSEntity) {
                        DBSEntity parentEntity = (DBSEntity) mapping.getParent().getTarget();
                        for (DBSEntityAttribute attr : CommonUtils.safeCollection(parentEntity.getAttributes(new VoidProgressMonitor()))) {
                            items.add(attr.getName());
                        }
                    }
                    items.add(DatabaseMappingAttribute.TARGET_NAME_SKIP);
                    return new CustomComboBoxCellEditor(mappingViewer, mappingViewer.getTable(), items.toArray(new String[0]), SWT.DROP_DOWN);
                } catch (DBException e) {
                    DBWorkbench.getPlatformUI().showError("Bad value", "Wrong target column", e);
                    return null;
                }
            }

            @Override
            protected boolean canEdit(Object element) {
                return true;
            }

            @Override
            protected Object getValue(Object element) {
                return ((DatabaseMappingAttribute) element).getTargetName();
            }

            @Override
            protected void setValue(Object element, Object value) {
                try {
                    String name = CommonUtils.toString(value);
                    DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) element;
                    if (DatabaseMappingAttribute.TARGET_NAME_SKIP.equals(name)) {
                        attrMapping.setMappingType(DatabaseMappingType.skip);
                    } else {
                        if (attrMapping.getParent().getMappingType() == DatabaseMappingType.existing && attrMapping.getParent().getTarget() instanceof DBSEntity) {
                            DBSEntity parentEntity = (DBSEntity) attrMapping.getParent().getTarget();
                            for (DBSEntityAttribute attr : CommonUtils.safeCollection(parentEntity.getAttributes(new VoidProgressMonitor()))) {
                                if (name.equalsIgnoreCase(attr.getName())) {
                                    attrMapping.setTarget(attr);
                                    attrMapping.setMappingType(DatabaseMappingType.existing);
                                    attrMapping.setTargetName(name);
                                    return;
                                }
                            }
                        }
                        attrMapping.setMappingType(DatabaseMappingType.create);
                        attrMapping.setTargetName(name);
                    }
                } catch (DBException e) {
                    DBWorkbench.getPlatformUI().showError("Bad value", "Wrong target", e);
                } finally {
                    mappingViewer.refresh();
                }
            }
        });
    }
    {
        TableViewerColumn columnTargetType = new TableViewerColumn(mappingViewer, SWT.LEFT);
        columnTargetType.setLabelProvider(new CellLabelProvider() {

            @Override
            public void update(ViewerCell cell) {
                DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) cell.getElement();
                DBPDataSource dataSource = settings.getTargetDataSource(attrMapping);
                cell.setText(attrMapping.getTargetType(dataSource, true));
                cell.setFont(boldFont);
            }
        });
        columnTargetType.getColumn().setText(DTUIMessages.columns_mapping_dialog_column_target_type_text);
        columnTargetType.getColumn().setWidth(100);
        columnTargetType.setEditingSupport(new EditingSupport(mappingViewer) {

            @Override
            protected CellEditor getCellEditor(Object element) {
                DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) element;
                Set<String> types = new TreeSet<>();
                DBPDataSource dataSource = settings.getTargetDataSource(attrMapping);
                if (dataSource instanceof DBPDataTypeProvider) {
                    for (DBSDataType type : ((DBPDataTypeProvider) dataSource).getLocalDataTypes()) {
                        types.add(type.getName());
                    }
                }
                types.add(attrMapping.getTargetType(dataSource, true));
                return new CustomComboBoxCellEditor(mappingViewer, mappingViewer.getTable(), types.toArray(new String[0]), SWT.BORDER);
            }

            @Override
            protected boolean canEdit(Object element) {
                return true;
            }

            @Override
            protected Object getValue(Object element) {
                DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) element;
                return attrMapping.getTargetType(settings.getTargetDataSource(attrMapping), true);
            }

            @Override
            protected void setValue(Object element, Object value) {
                DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) element;
                attrMapping.setTargetType(CommonUtils.toString(value));
                mappingViewer.refresh(element);
            }
        });
    }
    {
        TableViewerColumn columnType = new TableViewerColumn(mappingViewer, SWT.LEFT);
        columnType.setLabelProvider(new CellLabelProvider() {

            @Override
            public void update(ViewerCell cell) {
                DatabaseMappingAttribute mapping = (DatabaseMappingAttribute) cell.getElement();
                String text = "";
                switch(mapping.getMappingType()) {
                    case unspecified:
                        text = "?";
                        break;
                    case existing:
                        text = DTUIMessages.columns_mapping_dialog_cell_text_existing;
                        break;
                    case create:
                        text = DTUIMessages.columns_mapping_dialog_cell_text_new;
                        break;
                    case skip:
                        text = DTUIMessages.columns_mapping_dialog_cell_text_skip;
                        break;
                }
                cell.setText(text);
            }
        });
        columnType.getColumn().setText(DTUIMessages.columns_mapping_dialog_column_type_text_mapping);
        columnType.getColumn().setWidth(60);
    }
    mappingViewer.setInput(attributeMappings);
    return parent;
}
Also used : DBException(org.jkiss.dbeaver.DBException) KeyAdapter(org.eclipse.swt.events.KeyAdapter) TableItem(org.eclipse.swt.widgets.TableItem) CustomComboBoxCellEditor(org.jkiss.dbeaver.ui.controls.CustomComboBoxCellEditor) KeyEvent(org.eclipse.swt.events.KeyEvent) GridLayout(org.eclipse.swt.layout.GridLayout) ListContentProvider(org.jkiss.dbeaver.ui.controls.ListContentProvider) Composite(org.eclipse.swt.widgets.Composite) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) Text(org.eclipse.swt.widgets.Text) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DatabaseMappingAttribute(org.jkiss.dbeaver.tools.transfer.database.DatabaseMappingAttribute) java.util(java.util) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) GridData(org.eclipse.swt.layout.GridData) DBPDataTypeProvider(org.jkiss.dbeaver.model.DBPDataTypeProvider) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity)

Aggregations

ListContentProvider (org.jkiss.dbeaver.ui.controls.ListContentProvider)33 GridData (org.eclipse.swt.layout.GridData)29 Composite (org.eclipse.swt.widgets.Composite)20 List (java.util.List)14 DBException (org.jkiss.dbeaver.DBException)14 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)14 ArrayList (java.util.ArrayList)12 GridLayout (org.eclipse.swt.layout.GridLayout)12 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)11 SWT (org.eclipse.swt.SWT)10 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)10 SelectionEvent (org.eclipse.swt.events.SelectionEvent)10 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)9 IDialogConstants (org.eclipse.jface.dialogs.IDialogConstants)8 TableViewer (org.eclipse.jface.viewers.TableViewer)8 SashForm (org.eclipse.swt.custom.SashForm)8 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)8 DBPObject (org.jkiss.dbeaver.model.DBPObject)7 ViewerColumnController (org.jkiss.dbeaver.ui.controls.ViewerColumnController)7 Collection (java.util.Collection)6