2014-11-24 163 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