- 介绍
我们在开发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]
标签:插件,shell,return,String,title,Eclipse,开发资源,org,message From: https://blog.51cto.com/suren/5764414