Several ways to get current selected works in Eclipse plug-in development

Source: Internet
Author: User

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 ();     

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.