2013-12-09 35 views
0

我必須編寫一個Eclipse插件,但是我從來沒有這樣做過,所以我有一些問題。
右鍵單擊Eclipse項目瀏覽器中的Java項目時,該插件應該出現在上下文菜單中。它應該打開一個對話框,用戶可以在所選項目中輸入他正在查找的文件名,然後突出顯示該文件(如果存在具有該名稱的文件)。
我到目前爲止設法做的是設置插件開發項目,插件和對話框的擴展點。
但現在我不知道如何獲得選定的項目。你能告訴我這是如何完成的或者指向相應的API的鏈接嗎?
在此先感謝:)在項目中搜索文件的eclipse插件編程(需要基本幫助)

回答

1

我假設你有一個Handler類用於你的插件中的右鍵單擊動作。 Handler擴展了AbstractHandler並覆蓋了execute(..)方法。

然後,你可以做這樣的事情:

public class YourHandler extends AbstractHandler { 

private ExecutionEvent event; 

@Override 
public Object execute(ExecutionEvent event) throws ExecutionException { 

    // First get the tree of the right-clicked project. 
    ISelection sel = HandlerUtil.getActiveMenuSelection(event); 

    IResource resource = null; 
    IProject project = null; 

    try { 
     IStructuredSelection selection = (IStructuredSelection) sel; 

     // Get the first element of the tree (return type Object). 
     Object firstElement = selection.getFirstElement(); 

     // Get the IResource and from this the IProject of the selection. 
     if (firstElement instanceof IAdaptable) { 
      IResource resource = (IResource) (((IAdaptable) firstElement) 
       .getAdapter(IResource.class)); 

      project = res.getProject(); 
     } 
    } catch (ClassCastException e) { 
     // Do nothing. 
    } 

    // Then you can do something with the project. 

    return project; 
} 

看也是在爲爲IProject Eclipse的API,你可以做什麼:http://help.eclipse.org/kepler/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/resources/IProject.html

例如正從名稱的文件:

IFile getFile(String name)

返回帶有giv的文件句柄在這個項目中名稱。

希望這會有所幫助。

順便說一句:如果您需要了解開發Eclipse插件的一些不錯的教程,我可以推薦這個網站http://www.vogella.com/eclipse.html

乾杯。

+0

謝謝,看起來它會幫助我很多。一旦我有機會嘗試一下,我會再回答。 –

+0

嗨,弗蘭克。我的答案是否解決了您的問題?如果是這樣,請接受答案。謝謝。 – dwettstein

0

我寫了一些util類來完成這項工作。希望它對你有所幫助

public class SelectionUtil { 
    private IWorkbenchWindow window; 
    private IWorkbenchPage activePage; 
    private TreeSelection treeSelection; 
    private TreePath[] treePaths; 
    HashMap<Object, Object> selectData; 
    private IProject theProject; 
    private IResource theResource; 
    private IFile theFile; 
    private IPackageFragment theFragment; 
    private String workspaceName; 
    private String projectName; 
    private String fileName; 
    private String fileNameFile; 
    private String fragmentName; 
    private TreePath treePath; 
    public SelectionUtil(ExecutionEvent event) { 
     this.window = HandlerUtil.getActiveWorkbenchWindow(event); 
     // Get the active WorkbenchPage 
     this.activePage = this.window.getActivePage(); 

     // Get the Selection from the active WorkbenchPage page 
     ISelection selection = this.activePage.getSelection(); 
     if (selection instanceof ITreeSelection) { 
      this.treeSelection = (TreeSelection) selection; 
      this.treePaths = treeSelection.getPaths(); 
      this.treePath = treePaths[0]; 
      selectData = new ProjectSelectionUtil() 
        .populatePojectData(treePath); 
      setData(); 
     } else { 
      String selectionClass = selection.getClass().getSimpleName(); 
      MessageDialog 
        .openError(
          this.window.getShell(), 
          "Unexpected Selection Class", 
          String.format(
            "Expected a TreeSelection but got a %s instead.\nProcessing Terminated.", 
            selectionClass)); 
     } 
    } 
    public void setData() { 
     this.theProject = (IProject) selectData.get("Project"); 
     this.theResource = (IResource) selectData.get("Resource"); 
     this.theFragment = (IPackageFragment) selectData.get("Fragment"); 
     this.workspaceName = this.theResource.getWorkspace().getRoot() 
       .getLocation().toOSString(); 
     this.projectName = this.theProject.getName(); 
     if (this.theFragment != null) 
      this.fragmentName = this.theFragment.getElementName(); 
     try { 
      if (!this.theResource.getName().isEmpty() 
        && this.theResource.getName().length() > 5) 
       this.fileName = this.theResource.getName().substring(0, 
         this.theResource.getName().length() - 5); 
     } catch (NullPointerException e) { 
      System.out 
        .println(" GactusWindowSelectionUtil SetData NullPointerException" 
          + e.getMessage() + e.getLocalizedMessage()); 
     } catch (StringIndexOutOfBoundsException e) { 
      System.out 
        .println(" StringIndexOutOfBoundsException SetData NullPointerException" 
          + e.getMessage() + e.getLocalizedMessage()); 
     } 

    } 






    public String toString() { 
     ProjectInformation myProject = new ProjectInformation(theProject); 
     return "Segment Count " + treePath.getSegmentCount() + " Iproject" 
       + myProject.toString(); 
    } 
}