提交 3d578ee1 编写于 作者: S serge-rider

#3347 Bookmark name in DND

上级 04156ded
......@@ -313,7 +313,7 @@ public class NavigatorUtils {
nodeName = DBUtils.getObjectFullName(object, DBPEvaluationContext.UI);
objects.add(object);
} else {
nodeName = ((DBNNode)nextSelected).getNodeName();
nodeName = ((DBNNode)nextSelected).getNodeTargetName();
}
if (buf.length() > 0) {
buf.append(lineSeparator);
......
......@@ -137,7 +137,7 @@ public class BookmarkStorage {
return dataSourceId;
}
public Collection<String> getDataSourcePath()
public List<String> getDataSourcePath()
{
return dataSourcePath;
}
......
......@@ -21,7 +21,6 @@ import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
......@@ -30,7 +29,6 @@ import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.core.DBeaverUI;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.navigator.*;
import org.jkiss.dbeaver.model.runtime.DBRProgressListener;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress;
import org.jkiss.dbeaver.runtime.ui.DBUserInterface;
......@@ -122,23 +120,14 @@ public class BookmarksHandlerImpl extends AbstractResourceHandler {
if (dsNode == null) {
throw new DBException("Can't find datasource node for '" + dataSourceContainer.getName() + "'"); //$NON-NLS-2$
}
dsNode.initializeNode(null, new DBRProgressListener() {
@Override
public void onTaskFinished(IStatus status)
{
if (status.isOK()) {
DBeaverUI.syncExec(new Runnable() {
@Override
public void run() {
openNodeByPath(dsNode, (IFile) resource, storage);
}
});
} else {
DBUserInterface.getInstance().showError(
"Open bookmark",
"Can't open bookmark",
status);
}
dsNode.initializeNode(null, status -> {
if (status.isOK()) {
DBeaverUI.syncExec(() -> openNodeByPath(dsNode, (IFile) resource, storage));
} else {
DBUserInterface.getInstance().showError(
"Open bookmark",
"Can't open bookmark",
status);
}
});
}
......@@ -147,62 +136,52 @@ public class BookmarksHandlerImpl extends AbstractResourceHandler {
}
}
private void openNodeByPath(final DBNDataSource dsNode, final IFile file, final BookmarkStorage storage)
private static void openNodeByPath(final DBNDataSource dsNode, final IFile file, final BookmarkStorage storage)
{
try {
DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException
{
try {
DBNNode currentNode = dsNode;
final Collection<String> dataSourcePath = storage.getDataSourcePath();
for (String path : dataSourcePath) {
DBNNode nextChild = null;
final DBNNode[] children = currentNode.getChildren(monitor);
if (!ArrayUtils.isEmpty(children)) {
for (DBNNode node : children) {
if (path.equals(node.getNodeName())) {
nextChild = node;
break;
}
}
}
if (nextChild == null) {
throw new DBException("Can't find node '" + path + "' in '" + currentNode.getNodeFullName() + "'"); //$NON-NLS-2$ //$NON-NLS-3$
}
currentNode = nextChild;
}
if (currentNode instanceof DBNDatabaseNode) {
// Update bookmark image
storage.setImage(currentNode.getNodeIconDefault());
file.setContents(storage.serialize(), true, false, RuntimeUtils.getNestedMonitor(monitor));
// Open entity editor
final DBNDatabaseNode databaseNode = (DBNDatabaseNode) currentNode;
DBeaverUI.syncExec(new Runnable() {
@Override
public void run() {
NavigatorHandlerObjectOpen.openEntityEditor(databaseNode, null, DBeaverUI.getActiveWorkbenchWindow());
}
});
} else if (currentNode != null) {
throw new DBException("Node '" + currentNode.getNodeFullName() + "' is not a database object");
} else {
throw new DBException("Can't find database node by path");
}
} catch (Exception e) {
throw new InvocationTargetException(e);
}
}
});
BookmarkNodeLoader nodeLoader = new BookmarkNodeLoader(dsNode, storage, file);
DBeaverUI.runInProgressService(nodeLoader);
if (nodeLoader.databaseNode != null) {
DBeaverUI.syncExec(() -> NavigatorHandlerObjectOpen.openEntityEditor(
nodeLoader.databaseNode, null, DBeaverUI.getActiveWorkbenchWindow()));
}
} catch (InvocationTargetException e) {
DBUserInterface.getInstance().showError(CoreMessages.model_project_open_bookmark, CoreMessages.model_project_cant_open_bookmark, e.getTargetException());
DBUserInterface.getInstance().showError(
CoreMessages.model_project_open_bookmark, CoreMessages.model_project_cant_open_bookmark, e.getTargetException());
} catch (InterruptedException e) {
// do nothing
}
}
static DBNDatabaseNode getTargetBookmarkNode(DBRProgressMonitor monitor, DBNBookmark bookmark)
{
IFile resource = (IFile) bookmark.getResource();
final DBNProject projectNode = DBeaverCore.getInstance().getNavigatorModel().getRoot().getProject(resource.getProject());
if (projectNode != null) {
BookmarkStorage storage = bookmark.getStorage();
final DBPDataSourceContainer dataSourceContainer = projectNode.getDatabases().getDataSourceRegistry().getDataSource(storage.getDataSourceId());
if (dataSourceContainer != null) {
final DBNDataSource dsNode = (DBNDataSource) NavigatorUtils.getNodeByObject(dataSourceContainer);
if (dsNode != null) {
DBNDatabaseNode[] result = new DBNDatabaseNode[1];
dsNode.initializeNode(monitor, status -> {
if (status.isOK()) {
try {
BookmarkNodeLoader nodeLoader = new BookmarkNodeLoader(dsNode, storage, resource);
nodeLoader.run(monitor);
result[0] = nodeLoader.databaseNode;
} catch (Exception e) {
// Doesn't matter
}
}
});
return result[0];
}
}
}
return null;
}
public static void createBookmark(final DBNDatabaseNode node, String title, IFolder folder) throws DBException
{
if (folder == null) {
......@@ -254,4 +233,54 @@ public class BookmarksHandlerImpl extends AbstractResourceHandler {
}
}
private static class BookmarkNodeLoader implements DBRRunnableWithProgress {
private final DBNDataSource dsNode;
private final BookmarkStorage storage;
private final IFile file;
private DBNDatabaseNode databaseNode;
BookmarkNodeLoader(DBNDataSource dsNode, BookmarkStorage storage, IFile file) {
this.dsNode = dsNode;
this.storage = storage;
this.file = file;
}
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
DBNNode currentNode = dsNode;
final Collection<String> dataSourcePath = storage.getDataSourcePath();
for (String path : dataSourcePath) {
DBNNode nextChild = null;
final DBNNode[] children = currentNode.getChildren(monitor);
if (!ArrayUtils.isEmpty(children)) {
for (DBNNode node : children) {
if (path.equals(node.getNodeName())) {
nextChild = node;
break;
}
}
}
if (nextChild == null) {
throw new DBException("Can't find node '" + path + "' in '" + currentNode.getNodeFullName() + "'"); //$NON-NLS-2$ //$NON-NLS-3$
}
currentNode = nextChild;
}
if (currentNode instanceof DBNDatabaseNode) {
// Update bookmark image
storage.setImage(currentNode.getNodeIconDefault());
file.setContents(storage.serialize(), true, false, RuntimeUtils.getNestedMonitor(monitor));
// Open entity editor
databaseNode = (DBNDatabaseNode) currentNode;
} else if (currentNode != null) {
throw new DBException("Node '" + currentNode.getNodeFullName() + "' is not a database object");
} else {
throw new DBException("Can't find database node by path");
}
} catch (Exception e) {
throw new InvocationTargetException(e);
}
}
}
}
......@@ -24,9 +24,12 @@ import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.DBPImage;
import org.jkiss.dbeaver.model.app.DBPResourceHandler;
import org.jkiss.dbeaver.model.navigator.DBNDatabaseNode;
import org.jkiss.dbeaver.model.navigator.DBNNode;
import org.jkiss.dbeaver.model.navigator.DBNResource;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSAlias;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.registry.DataSourceRegistry;
import org.jkiss.dbeaver.utils.RuntimeUtils;
import org.jkiss.utils.CommonUtils;
......@@ -34,6 +37,7 @@ import org.jkiss.utils.CommonUtils;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* DBNBookmark
......@@ -42,7 +46,7 @@ public class DBNBookmark extends DBNResource
{
private BookmarkStorage storage;
public DBNBookmark(DBNNode parentNode, IResource resource, DBPResourceHandler handler) throws DBException, CoreException
DBNBookmark(DBNNode parentNode, IResource resource, DBPResourceHandler handler) throws DBException, CoreException
{
super(parentNode, resource, handler);
storage = new BookmarkStorage((IFile)resource, true);
......@@ -55,6 +59,10 @@ public class DBNBookmark extends DBNResource
super.dispose(reflect);
}
public BookmarkStorage getStorage() {
return storage;
}
@Override
public String getNodeName()
{
......@@ -79,6 +87,12 @@ public class DBNBookmark extends DBNResource
return storage.getImage();
}
@Override
public String getNodeTargetName() {
List<String> dsPath = storage.getDataSourcePath();
return CommonUtils.isEmpty(dsPath) ? super.getNodeName() : dsPath.get(dsPath.size() - 1);
}
@Override
public void rename(DBRProgressMonitor monitor, String newName) throws DBException
{
......
......@@ -151,6 +151,14 @@ public abstract class DBNNode implements DBPNamedObject, DBPPersistedObject, IAd
return pathName.toString();
}
/**
* Used to copy target name in clipboard and in DND operations.
* Equals to regular node name by default.
*/
public String getNodeTargetName() {
return getNodeName();
}
public boolean hasChildren(boolean navigableOnly) {
return navigableOnly ? allowsNavigableChildren() : allowsChildren();
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册