Search in sources :

Example 1 with ObjectContainerSelectorPanel

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

the class DatabaseConsumerPageMapping method createControl.

@Override
public void createControl(Composite parent) {
    initializeDialogUnits(parent);
    final DatabaseConsumerSettings settings = getDatabaseConsumerSettings();
    Composite composite = UIUtils.createComposite(parent, 1);
    {
        // Target container
        // Use first source object as cur selection (it's better than nothing)
        containerPanel = new ObjectContainerSelectorPanel(composite, getWizard().getProject(), "container.data-transfer.database-consumer", DTMessages.data_transfer_db_consumer_target_container, DTMessages.data_transfer_db_consumer_choose_container) {

            @Nullable
            @Override
            protected DBNNode getSelectedNode() {
                DBNNode selectedNode = settings.getContainerNode();
                if (selectedNode == null && !settings.getDataMappings().isEmpty()) {
                    // Use first source object as cur selection (it's better than nothing)
                    DBSDataContainer firstSource = settings.getDataMappings().keySet().iterator().next();
                    selectedNode = DBWorkbench.getPlatform().getNavigatorModel().getNodeByObject(firstSource);
                    while (selectedNode != null) {
                        if (selectedNode instanceof DBSWrapper && ((DBSWrapper) selectedNode).getObject() instanceof DBSObjectContainer) {
                            break;
                        } else {
                            selectedNode = selectedNode.getParentNode();
                        }
                    }
                }
                return selectedNode;
            }

            @Override
            protected void setSelectedNode(DBNDatabaseNode node) {
                settings.setContainerNode(node);
                setContainerInfo(node);
                // Reset mappings
                for (DatabaseMappingContainer mappingContainer : settings.getDataMappings().values()) {
                    if (mappingContainer.getMappingType() != DatabaseMappingType.unspecified) {
                        try {
                            mappingContainer.refreshMappingType(getWizard().getRunnableContext(), DatabaseMappingType.unspecified);
                        } catch (DBException e1) {
                            log.error(e1);
                        }
                    }
                }
                mappingViewer.refresh();
                updatePageCompletion();
                setMessage(null);
            }
        };
    }
    Composite mappingsGroup = UIUtils.createComposite(composite, 2);
    mappingsGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
    createMappingsTree(mappingsGroup);
    {
        // Control buttons
        Composite buttonsPanel = UIUtils.createComposite(mappingsGroup, 1);
        buttonsPanel.setLayoutData(new GridData(GridData.FILL_VERTICAL));
        autoAssignButton = UIUtils.createDialogButton(buttonsPanel, DTMessages.data_transfer_db_consumer_auto_assign, UIIcon.ASTERISK, DTMessages.data_transfer_db_consumer_auto_assign_description, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                autoAssignMappings();
            }
        });
        UIUtils.createLabelSeparator(buttonsPanel, SWT.HORIZONTAL);
        final Button mapTableButton = UIUtils.createDialogButton(buttonsPanel, DTMessages.data_transfer_db_consumer_existing_table, DBIcon.TREE_TABLE, DTMessages.data_transfer_db_consumer_existing_table_description, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                mapExistingTable((DatabaseMappingContainer) getSelectedMapping());
            }
        });
        mapTableButton.setEnabled(false);
        final Button createNewButton = UIUtils.createDialogButton(buttonsPanel, DTMessages.data_transfer_db_consumer_new_table, DBIcon.TREE_VIEW, DTMessages.data_transfer_db_consumer_new_table_description, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                mapNewTable((DatabaseMappingContainer) getSelectedMapping());
            }
        });
        createNewButton.setEnabled(false);
        final Button columnsButton = UIUtils.createDialogButton(buttonsPanel, DTMessages.data_transfer_db_consumer_column_mappings, DBIcon.TREE_COLUMNS, DTMessages.data_transfer_db_consumer_column_mappings_description, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                DatabaseMappingObject selectedMapping = getSelectedMapping();
                mapColumns(selectedMapping instanceof DatabaseMappingContainer ? (DatabaseMappingContainer) selectedMapping : ((DatabaseMappingAttribute) selectedMapping).getParent());
            }
        });
        columnsButton.setEnabled(false);
        UIUtils.createLabelSeparator(buttonsPanel, SWT.HORIZONTAL);
        final Button ddlButton = UIUtils.createDialogButton(buttonsPanel, DTMessages.data_transfer_wizard_page_ddl_name, UIIcon.SQL_TEXT, DTMessages.data_transfer_wizard_page_ddl_description, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                DatabaseMappingObject selectedMapping = getSelectedMapping();
                showDDL(selectedMapping instanceof DatabaseMappingContainer ? (DatabaseMappingContainer) selectedMapping : ((DatabaseMappingAttribute) selectedMapping).getParent());
            }
        });
        ddlButton.setEnabled(false);
        final Button previewButton = UIUtils.createDialogButton(buttonsPanel, DTMessages.data_transfer_wizard_page_preview_name, UIIcon.SQL_PREVIEW, DTMessages.data_transfer_wizard_page_preview_description, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                DBPDataSourceContainer dataSourceContainer = getDatabaseConsumerSettings().getContainerNode().getDataSourceContainer();
                if (!dataSourceContainer.hasModifyPermission(DBPDataSourcePermission.PERMISSION_EDIT_METADATA)) {
                    UIUtils.showMessageBox(getShell(), DTMessages.data_transfer_wizard_restricted_title, NLS.bind(DTMessages.data_transfer_wizard_restricted_description, dataSourceContainer.getName()), SWT.ICON_WARNING);
                    return;
                }
                DatabaseMappingObject selectedMapping = getSelectedMapping();
                showPreview(selectedMapping instanceof DatabaseMappingContainer ? (DatabaseMappingContainer) selectedMapping : ((DatabaseMappingAttribute) selectedMapping).getParent());
            }
        });
        previewButton.setEnabled(false);
        mappingViewer.getTree().addKeyListener(new KeyAdapter() {

            @Override
            public void keyReleased(KeyEvent e) {
                try {
                    boolean updated = false;
                    Object element = null;
                    if (e.character == SWT.CR) {
                    // Object element = mappingViewer.getStructuredSelection().getFirstElement();
                    // mappingViewer.editElement(element, 1);
                    } else if (e.character == SWT.DEL) {
                        for (TreeItem item : mappingViewer.getTree().getSelection()) {
                            element = item.getData();
                            if (element instanceof DatabaseMappingAttribute) {
                                DatabaseMappingAttribute attribute = (DatabaseMappingAttribute) element;
                                attribute.setMappingType(DatabaseMappingType.skip);
                            } else if (element instanceof DatabaseMappingContainer) {
                                DatabaseMappingContainer container = (DatabaseMappingContainer) element;
                                container.refreshMappingType(getWizard().getRunnableContext(), DatabaseMappingType.skip);
                            }
                            selectNextColumn(item);
                        }
                        updated = true;
                    } else if (e.character == SWT.SPACE) {
                        for (TreeItem item : mappingViewer.getTree().getSelection()) {
                            element = item.getData();
                            if (element instanceof DatabaseMappingAttribute) {
                                DatabaseMappingAttribute attribute = (DatabaseMappingAttribute) item.getData();
                                attribute.setMappingType(DatabaseMappingType.existing);
                                attribute.updateMappingType(new VoidProgressMonitor());
                            } else if (element instanceof DatabaseMappingContainer) {
                                DatabaseMappingContainer container = (DatabaseMappingContainer) element;
                                setMappingTarget(container, container.getTargetName());
                            }
                            selectNextColumn(item);
                        }
                        updated = true;
                    }
                    if (updated) {
                        updateMappingsAndButtons();
                        updatePageCompletion();
                        if (element instanceof DatabaseMappingContainer) {
                            // Select next container
                            @SuppressWarnings("unchecked") List<DatabaseMappingContainer> model = (List<DatabaseMappingContainer>) mappingViewer.getInput();
                            int curIndex = model.indexOf(element);
                            if (curIndex < model.size() - 1) {
                                mappingViewer.setSelection(new StructuredSelection(model.get(curIndex + 1)), true);
                            }
                        }
                    }
                } catch (DBException e1) {
                    DBWorkbench.getPlatformUI().showError(DTUIMessages.database_consumer_page_mapping_title_error_mapping_table, DTUIMessages.database_consumer_page_mapping_message_error_mapping_target_table, e1);
                }
            }
        });
        mappingViewer.addSelectionChangedListener(event -> {
            DatabaseMappingObject mapping = getSelectedMapping();
            mapTableButton.setEnabled(mapping instanceof DatabaseMappingContainer);
            createNewButton.setEnabled(mapping instanceof DatabaseMappingContainer && settings.getContainerNode() != null);
            final boolean hasMappings = settings.getContainerNode() != null && ((mapping instanceof DatabaseMappingContainer && mapping.getMappingType() != DatabaseMappingType.unspecified) || (mapping instanceof DatabaseMappingAttribute && ((DatabaseMappingAttribute) mapping).getParent().getMappingType() != DatabaseMappingType.unspecified));
            columnsButton.setEnabled(hasMappings);
            ddlButton.setEnabled(hasMappings);
            previewButton.setEnabled(hasMappings);
        });
        mappingViewer.addDoubleClickListener(event -> {
            DatabaseMappingObject selectedMapping = getSelectedMapping();
            if (selectedMapping != null) {
                if (selectedMapping instanceof DatabaseMappingContainer) {
                /*
                        if (selectedMapping.getMappingType() == DatabaseMappingType.unspecified) {
                            mapExistingTable((DatabaseMappingContainer) selectedMapping);
                        } else {
                            mapColumns((DatabaseMappingContainer) selectedMapping);
                        }
*/
                }
            }
        });
    }
    {
        Composite hintPanel = new Composite(composite, SWT.NONE);
        hintPanel.setLayout(new GridLayout(3, false));
        hintPanel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        new Label(hintPanel, SWT.NONE).setText(DTUIMessages.database_consumer_page_mapping_label_hint);
    }
    setControl(composite);
}
Also used : DBWorkbench(org.jkiss.dbeaver.runtime.DBWorkbench) DataTransferSettings(org.jkiss.dbeaver.tools.transfer.DataTransferSettings) ActiveWizardPage(org.jkiss.dbeaver.ui.dialogs.ActiveWizardPage) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) CustomComboBoxCellEditor(org.jkiss.dbeaver.ui.controls.CustomComboBoxCellEditor) ObjectContainerSelectorPanel(org.jkiss.dbeaver.ui.controls.ObjectContainerSelectorPanel) EnterNameDialog(org.jkiss.dbeaver.ui.dialogs.EnterNameDialog) IDialogConstants(org.eclipse.jface.dialogs.IDialogConstants) Nullable(org.jkiss.code.Nullable) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) UIServiceSQL(org.jkiss.dbeaver.runtime.ui.UIServiceSQL) ArrayList(java.util.ArrayList) IMessageProvider(org.eclipse.jface.dialogs.IMessageProvider) DBeaverIcons(org.jkiss.dbeaver.ui.DBeaverIcons) UIUtils(org.jkiss.dbeaver.ui.UIUtils) KeyEvent(org.eclipse.swt.events.KeyEvent) Log(org.jkiss.dbeaver.Log) UIIcon(org.jkiss.dbeaver.ui.UIIcon) GridData(org.eclipse.swt.layout.GridData) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) KeyAdapter(org.eclipse.swt.events.KeyAdapter) org.jkiss.dbeaver.model(org.jkiss.dbeaver.model) org.jkiss.dbeaver.tools.transfer.database(org.jkiss.dbeaver.tools.transfer.database) DTMessages(org.jkiss.dbeaver.tools.transfer.internal.DTMessages) DTUIMessages(org.jkiss.dbeaver.tools.transfer.ui.internal.DTUIMessages) TreeContentProvider(org.jkiss.dbeaver.ui.controls.TreeContentProvider) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) CommonUtils(org.jkiss.utils.CommonUtils) org.jkiss.dbeaver.model.struct(org.jkiss.dbeaver.model.struct) NLS(org.eclipse.osgi.util.NLS) org.eclipse.swt.widgets(org.eclipse.swt.widgets) DBObjectNameCaseTransformer(org.jkiss.dbeaver.model.impl.DBObjectNameCaseTransformer) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) ArrayUtils(org.jkiss.utils.ArrayUtils) org.eclipse.jface.viewers(org.eclipse.jface.viewers) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataTransferPipe(org.jkiss.dbeaver.tools.transfer.DataTransferPipe) DataTransferWizard(org.jkiss.dbeaver.tools.transfer.ui.wizard.DataTransferWizard) List(java.util.List) DBException(org.jkiss.dbeaver.DBException) SWT(org.eclipse.swt.SWT) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) SQLUtils(org.jkiss.dbeaver.model.sql.SQLUtils) SelectionEvent(org.eclipse.swt.events.SelectionEvent) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) SharedTextColors(org.jkiss.dbeaver.ui.SharedTextColors) GridLayout(org.eclipse.swt.layout.GridLayout) DBException(org.jkiss.dbeaver.DBException) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) ObjectContainerSelectorPanel(org.jkiss.dbeaver.ui.controls.ObjectContainerSelectorPanel) KeyAdapter(org.eclipse.swt.events.KeyAdapter) KeyEvent(org.eclipse.swt.events.KeyEvent) GridLayout(org.eclipse.swt.layout.GridLayout) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ArrayList(java.util.ArrayList) List(java.util.List) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) GridData(org.eclipse.swt.layout.GridData) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)

Example 2 with ObjectContainerSelectorPanel

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

the class EditForeignKeyPage method createContainerSelector.

private void createContainerSelector(Composite tableGroup) throws DBException {
    ObjectContainerSelectorPanel containerPanel = new ObjectContainerSelectorPanel(tableGroup, foreignKey.getDataSource().getContainer().getRegistry().getProject(), CONTAINER_LOGICAL_FK, "Reference table container", "Select reference table catalog/schema") {

        @Nullable
        @Override
        protected DBNNode getSelectedNode() {
            if (ownerContainerNode != null) {
                return ownerContainerNode;
            }
            DBSObject containerObject;
            if (ownerTableNode != null) {
                DBNNode containerNode = ownerTableNode.getParentNode();
                while (containerNode instanceof DBNDatabaseFolder) {
                    containerNode = containerNode.getParentNode();
                }
                if (containerNode instanceof DBNDatabaseNode) {
                    containerObject = ((DBNDatabaseNode) containerNode).getObject();
                } else {
                    containerObject = null;
                }
            } else {
                containerObject = foreignKey.getParentObject();
            }
            if (containerObject != null && containerObject.getParentObject() instanceof DBSObjectContainer) {
                containerObject = containerObject.getParentObject();
            }
            if (containerObject instanceof DBVContainer) {
                try {
                    containerObject = ((DBVContainer) containerObject).getRealContainer(new VoidProgressMonitor());
                } catch (DBException e) {
                    log.error("Error getting real object container", e);
                }
            }
            return ownerContainerNode = DBWorkbench.getPlatform().getNavigatorModel().getNodeByObject(containerObject);
        }

        @Override
        protected void setSelectedNode(DBNDatabaseNode node) {
            ownerContainerNode = node;
            if (ownerContainerNode == null) {
                setContainerInfo(null);
            } else {
                setContainerInfo(node);
                loadTableList(ownerContainerNode);
            }
        }
    };
    GridData gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalSpan = 2;
    containerPanel.setLayoutData(gd);
    if (ownerTableNode != null) {
        DBNNode containerNode = ownerTableNode.getParentNode();
        while (containerNode instanceof DBNDatabaseFolder) {
            containerNode = containerNode.getParentNode();
        }
        if (containerNode instanceof DBNDatabaseNode) {
            containerPanel.setContainerInfo((DBNDatabaseNode) containerNode);
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) ObjectContainerSelectorPanel(org.jkiss.dbeaver.ui.controls.ObjectContainerSelectorPanel) GridData(org.eclipse.swt.layout.GridData) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DBNDatabaseFolder(org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 3 with ObjectContainerSelectorPanel

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

the class DatabaseConsumerPageMapping method createControl.

@Override
public void createControl(Composite parent) {
    initializeDialogUnits(parent);
    final DatabaseConsumerSettings settings = getDatabaseConsumerSettings();
    Composite composite = UIUtils.createComposite(parent, 1);
    {
        // Target container
        // Use first source object as cur selection (it's better than nothing)
        containerPanel = new ObjectContainerSelectorPanel(composite, getWizard().getProject(), "container.data-transfer.database-consumer", DTMessages.data_transfer_db_consumer_target_container, DTMessages.data_transfer_db_consumer_choose_container) {

            @Nullable
            @Override
            protected DBNNode getSelectedNode() {
                DBNNode selectedNode = settings.getContainerNode();
                if (selectedNode == null && !settings.getDataMappings().isEmpty()) {
                    // Use first source object as cur selection (it's better than nothing)
                    DBSDataContainer firstSource = settings.getDataMappings().keySet().iterator().next();
                    selectedNode = DBWorkbench.getPlatform().getNavigatorModel().getNodeByObject(firstSource);
                    while (selectedNode != null) {
                        if (selectedNode instanceof DBSWrapper && ((DBSWrapper) selectedNode).getObject() instanceof DBSObjectContainer) {
                            break;
                        } else {
                            selectedNode = selectedNode.getParentNode();
                        }
                    }
                }
                return selectedNode;
            }

            @Override
            protected void setSelectedNode(DBNDatabaseNode node) {
                settings.setContainerNode(node);
                setContainerInfo(node);
                // Reset mappings
                for (DatabaseMappingContainer mappingContainer : settings.getDataMappings().values()) {
                    if (mappingContainer.getMappingType() != DatabaseMappingType.unspecified) {
                        try {
                            mappingContainer.refreshMappingType(getWizard().getRunnableContext(), DatabaseMappingType.unspecified);
                        } catch (DBException e1) {
                            log.error(e1);
                        }
                    }
                }
                mappingViewer.refresh();
                updatePageCompletion();
                setMessage(null);
            }
        };
    }
    Composite mappingsGroup = UIUtils.createComposite(composite, 2);
    mappingsGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
    createMappingsTree(mappingsGroup);
    {
        // Control buttons
        Composite buttonsPanel = UIUtils.createComposite(mappingsGroup, 1);
        buttonsPanel.setLayoutData(new GridData(GridData.FILL_VERTICAL));
        autoAssignButton = UIUtils.createDialogButton(buttonsPanel, DTMessages.data_transfer_db_consumer_auto_assign, UIIcon.ASTERISK, DTMessages.data_transfer_db_consumer_auto_assign_description, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                autoAssignMappings();
            }
        });
        UIUtils.createLabelSeparator(buttonsPanel, SWT.HORIZONTAL);
        final Button mapTableButton = UIUtils.createDialogButton(buttonsPanel, DTMessages.data_transfer_db_consumer_existing_table, DBIcon.TREE_TABLE, DTMessages.data_transfer_db_consumer_existing_table_description, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                mapExistingTable((DatabaseMappingContainer) getSelectedMapping());
            }
        });
        mapTableButton.setEnabled(false);
        final Button createNewButton = UIUtils.createDialogButton(buttonsPanel, DTMessages.data_transfer_db_consumer_new_table, DBIcon.TREE_VIEW, DTMessages.data_transfer_db_consumer_new_table_description, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                mapNewTable((DatabaseMappingContainer) getSelectedMapping());
            }
        });
        createNewButton.setEnabled(false);
        final Button columnsButton = UIUtils.createDialogButton(buttonsPanel, DTMessages.data_transfer_db_consumer_column_mappings, DBIcon.TREE_COLUMNS, DTMessages.data_transfer_db_consumer_column_mappings_description, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                DatabaseMappingObject selectedMapping = getSelectedMapping();
                mapColumns(selectedMapping instanceof DatabaseMappingContainer ? (DatabaseMappingContainer) selectedMapping : ((DatabaseMappingAttribute) selectedMapping).getParent());
            }
        });
        columnsButton.setEnabled(false);
        UIUtils.createLabelSeparator(buttonsPanel, SWT.HORIZONTAL);
        final Button ddlButton = UIUtils.createDialogButton(buttonsPanel, DTMessages.data_transfer_wizard_page_ddl_name, UIIcon.SQL_TEXT, DTMessages.data_transfer_wizard_page_ddl_description, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                DatabaseMappingObject selectedMapping = getSelectedMapping();
                showDDL(selectedMapping instanceof DatabaseMappingContainer ? (DatabaseMappingContainer) selectedMapping : ((DatabaseMappingAttribute) selectedMapping).getParent());
            }
        });
        ddlButton.setEnabled(false);
        final Button previewButton = UIUtils.createDialogButton(buttonsPanel, DTMessages.data_transfer_wizard_page_preview_name, UIIcon.SQL_PREVIEW, DTMessages.data_transfer_wizard_page_preview_description, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                DBPDataSourceContainer dataSourceContainer = getDatabaseConsumerSettings().getContainerNode().getDataSourceContainer();
                if (!dataSourceContainer.hasModifyPermission(DBPDataSourcePermission.PERMISSION_EDIT_METADATA)) {
                    UIUtils.showMessageBox(getShell(), DTMessages.data_transfer_wizard_restricted_title, NLS.bind(DTMessages.data_transfer_wizard_restricted_description, dataSourceContainer.getName()), SWT.ICON_WARNING);
                    return;
                }
                DatabaseMappingObject selectedMapping = getSelectedMapping();
                showPreview(selectedMapping instanceof DatabaseMappingContainer ? (DatabaseMappingContainer) selectedMapping : ((DatabaseMappingAttribute) selectedMapping).getParent());
            }
        });
        previewButton.setEnabled(false);
        mappingViewer.getTree().addKeyListener(new KeyAdapter() {

            @Override
            public void keyReleased(KeyEvent e) {
                try {
                    boolean updated = false;
                    Object element = null;
                    if (e.character == SWT.CR) {
                    // Object element = mappingViewer.getStructuredSelection().getFirstElement();
                    // mappingViewer.editElement(element, 1);
                    } else if (e.character == SWT.DEL) {
                        for (TreeItem item : mappingViewer.getTree().getSelection()) {
                            element = item.getData();
                            if (element instanceof DatabaseMappingAttribute) {
                                DatabaseMappingAttribute attribute = (DatabaseMappingAttribute) element;
                                attribute.setMappingType(DatabaseMappingType.skip);
                            } else if (element instanceof DatabaseMappingContainer) {
                                DatabaseMappingContainer container = (DatabaseMappingContainer) element;
                                container.refreshMappingType(getWizard().getRunnableContext(), DatabaseMappingType.skip);
                            }
                            selectNextColumn(item);
                        }
                        updated = true;
                    } else if (e.character == SWT.SPACE) {
                        for (TreeItem item : mappingViewer.getTree().getSelection()) {
                            element = item.getData();
                            if (element instanceof DatabaseMappingAttribute) {
                                DatabaseMappingAttribute attribute = (DatabaseMappingAttribute) item.getData();
                                attribute.setMappingType(DatabaseMappingType.existing);
                                attribute.updateMappingType(new VoidProgressMonitor());
                            } else if (element instanceof DatabaseMappingContainer) {
                                DatabaseMappingContainer container = (DatabaseMappingContainer) element;
                                setMappingTarget(container, container.getTargetName());
                            }
                            selectNextColumn(item);
                        }
                        updated = true;
                    }
                    if (updated) {
                        updateMappingsAndButtons();
                        updatePageCompletion();
                        if (element instanceof DatabaseMappingContainer) {
                            // Select next container
                            @SuppressWarnings("unchecked") List<DatabaseMappingContainer> model = (List<DatabaseMappingContainer>) mappingViewer.getInput();
                            int curIndex = model.indexOf(element);
                            if (curIndex < model.size() - 1) {
                                mappingViewer.setSelection(new StructuredSelection(model.get(curIndex + 1)), true);
                            }
                        }
                    }
                } catch (DBException e1) {
                    DBWorkbench.getPlatformUI().showError(DTUIMessages.database_consumer_page_mapping_title_error_mapping_table, DTUIMessages.database_consumer_page_mapping_message_error_mapping_target_table, e1);
                }
            }
        });
        mappingViewer.addSelectionChangedListener(event -> {
            DatabaseMappingObject mapping = getSelectedMapping();
            mapTableButton.setEnabled(mapping instanceof DatabaseMappingContainer);
            createNewButton.setEnabled(mapping instanceof DatabaseMappingContainer && settings.getContainerNode() != null);
            final boolean hasMappings = settings.getContainerNode() != null && ((mapping instanceof DatabaseMappingContainer && mapping.getMappingType() != DatabaseMappingType.unspecified) || (mapping instanceof DatabaseMappingAttribute && ((DatabaseMappingAttribute) mapping).getParent().getMappingType() != DatabaseMappingType.unspecified));
            columnsButton.setEnabled(hasMappings);
            ddlButton.setEnabled(hasMappings);
            previewButton.setEnabled(hasMappings);
        });
        mappingViewer.addDoubleClickListener(event -> {
            DatabaseMappingObject selectedMapping = getSelectedMapping();
            if (selectedMapping != null) {
                if (selectedMapping instanceof DatabaseMappingContainer) {
                /*
                        if (selectedMapping.getMappingType() == DatabaseMappingType.unspecified) {
                            mapExistingTable((DatabaseMappingContainer) selectedMapping);
                        } else {
                            mapColumns((DatabaseMappingContainer) selectedMapping);
                        }
*/
                }
            }
        });
    }
    {
        Composite hintPanel = new Composite(composite, SWT.NONE);
        hintPanel.setLayout(new GridLayout(3, false));
        hintPanel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        new Label(hintPanel, SWT.NONE).setText(DTUIMessages.database_consumer_page_mapping_label_hint);
    }
    setControl(composite);
}
Also used : DBWorkbench(org.jkiss.dbeaver.runtime.DBWorkbench) DataTransferSettings(org.jkiss.dbeaver.tools.transfer.DataTransferSettings) ActiveWizardPage(org.jkiss.dbeaver.ui.dialogs.ActiveWizardPage) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) CustomComboBoxCellEditor(org.jkiss.dbeaver.ui.controls.CustomComboBoxCellEditor) ObjectContainerSelectorPanel(org.jkiss.dbeaver.ui.controls.ObjectContainerSelectorPanel) EnterNameDialog(org.jkiss.dbeaver.ui.dialogs.EnterNameDialog) IDialogConstants(org.eclipse.jface.dialogs.IDialogConstants) Nullable(org.jkiss.code.Nullable) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) UIServiceSQL(org.jkiss.dbeaver.runtime.ui.UIServiceSQL) ArrayList(java.util.ArrayList) IMessageProvider(org.eclipse.jface.dialogs.IMessageProvider) DBeaverIcons(org.jkiss.dbeaver.ui.DBeaverIcons) UIUtils(org.jkiss.dbeaver.ui.UIUtils) KeyEvent(org.eclipse.swt.events.KeyEvent) Log(org.jkiss.dbeaver.Log) UIIcon(org.jkiss.dbeaver.ui.UIIcon) GridData(org.eclipse.swt.layout.GridData) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) KeyAdapter(org.eclipse.swt.events.KeyAdapter) org.jkiss.dbeaver.model(org.jkiss.dbeaver.model) org.jkiss.dbeaver.tools.transfer.database(org.jkiss.dbeaver.tools.transfer.database) DTMessages(org.jkiss.dbeaver.tools.transfer.internal.DTMessages) DTUIMessages(org.jkiss.dbeaver.tools.transfer.ui.internal.DTUIMessages) TreeContentProvider(org.jkiss.dbeaver.ui.controls.TreeContentProvider) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) CommonUtils(org.jkiss.utils.CommonUtils) org.jkiss.dbeaver.model.struct(org.jkiss.dbeaver.model.struct) NLS(org.eclipse.osgi.util.NLS) org.eclipse.swt.widgets(org.eclipse.swt.widgets) DBObjectNameCaseTransformer(org.jkiss.dbeaver.model.impl.DBObjectNameCaseTransformer) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) ArrayUtils(org.jkiss.utils.ArrayUtils) org.eclipse.jface.viewers(org.eclipse.jface.viewers) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataTransferPipe(org.jkiss.dbeaver.tools.transfer.DataTransferPipe) DataTransferWizard(org.jkiss.dbeaver.tools.transfer.ui.wizard.DataTransferWizard) List(java.util.List) DBException(org.jkiss.dbeaver.DBException) SWT(org.eclipse.swt.SWT) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) SQLUtils(org.jkiss.dbeaver.model.sql.SQLUtils) SelectionEvent(org.eclipse.swt.events.SelectionEvent) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) SharedTextColors(org.jkiss.dbeaver.ui.SharedTextColors) GridLayout(org.eclipse.swt.layout.GridLayout) DBException(org.jkiss.dbeaver.DBException) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) ObjectContainerSelectorPanel(org.jkiss.dbeaver.ui.controls.ObjectContainerSelectorPanel) KeyAdapter(org.eclipse.swt.events.KeyAdapter) KeyEvent(org.eclipse.swt.events.KeyEvent) GridLayout(org.eclipse.swt.layout.GridLayout) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ArrayList(java.util.ArrayList) List(java.util.List) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) GridData(org.eclipse.swt.layout.GridData) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)

Example 4 with ObjectContainerSelectorPanel

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

the class EditForeignKeyPage method createContainerSelector.

private void createContainerSelector(Composite tableGroup) throws DBException {
    ObjectContainerSelectorPanel containerPanel = new ObjectContainerSelectorPanel(tableGroup, foreignKey.getDataSource().getContainer().getRegistry().getProject(), CONTAINER_LOGICAL_FK, "Reference table container", "Select reference table catalog/schema") {

        @Nullable
        @Override
        protected DBNNode getSelectedNode() {
            if (ownerContainerNode != null) {
                return ownerContainerNode;
            }
            DBSObject containerObject;
            if (ownerTableNode != null) {
                DBNNode containerNode = ownerTableNode.getParentNode();
                while (containerNode instanceof DBNDatabaseFolder) {
                    containerNode = containerNode.getParentNode();
                }
                if (containerNode instanceof DBNDatabaseNode) {
                    containerObject = ((DBNDatabaseNode) containerNode).getObject();
                } else {
                    containerObject = null;
                }
            } else {
                containerObject = foreignKey.getParentObject();
            }
            if (containerObject != null && containerObject.getParentObject() instanceof DBSObjectContainer) {
                containerObject = containerObject.getParentObject();
            }
            if (containerObject instanceof DBVContainer) {
                try {
                    containerObject = ((DBVContainer) containerObject).getRealContainer(new VoidProgressMonitor());
                } catch (DBException e) {
                    log.error("Error getting real object container", e);
                }
            }
            return ownerContainerNode = DBWorkbench.getPlatform().getNavigatorModel().getNodeByObject(containerObject);
        }

        @Override
        protected void setSelectedNode(DBNDatabaseNode node) {
            ownerContainerNode = node;
            if (ownerContainerNode == null) {
                setContainerInfo(null);
            } else {
                setContainerInfo(node);
                loadTableList(ownerContainerNode);
            }
        }
    };
    GridData gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalSpan = 2;
    containerPanel.setLayoutData(gd);
    if (ownerTableNode != null) {
        DBNNode containerNode = ownerTableNode.getParentNode();
        while (containerNode instanceof DBNDatabaseFolder) {
            containerNode = containerNode.getParentNode();
        }
        if (containerNode instanceof DBNDatabaseNode) {
            containerPanel.setContainerInfo((DBNDatabaseNode) containerNode);
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) ObjectContainerSelectorPanel(org.jkiss.dbeaver.ui.controls.ObjectContainerSelectorPanel) GridData(org.eclipse.swt.layout.GridData) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DBNDatabaseFolder(org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Aggregations

GridData (org.eclipse.swt.layout.GridData)4 DBException (org.jkiss.dbeaver.DBException)4 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)4 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)4 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)4 ObjectContainerSelectorPanel (org.jkiss.dbeaver.ui.controls.ObjectContainerSelectorPanel)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 IDialogConstants (org.eclipse.jface.dialogs.IDialogConstants)2 IMessageProvider (org.eclipse.jface.dialogs.IMessageProvider)2 org.eclipse.jface.viewers (org.eclipse.jface.viewers)2 NLS (org.eclipse.osgi.util.NLS)2 SWT (org.eclipse.swt.SWT)2 KeyAdapter (org.eclipse.swt.events.KeyAdapter)2 KeyEvent (org.eclipse.swt.events.KeyEvent)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 GridLayout (org.eclipse.swt.layout.GridLayout)2 org.eclipse.swt.widgets (org.eclipse.swt.widgets)2