use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by dbeaver.
the class NavigatorHandlerLinkEditor method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final IWorkbenchPage activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
final IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
if (activeEditor == null) {
return null;
}
NavigatorViewBase navigatorView = NavigatorUtils.getActiveNavigatorView(event);
if (navigatorView == null) {
return null;
}
if (navigatorView instanceof ProjectExplorerView) {
if (activeEditor instanceof SQLEditor) {
IFile file = EditorUtils.getFileFromInput(activeEditor.getEditorInput());
if (file != null) {
showResourceInNavigator(navigatorView, file);
}
} else if (activeEditor.getEditorInput() instanceof ProjectFileEditorInput) {
IFile editorFile = ((ProjectFileEditorInput) activeEditor.getEditorInput()).getFile();
showResourceInNavigator(navigatorView, editorFile);
}
} else if (activeEditor.getEditorInput() instanceof IDatabaseEditorInput) {
IDatabaseEditorInput editorInput = (IDatabaseEditorInput) activeEditor.getEditorInput();
DBNNode dbnNode = editorInput.getNavigatorNode();
if (dbnNode != null) {
navigatorView.showNode(dbnNode);
}
} else if (activeEditor instanceof IDataSourceContainerProvider) {
DBPDataSourceContainer dsContainer = ((IDataSourceContainerProvider) activeEditor).getDataSourceContainer();
@NotNull final DBSObject activeObject;
if (dsContainer != null) {
DBPDataSource dataSource = dsContainer.getDataSource();
if (dataSource != null) {
activeObject = DBUtils.getDefaultOrActiveObject(dataSource);
} else {
activeObject = dsContainer;
}
final NavigatorViewBase view = navigatorView;
DBeaverUI.runInUI(activePage.getWorkbenchWindow(), new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
DBSObject showObject = activeObject;
if (showObject instanceof DBPDataSource) {
showObject = ((DBPDataSource) showObject).getContainer();
}
DBNDatabaseNode objectNode = view.getModel().getNodeByObject(monitor, showObject, true);
if (objectNode != null) {
view.showNode(objectNode);
}
}
});
}
}
activePage.activate(navigatorView);
return null;
}
use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by dbeaver.
the class NavigatorHandlerObjectBase method getCommandTarget.
protected static CommandTarget getCommandTarget(IWorkbenchWindow workbenchWindow, DBNContainer container, Class<?> childType, boolean openEditor) throws DBException {
final Object parentObject = container.getValueObject();
DBSObject objectToSeek = null;
if (parentObject instanceof DBSObject) {
final DBEStructEditor parentStructEditor = EntityEditorsRegistry.getInstance().getObjectManager(parentObject.getClass(), DBEStructEditor.class);
if (parentStructEditor != null && RuntimeUtils.isTypeSupported(childType, parentStructEditor.getChildTypes())) {
objectToSeek = (DBSObject) parentObject;
}
}
if (objectToSeek != null) {
for (final IEditorReference editorRef : workbenchWindow.getActivePage().getEditorReferences()) {
final IEditorPart editor = editorRef.getEditor(false);
if (editor instanceof IDatabaseEditor) {
final IDatabaseEditorInput editorInput = ((IDatabaseEditor) editor).getEditorInput();
if (editorInput.getDatabaseObject() == objectToSeek) {
workbenchWindow.getActivePage().activate(editor);
switchEditorFolder(container, editor);
return new CommandTarget((IDatabaseEditor) editor);
}
}
}
if (openEditor && container instanceof DBNDatabaseNode) {
final IDatabaseEditor editor = (IDatabaseEditor) NavigatorHandlerObjectOpen.openEntityEditor((DBNDatabaseNode) container, null, workbenchWindow);
if (editor != null) {
switchEditorFolder(container, editor);
return new CommandTarget(editor);
}
}
}
if (container instanceof DBNDatabaseNode) {
// No editor found - create new command context
DBPDataSource dataSource = ((DBNDatabaseNode) container).getObject().getDataSource();
if (dataSource != null) {
return new CommandTarget(new SimpleCommandContext(dataSource.getDefaultContext(true), !openEditor));
}
}
return new CommandTarget();
}
use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by dbeaver.
the class NavigatorHandlerObjectBase method getNodesByObjects.
public static Collection<DBNDatabaseNode> getNodesByObjects(Collection<Object> objects) {
DBNModel model = DBeaverCore.getInstance().getNavigatorModel();
List<DBNDatabaseNode> result = new ArrayList<>();
List<Object> missingObjects = new ArrayList<>();
for (Object object : objects) {
if (object instanceof DBSObject) {
DBNDatabaseNode node = model.findNode((DBSObject) object);
if (node != null) {
result.add(node);
continue;
}
}
missingObjects.add(object);
}
if (!missingObjects.isEmpty()) {
NodeLoader nodeLoader = new NodeLoader(model, missingObjects);
try {
DBeaverUI.runInProgressService(nodeLoader);
} catch (InvocationTargetException e) {
log.warn("Can't load node for objects " + missingObjects.size(), e.getTargetException());
} catch (InterruptedException e) {
// do nothing
}
result.addAll(nodeLoader.nodes);
}
return result;
}
use of org.jkiss.dbeaver.model.struct.DBSObject 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;
}
use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by dbeaver.
the class NavigatorHandlerObjectDelete method deleteObject.
private boolean deleteObject(IWorkbenchWindow workbenchWindow, DBNDatabaseNode node) {
try {
if (!(node.getParentNode() instanceof DBNContainer)) {
throw new DBException("Node '" + node + "' doesn't have a container");
}
final DBNContainer container = (DBNContainer) node.getParentNode();
// Try to delete object using object manager
DBSObject object = node.getObject();
if (object == null) {
throw new DBException("Can't delete node with null object");
}
DBEObjectMaker objectMaker = EntityEditorsRegistry.getInstance().getObjectManager(object.getClass(), DBEObjectMaker.class);
if (objectMaker == null) {
// $NON-NLS-2$
throw new DBException("Object maker not found for type '" + object.getClass().getName() + "'");
}
boolean supportsCascade = (objectMaker.getMakerOptions(object.getDataSource()) & DBEObjectMaker.FEATURE_DELETE_CASCADE) != 0;
CommandTarget commandTarget = getCommandTarget(workbenchWindow, container, object.getClass(), false);
if (deleteAll == null || !deleteAll) {
this.deleteOptions.clear();
}
ConfirmResult confirmResult = ConfirmResult.YES;
if (!object.isPersisted() || commandTarget.getEditor() != null) {
// There should be command context somewhere
if (deleteNewObject(workbenchWindow, node)) {
return true;
}
// No direct editor host found for this object -
// try to find corresponding command context
// and execute command within it
} else {
// Persisted object - confirm delete
// Show "View script" only if we are not in some editor (because it have its own "View script" button)
confirmResult = confirmObjectDelete(workbenchWindow, node, supportsCascade, deleteOptions, commandTarget.getContext() != null && commandTarget.getEditor() == null);
if (confirmResult == ConfirmResult.NO) {
return false;
}
}
objectMaker.deleteObject(commandTarget.getContext(), node.getObject(), deleteOptions);
if (confirmResult == ConfirmResult.DETAILS) {
if (!showScript(workbenchWindow, commandTarget.getContext(), deleteOptions, CoreMessages.actions_navigator_delete_script)) {
commandTarget.getContext().resetChanges();
// Show confirmation again
return deleteObject(workbenchWindow, node);
}
}
if (commandTarget.getEditor() == null && commandTarget.getContext() != null) {
// Persist object deletion - only if there is no host editor and we have a command context
ObjectSaver deleter = new ObjectSaver(commandTarget.getContext(), deleteOptions);
// DBeaverUI.runInProgressDialog(deleter);
tasksToExecute.add(deleter);
}
} catch (Throwable e) {
DBUserInterface.getInstance().showError(CoreMessages.actions_navigator_error_dialog_delete_object_title, "Can't delete object '" + node.getNodeName() + "'", e);
return false;
}
return true;
}
Aggregations