Eclipse源码阅读:Go To Resource

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


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值