This is a creation in Article, where the information may have evolved or changed.
Go to resource is the function of a submenu under Eclipse's Navigate menu, as follows:
Go to Resource action is a retargetaction defined in the Org.eclipse.ui.ide plugin, and the definition for 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"//Yes Retarget Action id= "Gotoresource" ></action>
When the Package Explorer view is active, its true action is org.eclipse.wst.jsdt.internal.ui.packageview.GotoResourceAction, It can select a resource and then locate it in the tree structure of the Packge Explorer view. The UI effect that is triggered may be this way, and the UI effect after it is triggered may be as follows:
The Go to Resource dialog box has a class of gotoresourcedialog (a static inner class), so let's focus on the implementation of this class. In essence it is a variant of Org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog (extended by 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 which types to display include Ifile,ifolder,iprojectsuper (Parentshell, False, container, Iresource.file | Iresource.folder | Iresource.project), fjavamodel= javacore.create (Resourcesplugin.getworkspace (). Getroot ());//jdt 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.ui.dialogs.FilteredResou Rcesselectiondialog.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 must is combined with the <code>matchItem</code> method * from Super CLA SS (<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= res Ource.getproject (); try {if (project.getnature (javacore.nature_id) = null) return Fjavamodel.contains (Resource);} catch (Coreexception e) {//Do nothing. consider resource;} return true;} public boolean Equalsfilter (Itemsfilter filter) {if (!super.equalsfilter (filter)) {return false;} if (! ( Filter instanceof Gotoresourcefilter) {return false;} return true;}}}
Then look at the core implementation of Gotoresourceaction.
public void Run () {treeviewer viewer= fpackageexplorer.gettreeviewer ();//construct Gotoresourcedialog instance, Incoming 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 exactly the structuredselection type if (element! = null && element.exists ()) selection= new structuredselection (element); elseselection= new Structuredselection (result[0]); Viewer.setselection (selection, true);}