Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » TreeViewer Plugin
TreeViewer Plugin [message #151775] Thu, 23 September 2004 04:44 Go to next message
Eclipse UserFriend
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) {}
};
}
}
Re: TreeViewer Plugin [message #151880 is a reply to message #151775] Thu, 23 September 2004 22:14 Go to previous message
Pratik Shah is currently offline Pratik ShahFriend
Messages: 1077
Registered: July 2009
Senior Member
The logic and shapes examples use GEF's TreeViewer for the outline view
(LogicEditor.OutlinePage and ShapesEditor.ShapesEditorOutlinePage,
respectively). Using the JFace TreeViewer instead should be fairly simple.


"Swami " <svenkat@asu.edu> wrote in message news:citkc5$8nr$1@eclipse.org...
> 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) {}
> };
> }
> }
>
>
Previous Topic:changing background color of palette
Next Topic:How to extend RulerProvider
Goto Forum:
  


Current Time: Thu Dec 26 11:08:15 GMT 2024

Powered by FUDForum. Page generated in 0.03661 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top