自从我写了上一个答案之后,我又写了另外几个实用程序插件,并且现在拥有了Eclipse导航器弹出窗口的通用启动程序。 文件如下:
基本清单文件
MANIFEST.MF
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: NavigatorPopup
Bundle-SymbolicName: com.nsd.NavigatorPopup;singleton:=true
Bundle-Version: 1.0.0.1
Bundle-Vendor: NSD
Require-Bundle: org.eclipse.ui,
org.eclipse.core.resources,
org.eclipse.core.runtime
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
基本的属性文件
build.properties
source.. = src/
output.. = bin/
bin.includes = plugin.xml,\
META-INF/,\
.
插件文件
需要特别注意在<menuContribution...> <menu..>中的<command commandId=...>,它在扩展点<extension point="org.eclipse.ui.menus">中链接到了<extension point="org.eclipse.ui.handlers">中的<handler commandId=...>,这使得我们可以将多个菜单贡献链接到一个处理程序。
还值得注意的是,有多个locationURI,因为这些可以从一个透视图更改为另一个透视图。
plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension point="org.eclipse.ui.commands">
<category name="NSD Category" id="com.nsd.NavigatorPopup.commands.category"/>
<command name="Navigator Popup" categoryId="com.nsd.NavigatorPopup.commands.category" id="com.nsd.NavigatorPopup.commands.navigatorPopupCommand"/>
</extension>
<extension point="org.eclipse.ui.handlers">
<handler commandId="com.nsd.NavigatorPopup.commands.navigatorPopupCommand" class="com.nsd.navigatorpopup.NavigatorPopupHandler"/>
</extension>
<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="popup:org.eclipse.jdt.ui.PackageExplorer?after=additions">
<menu label="Popup Utilities">
<command commandId="com.nsd.NavigatorPopup.commands.navigatorPopupCommand" id="com.nsd.NavigatorPopup.menus.navigatorPopupCommand"/>
</menu>
</menuContribution>
<menuContribution locationURI="popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu?after=additions">
<menu label="Popup Utilities">
<command commandId="com.nsd.NavigatorPopup.commands.navigatorPopupCommand" id="com.nsd.NavigatorPopup.menus.navigatorPopupCommand"/>
</menu>
</menuContribution>
</extension>
</plugin>
要了解有关当前选择的插件的更多信息,请单击文件并使用 ALT-SHIFT-F1 进行“插件选择侦察器”。
![Plug-in Selection Spy](https://istack.dev59.com/orvnB.webp)
使用 ALT-SHIFT-F2 进行“插件菜单侦察器”。请注意,在右键单击并选择感兴趣的弹出菜单项之前,按下 ALT-SHIFT-F2 组合键。
![Plug-in Menu Spy](https://istack.dev59.com/TMC2J.webp)
最后是插件代码。
NavigatorPopupHandler.java
package com.nsd.navigatorpopup;
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.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
public class NavigatorPopupHandler extends AbstractHandler {
private IWorkbenchWindow window;
private IWorkbenchPage activePage;
private IProject theProject;
private IResource theResource;
private IFile theFile;
private String workspaceName;
private String projectName;
private String fileName;
public NavigatorPopupHandler() {
}
public Object execute(ExecutionEvent event) throws ExecutionException {
if(!extractProjectAndFileFromInitiatingEvent(event)) {
return null;
}
MessageDialog.openInformation(this.window.getShell(), "NavigatorPopup", String.format("File Details.\n\nWorkspace=%s\nProject=%s\nFile=%s", workspaceName, projectName, fileName));
return null;
}
private boolean extractProjectAndFileFromInitiatingEvent(ExecutionEvent event) {
this.window = HandlerUtil.getActiveWorkbenchWindow(event);
this.activePage = this.window.getActivePage();
ISelection selection = this.activePage.getSelection();
if(selection instanceof ITreeSelection) {
TreeSelection treeSelection = (TreeSelection) selection;
TreePath[] treePaths = treeSelection.getPaths();
TreePath treePath = treePaths[0];
Object firstSegmentObj = treePath.getFirstSegment();
this.theProject = (IProject) ((IAdaptable) firstSegmentObj).getAdapter(IProject.class);
if(this.theProject == null) {
MessageDialog.openError(this.window.getShell(), "Navigator Popup", getClassHierarchyAsMsg(
"Expected the first segment to be IAdapatable to an IProject.\nBut got the following class hierarchy instead.", "Make sure to directly select a file.",
firstSegmentObj));
return false;
}
Object lastSegmentObj = treePath.getLastSegment();
this.theResource = (IResource) ((IAdaptable) lastSegmentObj).getAdapter(IResource.class);
if(this.theResource == null) {
MessageDialog.openError(this.window.getShell(), "Navigator Popup", getClassHierarchyAsMsg(
"Expected the last segment to be IAdapatable to an IResource.\nBut got the following class hierarchy instead.", "Make sure to directly select a file.",
firstSegmentObj));
return false;
}
this.theFile = (IFile) ((IAdaptable) lastSegmentObj).getAdapter(IFile.class);
this.workspaceName = this.theResource.getWorkspace().getRoot().getLocation().toOSString();
this.projectName = this.theProject.getName();
this.fileName = this.theResource.getName();
return true;
} else {
String selectionClass = selection.getClass().getSimpleName();
MessageDialog.openError(this.window.getShell(), "Unexpected Selection Class", String.format("Expected a TreeSelection but got a %s instead.\nProcessing Terminated.", selectionClass));
}
return false;
}
@SuppressWarnings("rawtypes")
private static String getClassHierarchyAsMsg(String msgHeader, String msgTrailer, Object theObj) {
String msg = msgHeader + "\n\n";
Class theClass = theObj.getClass();
while(theClass != null) {
msg = msg + String.format("Class=%s\n", theClass.getName());
Class[] interfaces = theClass.getInterfaces();
for(Class theInterface : interfaces) {
msg = msg + String.format(" Interface=%s\n", theInterface.getName());
}
theClass = theClass.getSuperclass();
}
msg = msg + "\n" + msgTrailer;
return msg;
}
}
运行插件在其自己的Java文件上的结果。
![Plugin Result](https://istack.dev59.com/TKHa6.webp)