Eclipse源码阅读:Go To Resource

cuser_online · · 2180 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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>

 

当Package Explorer视图激活时,其真实对应的Action为org.eclipse.wst.jsdt.internal.ui.packageview.GotoResourceAction,它可以选中某资源然后在Packge Explorer视图的树结构中进行定位.触发后的UI效果可能是这样的,其触发后的UI效果可能是如下图这样的:

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);
}


 

 


有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:cuser_online

查看原文:Eclipse源码阅读:Go To Resource

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2180 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传