Search in sources :

Example 11 with TreeNodeSpecial

use of org.jkiss.dbeaver.ui.navigator.database.load.TreeNodeSpecial in project dbeaver by serge-rider.

the class ObjectBrowserDialog method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    getShell().setText(title);
    Composite group = (Composite) super.createDialogArea(parent);
    GridData gd = new GridData(GridData.FILL_BOTH);
    group.setLayoutData(gd);
    DatabaseNavigatorTreeFilter filter = new DatabaseNavigatorTreeFilter() {

        @Override
        public boolean isLeafObject(Object object) {
            if (leafTypes != null && leafTypes.length > 0) {
                if (object instanceof DBNDatabaseNode) {
                    DBSObject dbObject = ((DBNDatabaseNode) object).getObject();
                    if (dbObject != null) {
                        for (Class<?> leafType : leafTypes) {
                            if (leafType.isAssignableFrom(dbObject.getClass())) {
                                return true;
                            }
                        }
                    }
                }
            }
            return super.isLeafObject(object);
        }
    };
    navigatorTree = new DatabaseNavigatorTree(group, rootNode, (singleSelection ? SWT.SINGLE : SWT.MULTI) | SWT.BORDER, false, filter);
    gd = new GridData(GridData.FILL_BOTH);
    gd.widthHint = 500;
    gd.heightHint = 500;
    navigatorTree.setLayoutData(gd);
    final TreeViewer treeViewer = navigatorTree.getViewer();
    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();
                }
            }
            if (element instanceof TreeNodeSpecial || element instanceof DBNLocalFolder) {
                return true;
            }
            if (element instanceof DBNNode) {
                if (element instanceof DBNDatabaseFolder) {
                    DBNDatabaseFolder folder = (DBNDatabaseFolder) element;
                    Class<? extends DBSObject> folderItemsClass = folder.getChildrenClass();
                    return folderItemsClass != null && matchesType(folderItemsClass, false);
                }
                if (element instanceof DBNProject || element instanceof DBNProjectDatabases || element instanceof DBNDataSource || (element instanceof DBSWrapper && matchesType(((DBSWrapper) element).getObject().getClass(), false))) {
                    return true;
                }
            }
            return false;
        }
    });
    if (selectedNode != null) {
        treeViewer.setSelection(new StructuredSelection(selectedNode));
        if (!(selectedNode instanceof DBNDataSource) || ((DBNDataSource) selectedNode).getDataSourceContainer().isConnected()) {
            treeViewer.expandToLevel(selectedNode, 1);
        }
        selectedObjects.add(selectedNode);
    }
    treeViewer.addSelectionChangedListener(event -> {
        selectedObjects.clear();
        specialNode = null;
        IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
        for (Iterator iter = selection.iterator(); iter.hasNext(); ) {
            Object node = iter.next();
            if (node instanceof DBNNode && node instanceof DBSWrapper) {
                DBSObject object = DBUtils.getAdapter(DBSObject.class, ((DBSWrapper) node).getObject());
                if (object != null) {
                    if (!matchesType(object.getClass(), true)) {
                        selectedObjects.clear();
                        break;
                    }
                    selectedObjects.add((DBNNode) node);
                }
            } else if (node instanceof TreeNodeSpecial) {
                specialNode = (TreeNodeSpecial) node;
            }
        }
        getButton(IDialogConstants.OK_ID).setEnabled(!selectedObjects.isEmpty());
    });
    treeViewer.addDoubleClickListener(event -> {
        if (!selectedObjects.isEmpty()) {
            okPressed();
        } else if (specialNode != null) {
            specialNode.handleDefaultAction(navigatorTree);
        }
    });
    treeViewer.getTree().setFocus();
    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);
            }
        }
    });
    return group;
}
Also used : DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) DatabaseNavigatorTreeFilter(org.jkiss.dbeaver.ui.navigator.database.DatabaseNavigatorTreeFilter) Button(org.eclipse.swt.widgets.Button) Iterator(java.util.Iterator) SelectionEvent(org.eclipse.swt.events.SelectionEvent) TreeNodeSpecial(org.jkiss.dbeaver.ui.navigator.database.load.TreeNodeSpecial) Composite(org.eclipse.swt.widgets.Composite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) DatabaseNavigatorTree(org.jkiss.dbeaver.ui.navigator.database.DatabaseNavigatorTree) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) GridData(org.eclipse.swt.layout.GridData) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject)

Example 12 with TreeNodeSpecial

use of org.jkiss.dbeaver.ui.navigator.database.load.TreeNodeSpecial in project dbeaver by serge-rider.

the class NavigatorViewBase method createNavigatorTree.

private DatabaseNavigatorTree createNavigatorTree(Composite parent, DBNNode rootNode) {
    // Create tree
    final DatabaseNavigatorTree navigatorTree = new DatabaseNavigatorTree(parent, rootNode, getTreeStyle(), false, getNavigatorFilter());
    createTreeColumns(navigatorTree);
    navigatorTree.getViewer().addSelectionChangedListener(event -> onSelectionChange((IStructuredSelection) event.getSelection()));
    navigatorTree.getViewer().getTree().addListener(SWT.MouseDoubleClick, event -> {
        event.doit = false;
    });
    navigatorTree.getViewer().getTree().addMouseListener(new MouseAdapter() {

        @Override
        public void mouseDoubleClick(MouseEvent e) {
            super.mouseDoubleClick(e);
        }

        @Override
        public void mouseDown(MouseEvent e) {
            super.mouseDown(e);
        }

        @Override
        public void mouseUp(MouseEvent e) {
            super.mouseUp(e);
        }
    });
    navigatorTree.getViewer().addDoubleClickListener(event -> {
        TreeViewer viewer = tree.getViewer();
        IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
        for (Object node : selection.toArray()) {
            // Object node = selection.getFirstElement();
            if ((node instanceof DBNResource && ((DBNResource) node).getResource() instanceof IFolder)) {
                toggleNode(viewer, node);
            } else if (node instanceof DBNDataSource) {
                NavigatorPreferences.DoubleClickBehavior dsBehaviorDefault = CommonUtils.valueOf(NavigatorPreferences.DoubleClickBehavior.class, DBWorkbench.getPlatform().getPreferenceStore().getString(NavigatorPreferences.NAVIGATOR_CONNECTION_DOUBLE_CLICK), NavigatorPreferences.DoubleClickBehavior.EDIT);
                if (dsBehaviorDefault == NavigatorPreferences.DoubleClickBehavior.EXPAND) {
                    toggleNode(viewer, node);
                } else {
                    DBPDataSourceContainer dataSource = ((DBNDataSource) node).getObject();
                    switch(dsBehaviorDefault) {
                        case EDIT:
                            NavigatorHandlerObjectOpen.openEntityEditor((DBNDataSource) node, null, UIUtils.getActiveWorkbenchWindow());
                            break;
                        case CONNECT:
                            {
                                UIServiceConnections serviceConnections = DBWorkbench.getService(UIServiceConnections.class);
                                if (serviceConnections != null) {
                                    if (dataSource.isConnected()) {
                                        serviceConnections.disconnectDataSource(dataSource);
                                    } else {
                                        serviceConnections.connectDataSource(dataSource, null);
                                    }
                                }
                                break;
                            }
                        case SQL_EDITOR:
                            {
                                UIServiceSQL serviceSQL = DBWorkbench.getService(UIServiceSQL.class);
                                if (serviceSQL != null) {
                                    serviceSQL.openRecentScript(dataSource);
                                }
                                break;
                            }
                        case SQL_EDITOR_NEW:
                            {
                                UIServiceSQL serviceSQL = DBWorkbench.getService(UIServiceSQL.class);
                                if (serviceSQL != null) {
                                    serviceSQL.openNewScript(dataSource);
                                }
                                break;
                            }
                    }
                }
            } else if (node instanceof TreeNodeSpecial) {
                ((TreeNodeSpecial) node).handleDefaultAction(navigatorTree);
            } else {
                String defaultEditorPageId = null;
                NavigatorPreferences.DoubleClickBehavior dcBehaviorDefault = CommonUtils.valueOf(NavigatorPreferences.DoubleClickBehavior.class, DBWorkbench.getPlatform().getPreferenceStore().getString(NavigatorPreferences.NAVIGATOR_OBJECT_DOUBLE_CLICK));
                if (node instanceof DBNDatabaseNode && ((DBNDatabaseNode) node).getObject() instanceof DBSDataContainer) {
                    defaultEditorPageId = DBWorkbench.getPlatform().getPreferenceStore().getString(NavigatorPreferences.NAVIGATOR_DEFAULT_EDITOR_PAGE);
                }
                boolean hasChildren = node instanceof DBNNode && ((DBNNode) node).hasChildren(true);
                if (hasChildren && dcBehaviorDefault == NavigatorPreferences.DoubleClickBehavior.EXPAND) {
                    toggleNode(viewer, node);
                } else {
                    Map<String, Object> parameters = null;
                    if (!CommonUtils.isEmpty(defaultEditorPageId)) {
                        parameters = Collections.singletonMap(MultiPageDatabaseEditor.PARAMETER_ACTIVE_PAGE, defaultEditorPageId);
                    }
                    NavigatorUtils.executeNodeAction(DBXTreeNodeHandler.Action.open, node, parameters, getSite());
                }
            }
        }
    });
    // Hook context menu
    NavigatorUtils.addContextMenu(this.getSite(), navigatorTree.getViewer());
    // Add drag and drop support
    NavigatorUtils.addDragAndDropSupport(navigatorTree.getViewer());
    DBWorkbench.getPlatform().getPreferenceStore().addPropertyChangeListener(this);
    return navigatorTree;
}
Also used : MouseEvent(org.eclipse.swt.events.MouseEvent) TreeViewer(org.eclipse.jface.viewers.TreeViewer) MouseAdapter(org.eclipse.swt.events.MouseAdapter) NavigatorPreferences(org.jkiss.dbeaver.ui.navigator.NavigatorPreferences) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) UIServiceSQL(org.jkiss.dbeaver.runtime.ui.UIServiceSQL) DBSDataContainer(org.jkiss.dbeaver.model.struct.DBSDataContainer) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) UIServiceConnections(org.jkiss.dbeaver.runtime.ui.UIServiceConnections) TreeNodeSpecial(org.jkiss.dbeaver.ui.navigator.database.load.TreeNodeSpecial) IFolder(org.eclipse.core.resources.IFolder)

Example 13 with TreeNodeSpecial

use of org.jkiss.dbeaver.ui.navigator.database.load.TreeNodeSpecial in project dbeaver by serge-rider.

the class DatabaseNavigatorContentProvider method getChildren.

@Override
public Object[] getChildren(final Object parent) {
    if (parent instanceof TreeNodeSpecial) {
        return EMPTY_CHILDREN;
    }
    if (!(parent instanceof DBNNode)) {
        return EMPTY_CHILDREN;
    }
    // view.getNavigatorModel().findNode(parent);
    final DBNNode parentNode = (DBNNode) parent;
    /*
        if (parentNode == null) {
            log.error("Can't find parent node '" + ((DBSObject) parent).getName() + "' in model");
            return EMPTY_CHILDREN;
        }
*/
    if (!parentNode.hasChildren(true)) {
        return EMPTY_CHILDREN;
    }
    if (parentNode instanceof DBNDatabaseNode && ((DBNDatabaseNode) parentNode).needsInitialization()) {
        return TreeLoadVisualizer.expandChildren(navigatorTree.getViewer(), new TreeLoadService("Loading", ((DBNDatabaseNode) parentNode)));
    } else {
        try {
            // Read children with null monitor cos' it's not a lazy node
            // and no blocking process will occur
            DBNNode[] children = DBNUtils.getNodeChildrenFiltered(new VoidProgressMonitor(), parentNode, true);
            if (ArrayUtils.isEmpty(children)) {
                return EMPTY_CHILDREN;
            } else {
                int longListFetchSize = Math.max(NavigatorPreferences.MIN_LONG_LIST_FETCH_SIZE, DBWorkbench.getPlatform().getPreferenceStore().getInt(NavigatorPreferences.NAVIGATOR_LONG_LIST_FETCH_SIZE));
                if (children.length > longListFetchSize) {
                    Object[] curChildren = new Object[longListFetchSize + 1];
                    System.arraycopy(children, 0, curChildren, 0, longListFetchSize);
                    curChildren[longListFetchSize] = new TreeNodeLazyExpander(parentNode, children, longListFetchSize);
                    return curChildren;
                }
                return children;
            }
        } catch (Throwable ex) {
            DBWorkbench.getPlatformUI().showError("Navigator error", ex.getMessage(), ex);
            // Collapse this item
            UIUtils.asyncExec(() -> {
                navigatorTree.getViewer().collapseToLevel(parent, 1);
                navigatorTree.getViewer().refresh(parent);
            });
            return EMPTY_CHILDREN;
        }
    }
}
Also used : TreeNodeLazyExpander(org.jkiss.dbeaver.ui.navigator.database.load.TreeNodeLazyExpander) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) TreeLoadService(org.jkiss.dbeaver.ui.navigator.database.load.TreeLoadService) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) TreeNodeSpecial(org.jkiss.dbeaver.ui.navigator.database.load.TreeNodeSpecial) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 14 with TreeNodeSpecial

use of org.jkiss.dbeaver.ui.navigator.database.load.TreeNodeSpecial in project dbeaver by dbeaver.

the class ObjectBrowserDialog method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    getShell().setText(title);
    Composite group = (Composite) super.createDialogArea(parent);
    GridData gd = new GridData(GridData.FILL_BOTH);
    group.setLayoutData(gd);
    DatabaseNavigatorTreeFilter filter = new DatabaseNavigatorTreeFilter() {

        @Override
        public boolean isLeafObject(Object object) {
            if (leafTypes != null && leafTypes.length > 0) {
                if (object instanceof DBNDatabaseNode) {
                    DBSObject dbObject = ((DBNDatabaseNode) object).getObject();
                    if (dbObject != null) {
                        for (Class<?> leafType : leafTypes) {
                            if (leafType.isAssignableFrom(dbObject.getClass())) {
                                return true;
                            }
                        }
                    }
                }
            }
            return super.isLeafObject(object);
        }
    };
    navigatorTree = new DatabaseNavigatorTree(group, rootNode, (singleSelection ? SWT.SINGLE : SWT.MULTI) | SWT.BORDER, false, filter);
    gd = new GridData(GridData.FILL_BOTH);
    gd.widthHint = 500;
    gd.heightHint = 500;
    navigatorTree.setLayoutData(gd);
    final TreeViewer treeViewer = navigatorTree.getViewer();
    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();
                }
            }
            if (element instanceof TreeNodeSpecial || element instanceof DBNLocalFolder) {
                return true;
            }
            if (element instanceof DBNNode) {
                if (element instanceof DBNDatabaseFolder) {
                    DBNDatabaseFolder folder = (DBNDatabaseFolder) element;
                    Class<? extends DBSObject> folderItemsClass = folder.getChildrenClass();
                    return folderItemsClass != null && matchesType(folderItemsClass, false);
                }
                if (element instanceof DBNProject || element instanceof DBNProjectDatabases || element instanceof DBNDataSource || (element instanceof DBSWrapper && matchesType(((DBSWrapper) element).getObject().getClass(), false))) {
                    return true;
                }
            }
            return false;
        }
    });
    if (selectedNode != null) {
        treeViewer.setSelection(new StructuredSelection(selectedNode));
        if (!(selectedNode instanceof DBNDataSource) || ((DBNDataSource) selectedNode).getDataSourceContainer().isConnected()) {
            treeViewer.expandToLevel(selectedNode, 1);
        }
        selectedObjects.add(selectedNode);
    }
    treeViewer.addSelectionChangedListener(event -> {
        selectedObjects.clear();
        specialNode = null;
        IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
        for (Iterator iter = selection.iterator(); iter.hasNext(); ) {
            Object node = iter.next();
            if (node instanceof DBNNode && node instanceof DBSWrapper) {
                DBSObject object = DBUtils.getAdapter(DBSObject.class, ((DBSWrapper) node).getObject());
                if (object != null) {
                    if (!matchesType(object.getClass(), true)) {
                        selectedObjects.clear();
                        break;
                    }
                    selectedObjects.add((DBNNode) node);
                }
            } else if (node instanceof TreeNodeSpecial) {
                specialNode = (TreeNodeSpecial) node;
            }
        }
        getButton(IDialogConstants.OK_ID).setEnabled(!selectedObjects.isEmpty());
    });
    treeViewer.addDoubleClickListener(event -> {
        if (!selectedObjects.isEmpty()) {
            okPressed();
        } else if (specialNode != null) {
            specialNode.handleDefaultAction(navigatorTree);
        }
    });
    treeViewer.getTree().setFocus();
    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);
            }
        }
    });
    return group;
}
Also used : DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) DatabaseNavigatorTreeFilter(org.jkiss.dbeaver.ui.navigator.database.DatabaseNavigatorTreeFilter) Button(org.eclipse.swt.widgets.Button) Iterator(java.util.Iterator) SelectionEvent(org.eclipse.swt.events.SelectionEvent) TreeNodeSpecial(org.jkiss.dbeaver.ui.navigator.database.load.TreeNodeSpecial) Composite(org.eclipse.swt.widgets.Composite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) DatabaseNavigatorTree(org.jkiss.dbeaver.ui.navigator.database.DatabaseNavigatorTree) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) GridData(org.eclipse.swt.layout.GridData) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject)

Aggregations

TreeNodeSpecial (org.jkiss.dbeaver.ui.navigator.database.load.TreeNodeSpecial)14 GridData (org.eclipse.swt.layout.GridData)10 DatabaseNavigatorTree (org.jkiss.dbeaver.ui.navigator.database.DatabaseNavigatorTree)10 Composite (org.eclipse.swt.widgets.Composite)8 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)6 SelectionEvent (org.eclipse.swt.events.SelectionEvent)6 INavigatorFilter (org.jkiss.dbeaver.ui.navigator.INavigatorFilter)6 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 MouseAdapter (org.eclipse.swt.events.MouseAdapter)4 MouseEvent (org.eclipse.swt.events.MouseEvent)4 Button (org.eclipse.swt.widgets.Button)4 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 java.util (java.util)2 Iterator (java.util.Iterator)2 List (java.util.List)2 IFile (org.eclipse.core.resources.IFile)2 IFolder (org.eclipse.core.resources.IFolder)2 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)2 TreeViewer (org.eclipse.jface.viewers.TreeViewer)2