Go to resource is a sub-menu function under the navigate menu of Eclipse, as shown below:
Go to resource action is a retargetaction defined in the org. Eclipse. UI. Ide plug-in. The content of this action in plugin. XML is:
<Action definitionid = "org. eclipse. UI. navigate. gotoresource "label =" % gotoresourceaction. label "icon =" $ NL $/icons/full/elcl16/gotoobj_tsk.gif "menubarpath =" navigate/goto/"retarget =" true "// retarget action id =" gotoresource"> </Action>
When the package explorer view is activated, the actual action is Org. eclipse. WST. jsdt. internal. UI. packageview. gotoresourceaction, which can select a resource and locate it in the tree structure of the packge explorer view. the UI effect after triggering may be like this, And the UI effect after triggering may be like this:
The class corresponding to the go to resource dialog box is gotoresourcedialog (a static internal class). Let's take a look at the implementation of this class. In essence, it is Org. eclipse. UI. dialogs. the filteredresourcesselectiondialog variant (which extends this class) filteredresourcesselectiondialog has a large number of applications in eclipse.
Private Static class gotoresourcedialog extends filteredresourcesselectiondialog {private ijavamodel fjavamodel; Public gotoresourcedialog (shell parentshell, icontainer container, structuredviewer viewer) {// iresource. file | iresource. folder | iresource. project indicates the types to be displayed, including ifile, ifolder, iprojectsuper (parentshell, false, container, and iresource. file | iresource. folder | iresource. project); fjavamodel = Java Core. create (resourcesplugin. getworkspace (). getroot (); // The rootsettitle (packagesmessages. gotoresource_dialog_title); // the title of the dialog box platformui. getworkbench (). gethelpsystem (). sethelp (parentshell, ijavahelpcontextids. goto_resource_dialog);} protected itemsfilter createfilter () {return New gotoresourcefilter ();} private class gotoresourcefilter extends resourcefilter {/** (non-javadoc) ** @ see Org. eclipse. u I. dialogs. filteredresourcesselectiondialog. resourcefilter # matchitem (Java. lang. object) */Public Boolean matchitem (Object item) {iresource resource = (iresource) item; return Super. matchitem (item) & select (Resource) ;}/ *** this is the orignal <code> select </code> method. since * <code> gotoresourcedialog </code> needs to extend * <code> filteredresourcesselectiondialog </code> result of this * method mus T be combined with the <code> matchitem </code> Method * from super class (<code> resourcefilter </code> ). ** @ Param resource * a resource * @ return <code> true </code> If item matches against given * conditions <code> false </code> otherwise */private Boolean select (iresource Resource) {iproject project = resource. getproject (); try {If (project. getnature (javacore. nature_id )! = NULL) return fjavamodel. contains (Resource);} catch (coreexception e) {// do nothing. consider resource;} return true;} public Boolean inclusfilter (itemsfilter filter) {If (! Super. ipvsfilter (filter) {return false;} If (! (Filter instanceof gotoresourcefilter) {return false;} return true ;}}}
Let's look at the core implementation of gotoresourceaction.
Public void run () {treeviewer viewer = fpackageexplorer. gettreeviewer (); // construct a gotoresourcedialog instance and input the model resourcesplugin. getworkspace (). getroot () gotoresourcedialog dialog = new gotoresourcedialog (fpackageexplorer. getsite (). getshell (), resourcesplugin. getworkspace (). getroot (), viewer); dialog. open (); object [] result = dialog. getresult (); If (result = NULL | result. length = 0 |! (Result [0] instanceof iresource) return; structuredselection selection = NULL; ijavaelement element = javacore. create (iresource) result [0]); // wrap the selected modle as structuredselection, because treeviewer needs the structuredselection type if (element! = NULL & element. exists () Selection = new structuredselection (element); elseselection = new structuredselection (result [0]); viewer. setselection (selection, true );}