From bef602e366b30348b33074816fe6d84930362c43 Mon Sep 17 00:00:00 2001 From: Alexander Fedorov Date: Sat, 14 Oct 2017 14:34:22 +0300 Subject: [PATCH] #2297 add "Link File" action --- .../OSGI-INF/l10n/bundle.properties | 3 + .../OSGI-INF/l10n/bundle_ru.properties | 5 +- plugins/org.jkiss.dbeaver.core/plugin.xml | 40 +++- .../runtime/ContentTypeParameterValues.java | 24 +++ .../navigator/NavigatorHandlerCreateLink.java | 188 ++++++++++-------- .../navigator/NavigatorHandlerLinkFile.java | 77 +++++++ .../navigator/NavigatorHandlerLinkFolder.java | 51 +++++ 7 files changed, 298 insertions(+), 90 deletions(-) create mode 100644 plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/runtime/ContentTypeParameterValues.java create mode 100644 plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/actions/navigator/NavigatorHandlerLinkFile.java create mode 100644 plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/actions/navigator/NavigatorHandlerLinkFolder.java diff --git a/plugins/org.jkiss.dbeaver.core/OSGI-INF/l10n/bundle.properties b/plugins/org.jkiss.dbeaver.core/OSGI-INF/l10n/bundle.properties index aab0e08d2f..b402d678f8 100644 --- a/plugins/org.jkiss.dbeaver.core/OSGI-INF/l10n/bundle.properties +++ b/plugins/org.jkiss.dbeaver.core/OSGI-INF/l10n/bundle.properties @@ -68,6 +68,9 @@ command.org.jkiss.dbeaver.core.project.select.name=Select Active Project command.org.jkiss.dbeaver.core.project.select.description=Select active project command.org.jkiss.dbeaver.core.resource.create.folder.name=Create New Folder command.org.jkiss.dbeaver.core.resource.create.folder.description=Create new folder +command.org.jkiss.dbeaver.core.resource.link.file.name=Link File +command.org.jkiss.dbeaver.core.resource.link.file.description=Create link to some file on file system +commandParameter.org.jkiss.dbeaver.core.resource.link.file.contenttype.name=Linked file content type command.org.jkiss.dbeaver.core.resource.link.folder.name=Link Folder command.org.jkiss.dbeaver.core.resource.link.folder.description=Create link to some folder on file system command.org.jkiss.dbeaver.core.sql.script.associate.name=Associate with data source diff --git a/plugins/org.jkiss.dbeaver.core/OSGI-INF/l10n/bundle_ru.properties b/plugins/org.jkiss.dbeaver.core/OSGI-INF/l10n/bundle_ru.properties index e10e676e8f..d4a2a4a8e1 100644 --- a/plugins/org.jkiss.dbeaver.core/OSGI-INF/l10n/bundle_ru.properties +++ b/plugins/org.jkiss.dbeaver.core/OSGI-INF/l10n/bundle_ru.properties @@ -36,7 +36,10 @@ command.org.jkiss.dbeaver.core.project.select.name=\u0412\u044B\u0431\u0440\u043 command.org.jkiss.dbeaver.core.project.select.description=\u0412\u044B\u0431\u0440\u0430\u0442\u044C \u0430\u043A\u0442\u0438\u0432\u043D\u044B\u0439 \u043F\u0440\u043E\u0435\u043A\u0442 command.org.jkiss.dbeaver.core.resource.create.folder.name=\u041D\u043E\u0432\u0430\u044F \u043F\u0430\u043F\u043A\u0430 command.org.jkiss.dbeaver.core.resource.create.folder.description=\u0421\u043E\u0437\u0434\u0430\u0442\u044C \u043D\u043E\u0432\u0443\u044E \u043F\u0430\u043F\u043A\u0443 -command.org.jkiss.dbeaver.core.resource.link.folder.name=\u0421\u043E\u0437\u0434\u0430\u0442\u044C \u0441\u0441\u044B\u043B\u043A\u0443 +command.org.jkiss.dbeaver.core.resource.link.file.name=\u0421\u043E\u0437\u0434\u0430\u0442\u044C \u0441\u0441\u044B\u043B\u043A\u0443 \u043D\u0430 \u0444\u0430\u0439\u043B +command.org.jkiss.dbeaver.core.resource.link.file.description=\u0421\u043E\u0437\u0434\u0430\u0442\u044C \u0441\u0441\u044B\u043B\u043A\u0443 \u043D\u0430 \u0444\u0430\u0439\u043B \u0432 \u0444\u0430\u0439\u043B\u043E\u0432\u043E\u0439 \u0441\u0438\u0441\u0442\u0435\u043C\u0435 +commandParameter.org.jkiss.dbeaver.core.resource.link.file.contenttype.name=Linked file content type +command.org.jkiss.dbeaver.core.resource.link.folder.name=\u0421\u043E\u0437\u0434\u0430\u0442\u044C \u0441\u0441\u044B\u043B\u043A\u0443 \u043D\u0430 \u043F\u0430\u043F\u043A\u0443 command.org.jkiss.dbeaver.core.resource.link.folder.description=\u0421\u043E\u0437\u0434\u0430\u0442\u044C \u0441\u0441\u044B\u043B\u043A\u0443 \u043D\u0430 \u043F\u0430\u043F\u043A\u0443 \u0432 \u0444\u0430\u0439\u043B\u043E\u0432\u043E\u0439 \u0441\u0438\u0441\u0442\u0435\u043C\u0435 command.org.jkiss.dbeaver.core.sql.script.associate.name=\u0421\u0432\u044F\u0437\u0430\u0442\u044C \u0441 \u0431\u0430\u0437\u043E\u0439 \u0434\u0430\u043D\u043D\u044B\u0445 command.org.jkiss.dbeaver.core.sql.script.associate.description=\u0421\u0432\u044F\u0437\u0430\u0442\u044C \u0441\u043A\u0440\u0438\u043F(\u044B) \u0441 \u0431\u0430\u0437\u043E\u0439 \u0434\u0430\u043D\u043D\u044B\u0445 diff --git a/plugins/org.jkiss.dbeaver.core/plugin.xml b/plugins/org.jkiss.dbeaver.core/plugin.xml index ab668e982d..fdf2ffdcc1 100644 --- a/plugins/org.jkiss.dbeaver.core/plugin.xml +++ b/plugins/org.jkiss.dbeaver.core/plugin.xml @@ -217,7 +217,19 @@ - + + + + + @@ -343,7 +355,8 @@ - + + @@ -574,7 +587,19 @@ - + + + + + + + + + + + + + @@ -1827,7 +1852,14 @@ - + + + + + + diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/runtime/ContentTypeParameterValues.java b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/runtime/ContentTypeParameterValues.java new file mode 100644 index 0000000000..c8a975ed39 --- /dev/null +++ b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/runtime/ContentTypeParameterValues.java @@ -0,0 +1,24 @@ +package org.jkiss.dbeaver.runtime; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.core.commands.IParameterValues; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.content.IContentType; + +public class ContentTypeParameterValues implements IParameterValues { + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Override + public Map getParameterValues() + { + final Map values = new HashMap(); + IContentType[] allContentTypes = Platform.getContentTypeManager().getAllContentTypes(); + for (IContentType contentType : allContentTypes) { + values.put(contentType.getName(), contentType.getId()); + } + return values; + } + +} diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/actions/navigator/NavigatorHandlerCreateLink.java b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/actions/navigator/NavigatorHandlerCreateLink.java index 6c62f55868..8ee75ae6d5 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/actions/navigator/NavigatorHandlerCreateLink.java +++ b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/actions/navigator/NavigatorHandlerCreateLink.java @@ -1,86 +1,104 @@ -/* - * DBeaver - Universal Database Manager - * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jkiss.dbeaver.ui.actions.navigator; - -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.core.resources.IFolder; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.jface.operation.IRunnableWithProgress; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.swt.SWT; -import org.eclipse.swt.widgets.DirectoryDialog; -import org.eclipse.ui.IWorkbenchWindow; -import org.eclipse.ui.handlers.HandlerUtil; -import org.jkiss.dbeaver.model.navigator.DBNResource; -import org.jkiss.dbeaver.runtime.ui.DBUserInterface; - -import java.io.File; -import java.lang.reflect.InvocationTargetException; - -public class NavigatorHandlerCreateLink extends NavigatorHandlerObjectBase { - - @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - final ISelection selection = HandlerUtil.getCurrentSelection(event); - - if (selection instanceof IStructuredSelection) { - final IStructuredSelection structSelection = (IStructuredSelection)selection; - Object element = structSelection.getFirstElement(); - if (!(element instanceof DBNResource)) { - return null; - } - final IResource resource = ((DBNResource) element).getResource(); - if (resource instanceof IFolder) { - final IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event); - DirectoryDialog dialog = new DirectoryDialog(workbenchWindow.getShell(), SWT.NONE); - String folder = dialog.open(); - if (folder != null) { - createLink(workbenchWindow, (IFolder)resource, folder); - } - } - } - return null; - } - - private void createLink(IWorkbenchWindow workbenchWindow, IFolder folder, String fsFolder) - { - final File externalFolder = new File(fsFolder); - try { - final IFolder linkedFolder = folder.getFolder(externalFolder.getName()); - workbenchWindow.run(true, true, new IRunnableWithProgress() { - @Override - public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException - { - try { - linkedFolder.createLink(externalFolder.toURI(), IResource.NONE, monitor); - } catch (CoreException e) { - throw new InvocationTargetException(e); - } - } - }); - } catch (InvocationTargetException e) { - DBUserInterface.getInstance().showError("Create link", "Can't create link", e); - } catch (InterruptedException e) { - // skip - } - } - +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jkiss.dbeaver.ui.actions.navigator; + +import java.lang.reflect.InvocationTargetException; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.Adapters; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jface.operation.IRunnableContext; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.osgi.util.NLS; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.actions.WorkspaceModifyOperation; +import org.eclipse.ui.handlers.HandlerUtil; +import org.jkiss.dbeaver.core.DBeaverCore; +import org.jkiss.dbeaver.runtime.ui.DBUserInterface; + +public abstract class NavigatorHandlerCreateLink extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException + { + IStructuredSelection structured = HandlerUtil.getCurrentStructuredSelection(event); + if (structured.isEmpty()) { + return null; + } + Object first = structured.getFirstElement(); + IResource resource = Adapters.adapt(first, IResource.class); + IStatus validation = validateSelected(resource); + if (!validation.isOK()) { + DBUserInterface.getInstance().showError("Create link", validation.getMessage()); + return null; + } + + String path = selectTarget(event); + if (path == null) { + return null; + } + WorkspaceModifyOperation operation = new WorkspaceModifyOperation() { + + @Override + protected void execute(IProgressMonitor monitor) + throws CoreException, InvocationTargetException, InterruptedException + { + createLink(resource, path, monitor); + } + }; + IRunnableContext context = getRunnableContext(event); + try { + context.run(true, true, operation); + } catch (InvocationTargetException e) { + DBUserInterface.getInstance().showError("Create link", "Unable to create link", e); + } catch (InterruptedException e) { + // skip + } + return null; + } + + protected IStatus validateSelected(IResource resource) + { + if (resource instanceof IContainer) { + return Status.OK_STATUS; + } + String message = NLS.bind("Unable to create link inside {0}" , resource); + return new Status(IStatus.ERROR, DBeaverCore.getCorePluginID(), message); + } + + protected abstract String selectTarget(ExecutionEvent event); + + protected IRunnableContext getRunnableContext(ExecutionEvent event) + { + final IWorkbenchWindow activeWindow = HandlerUtil.getActiveWorkbenchWindow(event); + if (activeWindow != null) { + return activeWindow; + } + return PlatformUI.getWorkbench().getProgressService(); + } + + protected abstract void createLink(IResource resource, String path, IProgressMonitor monitor) throws CoreException; + } \ No newline at end of file diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/actions/navigator/NavigatorHandlerLinkFile.java b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/actions/navigator/NavigatorHandlerLinkFile.java new file mode 100644 index 0000000000..1eab8cd9c4 --- /dev/null +++ b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/actions/navigator/NavigatorHandlerLinkFile.java @@ -0,0 +1,77 @@ +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jkiss.dbeaver.ui.actions.navigator; + +import java.io.File; + +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.content.IContentType; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.FileDialog; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.handlers.HandlerUtil; + +public class NavigatorHandlerLinkFile extends NavigatorHandlerCreateLink { + + private static final String COMMAND_PARAMETER_LINK_FILE_CONTENTTYPE = "org.jkiss.dbeaver.core.resource.link.file.contenttype"; //$NON-NLS-1$ + + @Override + protected String selectTarget(ExecutionEvent event) + { + Shell shell = HandlerUtil.getActiveShell(event); + FileDialog dialog = new FileDialog(shell, SWT.NONE); + String contentTypeId = event.getParameter(COMMAND_PARAMETER_LINK_FILE_CONTENTTYPE); + if (contentTypeId != null) { + IContentType contentType = Platform.getContentTypeManager().getContentType(contentTypeId); + if (contentType != null) { + StringBuilder sb = new StringBuilder(); + String[] fileSpecs = contentType.getFileSpecs(IContentType.FILE_EXTENSION_SPEC); + for (String extension : fileSpecs) { + if (sb.length() > 0) { + sb.append(';'); + } + sb.append('*').append('.').append(extension); + } + if (sb.length() > 0) { + String[] names = new String[] {contentType.getName()}; + String[] extensions = new String[] {sb.toString()}; + dialog.setFilterNames(names); + dialog.setFilterExtensions(extensions); + } + + } + } + String file = dialog.open(); + return file; + } + + @Override + protected void createLink(IResource resource, String path, IProgressMonitor monitor) throws CoreException + { + IFolder container = (IFolder)resource; + final File externalFolder = new File(path); + final IFile linked = container.getFile(externalFolder.getName()); + linked.createLink(externalFolder.toURI(), IResource.NONE, monitor); + } + +} \ No newline at end of file diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/actions/navigator/NavigatorHandlerLinkFolder.java b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/actions/navigator/NavigatorHandlerLinkFolder.java new file mode 100644 index 0000000000..ab5cb12be9 --- /dev/null +++ b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/actions/navigator/NavigatorHandlerLinkFolder.java @@ -0,0 +1,51 @@ +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jkiss.dbeaver.ui.actions.navigator; + +import java.io.File; + +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.DirectoryDialog; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.handlers.HandlerUtil; + +public class NavigatorHandlerLinkFolder extends NavigatorHandlerCreateLink { + + @Override + protected String selectTarget(ExecutionEvent event) + { + Shell shell = HandlerUtil.getActiveShell(event); + DirectoryDialog dialog = new DirectoryDialog(shell, SWT.NONE); + String folder = dialog.open(); + return folder; + } + + @Override + protected void createLink(IResource resource, String path, IProgressMonitor monitor) throws CoreException + { + IFolder container = (IFolder)resource; + final File externalFolder = new File(path); + final IFolder linkedFolder = container.getFolder(externalFolder.getName()); + linkedFolder.createLink(externalFolder.toURI(), IResource.NONE, monitor); + } + +} \ No newline at end of file -- GitLab