【发布时间】: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