[英]Eclipse PDE, Navigator View, TreeSelection - Obtaining the file type and name
我正在尝试在“导航器树”视图中获取Eclipse用户的结构化选择的详细信息。 目前我有以下基于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,它是内部的,并且对我想要的东西太具体了。
理想情况下,我希望能够获得基础文件名,类型和内容,而不必依赖:
这样,当用户右键单击导航器视图中的文件时,这将允许我获取通用文件的属性。
有人可以向我提供有关我如何做到这一点的任何指示吗?
[编辑:我添加了以下替代方案 - 最初的答案是父亲失望]
首先:如果您在Package Explorer中选择了某些内容,则所选项目都是Java Model对象 - 您必须在某个级别处理它们。 有两种方法可以解决这个问题:
你可以创建一个适配器工厂(它可以存在于你的主插件或不同的插件中),eclipse可以使用它来自动从ICompilationUnit转换为IFile。
注意:如果您在其他插件中创建适配器工厂,则可能需要为其设置早期启动以使适配器工厂加载。 否则,你需要让你的插件适用于选择取决于提供适配器的插件。
关于适配器的一些很好的细节在http://www.eclipse.org/resources/resource.php?id=407 ,但我将在这里讨论这个问题的实现。
将托管适配器的插件需要以下依赖项
在新插件中定义以下类
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,并且您的选择代码无关紧要。
[编辑:我已经改变了答案,但是将以下内容作为参考信息b / c,这是探索在包浏览器中选择的编译单元内容的标准方法]
这实际上是获取包浏览器中文件内容的首选方法...
您应该使用ICompilationUnit,而不是使用CompilationUnit。 大多数eclipse API使用接口进行公共消费,使用类来获取内部细节。
如果您将代码更改为
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 Model对象转换为文件。 假设存在这样的适配器,您可以请求AdapterManager将其转换为java文件。 我会先看看是否存在。
那(将资源与JDT分离)是E4(Eclipse 4)的目标之一。
REsources的插件列表不再提及JDT(同样,仅限Eclipse 4.x):
org.eclipse.core.filesystem
- 一个抽象的通用文件系统API,包括本地文件系统的此API实现。 这是资源插件通过其访问底层文件系统的API。 org.eclipse.core.resources
- 包含API和资源模型的实现 org.eclipse.core.resources.compatibility
- 为在Eclipse 3.1或更高版本中打开旧工作区的用户提供迁移支持的插件 像e4photo这样的演示项目不需要任何JDT来从选择IContainer访问IResource 。
void setSelection(@Named(IServiceConstants.ACTIVE_SELECTION)
IResource selection) {
...
IResource[] members = input.members();
...
IResource resource = members[i];
if (resource.getType() == IResource.FILE) {
InputStream contents = ((IFile) resource).getContents();
看起来JDT已经为所有Java元素定义了一个IAdapterFactory
,包括编译单元( org.eclipse.jdt.internal.ui.JavaElementAdapterFactory
)。 适配器是为IResource
而不是IFile
定义的,所以你应该能够做到:
Object firstElement = treeSelection.getFirstElement();
IResource resource = (IResource)Platform.getAdapterManager().getAdapter(firstElement, IResource.class);
你想要实现什么目标? 你想要获取文件内容吗? 然后你可以尝试:
IAdaptable firstElement = (IAdaptable) treeSelection.getFirstElement();
IFile file = (IFile) firstElement.getAdapter(IFile.class);
if (file != null && file.isAccessible()) {
// Use getContents API
....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.