package edu.ncsu.muclipse;
import java.util.ArrayList;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
/**
* The FileTreeContentProvider creates the information to be
* placed in a tree that contains the local file system
* underneath a given parent.
* @author Dright Ho
*/
public class FileTreeContentProvider implements ITreeContentProvider {
private boolean folderOnly;
/**
* A specialized constructor, giving the option of only
* allowing folders to be viewed in this selector.
* @param folderOnly If true, only files will be shown.
*/
public FileTreeContentProvider(boolean folderOnly) {
this.folderOnly = folderOnly;
}
/**
* Returns the children of a given element, if the element
* is a container (project or folder). Otherwise, returns
* the empty set.
* @param parentElement The element queried.
* @return An object array of children.
*/
public Object[] getChildren(Object parentElement) {
if(parentElement instanceof IFile) {
return new Object[]{};
} else if(parentElement instanceof IContainer) {
ArrayList list = new ArrayList();
IContainer folder = (IContainer)parentElement;
try{
IResource[] resources = folder.members(
IContainer.INCLUDE_TEAM_PRIVATE_MEMBERS);
for (int i = 0; i < resources.length; i++) {
IResource r = resources[i];
if(r instanceof IFolder || !folderOnly) {
list.add(r);
}
}
return list.toArray(new IResource[list.size()]);
} catch(CoreException e) {
}
}
return new Object[]{};
}
/**
* Nothing special here, just mapping the methods
* of each element to the content provider.
*
*/
public Object getParent(Object element) {
IResource rc = (IResource)element;
return rc.getParent();
}
/**
* Nothing special here, just mapping the methods
* of each element to the content provider.
*
*/
public boolean hasChildren(Object element) {
Object[] children = getChildren(element);
return children.length > 0;
}
/**
* Nothing special here, just mapping the methods
* of each element to the content provider.
*
*/
public Object[] getElements(Object inputElement) {
return getChildren(inputElement);
}
/**
* This method is here to satisfy the interface
* requirements.
*/
public void dispose() {
}
/**
* This method is here to satisfy the interface
* requirements.
*/
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}