/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.utils.ui;
import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
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.resources.IResourceVisitor;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
import de.innovationgate.eclipse.utils.Activator;
public class FileStructuredContentProvider implements
IStructuredContentProvider {
private String _filterExtension;
private Comparator<File> _comparator = new Comparator<File>() {
public int compare(File o1, File o2) {
String nameA = "";
String nameB = "";
if (o1 != null) {
nameA = o1.getName();
}
if (o2 != null) {
nameB = o2.getName();
}
return nameA.compareTo(nameB);
}
};
public FileStructuredContentProvider() {
}
public FileStructuredContentProvider(String filterExtension) {
_filterExtension = filterExtension;
}
public void setExtensionFilter(String filterExtension) {
_filterExtension = filterExtension;
}
public Object[] getElements(Object inputElement) {
final List<IFile> fileList = new ArrayList<IFile>();
if (inputElement instanceof File) {
File root = (File) inputElement;
IContainer rootContainer = ResourcesPlugin.getWorkspace().getRoot().getContainerForLocation(new Path(root.getAbsolutePath()));
try {
rootContainer.accept(new IResourceVisitor() {
public boolean visit(IResource resource) throws CoreException {
if(resource instanceof IFolder){
return true;
}
if(resource instanceof IFile){
IFile currentFile = (IFile)resource;
if(("."+currentFile.getFileExtension()).equals(_filterExtension)){
fileList.add(currentFile);
}
}
return false;
}
});
}
catch (CoreException e) {
Activator.getDefault().logError(e);
}
}
return fileList.toArray();
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}