更新:我的原始答案不起作用,因爲沒有上下文結構化的選擇。
This answer顯示如何將操作提供給上下文菜單並檢索結構化選擇。
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
try {
IStructuredSelection selection = SelectionConverter
.getStructuredSelection(activePart);
IJavaElement[] elements = SelectionConverter.getElements(selection);
if (elements != null && elements.length > 0) {
if (elements[0] != null && elements[0] instanceof IType) {
IType type = (IType)elements[0];
ITypeHierarchy hierarchy =
type.newTypeHierarchy(new NullProgressMonitor());
IType[] subTypes = hierarchy.getAllSubtypes(type);
StringBuffer buf = new StringBuffer();
for (IType iType : subTypes) {
buf.append(iType.getFullyQualifiedName()).append("\n");
}
Shell shell = HandlerUtil.getActiveShell(event);
Clipboard clipboard = new Clipboard(shell.getDisplay());
clipboard.setContents(
new Object[]{buf.toString()},
new Transfer[]{TextTransfer.getInstance()});
}
}
} catch (JavaModelException e) {
logException(e);
}
return null;
}
相關http://stackoverflow.com/questions/492184/how-do-you-find-all-subclasses-of-a-given-class-in-java – Narayan 2009-09-15 10:05:09
@Narayan:我見過這個問題,但那是關於以編程方式進行的。我希望只需點擊幾下即可獲得解決方案。 – 2009-09-15 10:08:37
你可以得到一個自定義的視圖,而不需要複雜的「層次視圖」的TypeHierarchyViewPart,但使用相同的底層機制來檢索/顯示類的層次結構。 – VonC 2009-09-15 11:35:17