Go To Resource是Eclipse的Navigate菜单下的一个子菜单的功能,如下所示:
Go To Resource Action是一个RetargetAction,在org.eclipse.ui.ide插件中定义,plugin.xml中关于此action的定义内容为:
<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>
Go To Resource对话框对应的类为GotoResourceDialog(是一个静态内部类),我们先关注一下此类的实现吧,本质上它是org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog的变种(扩展了此类)FilteredResourcesSelectionDialog在Eclipse中有大量的应用.
private static class GotoResourceDialog extends FilteredResourcesSelectionDialog { private IJavaModel fJavaModel; public GotoResourceDialog(Shell parentShell, IContainer container, StructuredViewer viewer) { //IResource.FILE | IResource.FOLDER | IResource.PROJECT表示要显示的类型包括IFile,IFolder,IProject super(parentShell, false, container, IResource.FILE | IResource.FOLDER | IResource.PROJECT); fJavaModel= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());//JDT的Root setTitle(PackagesMessages.GotoResource_dialog_title);//对话框的标题 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.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 must 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 equalsFilter(ItemsFilter filter) { if (!super.equalsFilter(filter)) { return false; } if (!(filter instanceof GotoResourceFilter)) { return false; } return true; } } }
再看看GoToResourceAction的核心实现.
public void run() { TreeViewer viewer= fPackageExplorer.getTreeViewer(); //构造GoToResourceDialog实例,传入模型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]); //将选中的modle包装为StructuredSelection,因为TreeViewer需要的正是StructuredSelection类型 if (element != null && element.exists()) selection= new StructuredSelection(element); else selection= new StructuredSelection(result[0]); viewer.setSelection(selection, true); }
有疑问加站长微信联系(非本文作者)