提交 bef602e3 编写于 作者: A Alexander Fedorov

#2297 add "Link File" action

上级 39b5efb1
......@@ -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
......
......@@ -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
......
......@@ -217,7 +217,19 @@
<command id="org.jkiss.dbeaver.core.project.active" name="%command.org.jkiss.dbeaver.core.project.active.name" description="%command.org.jkiss.dbeaver.core.project.active.description" categoryId="org.jkiss.dbeaver.core.navigator"/>
<command id="org.jkiss.dbeaver.core.project.select" name="%command.org.jkiss.dbeaver.core.project.select.name" description="%command.org.jkiss.dbeaver.core.project.select.description" categoryId="org.jkiss.dbeaver.core.navigator"/>
<command id="org.jkiss.dbeaver.core.resource.create.folder" name="%command.org.jkiss.dbeaver.core.resource.create.folder.name" description="%command.org.jkiss.dbeaver.core.resource.create.folder.description" categoryId="org.jkiss.dbeaver.core.navigator"/>
<command id="org.jkiss.dbeaver.core.resource.create.link" name="%command.org.jkiss.dbeaver.core.resource.link.folder.name" description="%command.org.jkiss.dbeaver.core.resource.link.folder.description" categoryId="org.jkiss.dbeaver.core.navigator"/>
<command
categoryId="org.jkiss.dbeaver.core.navigator"
description="%command.org.jkiss.dbeaver.core.resource.link.file.description"
id="org.jkiss.dbeaver.core.resource.link.file"
name="%command.org.jkiss.dbeaver.core.resource.link.file.name">
<commandParameter
id="org.jkiss.dbeaver.core.resource.link.file.contenttype"
name="%commandParameter.org.jkiss.dbeaver.core.resource.link.file.contenttype.name"
optional="true"
values="org.jkiss.dbeaver.runtime.ContentTypeParameterValues">
</commandParameter>
</command>
<command id="org.jkiss.dbeaver.core.resource.link.folder" name="%command.org.jkiss.dbeaver.core.resource.link.folder.name" description="%command.org.jkiss.dbeaver.core.resource.link.folder.description" categoryId="org.jkiss.dbeaver.core.navigator"/>
<command id="org.jkiss.dbeaver.core.sql.script.associate" name="%command.org.jkiss.dbeaver.core.sql.script.associate.name" description="%command.org.jkiss.dbeaver.core.sql.script.associate.description" categoryId="org.jkiss.dbeaver.core.navigator"/>
<command id="org.jkiss.dbeaver.core.connect" name="%command.org.jkiss.dbeaver.core.connect.name" description="%command.org.jkiss.dbeaver.core.connect.description" categoryId="org.jkiss.dbeaver.core.database"/>
......@@ -343,7 +355,8 @@
<image commandId="org.jkiss.dbeaver.core.project.refresh" icon="icons/refresh.png"/>
<image commandId="org.jkiss.dbeaver.core.project.select" icon="icons/project.png"/>
<image commandId="org.jkiss.dbeaver.core.resource.create.folder" icon="IMG_TOOL_NEW_WIZARD"/>
<image commandId="org.jkiss.dbeaver.core.resource.create.link" icon="icons/file/link.png"/>
<image commandId="org.jkiss.dbeaver.core.resource.link.file" icon="icons/file/link.png"/>
<image commandId="org.jkiss.dbeaver.core.resource.link.folder" icon="icons/file/link.png"/>
<image commandId="org.jkiss.dbeaver.core.sql.editor.open" icon="icons/sql/sql_script.png"/>
<image commandId="org.jkiss.dbeaver.core.sql.editor.recent" icon="icons/sql/sql_script_recent.png"/>
......@@ -574,7 +587,19 @@
</with>
</enabledWhen>
</handler>
<handler commandId="org.jkiss.dbeaver.core.resource.create.link" class="org.jkiss.dbeaver.ui.actions.navigator.NavigatorHandlerCreateLink">
<handler commandId="org.jkiss.dbeaver.core.resource.link.file" class="org.jkiss.dbeaver.ui.actions.navigator.NavigatorHandlerLinkFile">
<enabledWhen>
<with variable="selection">
<count value="1"/>
<iterate operator="and">
<adapt type="org.eclipse.core.resources.IResource">
<test property="org.jkiss.dbeaver.core.resource.canCreateLink"/>
</adapt>
</iterate>
</with>
</enabledWhen>
</handler>
<handler commandId="org.jkiss.dbeaver.core.resource.link.folder" class="org.jkiss.dbeaver.ui.actions.navigator.NavigatorHandlerLinkFolder">
<enabledWhen>
<with variable="selection">
<count value="1"/>
......@@ -1827,7 +1852,14 @@
<command commandId="org.jkiss.dbeaver.core.resource.create.folder">
<visibleWhen checkEnabled="true"/>
</command>
<command commandId="org.jkiss.dbeaver.core.resource.create.link">
<command commandId="org.jkiss.dbeaver.core.resource.link.file">
<parameter
name="org.jkiss.dbeaver.core.resource.link.file.contenttype"
value="org.jkiss.dbeaver.sql">
</parameter>
<visibleWhen checkEnabled="true"/>
</command>
<command commandId="org.jkiss.dbeaver.core.resource.link.folder">
<visibleWhen checkEnabled="true"/>
</command>
......
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;
}
}
/*
* 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
/*
* 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
/*
* 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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册