扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
上面的例子很简单,主要让大家明白几个关键Provider类的方法的使用。下面一个例子更接近于实际项目一些。这个例子有三个实体类:国家、城市、人,它们都实现了TreeEntry接口,这三个实体是有层级关系的。另外还写了一个工厂类来生成TreeViewer.setInput所需要的参数。
这个例子比较长,为了大家阅读方便,在此说明一下:实体类可以看做JAVA类对数据库表的一个一一映射,类的属性就相当于表的一个字段。按照 JAVA的通常规范写法实体类中每一个属性都附加了set/get方法,所以代码看起来很长,实际的内容却没多少,大家可以看看实体的UML图就应该对三个实体类的关系很清楚了。
/*
?* @author 陈刚 ,2004-8-21 18:25:18
?* Email: glchengang@yeah.net
?* Blog : glchengang.yeah.net
?*/
package book.c3.e2;
/**
?* 树的Entry的接口
?*/
public interface TreeEntry {
??? public String getName();
??? public void setName(String name);
}
?
/*
?* @author 陈刚 ,2004-8-21 15:32:15
?* Email: glchengang@yeah.net
?* Blog : glchengang.yeah.net
?*/
package book.c3.e2;
import java.util.List;
/**
?* 国家实体类
?*/
public class Country implements TreeEntry{
??? private String name; //国家名
??? private List cities; //此国家所包含的的城市的集合,集合元素为City对象
??? public Country() {}
??? public Country(String name) {
??????? this.name = name;
??? }
??? public String getName() {
??????? return name;
??? }
??? public void setName(String name) {
??????? this.name = name;
??? }
??? public List getCities() {
??????? return cities;
??? }
??? public void setCities(List cities) {
??????? this.cities = cities;
??? }
}
?
/*
?* @author 陈刚 ,2004-8-21 15:33:17
?* Email: glchengang@yeah.net
?* Blog : glchengang.yeah.net
?*/
package book.c3.e2;
import java.util.List;
/**
?* 城市的实体类
?*/
public class City implements TreeEntry{
??? private String name;//城市名
??? private List peoples;
??? public City() {}
??? public City(String name) {
??????? this.name = name;
??? }
??? public String getName() {
??????? return name;
??? }
??? public void setName(String name) {
??????? this.name = name;
??? }
??? public List getPeoples() {
??????? return peoples;
??? }
??? public void setPeoples(List peoples) {
??????? this.peoples = peoples;
??? }
}
?
/*
?* @author 陈刚 ,2004-8-21 18:15:53
?* Email: glchengang@yeah.net
?* Blog : glchengang.yeah.net
?*/
package book.c3.e2;
/**
?* 人的实体类
?*/
public class People implements TreeEntry{
??? private String name;
??? public People() {}
??? public People(String name) {
??????? this.name = name;
??? }
??? public String getName() {
??????? return name;
??? }
??? public void setName(String name) {
??????? this.name = name;
??? }
}
?
/*
?* @author 陈刚 ,2004-8-21 17:52:27
?* Email: glchengang@yeah.net
?* Blog : glchengang.yeah.net
?*/
package book.c3.e2;
import java.util.ArrayList;
import java.util.List;
/**
?* 此类负责生成TreeViewer的方法setInput所需要的参数
?*/
public class TvInputFactory {
??? public Object build() {
??????? /*
???????? * 生成顶级的三个对象
???????? */
??????? Country[] countryArray = new Country[3];
??????? countryArray[0] = new Country("中国");
??????? countryArray[1] = new Country("美国");
??????? countryArray[2] = new Country("英国");
??????? /*
???????? * 生成中国的三个城市
???????? */
??????? List chinaList = new ArrayList();
??????? chinaList.add(new City("北京"));
??????? City glCity = new City("桂林");
??????? chinaList.add(glCity);
??????? City nnCity = new City("南宁");
??????? chinaList.add(nnCity);
??????? countryArray[0].setCities(chinaList);
??????? /*
???????? * 生成美国的两个城市
???????? */
??????? List usaList = new ArrayList();
??????? usaList.add(new City("纽约"));
??????? usaList.add(new City("芝加哥"));
??????? countryArray[1].setCities(usaList);
??????? /*
???????? * 生成城市里的人
???????? */
??????? {
??????????? List list = new ArrayList();
??????????? list.add(new People("陈刚"));
??????????? list.add(new People("陈知行"));
??????????? list.add(new People("韩立新"));
??????????? glCity.setPeoples(list);
??????? }
??????? {
??????????? List list = new ArrayList();
??????????? list.add(new People("桃子"));
??????????? list.add(new People("林雅仕"));
??????????? list.add(new People("陈常恩"));
??????????? nnCity.setPeoples(list);
??????? }
??????? return countryArray;
??? }
}
?
?
/*
?* @author 陈刚 ,2004-8-21 0:54:20
?* Email: glchengang@yeah.net
?* Blog : glchengang.yeah.net
?*/
package book.c3.e2;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import java.util.List;
public class S3_1 {
??? public static void main(String[] args) {
??????? S3_1 window = new S3_1();
??????? window.open();
??? }
??? public void open() {
??????? final Display display = new Display();
??????? final Shell shell = new Shell();
??????? shell.setLayout(new FillLayout());
??????? ui(shell);
??????? shell.open();
??????? while (!shell.isDisposed()) {
??????????? if (!display.readAndDispatch())
??????????????? display.sleep();
??????? }
??? }
??? private void ui(Shell shell) {
??????? Composite c = new Composite(shell, SWT.NONE);
??????? c.setLayout(new FillLayout());
??????? TreeViewer tv = new TreeViewer(c, SWT.BORDER);
??????? tv.setContentProvider(new MyContentProvider());
??????? tv.setLabelProvider(new MyLableProvider());
??????? tv.setInput(new TvInputFactory().build());//设置输入对象的方法与前面相同
??? }
??? /**
???? * 内容提供器。由它决定那些对象应该输出在TreeViewer里显示
???? */
??? private static final class MyContentProvider implements ITreeContentProvider {
??????? /**
???????? * 由这个方法决定树的顶级显示那些对象。在此方法里生成了三个Country对象
???????? * @param inputElement? 用tv.setInput()方法输入的那个对象,在这里没有使用这个对象
???????? */
??????? public Object[] getElements(Object inputElement) {
??????????? if (inputElement instanceof Country[])
??????????????? return (Country[]) inputElement;
??????????? return new Object[0]; //生成一个空数组
??????? }
??????? /**
???????? * 由这个方法决定结点应该显示那些子结点。在这里也不管父结点是什么,每个结点都统一有三个字结点
???????? * @param parentElement 被点击的结点(父结点)
???????? */
??????? public Object[] getChildren(Object parentElement) {
??????????? if (parentElement instanceof Country)
??????????????? return ((Country) parentElement).getCities().toArray();
??????????? else if (parentElement instanceof City)
??????????????? return ((City) parentElement).getPeoples().toArray();
??????????? return new Object[0];
??????? }
??????? /**
???????? * 判断某结点是否有子结点。在这里不管3721,全返回真,即都有子结点。这时结点前都有一个“+”号图标
???????? * @param element 需要判断是否有子的结点
???????? */
??????? public boolean hasChildren(Object element) {
??????????? //People没有结点,Country和City才可能有子结点
??????????? if (element instanceof People) {
??????????????? return false;
??????????? } else if (element instanceof Country) {
??????????????? List list = ((Country) element).getCities();
??????????????? //其子的集合不为空
??????????????? if (list != null && !list.isEmpty())
??????????????????? return true;
??????????? } else if (element instanceof City) {
??????????????? List list = ((City) element).getPeoples();
??????????????? if (list != null && !list.isEmpty())
??????????????????? return true;
??????????? }
??????????? return false;
??????? }
??????? public Object getParent(Object element) {
??????????? return null;
??????? }
??????? public void dispose() {}
??????? public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
??? }
??? /**
???? * 标签提供器。
???? */
??? private static final class MyLableProvider extends LabelProvider {
//??????? /**
//???????? * 显示什么图片
//???????? * @param 结点
//???????? * @return 可以为null值
//???????? */
//??????? public Image getImage(Object element) {
//??????????? //??????????? ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
//??????????? //??????????? return sharedImages.getImage(ISharedImages.IMG_OBJS_WARN_TSK);
//??????????? return null;
//??????? }
??????? public String getText(Object element) {
??????????? if (element instanceof TreeEntry)
??????????????? return ((TreeEntry) element).getName();
??????????? return "";
??????? }
??? }
}
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。