public static void filterSelection(final ISelection selection, boolean exclude) {
        if (!(selection instanceof IStructuredSelection structuredSelection)) {
            log.error("Invalid selection type: " + selection);
            return;
        }
        try {
            Map<DBNDatabaseNode, DBSObjectFilter> folders = new HashMap<>();
            for (Object item : structuredSelection.toArray()) {
                if (!(item instanceof DBNDatabaseNode node)) {
                    continue;
                }
                DBNDatabaseNode parentNode = node.getParentNode() instanceof DBNDatabaseNode parent ? parent : node;
                {
                    DBXTreeItem nodeMeta = UIUtils.runWithMonitor(monitor -> {
                        DBXTreeItem meta = DBNUtils.getValidItemsMeta(monitor, node);
                        if (meta == null && node != parentNode) {
                            meta = DBNUtils.getValidItemsMeta(monitor, parentNode);
                        }
                        return meta;
                    });
                    if (nodeMeta == null) {
                        continue;
                    }

                    DBSObjectFilter nodeFilter = folders.get(parentNode);
                    if (nodeFilter == null) {
                        nodeFilter = parentNode.getNodeFilter(nodeMeta, true);
                        if (nodeFilter == null) {
                            nodeFilter = new DBSObjectFilter();
                        }
                        folders.put(parentNode, nodeFilter);
                    }
                    if (exclude) {
                        nodeFilter.addExclude(node.getNodeDisplayName());
                    } else {
                        nodeFilter.addInclude(node.getNodeDisplayName());
                    }
                    nodeFilter.setEnabled(true);
                }
            }
            // Save folders
            Set<DBPDataSourceContainer> changedContainers = new HashSet<>();
            for (Map.Entry<DBNDatabaseNode, DBSObjectFilter> entry : folders.entrySet()) {
                DBNDatabaseNode targetNode = entry.getKey();
                DBXTreeItem nodeMeta = UIUtils.runWithMonitor(monitor -> DBNUtils.getValidItemsMeta(monitor, targetNode));
                targetNode.setNodeFilter(
                    nodeMeta,
                    entry.getValue(),
                    false);
                changedContainers.add(targetNode.getDataSourceContainer());
            }
            // Save configs
            for (DBPDataSourceContainer ds : changedContainers) {
                ds.persistConfiguration();
            }
            // Refresh all folders
            NavigatorHandlerRefresh.refreshNavigator(folders.keySet());
        } catch (DBException e) {
            log.error(e);
        }
    }
Find selected node for specified UI element
@param element ui element
@return node or null
plugins/org.jkiss.dbeaver.ui.navigator/src/org/jkiss/dbeaver/ui/navigator/NavigatorUtils.java