Eclipse PDE,Navigator View,TreeSelection - 获取文件类型和名称

Jon*_*Jon 7 java eclipse eclipse-pde eclipse-plugin eclipse-rcp

我正在尝试在"导航器树"视图中获取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());
   }            
}
Run Code Online (Sandbox Code Playgroud)

这个问题是它目前已经加入了JDT编译单元API,它是内部的,并且对我想要的内容过于具体.

理想情况下,我希望能够获得基础文件名,类型和内容,而不必依赖:

  1. 内部API
  2. JDT编译单元代码.

这样,当用户右键单击导航器视图中的文件时,这将允许我获取通用文件的属性.

有人可以向我提供有关我如何做到这一点的任何指示吗?

Sco*_*eld 9

[编辑:我添加了以下替代方案 - 最初的答案是父亲失望]

首先:如果您在Package Explorer中选择了某些内容,则所选项目都是Java Model对象 - 您必须在某个级别处理它们.有两种方法可以解决这个问题:

  1. 直接使用ICompilationUnit(参见更远的地方)
  2. 创建一个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};
    }
}
Run Code Online (Sandbox Code Playgroud)

扩展名

在将托管适配器工厂的插件中,将以下内容添加到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>
Run Code Online (Sandbox Code Playgroud)

使用适配器

有了上述内容,您现在可以写:

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
Run Code Online (Sandbox Code Playgroud)

使用此方法,您可以添加其他适配器以将其他类型转换为IFile,并且您的选择代码无关紧要.


直接ICompilationUnit方法

[编辑:我改变了答案,但是将以下内容作为参考信息b/c,这是探索在包浏览器中选择的编译单元内容的标准方法]

这实际上是获取包浏览器中文件内容的首选方法...

您应该使用ICompilationUnit,而不是使用CompilationUnit.大多数eclipse API使用接口进行公共消费,使用类来获取内部细节.

如果您将代码更改为

if (firstElement instanceof ICompilationUnit) {
    ICompilationUnit unit = (ICompilationUnit firstElement;
    String contents = new String(unit.getContents());
}
Run Code Online (Sandbox Code Playgroud)

你会保持良好状态.

要查看检查/修改Java模型和源代码的详细信息:

(In Eclipse)
Help->
  Help Contents->
    JDT Plug-in Developer's Guide->
      Programmer's Guide->
        JDT Core
Run Code Online (Sandbox Code Playgroud)

这显示了如何适当地使用Java模型

要隔离引用java模型的位置,可以创建一个(eclipse)适配器,它将Java Model对象转换为文件.假设存在这样的适配器,您可以请求AdapterManager将其转换为java文件.我会先看看是否存在.

  • 如果您在Package Explorer中选择了某些内容,那么它*是一个ICompilationUnit.你无法解决这个问题.如果你想从中获取IFile,你可以调用unit.getResource(),但为什么在没有必要的时候转换它.我正在为您提供正确的答案.如果您在其他类型的查看器(非包浏览器)中进行选择,您可以选择其他内容.有关导航结构,请参阅上面的JDT核心指南. (2认同)
  • 这取决于你选择的东西.如果在Package Explorer中选择项目,则必须处理Java模型 - 没有办法解决它.如果您在不同的视图中选择内容,则取决于该视图所公开的内容. (2认同)

Eug*_*ene 3

你想达到什么目的?您想获取文件内容吗?那么你可以尝试:

IAdaptable firstElement = (IAdaptable) treeSelection.getFirstElement();

IFile file = (IFile) firstElement.getAdapter(IFile.class);
if (file != null && file.isAccessible()) {
    // Use getContents API
    ....
}
Run Code Online (Sandbox Code Playgroud)

  • 除非已注册适配器以将选择转换为 IFile,否则这将不起作用 - 请参阅下面我的答案以了解如何执行此操作 (2认同)