Search in sources :

Example 1 with EntityEditorsRegistry

use of org.jkiss.dbeaver.registry.editor.EntityEditorsRegistry in project dbeaver by dbeaver.

the class NavigatorHandlerObjectCreateBase method createNewObject.

protected boolean createNewObject(final IWorkbenchWindow workbenchWindow, DBNNode element, DBNDatabaseNode copyFrom) {
    try {
        DBNContainer container = null;
        if (element instanceof DBNContainer && !(element instanceof DBNDataSource)) {
            container = (DBNContainer) element;
        } else {
            DBNNode parentNode = element.getParentNode();
            if (parentNode instanceof DBNContainer) {
                container = (DBNContainer) parentNode;
            }
        }
        if (container == null) {
            throw new DBException("Can't detect container for '" + element.getNodeName() + "'");
        }
        Class<?> childType = container.getChildrenClass();
        if (childType == null) {
            throw new DBException("Can't determine child element type for container '" + container + "'");
        }
        if (childType == IProject.class) {
            return NavigatorHandlerProjectCreate.createNewProject(workbenchWindow);
        }
        DBSObject sourceObject = copyFrom == null ? null : copyFrom.getObject();
        // Do not check for type - manager must do it. Potentially we can copy anything into anything.
        // if (sourceObject != null && !childType.isAssignableFrom(sourceObject.getClass())) {
        // throw new DBException("Can't create '" + childType.getName() + "' from '" + sourceObject.getClass().getName() + "'");
        // }
        final EntityEditorsRegistry editorsRegistry = EntityEditorsRegistry.getInstance();
        DBEObjectManager<?> objectManager = editorsRegistry.getObjectManager(childType);
        if (objectManager == null) {
            throw new DBException("Object manager not found for type '" + childType.getName() + "'");
        }
        DBEObjectMaker objectMaker = (DBEObjectMaker) objectManager;
        DBPDataSource dataSource = container instanceof DBNDatabaseNode ? ((DBNDatabaseNode) container).getDataSource() : null;
        final boolean openEditor = dataSource != null && (objectMaker.getMakerOptions(dataSource) & DBEObjectMaker.FEATURE_EDITOR_ON_CREATE) != 0;
        CommandTarget commandTarget = getCommandTarget(workbenchWindow, container, childType, openEditor);
        // Parent is model object - not node
        final Object parentObject = container.getValueObject();
        if (parentObject instanceof DBPObject) {
            createDatabaseObject(commandTarget, objectMaker, (DBPObject) parentObject, sourceObject);
        } else {
            throw new DBException("Parent object type is not supported: " + parentObject);
        }
    } catch (Throwable e) {
        DBUserInterface.getInstance().showError("Create object", null, e);
        return false;
    }
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPObject(org.jkiss.dbeaver.model.DBPObject) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) DBNContainer(org.jkiss.dbeaver.model.navigator.DBNContainer) DBEObjectMaker(org.jkiss.dbeaver.model.edit.DBEObjectMaker) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) EntityEditorsRegistry(org.jkiss.dbeaver.registry.editor.EntityEditorsRegistry) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPScriptObject(org.jkiss.dbeaver.model.DBPScriptObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPRefreshableObject(org.jkiss.dbeaver.model.DBPRefreshableObject) DBPObject(org.jkiss.dbeaver.model.DBPObject) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 2 with EntityEditorsRegistry

use of org.jkiss.dbeaver.registry.editor.EntityEditorsRegistry in project dbeaver by serge-rider.

the class NavigatorHandlerObjectCreateBase method createNewObject.

protected boolean createNewObject(final IWorkbenchWindow workbenchWindow, DBNNode element, DBNDatabaseNode copyFrom) {
    try {
        DBNContainer container = null;
        if (element instanceof DBNContainer) {
            container = (DBNContainer) element;
        } else {
            DBNNode parentNode = element.getParentNode();
            if (parentNode instanceof DBNContainer) {
                container = (DBNContainer) parentNode;
            }
        }
        if (container == null) {
            throw new DBException("Can't detect container for '" + element.getNodeName() + "'");
        }
        Class<?> childType = container.getChildrenClass();
        if (childType == null) {
            throw new DBException("Can't determine child element type for container '" + container + "'");
        }
        if (childType == IProject.class) {
            return NavigatorHandlerProjectCreate.createNewProject(workbenchWindow);
        }
        DBSObject sourceObject = copyFrom == null ? null : copyFrom.getObject();
        // Do not check for type - manager must do it. Potentially we can copy anything into anything.
        //            if (sourceObject != null && !childType.isAssignableFrom(sourceObject.getClass())) {
        //                throw new DBException("Can't create '" + childType.getName() + "' from '" + sourceObject.getClass().getName() + "'");
        //            }
        final EntityEditorsRegistry editorsRegistry = EntityEditorsRegistry.getInstance();
        DBEObjectManager<?> objectManager = editorsRegistry.getObjectManager(childType);
        if (objectManager == null) {
            throw new DBException("Object manager not found for type '" + childType.getName() + "'");
        }
        DBEObjectMaker objectMaker = (DBEObjectMaker) objectManager;
        final boolean openEditor = (objectMaker.getMakerOptions() & DBEObjectMaker.FEATURE_EDITOR_ON_CREATE) != 0;
        CommandTarget commandTarget = getCommandTarget(workbenchWindow, container, childType, openEditor);
        final Object parentObject = container.getValueObject();
        createDatabaseObject(commandTarget, objectMaker, parentObject, sourceObject);
    } catch (Throwable e) {
        UIUtils.showErrorDialog(workbenchWindow.getShell(), "Create object", null, e);
        return false;
    }
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBNContainer(org.jkiss.dbeaver.model.navigator.DBNContainer) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPRefreshableObject(org.jkiss.dbeaver.model.DBPRefreshableObject) DBEObjectMaker(org.jkiss.dbeaver.model.edit.DBEObjectMaker) EntityEditorsRegistry(org.jkiss.dbeaver.registry.editor.EntityEditorsRegistry)

Example 3 with EntityEditorsRegistry

use of org.jkiss.dbeaver.registry.editor.EntityEditorsRegistry in project dbeaver by dbeaver.

the class EntityEditor method addContributions.

private void addContributions(String position) {
    EntityEditorsRegistry editorsRegistry = EntityEditorsRegistry.getInstance();
    final DBSObject databaseObject = getEditorInput().getDatabaseObject();
    DBPObject object;
    if (databaseObject instanceof DBPDataSourceContainer && databaseObject.getDataSource() != null) {
        object = databaseObject.getDataSource();
    } else {
        object = databaseObject;
    }
    List<EntityEditorDescriptor> descriptors = editorsRegistry.getEntityEditors(object, this, position);
    for (EntityEditorDescriptor descriptor : descriptors) {
        if (descriptor.getType() == EntityEditorDescriptor.Type.editor) {
            addEditorTab(descriptor);
        }
    }
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) EntityEditorDescriptor(org.jkiss.dbeaver.registry.editor.EntityEditorDescriptor) EntityEditorsRegistry(org.jkiss.dbeaver.registry.editor.EntityEditorsRegistry)

Example 4 with EntityEditorsRegistry

use of org.jkiss.dbeaver.registry.editor.EntityEditorsRegistry in project dbeaver by dbeaver.

the class EntityEditor method createPages.

@Override
protected void createPages() {
    final IDatabaseEditorInput editorInput = getEditorInput();
    if (editorInput instanceof DatabaseLazyEditorInput) {
        try {
            addPage(new ProgressEditorPart(this), editorInput);
            setPageText(0, "Initializing ...");
            setActivePage(0);
        } catch (PartInitException e) {
            log.error(e);
        }
        return;
    } else if (editorInput instanceof ErrorEditorInput) {
        ErrorEditorInput errorInput = (ErrorEditorInput) editorInput;
        try {
            addPage(new ErrorEditorPartEx(errorInput.getError()), errorInput);
            setPageImage(0, UIUtils.getShardImage(ISharedImages.IMG_OBJS_ERROR_TSK));
            setPageText(0, "Error");
            setActivePage(0);
        } catch (PartInitException e) {
            log.error(e);
        }
        return;
    }
    // Command listener
    commandListener = new DBECommandAdapter() {

        @Override
        public void onCommandChange(DBECommand command) {
            DBeaverUI.syncExec(() -> firePropertyChange(IEditorPart.PROP_DIRTY));
        }
    };
    DBECommandContext commandContext = getCommandContext();
    if (commandContext != null) {
        commandContext.addCommandListener(commandListener);
    }
    // Property listener
    addPropertyListener((source, propId) -> {
        if (propId == IEditorPart.PROP_DIRTY) {
            EntityEditorPropertyTester.firePropertyChange(EntityEditorPropertyTester.PROP_DIRTY);
            EntityEditorPropertyTester.firePropertyChange(EntityEditorPropertyTester.PROP_CAN_UNDO);
            EntityEditorPropertyTester.firePropertyChange(EntityEditorPropertyTester.PROP_CAN_REDO);
        }
    });
    super.createPages();
    DBSObject databaseObject = editorInput.getDatabaseObject();
    EditorDefaults editorDefaults = null;
    if (databaseObject == null) {
        // Weird
        log.debug("Null database object in EntityEditor");
    } else {
        synchronized (defaultPageMap) {
            editorDefaults = defaultPageMap.get(databaseObject.getClass().getName());
        }
        EntityEditorsRegistry editorsRegistry = EntityEditorsRegistry.getInstance();
        // Add object editor page
        EntityEditorDescriptor defaultEditor = editorsRegistry.getMainEntityEditor(databaseObject, this);
        hasPropertiesEditor = false;
        if (defaultEditor != null) {
            hasPropertiesEditor = addEditorTab(defaultEditor);
        }
        if (hasPropertiesEditor) {
            DBNNode node = editorInput.getNavigatorNode();
            int propEditorIndex = getPageCount() - 1;
            setPageText(propEditorIndex, CoreMessages.editors_entity_properties_text);
            setPageToolTip(propEditorIndex, node.getNodeType() + CoreMessages.editors_entity_properties_tooltip_suffix);
            setPageImage(propEditorIndex, DBeaverIcons.getImage(node.getNodeIconDefault()));
        }
    }
    // Add contributed pages
    addContributions(EntityEditorDescriptor.POSITION_PROPS);
    addContributions(EntityEditorDescriptor.POSITION_START);
    addContributions(EntityEditorDescriptor.POSITION_MIDDLE);
    // Add contributed pages
    addContributions(EntityEditorDescriptor.POSITION_END);
    String defPageId = editorInput.getDefaultPageId();
    String defFolderId = editorInput.getDefaultFolderId();
    if (defPageId == null && editorDefaults != null) {
        defPageId = editorDefaults.pageId;
    }
    if (defPageId != null) {
        IEditorPart defEditorPage = editorMap.get(defPageId);
        if (defEditorPage != null) {
            setActiveEditor(defEditorPage);
        }
    } else {
        setActiveEditor(getEditor(0));
    }
    this.activeEditor = getActiveEditor();
    if (activeEditor instanceof ITabbedFolderContainer) {
        if (defFolderId == null && editorDefaults != null) {
            defFolderId = editorDefaults.folderId;
        }
        if (defFolderId != null) {
            ((ITabbedFolderContainer) activeEditor).switchFolder(defFolderId);
        }
    }
    UIUtils.setHelp(getContainer(), IHelpContextIds.CTX_ENTITY_EDITOR);
}
Also used : DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) EntityEditorDescriptor(org.jkiss.dbeaver.registry.editor.EntityEditorDescriptor) EntityEditorsRegistry(org.jkiss.dbeaver.registry.editor.EntityEditorsRegistry) Point(org.eclipse.swt.graphics.Point) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBECommand(org.jkiss.dbeaver.model.edit.DBECommand) ITabbedFolderContainer(org.jkiss.dbeaver.ui.controls.folders.ITabbedFolderContainer) DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext) DBECommandAdapter(org.jkiss.dbeaver.model.impl.edit.DBECommandAdapter)

Aggregations

DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)4 EntityEditorsRegistry (org.jkiss.dbeaver.registry.editor.EntityEditorsRegistry)4 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)3 DBException (org.jkiss.dbeaver.DBException)2 DBPRefreshableObject (org.jkiss.dbeaver.model.DBPRefreshableObject)2 DBEObjectMaker (org.jkiss.dbeaver.model.edit.DBEObjectMaker)2 DBNContainer (org.jkiss.dbeaver.model.navigator.DBNContainer)2 EntityEditorDescriptor (org.jkiss.dbeaver.registry.editor.EntityEditorDescriptor)2 Point (org.eclipse.swt.graphics.Point)1 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)1 DBPObject (org.jkiss.dbeaver.model.DBPObject)1 DBPScriptObject (org.jkiss.dbeaver.model.DBPScriptObject)1 DBECommand (org.jkiss.dbeaver.model.edit.DBECommand)1 DBECommandContext (org.jkiss.dbeaver.model.edit.DBECommandContext)1 DBECommandAdapter (org.jkiss.dbeaver.model.impl.edit.DBECommandAdapter)1 DBNDataSource (org.jkiss.dbeaver.model.navigator.DBNDataSource)1 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)1 ITabbedFolderContainer (org.jkiss.dbeaver.ui.controls.folders.ITabbedFolderContainer)1