eclipse外掛開發入門——使用command實現在資源管理器中定位資源
Eclipse外掛開發之command
Eclipse提供了三種命令與操作的方式:動作ActionSets、彈出選單popupmenus、命令Command,其中前兩種因為耦合過於緊密,在未來版本中可能棄用,不建議使用。
ActionSets方式使用的是擴充套件點org.eclipse.ui.actionsets;
popupmenus方式使用了一個擴充套件點:org.eclipse.ui.popupmenus;
Command方式使用了三個擴充套件點:org.eclipse.ui.menus; org.eclipse.ui.commands;org.eclipse.ui.handlers;
命令代表了外觀和行為之間的抽象。命令本身不代表外觀和行為,其捆綁一個或多個有一個或多個處理程式(行為)的Menu共享(外觀)。
下面開發一個在資源管理中開啟資源的外掛,如在java包或檔案中右鍵點選,開啟資原始檔。通過command定義擴充套件點。
1. 增加依賴
2. 新增擴充套件點
xml配置如下:
<extension
point="org.eclipse.ui.commands">
<category
name="Sample Category"
id="com.cnofe.explorer.commands.category">
</category>
<command
name="Explorer(定位資源位置)"
categoryId="com.cnofe.explorer.commands.category"
id="com.cnofe.explorer.commands.sampleCommand">
</command>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
commandId="com.cnofe.explorer.commands.sampleCommand"
class="com.cnofe.explorer.handlers.OpenExplorerHandler">
</handler>
</extension>
<extension
point="org.eclipse.ui.bindings">
<key
commandId="com.cnofe.explorer.commands.sampleCommand"
contextId="org.eclipse.ui.contexts.window"
sequence="ALT+O"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
</key>
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="popup:org.eclipse.ui.popup.any?after=additions">
<separator
name="com.cnofe.explorer.separator1">
</separator>
<command
commandId="com.cnofe.explorer.commands.sampleCommand"
icon="icons/folderlink.png"
style="push">
<visibleWhen>
<iterateifEmpty="false">
<or>
<instanceof
value="org.eclipse.team.ui.synchronize.ISynchronizeModelElement">
</instanceof>
<instanceof
value="org.eclipse.jdt.core.IJavaElement">
</instanceof>
<instanceof
value="org.eclipse.core.resources.IResource">
</instanceof>
</or>
</iterate>
</visibleWhen>
</command>
</menuContribution>
</extension>
3. 實現handler
publicclass OpenExplorerHandler extends AbstractHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil
.getActiveWorkbenchWindowChecked(event);
ISelection sel = window.getSelectionService().getSelection();
if (selinstanceof IStructuredSelection) {
Object obj = ((IStructuredSelection) sel).getFirstElement();
IResource resource = null;
String path = null;
// common resource file
if (objinstanceof IFile) {
resource = (IResource) obj;
path = resource.getLocation().toOSString();
path = path.substring(0, path.lastIndexOf(File.separator));
}
// other resource such as folder,project
elseif (objinstanceof IResource) {
resource = (IResource) obj;
path = resource.getLocation().toOSString();
}
// explorer java element, containfield,method,package
elseif (objinstanceof IJavaElement) {
// jar resource is null
if (objinstanceof JarPackageFragmentRoot) {
path = ((IPackageFragmentRoot) obj).getPath().toOSString();
// get folder
path = path.substring(0, path.lastIndexOf(File.separator));
} elseif (objinstanceof IPackageFragmentRoot) {
// src folder
String prjPath = ((IPackageFragmentRoot) obj)
.getJavaProject().getProject().getParent()
.getLocation().toOSString();
path = prjPath
+((IPackageFragmentRoot) obj).getPath()
.toOSString();
} elseif (objinstanceof IPackageFragment) {// other : package
resource = ((IPackageFragment) obj).getResource();
path = resource.getLocation().toOSString();
} else {// member:filed:
resource = ((IJavaElement) obj).getResource();
path = resource.getLocation().toOSString();
// get folder
path = path.substring(0, path.lastIndexOf(File.separator));
}
}
// explorer team ui resource
elseif (objinstanceof ISynchronizeModelElement) {
resource = ((ISynchronizeModelElement) obj).getResource();
}
if (path != null) {
try {
Runtime.getRuntime().exec("explorer " + path); //$NON-NLS-1$
} catch (IOException e) {
//
}
}
}
returnnull;
}
}
4. 執行截圖