2014-11-24 165 views
0

我是eclipse插件开发新手。我写了一个代码,它获取了选定项目名称&的路径。但是,如果选择多个项目,则会列出第一个项目。检查是否选择多个项目

这里是我的代码

IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection(); 
Object firstElement = selection.getFirstElement(); 
if (firstElement != null) { 
    if (firstElement instanceof IAdaptable) { 
     IProject project = (IProject) ((IAdaptable) firstElement).getAdapter(IProject.class); 
     IPath path = project.getFullPath(); 
     IPath location = project.getLocation(); 
    } 
} 

如何检查多个项目是否被选中呢?

回答

0

使用selection.iterator并迭代选定的项目。它可能是这样的!

Iterator it = selection.iterator(); 
    while(it.hasNext()){ 
    Object firstElement = it.next(); 
if (firstElement != null) { 
    if (firstElement instanceof IAdaptable) { 
     IProject project = (IProject) ((IAdaptable) firstElement).getAdapter(IProject.class); 
     IPath path = project.getFullPath(); 
     IPath location = project.getLocation(); 
    } 
} 
    } 

编辑:更详细的代码

纳入你的逻辑更新,看看singlr或多个项目是否处于选中状态。使用计数器检查选择了多少个项目。

+0

它导致无限循环 – 2014-11-24 07:17:38

+0

您可以查看迭代器(it)的大小并结束它。希望您使用“instanceOf IAdaptable”来查看是否选择了多个项目。 – hemanth 2014-11-24 07:56:30

+0

您必须在循环中调用“it.next()”才能移动到每个新项目。 – 2014-11-24 08:14:12