At the time of the Eclipse plug-in development, we often need to know what the currently selected project is, in order to do some processing for the project. With the current project, we can get any directories and files in the project. So it's very useful to get the current project.
Google the information on this. Basically said to be obtained through iselection. But Iselection did not say where to come from, generally as an abstract method of the parameters, in our implementation of the method newsletters to us. But this is not very good, we may often need to get the current project object in a method that does not have iselection parameters.
It doesn't matter, use the following code to get the current project anywhere in our plugin:
Java code public static iproject getcurrentproject () { ISelectionService selectionService = workbench.getinstance (). Getactiveworkbenchwindow () Getselectionservice (); iselection selection = selectionservice.getselection (); IProject project = null; if (selection instanceof istructuredselection) { Object element = ((istructuredselection) selection). Getfirstelement (); if (element instanceof iresource) { project= ((Iresource) Element). Getproject (); } else if (Element instanceof packagefragmentrootcontainer) { ijavaproject jproject = ((Packagefragmentrootcontainer) Element). Getjavaproject (); &Nbsp; project = jproject.getproject (); } else if (element instanceof ijavaelement) { IJavaProject jProject= ((ijavaelement) Element). Getjavaproject (); project = jproject.getproject (); } } return project; }
The above is a copy of the online approach, get the selected node and then get the project is of course reasonable. It's just not always easy to use because you don't know what you're going to choose.
Java code public static iproject getproject () { IProject project = null;         //1. Get engineering from current editor ieditorpart part = getactiveeditor (); if (part != null) { object object = part.geteditorinput (). Getadapter (ifile.class); if (object != null) { project = ((ifile) object). Getproject (); } } if ( Project == null) { ISelectionService selectionService = Workbench.getinstance (). Getactiveworkbenchwindow (). Getselectionservice (); ISelection selection = Selectionservice.getselection (); if (selection instanceof istructuredselection) { object element = ((IStructuredSelection) selection) . Getfirstelement (); if (element instanceof iresource) { project= ((Iresource) Element). Getproject (); } else if (Element instanceof packagefragmentrootcontainer) { IJavaProject jProject = ((Packagefragmentrootcontainer) Element). Getjavaproject (); project = jproject.getproject (); } else if (element instanceof ijavaelement) { IJavaProject jProject= ((ijavaelement) Element) . Getjavaproject (); project = jproject.getproject ();