我正在尝试获取 Eclipse 用户在导航 TreeView 中的结构化选择的详细信息。目前,我有以下基于 org.eclipse.ui.popMenus 扩展点的内容:
public void run(IAction action) {
Shell shell = new Shell();
ISelection selection = workbenchPart.getSite().getSelectionProvider().getSelection();
if (structuredSelection instanceof org.eclipse.jface.viewers.TreeSelection) {
org.eclipse.jface.viewers.TreeSelection treeSelection = (org.eclipse.jface.viewers.TreeSelection) structuredSelection;
IAdaptable firstElement = (IAdaptable) treeSelection.getFirstElement();
// Relies on an internal API, bad juju
if (firstElement instanceof org.eclipse.jdt.internal.core.CompilationUnit) {
org.eclipse.jdt.internal.core.CompilationUnit compilationUnit = (org.eclipse.jdt.internal.core.CompilationUnit) firstElement;
String editorSelection = new String(compilationUnit.getContents());
}
}
问题是它目前耦合到 JDT 编译单元 API,它是内部的并且对于我想要的来说太具体了。
理想情况下,我希望能够获取底层文件名、类型和内容,而不必依赖:
- 内部 API
- JDT 编译单元代码。
这将允许我在用户右键单击导航器 View 中的文件时获取通用文件的属性。
有人可以向我提供有关如何执行此操作的任何指示吗?
最佳答案
[编辑:我添加了以下备选方案——原来的答案是 father down]
首先:如果您在 Package Explorer 中选择了一些东西,那么所选的项目都是 Java 模型对象——您必须在某种程度上处理它们。有两种方法可以处理此问题:
- 直接使用 ICompilationUnit(往下看)
- 创建一个 Eclipe 适配器工厂来自动转换
适配器工厂方法
您可以创建一个适配器工厂(它可以存在于您的主插件或其他插件中),eclipse 可以使用它来自动从 ICompilationUnit 转换为 IFile。
注意:如果您在不同的插件中创建适配器工厂,您可能需要为其设置早期启动以加载适配器工厂。否则,您将需要根据提供适配器的插件来让您的插件与选择配合使用。
在 http://www.eclipse.org/resources/resource.php?id=407 上有一些关于适配器的重要细节,但我将在此处讨论此问题的实现。
依赖
将托管适配器的插件需要以下依赖项
- org.eclispe.core.resources
- org.eclipse.jdt.core
适配器工厂类
在你的新插件中定义以下类
package com.javadude.foo;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.jdt.core.ICompilationUnit;
public class CompilationUnitToFileAdapter implements IAdapterFactory {
@Override
public Object getAdapter(Object adaptableObject, Class adapterType) {
if (adaptableObject instanceof ICompilationUnit)
// note: "adapting" it here just means returning the ref'd IFile
return (IFile) ((ICompilationUnit)adaptableObject).getResource();
return null;
}
@Override
public Class[] getAdapterList() {
return new Class[] {IFile.class};
}
}
扩展
在将托管适配器工厂的插件中,将以下内容添加到您的 plugin.xml 中:
<extension point="org.eclipse.core.runtime.adapters">
<factory
adaptableType="org.eclipse.jdt.core.ICompilationUnit"
class="com.javadude.foo.AdapterFactory1">
<adapter type="org.eclipse.core.resources.IFile" />
</factory>
</extension>
使用适配器
有了上面的内容,你现在可以写:
Object firstElement = ((ITreeSelection) selection).getFirstElement();
IFile file = (IFile) Platform.getAdapterManager().
getAdapter(firstElement, IFile.class);
if (file == null)
// adapter not present; cannot use as IFile
else
// adapter present - you can use it as an IFile
使用这种方法,您可以添加额外的适配器以将其他类型转换为 IFile,而您的选择代码并不关心。
直接 ICompilationUnit 方法
[编辑:我已经更改了答案,但将以下内容保留为引用信息 b/c 这是探索在包资源管理器中选择的编译单元内容的标准方法]
这实际上是在包资源管理器中获取文件内容的首选方式...
与其使用 CompilationUnit,不如使用 ICompilationUnit。大多数 Eclipse API 使用接口(interface)供公众使用,使用类处理内部细节。
如果您将代码更改为
if (firstElement instanceof ICompilationUnit) {
ICompilationUnit unit = (ICompilationUnit firstElement;
String contents = new String(unit.getContents());
}
你的状态会很好。
要查看检查/修改 Java 模型和源代码的详细信息:
(In Eclipse)
Help->
Help Contents->
JDT Plug-in Developer's Guide->
Programmer's Guide->
JDT Core
这展示了如何适本地使用 Java 模型
要隔离引用 java 模型的位置,您可以创建一个 (eclipse) 适配器,它将 Java 模型对象转换为文件。假设存在这样的适配器,您可以要求 AdapterManager 为您将其转换为 java 文件。我会看一眼,看看是否存在。
关于java - Eclipse PDE、Navigator View、TreeSelection - 获取文件类型和名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/775709/