Eclipse插件开发资源查找

  • 介绍
我们在开发Eclipse插件的时候,需要查找类、接口等资源的时候,怎么办呢?本文介绍如何使用Eclipse自带的查找对话框来获取类路径。
  • 查找类
[codesyntax lang="java"]
/**
* http://surenpi.com
*/
package org.surenpi.dev.plugin.utill;

import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.ui.IJavaElementSearchConstants;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.SelectionDialog;

/**
 * @author surenpi.com
 * @since jdk1.6
 * 2015年9月24日
 */
public class SearchDialog {
	/**
	 * 查找类
	 * @param shell
	 * @param multipleSelection
	 * @param title 对话框标题
	 * @param message
	 * @return
	 */
	public static Object[] searchType(Shell shell, int type, boolean multipleSelection,
			String title, String message){
		try {
			SelectionDialog dialog = JavaUI.createTypeDialog(shell,
					new ProgressMonitorDialog(shell),
					SearchEngine.createWorkspaceScope(), type,
					multipleSelection);
			
			dialog.setTitle(title);
			dialog.setMessage(message);
			
			if(dialog.open() == IDialogConstants.CANCEL_ID){
				return null;
			}
			
			return dialog.getResult();
		} catch (JavaModelException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static Object[] searchClass(Shell shell, boolean multipleSelection,
			String title, String message){
		return searchType(shell, IJavaElementSearchConstants.CONSIDER_CLASSES,
				multipleSelection, title, message);
	}
	
	public static IType searchClass(Shell shell, String title, String message){
		Object[] result = searchClass(shell, false, title, message);
		
		if(result != null && result.length > 0){
			return (IType) result[0];
		}
		
		return null;
	}
	
	/**
	 * 查找Action中的Module类
	 * @param shell
	 * @return
	 */
	public static IType searchModuleClass(Shell shell){
		String title = "查找Module类";
		String message = "请选择PO或者VO作为Action的Module(ListModule)";
		
		return searchClass(shell, title, message);
	}
	
	public static Object[] searchInterface(Shell shell, boolean multipleSelection,
			String title, String message){
		return searchType(shell, IJavaElementSearchConstants.CONSIDER_INTERFACES,
				multipleSelection, title, message);
	}
	
	public static IType searchInterface(Shell shell, String title, String message){
		Object[] result = searchType(shell, IJavaElementSearchConstants.CONSIDER_INTERFACES,
				false, title, message);
		
		if(result != null && result.length > 0){
			return (IType) result[0];
		}
		
		return null;
	}
	
	/**
	 * 查找Action中的Module类
	 * @param shell
	 * @return
	 */
	public static IType searchInjectClass(Shell shell){
		String title = "查找依赖类";
		String message = "请选择服务";
		
		return searchInterface(shell, title, message);
	}
}
[/codesyntax]
  • 查找所有的工程(project)
[codesyntax lang="java"]
/**
* Copyright © 1998-2015, surenpi.com. All Rights Reserved.
*/

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.ListSelectionDialog;

/**
 * @author surenpi
 * @since jdk1.6
 * 2015年11月17日
 */
public class ProjectDialog {
	/**
	 * 从当前工作空间(workspace)中选择工程(project)
	 * @param shell
	 * @param selection
	 * @return
	 */
	public static Object[] getSelectProjects(Shell shell, SelectionAdapter selection){
		IProject[] allProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects();

		ILabelProvider labelProvider = new LabelProvider(){
			public String getText(Object element){
				if(element instanceof IProject){
					return ((IProject) element).getName();
				}else{
					return element.toString();
				}
			}
		};
		
		SimpleListContentProvider contentsProvider = new SimpleListContentProvider();
		contentsProvider.setElements(allProjects);
		
		ListSelectionDialog dialog = new ListSelectionDialog(shell, selection,
				contentsProvider, labelProvider, "从列表中选择工程"){
			protected int getShellStyle(){
				return super.getShellStyle() | SWT.LONG;
			}
		};
		
		if(dialog.open() != 0){
			return null;
		}else{
			return dialog.getResult();
		}
	}
	
	private static class SimpleListContentProvider implements IStructuredContentProvider{

		private Object[] elements;
		
		@Override
		public void dispose() {
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}

		@Override
		public Object[] getElements(Object inputElement) {
			return this.elements;
		}
		
		public void setElements(Object[] items){
			this.elements = items;
		}
	}
}
[/codesyntax] 注意:上面的对话框,返回的结果集是IProject对象。

转载于:https://my.oschina.net/surenpi/blog/604729

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
原创Eclipse件: 高级搜索功能 - com.realzyc.search.jar Eclipse Plugin 件 java 全局 搜索 + 特性: 相比与原有eclipse仅支持的在当前文档的搜索功能, 本件支持了如下的搜索范围: - 当前文档 原有 查找/替换 已支持 - 当前选中 原有 查找/替换 已支持 - 所有打开的文档 原有 查找/替换 不支持 - 当前项目 原有 查找/替换 不支持 - 所有打开的项目 原有 查找/替换 不支持 - 整个工作空间 原有 查找/替换 不支持 仅以在“整个工作空间”搜索为例, 件提供在整个工作空间(workspace)中的全部文档的搜索.即使项目在关闭状态, 也可以获得搜索结果. + 特性: 本搜索件提供了“搜索全部”功能, 并将结果全部在列表中显示.用户可以全局通观搜索结果, 并任意的导航的希望的结果.(而不是像原有的功能一样逐一的导航到结果). + 其他: 件还提供: 字体支持, 中英语言界面切换, 搜索摘要显示等特性. + 本发布包中含有的文件: com.realzyc.search_1.3.5.jar 件最小执行文档, 不含源文件/javadoc com.realzyc.search.introduction.en.pdf 件介绍文档, 英文 com.realzyc.search.introduction.cn.pdf 件介绍文档, 中文 readme.txt 件介绍摘要 + 声明: 本件由RealZYC独立开发, 仅用于个人使用. 如果有爱好这方面件的同志们, 请回复或发邮件联系, 也欢迎大家加入编程Q群(78728985)互相切磋 欢迎大家下载评论.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值