Search in sources :

Example 1 with DBPObject

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

the class AbstractContextDescriptor method appliesTo.

public boolean appliesTo(DBPObject object, Object context) {
    object = DBUtils.getPublicObject(object);
    if (object == null) {
        return false;
    }
    Object adapted = adaptType(object);
    for (ObjectType objectType : objectTypes) {
        if (objectType.appliesTo(object, context) || (adapted != null && objectType.appliesTo(adapted, context))) {
            return true;
        }
    }
    return false;
}
Also used : DBPObject(org.jkiss.dbeaver.model.DBPObject)

Example 2 with DBPObject

use of org.jkiss.dbeaver.model.DBPObject in project dbeaver by dbeaver.

the class AbstractCommandContext method getCommandQueues.

private List<CommandQueue> getCommandQueues() {
    if (commandQueues != null) {
        return commandQueues;
    }
    commandQueues = new ArrayList<>();
    CommandInfo aggregator = null;
    // Create queues from commands
    for (CommandInfo commandInfo : commands) {
        if (commandInfo.command instanceof DBECommandAggregator) {
            aggregator = commandInfo;
        }
        DBPObject object = commandInfo.command.getObject();
        CommandQueue queue = null;
        if (!commandQueues.isEmpty()) {
            for (CommandQueue tmpQueue : commandQueues) {
                if (tmpQueue.getObject() == object) {
                    queue = tmpQueue;
                    break;
                }
            }
        }
        if (queue == null) {
            DBEObjectManager<?> objectManager = executionContext.getDataSource().getContainer().getPlatform().getEditorsRegistry().getObjectManager(object.getClass());
            if (objectManager == null) {
                throw new IllegalStateException("Can't find object manager for '" + object.getClass().getName() + "'");
            }
            queue = new CommandQueue(objectManager, null, object);
            commandQueues.add(queue);
        }
        queue.addCommand(commandInfo);
    }
    // Merge commands
    for (CommandQueue queue : commandQueues) {
        final Map<DBECommand, CommandInfo> mergedByMap = new IdentityHashMap<>();
        final List<CommandInfo> mergedCommands = new ArrayList<>();
        for (int i = 0; i < queue.commands.size(); i++) {
            CommandInfo lastCommand = queue.commands.get(i);
            lastCommand.mergedBy = null;
            CommandInfo firstCommand = null;
            DBECommand<?> result = lastCommand.command;
            if (mergedCommands.isEmpty()) {
                result = lastCommand.command.merge(null, userParams);
            } else {
                boolean skipCommand = false;
                for (int k = mergedCommands.size(); k > 0; k--) {
                    firstCommand = mergedCommands.get(k - 1);
                    result = lastCommand.command.merge(firstCommand.command, userParams);
                    if (result == null) {
                        // Remove first and skip last command
                        mergedCommands.remove(firstCommand);
                        skipCommand = true;
                    } else if (result != lastCommand.command) {
                        break;
                    }
                }
                if (skipCommand) {
                    continue;
                }
            }
            mergedCommands.add(lastCommand);
            if (result == lastCommand.command) {
            // No changes
            // firstCommand.mergedBy = lastCommand;
            } else if (firstCommand != null && result == firstCommand.command) {
                // Remove last command from queue
                lastCommand.mergedBy = firstCommand;
            } else {
                // Some other command
                // May be it is some earlier command from queue or some new command (e.g. composite)
                CommandInfo mergedBy = mergedByMap.get(result);
                if (mergedBy == null) {
                    // Try to find in command stack
                    for (int k = i; k >= 0; k--) {
                        if (queue.commands.get(k).command == result) {
                            mergedBy = queue.commands.get(k);
                            break;
                        }
                    }
                    if (mergedBy == null) {
                        // Create new command info
                        mergedBy = new CommandInfo(result, null);
                    }
                    mergedByMap.put(result, mergedBy);
                }
                lastCommand.mergedBy = mergedBy;
                if (!mergedCommands.contains(mergedBy)) {
                    mergedCommands.add(mergedBy);
                }
            }
        }
        queue.commands = mergedCommands;
    }
    // Filter commands
    for (CommandQueue queue : commandQueues) {
        if (queue.objectManager instanceof DBECommandFilter) {
            ((DBECommandFilter) queue.objectManager).filterCommands(queue);
        }
    }
    // Aggregate commands
    if (aggregator != null) {
        ((DBECommandAggregator) aggregator.command).resetAggregatedCommands();
        for (CommandQueue queue : commandQueues) {
            for (CommandInfo cmd : queue.commands) {
                if (cmd.command != aggregator.command && cmd.mergedBy == null && ((DBECommandAggregator) aggregator.command).aggregateCommand(cmd.command)) {
                    cmd.mergedBy = aggregator;
                }
            }
        }
    }
    return commandQueues;
}
Also used : DBPObject(org.jkiss.dbeaver.model.DBPObject)

Example 3 with DBPObject

use of org.jkiss.dbeaver.model.DBPObject 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 4 with DBPObject

use of org.jkiss.dbeaver.model.DBPObject in project dbeaver by dbeaver.

the class AbstractContextDescriptor method appliesTo.

public boolean appliesTo(DBPObject object, Object context) {
    object = DBUtils.getPublicObject(object);
    if (object == null) {
        return false;
    }
    Object adapted = adaptType(object);
    for (ObjectType objectType : objectTypes) {
        if (objectType.appliesTo(object, context) || (adapted != null && objectType.appliesTo(adapted, context))) {
            return true;
        }
    }
    return false;
}
Also used : DBPObject(org.jkiss.dbeaver.model.DBPObject)

Example 5 with DBPObject

use of org.jkiss.dbeaver.model.DBPObject in project dbeaver by dbeaver.

the class PropertySourceEditable method setPropertyValue.

@Override
@SuppressWarnings("unchecked")
public void setPropertyValue(@Nullable DBRProgressMonitor monitor, Object editableValue, ObjectPropertyDescriptor prop, Object newValue) throws IllegalArgumentException {
    if (prop.getValueTransformer() != null) {
        newValue = prop.getValueTransformer().transform(editableValue, newValue);
    }
    final Object oldValue = getPropertyValue(monitor, editableValue, prop);
    if (!updatePropertyValue(monitor, editableValue, prop, newValue, false)) {
        return;
    }
    if (commandContext != null) {
        if (lastCommand == null || lastCommand.getObject() != editableValue || lastCommand.property != prop) {
            final DBEObjectEditor<DBPObject> objectEditor = getObjectEditor(DBEObjectEditor.class);
            if (objectEditor == null) {
                log.error("Can't obtain object editor for " + getEditableValue());
                return;
            }
            final DBEPropertyHandler<DBPObject> propertyHandler = objectEditor.makePropertyHandler((DBPObject) editableValue, prop);
            PropertyChangeCommand curCommand = new PropertyChangeCommand((DBPObject) editableValue, prop, propertyHandler, oldValue, newValue);
            commandContext.addCommand(curCommand, commandReflector);
            lastCommand = curCommand;
        } else {
            lastCommand.setNewValue(newValue);
            commandContext.updateCommand(lastCommand, commandReflector);
        }
    }
    // To update name-based cache
    if (prop.isNameProperty() && editableValue instanceof DBSObject) {
        DBEObjectMaker objectManager = getObjectEditor(DBEObjectMaker.class);
        if (objectManager != null) {
            DBSObjectCache cache = objectManager.getObjectsCache((DBSObject) editableValue);
            if (cache != null && cache.isFullyCached()) {
                List<? extends DBSObject> cachedObjects = CommonUtils.copyList(cache.getCachedObjects());
                cache.setCache(cachedObjects);
            }
        }
    }
/*
        // Notify listeners
        for (IPropertySourceListener listener : listeners) {
            listener.handlePropertyChange(editableValue, prop, newValue);
        }
*/
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSObjectCache(org.jkiss.dbeaver.model.impl.DBSObjectCache) DBPObject(org.jkiss.dbeaver.model.DBPObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPObject(org.jkiss.dbeaver.model.DBPObject) DBPNamedObject(org.jkiss.dbeaver.model.DBPNamedObject) DBEObjectMaker(org.jkiss.dbeaver.model.edit.DBEObjectMaker)

Aggregations

DBPObject (org.jkiss.dbeaver.model.DBPObject)14 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)5 DBEObjectMaker (org.jkiss.dbeaver.model.edit.DBEObjectMaker)4 ArrayList (java.util.ArrayList)3 DBException (org.jkiss.dbeaver.DBException)3 DBPScriptObject (org.jkiss.dbeaver.model.DBPScriptObject)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)2 DBPNamedObject (org.jkiss.dbeaver.model.DBPNamedObject)2 DBPRefreshableObject (org.jkiss.dbeaver.model.DBPRefreshableObject)2 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)2 DBSWrapper (org.jkiss.dbeaver.model.struct.DBSWrapper)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 IFile (org.eclipse.core.resources.IFile)1 Action (org.eclipse.jface.action.Action)1 IContributionManager (org.eclipse.jface.action.IContributionManager)1 IDialogConstants (org.eclipse.jface.dialogs.IDialogConstants)1