Search in sources :

Example 16 with DBPDataSourceFolder

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

the class SelectDataSourceDialog method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    showConnected = getDialogBoundsSettings().getBoolean(PARAM_SHOW_CONNECTED);
    showAllProjects = getDialogBoundsSettings().getBoolean(PARAM_SHOW_ALL_PROJECTS);
    Composite group = (Composite) super.createDialogArea(parent);
    GridData gd = new GridData(GridData.FILL_BOTH);
    group.setLayoutData(gd);
    rootNode = DBWorkbench.getPlatform().getNavigatorModel().getRoot();
    projectNode = null;
    if (project != null) {
        DBNProject projectBaseNode = DBWorkbench.getPlatform().getNavigatorModel().getRoot().getProjectNode(project);
        if (projectBaseNode != null) {
            projectNode = projectBaseNode.getDatabases();
        }
    }
    INavigatorFilter dsFilter = new INavigatorFilter() {

        @Override
        public boolean filterFolders() {
            return true;
        }

        @Override
        public boolean isLeafObject(Object object) {
            return object instanceof DBNDataSource;
        }

        @Override
        public boolean select(Object element) {
            return element instanceof DBNProject || element instanceof DBNProjectDatabases || element instanceof DBNLocalFolder;
        }
    };
    DatabaseNavigatorTree dataSourceTree = new DatabaseNavigatorTree(group, getTreeRootNode(), SWT.SINGLE | SWT.BORDER, false, dsFilter) {

        @Override
        protected void onTreeRefresh() {
            DBNNode treeRootNode = getTreeRootNode();
            if (dataSource != null && projectNode != null) {
                DBPDataSourceFolder dsFolder;
                for (dsFolder = dataSource.getFolder(); dsFolder != null; dsFolder = dsFolder.getParent()) {
                    if (dsFolder.getParent() == null) {
                        break;
                    }
                }
                if (dsFolder != null) {
                    // Expand only current DS folder
                    DBNLocalFolder folderNode = projectNode.getFolderNode(dsFolder);
                    if (folderNode != null) {
                        expandFolders(this, folderNode);
                    }
                } else {
                // Do not expand anything
                }
                return;
            }
            expandFolders(this, treeRootNode);
        }
    };
    gd = new GridData(GridData.FILL_BOTH);
    gd.heightHint = 500;
    gd.minimumHeight = 100;
    gd.minimumWidth = 100;
    dataSourceTree.setLayoutData(gd);
    final TreeViewer treeViewer = dataSourceTree.getViewer();
    final Text descriptionText = new Text(group, SWT.READ_ONLY);
    descriptionText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    final Button showConnectedCheck = new Button(group, SWT.CHECK);
    showConnectedCheck.setText(UINavigatorMessages.label_show_connected);
    showConnectedCheck.setSelection(showConnected);
    showConnectedCheck.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            showConnected = showConnectedCheck.getSelection();
            treeViewer.getControl().setRedraw(false);
            try {
                treeViewer.refresh();
                if (showConnected) {
                    treeViewer.expandAll();
                }
            } finally {
                treeViewer.getControl().setRedraw(true);
            }
            getDialogBoundsSettings().put(PARAM_SHOW_CONNECTED, showConnected);
        }
    });
    final Button showAllProjectsCheck = new Button(group, SWT.CHECK);
    showAllProjectsCheck.setText(UINavigatorMessages.label_show_all_projects);
    showAllProjectsCheck.setSelection(showAllProjects);
    showAllProjectsCheck.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            showAllProjects = showAllProjectsCheck.getSelection();
            treeViewer.getControl().setRedraw(false);
            try {
                dataSourceTree.reloadTree(getTreeRootNode());
                if (showAllProjects) {
                    treeViewer.expandToLevel(3);
                }
            } finally {
                treeViewer.getControl().setRedraw(true);
            }
            getDialogBoundsSettings().put(PARAM_SHOW_ALL_PROJECTS, showAllProjects);
        }
    });
    if (this.dataSource != null) {
        DBNDatabaseNode dsNode = DBWorkbench.getPlatform().getNavigatorModel().getNodeByObject(this.dataSource);
        if (dsNode != null) {
            treeViewer.setSelection(new StructuredSelection(dsNode), true);
        }
    }
    group.setTabList(new Control[] { dataSourceTree, showConnectedCheck, showAllProjectsCheck });
    treeViewer.addFilter(new ViewerFilter() {

        @Override
        public boolean select(Viewer viewer, Object parentElement, Object element) {
            if (showConnected) {
                if (element instanceof DBNDataSource) {
                    return ((DBNDataSource) element).getDataSource() != null;
                }
                if (element instanceof DBNLocalFolder) {
                    return ((DBNLocalFolder) element).hasConnected();
                }
            }
            return element instanceof DBNProject || element instanceof DBNProjectDatabases || element instanceof DBNLocalFolder || element instanceof DBNDataSource;
        }
    });
    treeViewer.addSelectionChangedListener(event -> {
        IStructuredSelection structSel = (IStructuredSelection) event.getSelection();
        Object selNode = structSel.isEmpty() ? null : structSel.getFirstElement();
        if (selNode instanceof DBNDataSource) {
            dataSource = ((DBNDataSource) selNode).getObject();
            getButton(IDialogConstants.OK_ID).setEnabled(true);
            String description = dataSource.getDescription();
            if (description == null) {
                description = dataSource.getName();
            }
            descriptionText.setText(description);
        } else {
            dataSource = null;
            getButton(IDialogConstants.OK_ID).setEnabled(false);
        }
    });
    treeViewer.addDoubleClickListener(event -> {
        if (getButton(IDialogConstants.OK_ID).isEnabled()) {
            okPressed();
        }
    });
    UIUtils.asyncExec(() -> {
        Point treeSize = dataSourceTree.getViewer().getTree().computeSize(SWT.DEFAULT, SWT.DEFAULT);
        Point shellSize = getShell().getSize();
        if (treeSize.x >= shellSize.x) {
            Point shellCompSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
            getShell().setSize(shellCompSize.x, shellSize.y);
            getShell().layout(true);
        }
        dataSourceTree.getFilterControl().setFocus();
        if (showConnected) {
            treeViewer.expandAll();
        }
    });
    closeOnFocusLost(treeViewer.getControl(), dataSourceTree.getFilterControl(), descriptionText, showConnectedCheck, showAllProjectsCheck);
    return group;
}
Also used : SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) INavigatorFilter(org.jkiss.dbeaver.ui.navigator.INavigatorFilter) Point(org.eclipse.swt.graphics.Point) DatabaseNavigatorTree(org.jkiss.dbeaver.ui.navigator.database.DatabaseNavigatorTree) DBPDataSourceFolder(org.jkiss.dbeaver.model.DBPDataSourceFolder) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent)

Aggregations

DBPDataSourceFolder (org.jkiss.dbeaver.model.DBPDataSourceFolder)16 DBPDataSourceRegistry (org.jkiss.dbeaver.model.app.DBPDataSourceRegistry)10 DBPConnectionConfiguration (org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)7 TreeViewer (org.eclipse.jface.viewers.TreeViewer)4 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)4 DBNDataSource (org.jkiss.dbeaver.model.navigator.DBNDataSource)4 NavigatorViewBase (org.jkiss.dbeaver.ui.navigator.database.NavigatorViewBase)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 IFile (org.eclipse.core.resources.IFile)3 IFolder (org.eclipse.core.resources.IFolder)3 Map (java.util.Map)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 Point (org.eclipse.swt.graphics.Point)2 GridData (org.eclipse.swt.layout.GridData)2 DBPProject (org.jkiss.dbeaver.model.app.DBPProject)2 DBPDriver (org.jkiss.dbeaver.model.connection.DBPDriver)2 DBWHandlerConfiguration (org.jkiss.dbeaver.model.net.DBWHandlerConfiguration)2