Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Sample JFace TreeViewer snippet code
Sample JFace TreeViewer snippet code [message #438416] Wed, 23 June 2004 19:58 Go to next message
Srimanth  is currently offline Srimanth Friend
Messages: 225
Registered: July 2009
Senior Member
Thought it would be useful for someone..

============================================================ =

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: Sample JFace TreeViewer snippet code [message #438456 is a reply to message #438416] Thu, 24 June 2004 20:03 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: owen.thomas.salestech.co.nz

You don't have an example that would take an XML file as an input do you???

Cheers

Owen

Sri Gunturi wrote:
> Thought it would be useful for someone..
>
> ============================================================ =
>
> 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: Sample JFace TreeViewer snippet code [message #438497 is a reply to message #438456] Sat, 26 June 2004 07:24 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cburleso.us.ibm.com

Something you might want to try:

Take a look at the JAXB package. JAXB allows you to create a set of java
files from an XML schema. Then, you can marshall an unmarshall an XML file
into runtime objects and then unmarshall them back into an XML file. JAXB
is part of the Sun Java Web Services pack. It's pretty easy to use. Then,
you can simply set the top-level model object that the unmarshaller
creates to be your tree viewer's input.
Re: Sample JFace TreeViewer snippet code [message #438508 is a reply to message #438497] Sun, 27 June 2004 01:19 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: owen.thomas.salestech.co.nz

I know of JAXB one of my colleagues has been using as part of a couple
of his modules for another project.......However the plan for the system
I am developing is to have a treeviewer that retrieves data either
1. straight from a database.
2. via a web service
3. if off-line a locally stored xml file.

I'm not entirely certain that JAXB although it can do what I need will
be the right choice for this. The application will be deployed through a
Java web start platform so trying to keep the number of extra libraries
as minimal as possible.

Owen

cburleso@us.ibm.com wrote:
> Something you might want to try:
>
> Take a look at the JAXB package. JAXB allows you to create a set of java
> files from an XML schema. Then, you can marshall an unmarshall an XML file
> into runtime objects and then unmarshall them back into an XML file. JAXB
> is part of the Sun Java Web Services pack. It's pretty easy to use. Then,
> you can simply set the top-level model object that the unmarshaller
> creates to be your tree viewer's input.
>
>
Re: Sample JFace TreeViewer snippet code [message #438515 is a reply to message #438456] Sun, 27 June 2004 11:52 Go to previous messageGo to next message
Stefan Zeiger is currently offline Stefan ZeigerFriend
Messages: 102
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------040009090607090405090804
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Owen Thomas wrote:

> You don't have an example that would take an XML file as an input do you???

When I read this question a few days ago I thought this should be so
simple that there must be many examples. But since none have been posted
so far, I gave it a try myself. See the attached file for a simple XML
TreeViewer example that uses W3C DOM.

--
Stefan Zeiger http://www.szeiger.de http://www.novocode.com
My SWT controls: http://www.novocode.com/swt

--------------040009090607090405090804
Content-Type: text/plain;
name="XMLTreeDemo.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="XMLTreeDemo.java"

import java.io.*;
import java.util.ArrayList;

import javax.xml.parsers.*;
import org.w3c.dom.*;

import org.eclipse.jface.viewers.*;
import org.eclipse.jface.window.*;
import org.eclipse.swt.widgets.*;


public class XMLTreeDemo extends ApplicationWindow
{
private static final String DEFAULT_XML_FILE =
"../Novocode Application Framework/examples/com/novocode/naf/example/explorer/explore r.naf ";

private static Document doc;

public XMLTreeDemo() { super(null); }

protected Control createContents(Composite parent)
{
getShell().setSize(350, 550);
getShell().setText("JFace XML Tree");
TreeViewer tv = new TreeViewer(parent);

tv.setContentProvider(new ITreeContentProvider()
{
public Object[] getChildren(Object element)
{
ArrayList ch = new ArrayList();
NamedNodeMap atrs = ((Node)element).getAttributes();
if(atrs != null)
for(int i=0; i<atrs.getLength(); i++) ch.add(atrs.item(i));
NodeList nl = ((Node)element).getChildNodes();
for(int i=0; i<nl.getLength(); i++)
if(nl.item(i).getNodeType() == Node.ELEMENT_NODE) ch.add(nl.item(i));
return ch.toArray();
}
public Object getParent(Object element) { return ((Node)element).getParentNode(); }
public Object[] getElements(Object element) { return getChildren(element); }
public boolean hasChildren(Object element) { return getChildren(element).length > 0; }
public void dispose() {}
public void inputChanged(Viewer viewer, Object old_input, Object new_input) {}
});

tv.setLabelProvider(new LabelProvider()
{
public String getText(Object element)
{
if(element instanceof Attr) return "@" + ((Attr)element).getNodeName();
else return ((Node)element).getNodeName();
}
});

tv.setInput(doc);
return tv.getControl();
}

public static void main(String[] args) throws Exception
{
File in = new File(args.length > 0 ? args[0] : DEFAULT_XML_FILE);
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().pa rse(in);
XMLTreeDemo w = new XMLTreeDemo();
w.setBlockOnOpen(true);
w.open();
Display.getCurrent().dispose();
}
}
--------------040009090607090405090804--
Re: Sample JFace TreeViewer snippet code [message #438568 is a reply to message #438515] Mon, 28 June 2004 01:27 Go to previous message
Eclipse UserFriend
Originally posted by: owen.thomas.salestech.co.nz

You are the man.....Almost perfect for what I need.......But the rest I
can do myself.
I also thought there would be many examples like this but after a couple
of days of searching decided to try and see if anyone in the newsgroup
had done something similar.

Thanks again.

Owen

Stefan Zeiger wrote:
> Owen Thomas wrote:
>
>> You don't have an example that would take an XML file as an input do
>> you???
>
>
> When I read this question a few days ago I thought this should be so
> simple that there must be many examples. But since none have been posted
> so far, I gave it a try myself. See the attached file for a simple XML
> TreeViewer example that uses W3C DOM.
>
>
> ------------------------------------------------------------ ------------
>
> import java.io.*;
> import java.util.ArrayList;
>
> import javax.xml.parsers.*;
> import org.w3c.dom.*;
>
> import org.eclipse.jface.viewers.*;
> import org.eclipse.jface.window.*;
> import org.eclipse.swt.widgets.*;
>
>
> public class XMLTreeDemo extends ApplicationWindow
> {
> private static final String DEFAULT_XML_FILE =
> "../Novocode Application Framework/examples/com/novocode/naf/example/explorer/explore r.naf ";
>
> private static Document doc;
>
> public XMLTreeDemo() { super(null); }
>
> protected Control createContents(Composite parent)
> {
> getShell().setSize(350, 550);
> getShell().setText("JFace XML Tree");
> TreeViewer tv = new TreeViewer(parent);
>
> tv.setContentProvider(new ITreeContentProvider()
> {
> public Object[] getChildren(Object element)
> {
> ArrayList ch = new ArrayList();
> NamedNodeMap atrs = ((Node)element).getAttributes();
> if(atrs != null)
> for(int i=0; i<atrs.getLength(); i++) ch.add(atrs.item(i));
> NodeList nl = ((Node)element).getChildNodes();
> for(int i=0; i<nl.getLength(); i++)
> if(nl.item(i).getNodeType() == Node.ELEMENT_NODE) ch.add(nl.item(i));
> return ch.toArray();
> }
> public Object getParent(Object element) { return ((Node)element).getParentNode(); }
> public Object[] getElements(Object element) { return getChildren(element); }
> public boolean hasChildren(Object element) { return getChildren(element).length > 0; }
> public void dispose() {}
> public void inputChanged(Viewer viewer, Object old_input, Object new_input) {}
> });
>
> tv.setLabelProvider(new LabelProvider()
> {
> public String getText(Object element)
> {
> if(element instanceof Attr) return "@" + ((Attr)element).getNodeName();
> else return ((Node)element).getNodeName();
> }
> });
>
> tv.setInput(doc);
> return tv.getControl();
> }
>
> public static void main(String[] args) throws Exception
> {
> File in = new File(args.length > 0 ? args[0] : DEFAULT_XML_FILE);
> doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().pa rse(in);
> XMLTreeDemo w = new XMLTreeDemo();
> w.setBlockOnOpen(true);
> w.open();
> Display.getCurrent().dispose();
> }
> }
Previous Topic:plugins and the Broswer widget
Next Topic:get DOM Object from IE current Page
Goto Forum:
  


Current Time: Fri Apr 26 13:07:20 GMT 2024

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

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

Back to the top