import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.search.IJavaSearchConstants; import org.eclipse.jdt.core.search.SearchEngine; import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; import org.eclipse.jdt.internal.ui.JavaPlugin; import org.eclipse.jdt.internal.ui.JavaPluginImages; import org.eclipse.jdt.internal.ui.JavaUIMessages; import org.eclipse.jdt.internal.ui.actions.OpenTypeAction; import org.eclipse.jdt.internal.ui.dialogs.OpenTypeSelectionDialog2; import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; import org.eclipse.jdt.internal.ui.util.ExceptionHandler; import org.eclipse.jface.action.IAction; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.help.WorkbenchHelp; public class OpenJavaAction extends OpenTypeAction { private OpenTypeSelectionDialog2 dialog; private Shell parent; private boolean bool = true; public OpenJavaAction() { super(); setText(JavaUIMessages.OpenTypeAction_label); //$NON-NLS-1$ setDescription(JavaUIMessages.OpenTypeAction_description); //$NON-NLS-1$ setToolTipText(JavaUIMessages.OpenTypeAction_tooltip); //$NON-NLS-1$ setImageDescriptor(JavaPluginImages.DESC_TOOL_OPENTYPE); WorkbenchHelp.setHelp(this, IJavaHelpContextIds.OPEN_TYPE_ACTION); parent = JavaPlugin.getActiveWorkbenchShell(); try { dialog = new OpenTypeSelectionDialog2(parent,true, PlatformUI .getWorkbench().getProgressService(), SearchEngine .createWorkspaceScope(),IJavaSearchConstants.TYPE); } catch (OperationCanceledException e) { return; } } public OpenTypeSelectionDialog2 getDialog() { return dialog; } public Object[] getType(){ return dialog.getResult(); } public void setBool(boolean bool){ this.bool = bool; } public void run() { // dialog.setMatchEmptyString(true); dialog.setTitle(JavaUIMessages.OpenTypeAction_dialogTitle); //$NON-NLS-1$ dialog.setMessage(JavaUIMessages.OpenTypeAction_dialogMessage); //$NON-NLS-1$ //$NON-NLS-1$ int result = dialog.open(); if (result != IDialogConstants.OK_ID) return; Object[] types = dialog.getResult(); if (types != null && types.length > 0) { IType type = (IType) types[0]; if(bool){ try { IEditorPart part = EditorUtility.openInEditor(type, true); EditorUtility.revealInEditor(part, type); } catch (CoreException x) { String title= JavaUIMessages.OpenTypeAction_errorTitle; //$NON-NLS-1$ String message= JavaUIMessages.OpenTypeAction_errorMessage; //$NON-NLS-1$ //$NON-NLS-1$ ExceptionHandler.handle(x, title, message); } } } } public void run(IAction action) { run(); } }
import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.search.IJavaSearchConstants; import org.eclipse.jdt.core.search.SearchEngine; import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; import org.eclipse.jdt.internal.ui.JavaPlugin; import org.eclipse.jdt.internal.ui.JavaPluginImages; import org.eclipse.jdt.internal.ui.JavaUIMessages; import org.eclipse.jdt.internal.ui.actions.OpenTypeAction; import org.eclipse.jdt.internal.ui.dialogs.OpenTypeSelectionDialog2; import org.eclipse.jface.action.IAction; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.help.WorkbenchHelp; public class SelectionClassAction extends OpenTypeAction { private OpenTypeSelectionDialog2 dialog; private Shell parent; private Text classValue; public SelectionClassAction() { super(); setText(JavaUIMessages.OpenTypeAction_label); //$NON-NLS-1$ setDescription(JavaUIMessages.OpenTypeAction_description); //$NON-NLS-1$ setToolTipText(JavaUIMessages.OpenTypeAction_tooltip); //$NON-NLS-1$ setImageDescriptor(JavaPluginImages.DESC_TOOL_OPENTYPE); WorkbenchHelp.setHelp(this, IJavaHelpContextIds.OPEN_TYPE_ACTION); parent = JavaPlugin.getActiveWorkbenchShell(); try { dialog = new OpenTypeSelectionDialog2(parent, true, PlatformUI .getWorkbench().getProgressService(), SearchEngine .createWorkspaceScope(), IJavaSearchConstants.TYPE); } catch (OperationCanceledException e) { return; } } public OpenTypeSelectionDialog2 getDialog() { return dialog; } public void run() { // dialog.setMatchEmptyString(true); dialog.setTitle(JavaUIMessages.OpenTypeAction_dialogTitle); //$NON-NLS-1$ dialog.setMessage(JavaUIMessages.OpenTypeAction_dialogMessage); //$NON-NLS-1$//$NON-NLS-1$ int result = dialog.open(); if (result != IDialogConstants.OK_ID) return; Object[] types = dialog.getResult(); if (types != null && types.length > 0) { IType type = (IType) types[0]; type.getPackageFragment().getElementName(); classValue.setText(type.getPackageFragment().getElementName() + "." + type.getElementName()); } } public void run(IAction action) { run(); } public void run(Text classValue) { this.classValue = classValue; run(); } }
对于Floder:
源码如下:帮助刚学习的同学一起进步 IJavaProject currProject = ActionUtil.findSelectedJavaProject(this.selection); ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), currProject.getProject().getWorkspace().getRoot(), false,"Select Mapping Folder"); if (dialog.open() == ContainerSelectionDialog.OK) { Object[] result = dialog.getResult(); if (result.length == 1) {javascript:; mappingPath.setText(((Path) result[0]).toString()); } } 其中 public static IJavaProject findSelectedJavaProject(ISelection selection) { IJavaProject currentProject = null; if (selection != null) { if (selection instanceof IStructuredSelection) { IStructuredSelection ss = (IStructuredSelection)selection; Object obj = ss.getFirstElement(); if (obj instanceof IJavaProject) { currentProject = (IJavaProject)obj; } } } return currentProject; } 例子: ContainerSelectionDialog= new ContainerSelectionDialog(...); if(dialog.open == ContainerSelectionDialog.OK){ Object[] result = dialog.getResult(); String containerFullName = ((Path)reslut[0]).toString(); createFile(); } public createFile(){ .... }
TypeSelectionDialog TypeSelectionDialog是JDT内核中的一个UI显示部件,是不鼓励使用的,不过我觉得很好用,拿来与大家分享,呵呵:) TypeSelectionDialog提供一个在给定的搜索域内搜索Java Type的选择对话框。比如我们在新建Class时选择Super Class时那样。选择后得到了一个或多个Eclipse平台提供的针对Java Type的对象:IType。 所以在使用时,需要以下几步工作: 1、利用SearchEngine创建搜索域 2、指定搜索内容为Class还是Interface还是其它的什么 3、创建Dialog: try{ TypeSelectionDialog2 dialog = new TypeSelectionDialog2(...); dialog.setFilter(...); dialog.setTitle(...); dialog.setMessage(...); if (dialog.open() == IDialogConstants.CANCEL_ID) return; Object[] types= dialog.getResult(); if (types == null || types.length == 0) return; IType type = (IType)types[0]; IResource res = type.getResource(); }catch(Exception e){ ... }
ElementTreeSelectionDialog ElementTreeSelectionDialog不属于JDT内核UI,但是我们构造了合适的validator和filter之后,ElementTreeSelectionDialog就可以提供出一个选择Java源文件容器的对话框,在Eclipse JDT 的概念中,Java程序中的每一个Package叫做IPackageFragment,那么这个Java源文件容器叫做IPackageFragmentRoot。 示例代码: ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(...); dialog.setValidator(validator);//校验所选中的是否是合法的 dialog.setSorter(new JavaElementSorter()); dialog.setTitle("Select Java Source Container"); dialog.setMessage("Select one of java source containers from workspace."); dialog.addFilter(filter);//对于选择操作的过滤 dialog.setInput(IJavaModel)//将工作区的Java模型传入 dialog.setInitialSelection(fWorkspaceRoot.getProject());//初始选择 ... ...
ElementListSelectionDialog ElementListSelectionDialog也不是JDT核心UI,但是我们通过构造这个Dialog中的元素列表,让其提供出一个Java 程序中Package选择列表对话框。 ElementListSelectionDialog dialog= new ElementListSelectionDialog(...)); dialog.setIgnoreCase(false); dialog.setTitle("Select Packages From Java Project "); dialog.setMessage("Select packages from java project which you pre-selected."); dialog.setEmptyListMessage("There is no package in selected project."); dialog.setElements(packages);//列表中的元素