/**
*
*/
package de.tutorials;
import java.io.File;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* @author Tom
*
*/
public class JFaceTreeExample extends ApplicationWindow {
public JFaceTreeExample(Shell parentShell) {
super(parentShell);
}
protected Control createContents(Composite parent) {
getShell().setText("JFaceTreeExample");
parent.setSize(640, 480);
TreeViewer treeViewer = new TreeViewer(parent);
treeViewer.setContentProvider(new FileContentProvider());
treeViewer.setLabelProvider(new FileNameLabelProvider());
treeViewer.setInput(new File("E:/springframework/2.0M1"));
treeViewer.addFilter(new ViewerFilter() {
public boolean select(Viewer viewer, Object parentElement,
Object element) {
File currentFile = (File) element;
return currentFile.isDirectory() ? directoryContainsJar(currentFile)
: isJarFile(currentFile);
}
private boolean directoryContainsJar(File currentDirectory) {
File[] files = currentDirectory.listFiles();
boolean containsJarFile = false;
for (int i = 0; files != null && i < files.length && !containsJarFile; i++) {
if (files[i].isDirectory()) {
containsJarFile = directoryContainsJar(files[i]);
} else {
containsJarFile = isJarFile(files[i]);
}
}
return containsJarFile;
}
private boolean isJarFile(File currentFile) {
return currentFile.getName().endsWith(".jar");
}
});
return super.createContents(parent);
}
/**
* @param args
*/
public static void main(String[] args) {
ApplicationWindow applicationWindow = new JFaceTreeExample(null);
applicationWindow.setBlockOnOpen(true);
applicationWindow.open();
Display.getCurrent().dispose();
}
public class FileContentProvider implements ITreeContentProvider {
public Object[] getChildren(Object parentElement) {
return ((File) parentElement).listFiles();
}
public Object getParent(Object element) {
return ((File) element).getParentFile();
}
public boolean hasChildren(Object element) {
String[] files = ((File) element).list();
return files != null && files.length > 0;
}
public Object[] getElements(Object inputElement) {
return getChildren(inputElement);
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
public class FileNameLabelProvider implements ILabelProvider {
public Image getImage(Object element) {
return null;
}
public String getText(Object element) {
return ((File) element).getName();
}
public void addListener(ILabelProviderListener listener) {
}
public void dispose() {
}
public boolean isLabelProperty(Object element, String property) {
return false;
}
public void removeListener(ILabelProviderListener listener) {
}
}
}