Search in sources :

Example 1 with ObjectEditorInput

use of org.jkiss.dbeaver.ui.editors.object.ObjectEditorInput in project dbeaver by serge-rider.

the class NavigatorHandlerObjectOpen method openEntityEditor.

public static IEditorPart openEntityEditor(@NotNull DBNNode selectedNode, @Nullable String defaultPageId, @Nullable Map<String, Object> attributes, IWorkbenchWindow workbenchWindow) {
    if (selectedNode instanceof DBNDataSource) {
        final DataSourceDescriptor dataSourceContainer = (DataSourceDescriptor) ((DBNDataSource) selectedNode).getDataSourceContainer();
        openConnectionEditor(workbenchWindow, dataSourceContainer);
        return null;
    }
    if (!selectedNode.isPersisted()) {
        log.debug("Node '" + selectedNode.getNodeName() + "' s not persisted. Open not possible.");
        return null;
    }
    try {
        String defaultFolderId = null;
        if (selectedNode instanceof DBNDatabaseFolder && !(selectedNode.getParentNode() instanceof DBNDatabaseFolder) && selectedNode.getParentNode() instanceof DBNDatabaseNode) {
            defaultFolderId = selectedNode.getNodeType();
            selectedNode = selectedNode.getParentNode();
        }
        DatabaseEditorInputFactory.setLookupEditor(true);
        try {
            for (IEditorReference ref : workbenchWindow.getActivePage().getEditorReferences()) {
                IEditorInput editorInput;
                try {
                    editorInput = ref.getEditorInput();
                } catch (Throwable e) {
                    continue;
                }
                if (editorInput instanceof INavigatorEditorInput) {
                    boolean matches;
                    if (editorInput instanceof DatabaseLazyEditorInput) {
                        matches = selectedNode.getNodeItemPath().equals(((DatabaseLazyEditorInput) editorInput).getNodePath());
                    } else {
                        matches = ((INavigatorEditorInput) editorInput).getNavigatorNode() == selectedNode;
                    }
                    if (matches) {
                        final IEditorPart editor = ref.getEditor(true);
                        if (editor instanceof ITabbedFolderContainer && defaultFolderId != null) {
                            // Activate default folder
                            ((ITabbedFolderContainer) editor).switchFolder(defaultFolderId);
                        }
                        workbenchWindow.getActivePage().activate(editor);
                        return editor;
                    }
                }
            }
        } finally {
            DatabaseEditorInputFactory.setLookupEditor(false);
        }
        if (selectedNode instanceof DBNDatabaseObject) {
            DBNDatabaseObject objectNode = (DBNDatabaseObject) selectedNode;
            ObjectEditorInput objectInput = new ObjectEditorInput(objectNode);
            setInputAttributes(objectInput, defaultPageId, defaultFolderId, attributes);
            return workbenchWindow.getActivePage().openEditor(objectInput, objectNode.getMeta().getEditorId());
        } else if (selectedNode instanceof DBNDatabaseNode) {
            DBNDatabaseNode dnNode = (DBNDatabaseNode) selectedNode;
            if (dnNode.getObject() != null) {
                EntityEditorInput editorInput = new EntityEditorInput(dnNode);
                if (DBeaverCore.getGlobalPreferenceStore().getBoolean(DBeaverPreferences.NAVIGATOR_REFRESH_EDITORS_ON_OPEN)) {
                    if (dnNode.getObject() instanceof DBSObjectContainer) {
                    // do not auto-refresh object containers (too expensive)
                    } else {
                        refreshDatabaseNode(dnNode);
                    }
                }
                setInputAttributes(editorInput, defaultPageId, defaultFolderId, attributes);
                return workbenchWindow.getActivePage().openEditor(editorInput, EntityEditor.class.getName());
            } else {
                UIUtils.showErrorDialog(workbenchWindow.getShell(), "No object", "Node do not has associated database object");
                return null;
            }
        } else {
            NodeEditorInput folderInput = new NodeEditorInput(selectedNode);
            return workbenchWindow.getActivePage().openEditor(folderInput, FolderEditor.class.getName());
        }
    } catch (Exception ex) {
        UIUtils.showErrorDialog(workbenchWindow.getShell(), CoreMessages.actions_navigator_error_dialog_open_entity_title, "Can't open entity '" + selectedNode.getNodeName() + "'", ex);
        return null;
    }
}
Also used : EntityEditorInput(org.jkiss.dbeaver.ui.editors.entity.EntityEditorInput) ExecutionException(org.eclipse.core.commands.ExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) ITabbedFolderContainer(org.jkiss.dbeaver.ui.controls.folders.ITabbedFolderContainer) ObjectEditorInput(org.jkiss.dbeaver.ui.editors.object.ObjectEditorInput) NodeEditorInput(org.jkiss.dbeaver.ui.editors.entity.NodeEditorInput) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor)

Example 2 with ObjectEditorInput

use of org.jkiss.dbeaver.ui.editors.object.ObjectEditorInput in project dbeaver by dbeaver.

the class NavigatorHandlerObjectOpen method openEntityEditor.

public static IEditorPart openEntityEditor(@NotNull DBNNode selectedNode, @Nullable String defaultPageId, @Nullable Map<String, Object> attributes, IWorkbenchWindow workbenchWindow) {
    if (selectedNode instanceof DBNDataSource) {
        final DataSourceDescriptor dataSourceContainer = (DataSourceDescriptor) ((DBNDataSource) selectedNode).getDataSourceContainer();
        openConnectionEditor(workbenchWindow, dataSourceContainer);
        return null;
    }
    try {
        String defaultFolderId = null;
        if (selectedNode instanceof DBNDatabaseFolder && !(selectedNode.getParentNode() instanceof DBNDatabaseFolder) && selectedNode.getParentNode() instanceof DBNDatabaseNode) {
            defaultFolderId = selectedNode.getNodeType();
            selectedNode = selectedNode.getParentNode();
        }
        IEditorPart editor = findEntityEditor(workbenchWindow, selectedNode);
        if (editor != null) {
            if (editor instanceof ITabbedFolderContainer && defaultFolderId != null) {
                // Activate default folder
                ((ITabbedFolderContainer) editor).switchFolder(defaultFolderId);
            }
            workbenchWindow.getActivePage().activate(editor);
            return editor;
        }
        if (selectedNode instanceof DBNDatabaseObject) {
            DBNDatabaseObject objectNode = (DBNDatabaseObject) selectedNode;
            ObjectEditorInput objectInput = new ObjectEditorInput(objectNode);
            setInputAttributes(objectInput, defaultPageId, defaultFolderId, attributes);
            return workbenchWindow.getActivePage().openEditor(objectInput, objectNode.getMeta().getEditorId());
        } else if (selectedNode instanceof DBNDatabaseNode) {
            DBNDatabaseNode dnNode = (DBNDatabaseNode) selectedNode;
            if (dnNode.getObject() != null) {
                EntityEditorInput editorInput = new EntityEditorInput(dnNode);
                if (DBeaverCore.getGlobalPreferenceStore().getBoolean(DBeaverPreferences.NAVIGATOR_REFRESH_EDITORS_ON_OPEN)) {
                    if (dnNode.getObject() instanceof DBSObjectContainer) {
                    // do not auto-refresh object containers (too expensive)
                    } else {
                        refreshDatabaseNode(dnNode);
                    }
                }
                setInputAttributes(editorInput, defaultPageId, defaultFolderId, attributes);
                return workbenchWindow.getActivePage().openEditor(editorInput, EntityEditor.class.getName());
            } else {
                DBUserInterface.getInstance().showError("No object", "Node do not has associated database object");
                return null;
            }
        } else {
            NodeEditorInput folderInput = new NodeEditorInput(selectedNode);
            return workbenchWindow.getActivePage().openEditor(folderInput, FolderEditor.class.getName());
        }
    } catch (Exception ex) {
        DBUserInterface.getInstance().showError(CoreMessages.actions_navigator_error_dialog_open_entity_title, "Can't open entity '" + selectedNode.getNodeName() + "'", ex);
        return null;
    }
}
Also used : EntityEditorInput(org.jkiss.dbeaver.ui.editors.entity.EntityEditorInput) IEditorPart(org.eclipse.ui.IEditorPart) ExecutionException(org.eclipse.core.commands.ExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) ITabbedFolderContainer(org.jkiss.dbeaver.ui.controls.folders.ITabbedFolderContainer) ObjectEditorInput(org.jkiss.dbeaver.ui.editors.object.ObjectEditorInput) NodeEditorInput(org.jkiss.dbeaver.ui.editors.entity.NodeEditorInput) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor)

Example 3 with ObjectEditorInput

use of org.jkiss.dbeaver.ui.editors.object.ObjectEditorInput in project dbeaver by serge-rider.

the class NavigatorHandlerObjectOpen method openEntityEditor.

public static IEditorPart openEntityEditor(@NotNull DBNNode selectedNode, @Nullable String defaultPageId, @Nullable String defaultFolderId, @Nullable Map<String, Object> attributes, IWorkbenchWindow workbenchWindow, boolean activate) {
    if (selectedNode instanceof DBNDataSource) {
        final DBPDataSourceContainer dataSourceContainer = ((DBNDataSource) selectedNode).getDataSourceContainer();
        openConnectionEditor(workbenchWindow, dataSourceContainer);
        return null;
    }
    try {
        if (selectedNode instanceof DBNDatabaseFolder && !(selectedNode.getParentNode() instanceof DBNDatabaseFolder) && selectedNode.getParentNode() instanceof DBNDatabaseNode) {
            if (defaultFolderId == null) {
                defaultFolderId = selectedNode.getNodeType();
            }
            selectedNode = selectedNode.getParentNode();
        }
        IEditorPart editor = findEntityEditor(workbenchWindow, selectedNode);
        if (editor != null) {
            boolean settingsChanged = false;
            IEditorInput editorInput = editor.getEditorInput();
            if (editorInput instanceof DatabaseEditorInput) {
                settingsChanged = setInputAttributes((DatabaseEditorInput<?>) editorInput, defaultPageId, defaultFolderId, attributes);
            }
            if (editor instanceof EntityEditor && defaultPageId != null) {
                // Set active page
                IEditorPart pageEditor = ((EntityEditor) editor).getPageEditor(defaultPageId);
                if (pageEditor != null && pageEditor != ((EntityEditor) editor).getActiveEditor()) {
                    ((EntityEditor) editor).setActiveEditor(pageEditor);
                }
            }
            if (editor instanceof ITabbedFolderContainer && defaultFolderId != null) {
                // Activate default folder
                if (((ITabbedFolderContainer) editor).switchFolder(defaultFolderId)) {
                    settingsChanged = true;
                }
            }
            if (settingsChanged) {
                if (editor instanceof IRefreshablePart) {
                    ((IRefreshablePart) editor).refreshPart(selectedNode, true);
                }
            }
            if (workbenchWindow.getActivePage().getActiveEditor() != editor || activate) {
                workbenchWindow.getActivePage().activate(editor);
            }
            return editor;
        }
        if (selectedNode instanceof DBNDatabaseNode) {
            DBNDatabaseNode dnNode = (DBNDatabaseNode) selectedNode;
            DBSObject databaseObject = dnNode.getObject();
            if (databaseObject != null) {
                if (!databaseObject.isPersisted()) {
                    return null;
                }
                try {
                    DBUtils.getOrOpenDefaultContext(databaseObject, false);
                } catch (DBCException ignored) {
                    return null;
                }
                if (selectedNode instanceof DBNDatabaseObject) {
                    DBNDatabaseObject objectNode = (DBNDatabaseObject) selectedNode;
                    if (!objectNode.isPersisted()) {
                        return null;
                    }
                    ObjectEditorInput objectInput = new ObjectEditorInput(objectNode);
                    setInputAttributes(objectInput, defaultPageId, defaultFolderId, attributes);
                    return workbenchWindow.getActivePage().openEditor(objectInput, objectNode.getMeta().getEditorId());
                } else {
                    DatabaseNodeEditorInput editorInput = new DatabaseNodeEditorInput(dnNode);
                    if (DBWorkbench.getPlatform().getPreferenceStore().getBoolean(NavigatorPreferences.NAVIGATOR_REFRESH_EDITORS_ON_OPEN)) {
                        refreshDatabaseNode(dnNode);
                    }
                    setInputAttributes(editorInput, defaultPageId, defaultFolderId, attributes);
                    return workbenchWindow.getActivePage().openEditor(editorInput, EntityEditor.class.getName());
                }
            } else {
                DBWorkbench.getPlatformUI().showError("No object", "Node has no associated database object");
                return null;
            }
        } else {
            NodeEditorInput folderInput = new NodeEditorInput(selectedNode);
            return workbenchWindow.getActivePage().openEditor(folderInput, FolderEditor.class.getName());
        }
    } catch (Exception ex) {
        DBWorkbench.getPlatformUI().showError(UINavigatorMessages.actions_navigator_error_dialog_open_entity_title, "Can't open entity '" + selectedNode.getNodeName() + "'", ex);
        return null;
    }
}
Also used : DBCException(org.jkiss.dbeaver.model.exec.DBCException) IEditorPart(org.eclipse.ui.IEditorPart) DBCException(org.jkiss.dbeaver.model.exec.DBCException) ExecutionException(org.eclipse.core.commands.ExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) ITabbedFolderContainer(org.jkiss.dbeaver.ui.controls.folders.ITabbedFolderContainer) IRefreshablePart(org.jkiss.dbeaver.ui.IRefreshablePart) ObjectEditorInput(org.jkiss.dbeaver.ui.editors.object.ObjectEditorInput) EntityEditor(org.jkiss.dbeaver.ui.editors.entity.EntityEditor) FolderEditor(org.jkiss.dbeaver.ui.editors.entity.FolderEditor) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) IEditorInput(org.eclipse.ui.IEditorInput)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ExecutionException (org.eclipse.core.commands.ExecutionException)3 DBException (org.jkiss.dbeaver.DBException)3 ITabbedFolderContainer (org.jkiss.dbeaver.ui.controls.folders.ITabbedFolderContainer)3 ObjectEditorInput (org.jkiss.dbeaver.ui.editors.object.ObjectEditorInput)3 IEditorPart (org.eclipse.ui.IEditorPart)2 DBSObjectContainer (org.jkiss.dbeaver.model.struct.DBSObjectContainer)2 DataSourceDescriptor (org.jkiss.dbeaver.registry.DataSourceDescriptor)2 EntityEditorInput (org.jkiss.dbeaver.ui.editors.entity.EntityEditorInput)2 NodeEditorInput (org.jkiss.dbeaver.ui.editors.entity.NodeEditorInput)2 IEditorInput (org.eclipse.ui.IEditorInput)1 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)1 DBCException (org.jkiss.dbeaver.model.exec.DBCException)1 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)1 IRefreshablePart (org.jkiss.dbeaver.ui.IRefreshablePart)1 EntityEditor (org.jkiss.dbeaver.ui.editors.entity.EntityEditor)1 FolderEditor (org.jkiss.dbeaver.ui.editors.entity.FolderEditor)1