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.