上边介绍了,tableviewer,这里介绍一下treeViewer,总的流程类似。
一、构造一个树形结构的数据出来,这里选择国家、城市、人来构造一个树形结构,代码如下:
1,定义一个接口
package
model;
import java.util.List;
public interface Itree {
public String getName();
public void setName(String name);
public void setChildren(List Children);
public List getChildren();
}
import java.util.List;
public interface Itree {
public String getName();
public void setName(String name);
public void setChildren(List Children);
public List getChildren();
}
2,国家
package
model;
import java.util.ArrayList;
import java.util.List;
public class Country implements Itree {
private Long id;
private String name;
private List children = new ArrayList();
public Country(){
}
public Country(String name){
this.name = name;
}
public List getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.ArrayList;
import java.util.List;
public class Country implements Itree {
private Long id;
private String name;
private List children = new ArrayList();
public Country(){
}
public Country(String name){
this.name = name;
}
public List getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3,城市
package
model;
import java.util.ArrayList;
import java.util.List;
public class City implements Itree {
private Long id;
private String name;
private List children = new ArrayList();
public City(){
}
public City(String name){
this.name = name;
}
public List getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.ArrayList;
import java.util.List;
public class City implements Itree {
private Long id;
private String name;
private List children = new ArrayList();
public City(){
}
public City(String name){
this.name = name;
}
public List getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4,人
package
model;
import java.util.ArrayList;
import java.util.List;
public class People implements Itree {
private Long id;
private String name;
public People(){
}
public People(String name){
this.name = name;
}
public List getChildren() {
return null;
}
public void setChildren(List children) {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.ArrayList;
import java.util.List;
public class People implements Itree {
private Long id;
private String name;
public People(){
}
public People(String name){
this.name = name;
}
public List getChildren() {
return null;
}
public void setChildren(List children) {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
5,把这些元素组织起来
package
model;
import java.util.ArrayList;
import java.util.List;
public class Factory {
@SuppressWarnings("unchecked")
public static List createTree(){
//生成国家
Country cn = new Country("中国");
Country us = new Country("美国");
Country jp = new Country("日本");
//生成城市
City beijing = new City("北京");
City shanghai = new City("上海");
City newyork = new City("纽约");
City la = new City("洛杉矶");
City tokyo = new City("东京");
City osaka = new City("大阪");
//北京人
ArrayList list = new ArrayList();
list.add(new People("张三"));
list.add(new People("李四"));
list.add(new People("王五"));
beijing.setChildren(list);
//上海人
list = new ArrayList();
list.add(new People("翠花"));
list.add(new People("小红"));
list.add(new People("小崔"));
shanghai.setChildren(list);
//纽约人
list = new ArrayList();
list.add(new People("tom"));
list.add(new People("rose"));
list.add(new People("john"));
newyork.setChildren(list);
//洛杉矶人
list = new ArrayList();
list.add(new People("Sofia"));
list.add(new People("sarah"));
list.add(new People("Jennifer"));
la.setChildren(list);
//东京人
list = new ArrayList();
list.add(new People("渡边"));
list.add(new People("鬼冢"));
list.add(new People("山本"));
tokyo.setChildren(list);
//大阪人
list = new ArrayList();
list.add(new People("奈奈子"));
list.add(new People("菜菜子"));
list.add(new People("新垣结衣"));
osaka.setChildren(list);
//关联城市与国家
//中国
ArrayList citys = new ArrayList();
citys.add(beijing);
citys.add(shanghai);
cn.setChildren(citys);
//美国
citys = new ArrayList();
citys.add(newyork);
citys.add(la);
us.setChildren(citys);
//日本
citys = new ArrayList();
citys.add(tokyo);
citys.add(osaka);
jp.setChildren(citys);
//国家列表
ArrayList countrys = new ArrayList();
countrys.add(cn);
countrys.add(us);
countrys.add(jp);
return countrys;
}
}
import java.util.ArrayList;
import java.util.List;
public class Factory {
@SuppressWarnings("unchecked")
public static List createTree(){
//生成国家
Country cn = new Country("中国");
Country us = new Country("美国");
Country jp = new Country("日本");
//生成城市
City beijing = new City("北京");
City shanghai = new City("上海");
City newyork = new City("纽约");
City la = new City("洛杉矶");
City tokyo = new City("东京");
City osaka = new City("大阪");
//北京人
ArrayList list = new ArrayList();
list.add(new People("张三"));
list.add(new People("李四"));
list.add(new People("王五"));
beijing.setChildren(list);
//上海人
list = new ArrayList();
list.add(new People("翠花"));
list.add(new People("小红"));
list.add(new People("小崔"));
shanghai.setChildren(list);
//纽约人
list = new ArrayList();
list.add(new People("tom"));
list.add(new People("rose"));
list.add(new People("john"));
newyork.setChildren(list);
//洛杉矶人
list = new ArrayList();
list.add(new People("Sofia"));
list.add(new People("sarah"));
list.add(new People("Jennifer"));
la.setChildren(list);
//东京人
list = new ArrayList();
list.add(new People("渡边"));
list.add(new People("鬼冢"));
list.add(new People("山本"));
tokyo.setChildren(list);
//大阪人
list = new ArrayList();
list.add(new People("奈奈子"));
list.add(new People("菜菜子"));
list.add(new People("新垣结衣"));
osaka.setChildren(list);
//关联城市与国家
//中国
ArrayList citys = new ArrayList();
citys.add(beijing);
citys.add(shanghai);
cn.setChildren(citys);
//美国
citys = new ArrayList();
citys.add(newyork);
citys.add(la);
us.setChildren(citys);
//日本
citys = new ArrayList();
citys.add(tokyo);
citys.add(osaka);
jp.setChildren(citys);
//国家列表
ArrayList countrys = new ArrayList();
countrys.add(cn);
countrys.add(us);
countrys.add(jp);
return countrys;
}
}
二、定义内容器和标签器
1,内容器
import
java.util.List;
import model.Itree;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
public class TreeContentProvider implements IStructuredContentProvider, ITreeContentProvider {
public Object[] getElements(Object inputElement) {
if (inputElement instanceof List){
List input = (List)inputElement;
return input.toArray();
}
return new Object[0];
}
public Object[] getChildren(Object parentElement) {
Itree node = (Itree)parentElement;
List list = node.getChildren();
if(list == null){
return new Object[0];
}
return list.toArray();
}
public boolean hasChildren(Object element) {
Itree node = (Itree)element;
List list = node.getChildren();
return !(list == null || list.isEmpty());
}
//以下三个函数根据需要填充
public Object getParent(Object element) {
return null;
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
public void dispose() {
}
}
2,标签器:
import model.Itree;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
public class TreeContentProvider implements IStructuredContentProvider, ITreeContentProvider {
public Object[] getElements(Object inputElement) {
if (inputElement instanceof List){
List input = (List)inputElement;
return input.toArray();
}
return new Object[0];
}
public Object[] getChildren(Object parentElement) {
Itree node = (Itree)parentElement;
List list = node.getChildren();
if(list == null){
return new Object[0];
}
return list.toArray();
}
public boolean hasChildren(Object element) {
Itree node = (Itree)element;
List list = node.getChildren();
return !(list == null || list.isEmpty());
}
//以下三个函数根据需要填充
public Object getParent(Object element) {
return null;
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
public void dispose() {
}
}
import
model.Itree;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
public class TreeLabelProvider extends LabelProvider implements ILabelProvider {
public String getText(Object element) {
Itree node = (Itree)element;
return node.getName();
}
public Image getImage(Object element) {
return null;
}
}
三、好了,准备工作做好了,把上边的内容利用起来就好了。
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
public class TreeLabelProvider extends LabelProvider implements ILabelProvider {
public String getText(Object element) {
Itree node = (Itree)element;
return node.getName();
}
public Image getImage(Object element) {
return null;
}
}
import
model.Factory;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
public class TestTreeViewer {
private static Tree tree;
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
//
final TreeViewer treeViewer = new TreeViewer(shell, SWT.BORDER|SWT.H_SCROLL);
tree = treeViewer.getTree();
tree.setBounds(83, 75, 264, 185);
treeViewer.setLabelProvider(new TreeLabelProvider());
treeViewer.setContentProvider(new TreeContentProvider());
treeViewer.setInput(Factory.createTree());
shell.open();
shell.setLayout(new FillLayout());
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
public class TestTreeViewer {
private static Tree tree;
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
//
final TreeViewer treeViewer = new TreeViewer(shell, SWT.BORDER|SWT.H_SCROLL);
tree = treeViewer.getTree();
tree.setBounds(83, 75, 264, 185);
treeViewer.setLabelProvider(new TreeLabelProvider());
treeViewer.setContentProvider(new TreeContentProvider());
treeViewer.setInput(Factory.createTree());
shell.open();
shell.setLayout(new FillLayout());
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
看看是不是已经实现了treeViewer
四、上边是简单的treeViewer,如果我们需要带checkbox的treeViewer,简单,只需要更改
final
TreeViewer treeViewer
=
new
TreeViewer(shell, SWT.BORDER
|
SWT.H_SCROLL);
把TreeViewer换成CheckboxTreeViewer。这就是mvc的好处了。
但现实中我们多用ContainerCheckedTreeViewer代替CheckboxTreeViewer,因为这个提供了更多的功能。
CheckboxTreeViewer是TreeViewer的子类, ContainerCheckedTreeViewer是CheckboxTreeViewer的子类,所以可以随便替换。
替换后:
final
ContainerCheckedTreeViewer treeViewer
=
new
ContainerCheckedTreeViewer(shell, SWT.BORDER
|
SWT.H_SCROLL);
五、treeviewer和tableviewer都介绍过了,考虑一下把两个结合起来是不是我们经常需要的情况。尝试一下吧。
六、souce下载: http://www.blogjava.net/Files/dreamstone/jface-6.rar