Eclipse中打开文件 ,可以使用系统的 System Explorer ,但是操作步骤相对繁琐,而且没有快捷键设置.
Open Explorer 很好的解决了这个可以,既可以在工具栏中点击,也可以直接出现在右键菜单中 ,但是有时候感觉还是没有快捷键操作方便.
不过Eclipse可以自定义插件, 可以通过 Plug-in-Project 创建一个基本的插件项目,
创建一个简单的plug项目
然后可以通过jar反编译插件来获取Open Explorer的源码 ,并save all 所有的java源码,然后可以通过自己来动手 添加一个快捷键
为了与openExplorer共存,那么定义自己的插件名为openMyExplorer ,然后 创建好包名,拷贝所有的open Explorer的代码至src . (下图多了一个OpenExplorerHandler.java 待会解释), 拷贝完源码之后 ,可能会有疑问, 现在类名的名称没变,但是包名变了, 所以 还需要拷贝plugins.xml
在plugins.xml的编辑器中你需要修改原来class指向, 如果需要与Open Explorer共存的话, 所有id 也需要修改.那么 可以不用Extensions这个页面,直接使用Xml编辑器
修改完成之后,你可以运行Eclipse Application来执行这个插件, 同时在Console中可以看到启动的日志, 如果配置的类的路径不对,名字不对, 在点击你自己的插件过程中,会有明显的报错
废话结束,开始主题, 如果绑定快捷键的话 ,需要使用eclipse的快捷键接口添加 3个依赖 ,在extension页面直接搜索拓展的名称,既可以添加
<extension
point="org.eclipse.ui.handlers">
<handler
class="openmyexplorer.actions.OpenExplorerHandler"
commandId="openMyExplorer.commands.openExplorerCommand">
</handler>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
class="openmyexplorer.actions.OpenExplorerHandler"
commandId="openMyExplorer.commands.openExplorerCommand">
</handler>
</extension>
<extension
point="org.eclipse.ui.bindings">
<key
commandId="openMyExplorer.commands.openExplorerCommand"
contextId="org.eclipse.ui.contexts.window"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="M1+`">
</key>
</extension>
创建分类,自定义id 和name
创建命令名称 ,绑定上分类id ,自定义id 和name (name为keys上的名称)
给命令绑定触发的handler ,OpenExplorerHandler.java源码如下,核心方法都是从Open Explorer的AbstractOpenExplorerAction.java拷贝进来, 抽象类中主要是需要实现window ,shell,currentSelection,systemBrowser 这个对象的值
package openmyexplorer.actions;
import java.io.IOException;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import openmyexplorer.util.Messages;
import openmyexplorer.util.OperatingSystem;
public class OpenExplorerHandler extends AbstractHandler {
protected IWorkbenchWindow window;
protected Shell shell;
protected ISelection currentSelection;
protected String systemBrowser;
public OpenExplorerHandler() {
this.systemBrowser = OperatingSystem.INSTANCE.getSystemBrowser();
}
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
// 系统路径
if (OperatingSystem.INSTANCE.isLinux()) {
this.systemBrowser = OperatingSystem.INSTANCE.getSystemBrowser();
}
window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
StringBuilder sb = new StringBuilder();
sb.append("-------------------------------------------------------------").append("\n");
sb.append(formatInfo("systemBrowser")).append(systemBrowser).append("\n");
this.currentSelection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
if (this.currentSelection instanceof ITreeSelection) {
sb.append(formatInfo("currentSelection")).append(ITreeSelection.class.getSimpleName()).append("\n");;
}
if (this.currentSelection instanceof ITextSelection) {
sb.append(formatInfo("currentSelection")).append(ITextSelection.class.getSimpleName()).append("\n");;
}
if (this.currentSelection instanceof IStructuredSelection) {
sb.append(formatInfo("currentSelection")).append(IStructuredSelection.class.getSimpleName()).append("\n");
}
sb.append("-------------------------------------------------------------").append("\n");
System.out.println(sb);
try {
if ((this.currentSelection == null) || (this.currentSelection.isEmpty())) {
return null;
}
if ((this.currentSelection instanceof ITreeSelection)) {
ITreeSelection treeSelection = (ITreeSelection) this.currentSelection;
TreePath[] paths = treeSelection.getPaths();
for (int i = 0; i < paths.length; i++) {
TreePath path = paths[i];
IResource resource = null;
Object segment = path.getLastSegment();
if ((segment instanceof IResource))
resource = (IResource) segment;
else if ((segment instanceof IJavaElement)) {
resource = ((IJavaElement) segment).getResource();
}
if (resource != null) {
String browser = this.systemBrowser;
String location = resource.getLocation().toOSString();
if ((resource instanceof IFile)) {
location = ((IFile) resource).getParent().getLocation().toOSString();
if (OperatingSystem.INSTANCE.isWindows()) {
browser = this.systemBrowser + " /select,";
location = ((IFile) resource).getLocation().toOSString();
}
}
openInBrowser(browser, location);
}
}
} else if (((this.currentSelection instanceof ITextSelection))
|| ((this.currentSelection instanceof IStructuredSelection))) {
IEditorPart editor = this.window.getActivePage().getActiveEditor();
if (editor != null) {
IFile current_editing_file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
String browser = this.systemBrowser;
String location = current_editing_file.getParent().getLocation().toOSString();
if (OperatingSystem.INSTANCE.isWindows()) {
browser = this.systemBrowser + " /select,";
location = current_editing_file.getLocation().toOSString();
}
openInBrowser(browser, location);
}
}
} catch (Exception e) {
MessageDialog.openInformation(window.getShell(), "打开资源管理器异常", e.getMessage() + "!!!");
}
return null;
}
protected void openInBrowser(String browser, String location) {
try {
if (OperatingSystem.INSTANCE.isWindows())
Runtime.getRuntime().exec(browser + " \"" + location + "\"");
else
Runtime.getRuntime().exec(new String[] { browser, location });
} catch (IOException e) {
MessageDialog.openError(this.shell, Messages.OpenExploer_Error,
Messages.Cant_Open + " \"" + location + "\"");
e.printStackTrace();
}
}
public String formatInfo(String str) {
return String.format("===%s===>", str);
}
}