TreeViewer Plugin [message #151775] |
Thu, 23 September 2004 04:44 |
Eclipse User |
|
|
|
Originally posted by: svenkat.asu.edu
I found this snippet code for implementing a tree viewer in the archives.
Can any body tell me how to incorporate this into an exsiting plug-in
project, rather than a standalone application?
Thanks,
Swami.
---------------------------
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MyTreeViewerApp {
public static void main(String[] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setSize (200, 200);
createTreeViewer(shell);
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
private static void createTreeViewer(Shell shell) {
TreeViewer tv = new TreeViewer(shell);
tv.setContentProvider(getContentProvider());
List input = new ArrayList();
input.add("Hello1");
input.add("Hello2");
input.add("Hello3");
List input2 = new ArrayList();
input2.add("World1");
input2.add("World2");
input2.add("World3");
input.add(input2);
tv.setInput(input);
tv.setLabelProvider(getLabelProvider());
tv.getTree().setSize(200,200);
}
private static ILabelProvider getLabelProvider(){
return new ILabelProvider() {
public Image getImage(Object element) {return null;}
public String getText(Object element) {
if (element instanceof String) {
String str = (String) element;
return str;
}
if (element instanceof List) {
List list = (List) element;
return "List ("+list.size()+")";
}
return "?";
}
public void addListener(ILabelProviderListener listener) {}
public void dispose() {}
public boolean isLabelProperty(Object element, String property)
{return false; }
public void removeListener(ILabelProviderListener listener) {}
};
}
private static ITreeContentProvider getContentProvider(){
return new ITreeContentProvider(){
public Object[] getChildren(Object parentElement) {
if (parentElement instanceof List) {
List list = (List) parentElement;
return list.toArray();
}
return null;
}
public Object getParent(Object element) {return null;}
public boolean hasChildren(Object element) {
if (element instanceof List) {
List list = (List) element;
if(list.size()>0)
return true;
}
return false;
}
public Object[] getElements(Object inputElement) {
if (inputElement instanceof List) {
List list = (List) inputElement;
return list.toArray();
}
return null;
}
public void dispose() {}
public void inputChanged(Viewer viewer, Object oldInput, Object
newInput) {}
};
}
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.03661 seconds