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();
}
}
};