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();
}
}
};
- 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) {<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(){
- ....
- }
- TypeSelectionDialog
- TypeSelectionDialog是JDT內(nèi)核中的一個UI顯示部件,是不鼓勵使用的,不過我覺得很好用,拿來與大家分享,呵呵:)
- TypeSelectionDialog提供一個在給定的搜索域內(nèi)搜索Java Type的選擇對話框。比如我們在新建Class時選擇Super Class時那樣。選擇后得到了一個或多個Eclipse平臺提供的針對Java Type的對象:IType。
- 所以在使用時,需要以下幾步工作:
- 1、利用SearchEngine創(chuàng)建搜索域
- 2、指定搜索內(nèi)容為Class還是Interface還是其它的什么
- 3、創(chuàng)建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內(nèi)核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)//將工作區(qū)的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);//列表中的元素