Eclipse plug-in development (1)-Get project objects

Source: Internet
Author: User

When an element is selected from the navigation view or the package explorer view of jdt, you can open the editor, open the view, and right-click the action in the menu, when writing the Corresponding Editor, view, or action plug-in, you may need to obtain the selected element and its project object. This document takes the execution of action as an example to describe how to obtain it.

1. Add the private property selection of istructuredselection to the action object (implementing iobjectactiondelegate) and implement the selectionchanged method as follows:

Public void selectionchanged (iaction action, iselection selection ){
This. Selection = (istructuredselection) selection;
Processselection ();
}

2. Implement the processselection () method, obtain the selected element from the selection object, and obtain the project object from it, as follows:

Private void processselection (){
Iterator it = selection. iterator ();
If (it. hasnext ()){
Object next = it. Next ();
If (next instanceof Resource ){
Proj = (Resource) Next). getproject ();
} Else if (next instanceof ijavaelement) {// if it is an ijavaelement object, obtain the javaproject object first.
Proj = (ijavaelement) Next). getjavaproject (). getproject ();
}

}

3. Obtain the ijavaproject () object.

If the selected element is an ijavaelement object, you can directly obtain the ijavaproject object.

If the selected element is only a resource, you can obtain the iproject object before calling:

Ijavaproject javapro = javacore. Create (proj );

Obtain the ijavaproject object.

 

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.