Java by API/org.eclipse.jface.viewers/TreeViewer
TreeViewer: getTree()
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProviderChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass extends ApplicationWindow {
public MainClass() {
super(null);
setBlockOnOpen(true);
open();
Display.getCurrent().dispose();
}
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText("File Tree");
shell.setSize(400, 400);
}
protected Control createContents(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
Button preserveCase = new Button(composite, SWT.CHECK);
preserveCase.setText("&Preserve case");
final TreeViewer tv = new TreeViewer(composite);
tv.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
tv.setContentProvider(new FileTreeContentProvider());
tv.setLabelProvider(new FileTreeLabelProvider());
tv.setInput("root");
preserveCase.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
boolean preserveCase = ((Button) event.widget).getSelection();
FileTreeLabelProvider ftlp = (FileTreeLabelProvider) tv.getLabelProvider();
ftlp.setPreserveCase(preserveCase);
}
});
return composite;
}
public static void main(String[] args) {
new MainClass();
}
}
class FileTreeLabelProvider implements ILabelProvider {
private List listeners = new ArrayList();
private Image file;
private Image dir;
boolean preserveCase;
public FileTreeLabelProvider() {
try {
file = new Image(null, new FileInputStream("file.gif"));
dir = new Image(null, new FileInputStream("directory.gif"));
} catch (FileNotFoundException e) {
}
}
public void setPreserveCase(boolean preserveCase) {
this.preserveCase = preserveCase;
LabelProviderChangedEvent event = new LabelProviderChangedEvent(this);
for (int i = 0, n = listeners.size(); i < n; i++) {
ILabelProviderListener ilpl = (ILabelProviderListener) listeners.get(i);
ilpl.labelProviderChanged(event);
}
}
public Image getImage(Object arg0) {
return ((File) arg0).isDirectory() ? dir : file;
}
public String getText(Object arg0) {
String text = ((File) arg0).getName();
if (text.length() == 0) {
text = ((File) arg0).getPath();
}
return preserveCase ? text : text.toUpperCase();
}
public void addListener(ILabelProviderListener arg0) {
listeners.add(arg0);
}
public void dispose() {
if (dir != null)
dir.dispose();
if (file != null)
file.dispose();
}
public boolean isLabelProperty(Object arg0, String arg1) {
return false;
}
public void removeListener(ILabelProviderListener arg0) {
listeners.remove(arg0);
}
}
class FileTreeContentProvider implements ITreeContentProvider {
public Object[] getChildren(Object arg0) {
return ((File) arg0).listFiles();
}
public Object getParent(Object arg0) {
return ((File) arg0).getParentFile();
}
public boolean hasChildren(Object arg0) {
Object[] obj = getChildren(arg0);
return obj == null ? false : obj.length > 0;
}
public Object[] getElements(Object arg0) {
return File.listRoots();
}
public void dispose() {
}
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
}
}
TreeViewer: setContentProvider(IContentProvider arg0)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProviderChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass extends ApplicationWindow {
public MainClass() {
super(null);
setBlockOnOpen(true);
open();
Display.getCurrent().dispose();
}
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText("File Tree");
shell.setSize(400, 400);
}
protected Control createContents(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
Button preserveCase = new Button(composite, SWT.CHECK);
preserveCase.setText("&Preserve case");
final TreeViewer tv = new TreeViewer(composite);
tv.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
tv.setContentProvider(new FileTreeContentProvider());
tv.setLabelProvider(new FileTreeLabelProvider());
tv.setInput("root");
preserveCase.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
boolean preserveCase = ((Button) event.widget).getSelection();
FileTreeLabelProvider ftlp = (FileTreeLabelProvider) tv.getLabelProvider();
ftlp.setPreserveCase(preserveCase);
}
});
return composite;
}
public static void main(String[] args) {
new MainClass();
}
}
class FileTreeLabelProvider implements ILabelProvider {
private List listeners = new ArrayList();
private Image file;
private Image dir;
boolean preserveCase;
public FileTreeLabelProvider() {
try {
file = new Image(null, new FileInputStream("file.gif"));
dir = new Image(null, new FileInputStream("directory.gif"));
} catch (FileNotFoundException e) {
}
}
public void setPreserveCase(boolean preserveCase) {
this.preserveCase = preserveCase;
LabelProviderChangedEvent event = new LabelProviderChangedEvent(this);
for (int i = 0, n = listeners.size(); i < n; i++) {
ILabelProviderListener ilpl = (ILabelProviderListener) listeners.get(i);
ilpl.labelProviderChanged(event);
}
}
public Image getImage(Object arg0) {
return ((File) arg0).isDirectory() ? dir : file;
}
public String getText(Object arg0) {
String text = ((File) arg0).getName();
if (text.length() == 0) {
text = ((File) arg0).getPath();
}
return preserveCase ? text : text.toUpperCase();
}
public void addListener(ILabelProviderListener arg0) {
listeners.add(arg0);
}
public void dispose() {
if (dir != null)
dir.dispose();
if (file != null)
file.dispose();
}
public boolean isLabelProperty(Object arg0, String arg1) {
return false;
}
public void removeListener(ILabelProviderListener arg0) {
listeners.remove(arg0);
}
}
class FileTreeContentProvider implements ITreeContentProvider {
public Object[] getChildren(Object arg0) {
return ((File) arg0).listFiles();
}
public Object getParent(Object arg0) {
return ((File) arg0).getParentFile();
}
public boolean hasChildren(Object arg0) {
Object[] obj = getChildren(arg0);
return obj == null ? false : obj.length > 0;
}
public Object[] getElements(Object arg0) {
return File.listRoots();
}
public void dispose() {
}
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
}
}