我如何获得“IStructuredSelection”当用户选择文本时从编辑器中?
我正在开发 eclipse 插件。
我已经成功添加了一个上下文菜单(当用户在 Java 编辑器中并在编辑器中单击鼠标右键时)并将其部分连接到我创建的操作类。
显然,当用户单击编辑器上的任何元素(任何文本)并右键单击打开上下文菜单,然后选择我创建的菜单时,插件将开始跟踪用户选择的选择。但是,它跟踪的选择始终位于 ITextSelection 的实例中,我无法用它做任何有用的事情。我想将选择作为 IStructuredSelection
的实例来获取,以便我的插件可以使用它。 无论如何,我可以做到这一点吗?
public class MyPluginAction extends ActionDelegate implements IEditorActionDelegate {
public void run(IAction action) {
}
public void selectionChanged(IAction action, ISelection selection) {
if(selection instanceof IStructuredSelection){
System.out.println("IStructure");
} else if(selection instanceof ITextSelection){
System.out.println("IText");
}
}
public void setActiveEditor(IAction action, IEditorPart targetEditor) {
// TODO Auto-generated method stub
}
}
I am working on eclipse-plugin.
I have successfully added a context menu (while user is in Java editor and right click in the editor) and connected it partially to action class that I created.
Apparently, when user clicks on any element on the editor (any text) and right click to open the context menu, and select the menu that I created, the plugin will start tracing the selection that user selects. However, the selection that it trace is always in instance of ITextSelection
which I cannot do anything useful with it. I want to get selection as an instance of IStructuredSelection
so that my plugin can play with it.
Is there anyway that I can do that?
public class MyPluginAction extends ActionDelegate implements IEditorActionDelegate {
public void run(IAction action) {
}
public void selectionChanged(IAction action, ISelection selection) {
if(selection instanceof IStructuredSelection){
System.out.println("IStructure");
} else if(selection instanceof ITextSelection){
System.out.println("IText");
}
}
public void setActiveEditor(IAction action, IEditorPart targetEditor) {
// TODO Auto-generated method stub
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您从编辑器中选择文本,您将始终获得 ITextSelection,而获得 IStructuredSelection 根本没有意义。我想这个问题会解决您的问题。
If you select text from an Editor you'll get always an ITextSelection, getting an IStructuredSelection does not make sense at all. I guess this question will solve your problem.