【问题标题】:Get file path after selection of an object选择对象后获取文件路径
【发布时间】:2018-09-17 10:30:58
【问题描述】:

我正在编写一个简单的 Eclipse 插件,它会在鼠标右键单击位于 xml 文件中的特定元素后显示 UI。在处理程序中,我需要首先获取包含所选对象的文件路径。不幸的是,我尝试了以下帖子对我不起作用。

How to get the Path of current selected file in Eclipse?

IFile file = (IFile) Platform.getAdapterManager().getAdapter(obj,
            IFile.class);

Eclipse Plugin Get Code from Current Open File

IFile file = (IFile) workbenchPart.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class);

注意:文件不需要在活动编辑器中打开。

【问题讨论】:

  • 选中的对象是什么,你是怎么获得的?会发生什么而不是您的预期?
  • Tnx @nitind,选择的对象是Ecore的EObject类型,使用`IStructuredSelection ssel = (IStructuredSelection) selection; Object obj = ssel.getFirstElement();`文件在第一种方法中返回null,在第二种方法中返回活动文件。注意:无论是否激活,我都想获取文件路径,但仅在所选对象上。

标签: eclipse eclipse-plugin selection


【解决方案1】:

您需要从 EObject 中检索 EResource。 请注意,EObject 可能在内存中,在这种情况下,没有与之关联的 EResource/File。

https://www.eclipse.org/forums/index.php?t=msg&th=796431&goto=1404419&#msg_1404419

问候,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多