Eclipse UI和JDT UI中几个重要的对话框
IContainer 中相对路径的选择文件对话框:
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
/**
* Provides content for a tree viewer that shows only containers.
*/
public class FileContentProvider implements ITreeContentProvider {
private boolean showClosedProjects = true;
/**
* Creates a new ContainerContentProvider.
*/
public FileContentProvider() {
}
/**
* The visual part that is using this content provider is about
* to be disposed. Deallocate all allocated SWT resources.
*/
public void dispose() {
}
/*
* @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
*/
public Object[] getChildren(Object element) {
if (element instanceof IWorkspace) {
// check if closed projects should be shown
IProject[] allProjects = ((IWorkspace) element).getRoot()
.getProjects();
if (showClosedProjects) {
return allProjects;
}
ArrayList accessibleProjects = new ArrayList();
for (int i = 0; i < allProjects.length; i++) {
if (allProjects.isOpen()) {
accessibleProjects.add(allProjects);
}
}
return accessibleProjects.toArray();
} else if (element instanceof IContainer) {
IContainer container = (IContainer) element;
if (container.isAccessible()) {
try {
List children = new ArrayList();
IResource[] members = container.members();
for (int i = 0; i < members.length; i++) {
if (members.getType() == IResource.FILE) {
IResource res = members;
if(isValidateFile(res.getName())){
children.add(members);
}
}else{
children.add(members);
}
}
return children.toArray();
} catch (CoreException e) {
// this should never happen because we call #isAccessible before invoking #members
}
}
}
return new Object[0];
}
public boolean isValidateFile(String fileName){
return true;
}
/*
* @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
*/
public Object[] getElements(Object element) {
return getChildren(element);
}
/*
* @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
*/
public Object getParent(Object element) {
if (element instanceof IResource) {
return ((IResource) element).getParent();
}
return null;
}
/*
* @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
*/
public boolean hasChildren(Object element) {
return getChildren(element).length > 0;
}
/*
* @see org.eclipse.jface.viewers.IContentProvider#inputChanged
*/
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
/**
* Specify whether or not to show closed projects in the tree
* viewer. Default is to show closed projects.
*
* @param show boolean if false, do not show closed projects in the tree
*/
public void showClosedProjects(boolean show) {
showClosedProjects = show;
}
}
//上边那个是改ContainerFolderProvider
IProject project = WorkbenchPlugin.getCurrentProject();
ILabelProvider labelProvider = WorkbenchLabelProvider
.getDecoratingWorkbenchLabelProvider();
FileContentProvider contentProvider = new FileContentProvider(){
public boolean isValidateFile(String fileName){
if(fileName.endsWith(".java")){
return true;
}
return false;
}
};
Shell shell = WorkbenchPlugin.getShell();
ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(shell,labelProvider,contentProvider){
protected void okPressed() {
Object result = this.getFirstResult();
if(result instanceof IFile){
super.okPressed();
}
}
};
Folder/Package/class路径选择
- 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.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();
- }
- }
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) {<A>javascript:;</A>
- 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(){
- ....
- }
源码如下:帮助刚学习的同学一起进步 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){
- ...
- }
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());//初始选择
- ... ...
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);//列表中的元素
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);//列表中的元素