Eclipse插件:获取用户点击/选择的类名 [英] Eclipse Plugin: Get the class name on user's click/selection
问题描述
(广义地说,我想在对应于该类名称的自定义视图中调用另一个软件的API)。
但是我无法在用户点击时提取类名。此功能与类型视图类似,只要用户在项目资源管理器中单击.java,相应的类文件就会在视图中出现。我很新的插件开发,请指导我。谢谢提前。
首先找到视图:
IWorkbenchPage page = PlatformUI.getWorkbench()。getActiveWorkbenchWindow()。getActivePage();
IViewPart viewPart = page.findView(id);
Packages Explorer视图的ID为org.eclipse.jdt.ui.PackageExplorer
如果视图未打开,则ViewPart将为空。
从视图站点获取选择提供者:
ISelectionProvider selProvider = viewPart.getSite()。getSelectionProvider();您可以使用 addSelectionChangedListener
来监听选择更改
在选择事件 getSelection()
通常会返回一个 IStructuredSelection
包含当前选择。使用 IStructuredSelection.iterator()
遍历选择对象。
在包中浏览每个选定的对象将作为类型的对象,例如 IProject
或 IFile
或者可能是JDT代码的特定内容。
I am creating an Eclipse Plugin. I am stuck in the part where I need to get the class name as soon as the user selects/clicks on classname in the package explorer and then I want to display it in a custom view.
(Broadly I want to call API of another software in the custom view corresponding to that class name).
But I am not able to extract the class name on user's click. This feature is similar to the "Types" view, where corresponding class file appears in the view as soon as user clicks .java in the project explorer. I am quite new to plugin development, please guide me.Thanks in advance.
解决方案 First find the view:
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IViewPart viewPart = page.findView(id);
The id for the Packages Explorer View is org.eclipse.jdt.ui.PackageExplorer
If the view is not open viewPart will be null.
Get the selection provider from the view site:
ISelectionProvider selProvider = viewPart.getSite().getSelectionProvider();
You can now use addSelectionChangedListener
to listen for selection changes.
In the selection event getSelection()
will normally return an instance of IStructuredSelection
containing the current selections. Use IStructuredSelection.iterator()
to iterate through the selection objects.
In the Packages Explorer View each selected object will be an object of a type such as IProject
or IFile
or perhaps something specific to the JDT code.
这篇关于Eclipse插件:获取用户点击/选择的类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!