Swt/Jface treeViewer的使用

 

上边介绍了,tableviewer,这里介绍一下treeViewer,总的流程类似。
一、构造一个树形结构的数据出来,这里选择国家、城市、人来构造一个树形结构,代码如下:
1,定义一个接口

None.gif package  model;
None.gif
None.gif
import  java.util.List;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   interface  Itree  dot.gif {
InBlock.gif    
public String getName();
InBlock.gif    
public void setName(String name);
InBlock.gif    
public void setChildren(List Children);
InBlock.gif    
public List getChildren();
ExpandedBlockEnd.gif}

None.gif

2,国家

None.gif package  model;
None.gif
None.gif
import  java.util.ArrayList;
None.gif
import  java.util.List;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  Country  implements  Itree dot.gif {
InBlock.gif    
private Long id;
InBlock.gif    
private String name;
InBlock.gif    
private List children = new ArrayList();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Country()dot.gif{
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Country(String name)dot.gif{
InBlock.gif        
this.name = name;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public List getChildren() dot.gif{
InBlock.gif        
return children;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setChildren(List children) dot.gif{
InBlock.gif        
this.children = children;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Long getId() dot.gif{
InBlock.gif        
return id;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setId(Long id) dot.gif{
InBlock.gif        
this.id = id;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String getName() dot.gif{
InBlock.gif        
return name;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setName(String name) dot.gif{
InBlock.gif        
this.name = name;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedBlockEnd.gif}

None.gif

3,城市
None.gif package  model;
None.gif
None.gif
import  java.util.ArrayList;
None.gif
import  java.util.List;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  City  implements  Itree dot.gif {
InBlock.gif    
private Long id;
InBlock.gif    
private String name;
InBlock.gif    
private List children = new ArrayList();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public City()dot.gif{
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public City(String name)dot.gif{
InBlock.gif        
this.name = name;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public List getChildren() dot.gif{
InBlock.gif        
return children;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setChildren(List children) dot.gif{
InBlock.gif        
this.children = children;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Long getId() dot.gif{
InBlock.gif        
return id;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setId(Long id) dot.gif{
InBlock.gif        
this.id = id;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String getName() dot.gif{
InBlock.gif        
return name;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setName(String name) dot.gif{
InBlock.gif        
this.name = name;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

4,人
None.gif package  model;
None.gif
None.gif
import  java.util.ArrayList;
None.gif
import  java.util.List;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  People  implements  Itree dot.gif {
InBlock.gif    
private Long id;
InBlock.gif    
private String name;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public People()dot.gif{
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public People(String name)dot.gif{
InBlock.gif        
this.name = name;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public List getChildren() dot.gif{
InBlock.gif        
return null;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setChildren(List children) dot.gif{
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Long getId() dot.gif{
InBlock.gif        
return id;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setId(Long id) dot.gif{
InBlock.gif        
this.id = id;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String getName() dot.gif{
InBlock.gif        
return name;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setName(String name) dot.gif{
InBlock.gif        
this.name = name;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

5,把这些元素组织起来
None.gif package  model;
None.gif
None.gif
import  java.util.ArrayList;
None.gif
import  java.util.List;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  Factory  dot.gif {
InBlock.gif    @SuppressWarnings(
"unchecked")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static List createTree()dot.gif{
InBlock.gif        
//生成国家
InBlock.gif
        Country cn = new Country("中国");
InBlock.gif        Country us 
= new Country("美国");
InBlock.gif        Country jp 
= new Country("日本");
InBlock.gif        
//生成城市
InBlock.gif
        City beijing  = new City("北京");
InBlock.gif        City shanghai 
= new City("上海");
InBlock.gif        City newyork 
= new City("纽约");
InBlock.gif        City la 
= new City("洛杉矶");
InBlock.gif        City tokyo 
= new City("东京");
InBlock.gif        City osaka 
= new City("大阪");
InBlock.gif        
//北京人
InBlock.gif
        ArrayList list = new ArrayList();
InBlock.gif        list.add(
new People("张三"));
InBlock.gif        list.add(
new People("李四"));
InBlock.gif        list.add(
new People("王五"));
InBlock.gif        beijing.setChildren(list);
InBlock.gif        
//上海人
InBlock.gif
        list = new ArrayList();
InBlock.gif        list.add(
new People("翠花"));
InBlock.gif        list.add(
new People("小红"));
InBlock.gif        list.add(
new People("小崔"));
InBlock.gif        shanghai.setChildren(list);
InBlock.gif        
//纽约人
InBlock.gif
        list = new ArrayList();
InBlock.gif        list.add(
new People("tom"));
InBlock.gif        list.add(
new People("rose"));
InBlock.gif        list.add(
new People("john"));
InBlock.gif        newyork.setChildren(list);
InBlock.gif        
//洛杉矶人
InBlock.gif
        list = new ArrayList();
InBlock.gif        list.add(
new People("Sofia"));
InBlock.gif        list.add(
new People("sarah"));
InBlock.gif        list.add(
new People("Jennifer"));
InBlock.gif        la.setChildren(list);
InBlock.gif        
//东京人
InBlock.gif
        list = new ArrayList();
InBlock.gif        list.add(
new People("渡边"));
InBlock.gif        list.add(
new People("鬼冢"));
InBlock.gif        list.add(
new People("山本"));
InBlock.gif        tokyo.setChildren(list);
InBlock.gif        
//大阪人
InBlock.gif
        list = new ArrayList();
InBlock.gif        list.add(
new People("奈奈子"));
InBlock.gif        list.add(
new People("菜菜子"));
InBlock.gif        list.add(
new People("新垣结衣"));
InBlock.gif        osaka.setChildren(list);
InBlock.gif        
InBlock.gif        
//关联城市与国家
InBlock.gif        
//中国
InBlock.gif
        ArrayList citys = new ArrayList();
InBlock.gif        citys.add(beijing);
InBlock.gif        citys.add(shanghai);
InBlock.gif        cn.setChildren(citys);
InBlock.gif        
//美国
InBlock.gif
        citys = new ArrayList();
InBlock.gif        citys.add(newyork);
InBlock.gif        citys.add(la);
InBlock.gif        us.setChildren(citys);
InBlock.gif        
//日本
InBlock.gif
        citys = new ArrayList();
InBlock.gif        citys.add(tokyo);
InBlock.gif        citys.add(osaka);
InBlock.gif        jp.setChildren(citys);
InBlock.gif        
//国家列表
InBlock.gif
        ArrayList countrys = new ArrayList();
InBlock.gif        countrys.add(cn);
InBlock.gif        countrys.add(us);
InBlock.gif        countrys.add(jp);
InBlock.gif        
return countrys;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

二、定义内容器和标签器
1,内容器
None.gif import  java.util.List;
None.gif
None.gif
import  model.Itree;
None.gif
None.gif
import  org.eclipse.jface.viewers.IStructuredContentProvider;
None.gif
import  org.eclipse.jface.viewers.ITreeContentProvider;
None.gif
import  org.eclipse.jface.viewers.Viewer;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  TreeContentProvider  implements  IStructuredContentProvider, ITreeContentProvider  dot.gif {
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public Object[] getElements(Object inputElement) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (inputElement instanceof List)dot.gif{
InBlock.gif                List input 
= (List)inputElement;
InBlock.gif                
return input.toArray();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return new Object[0];
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public Object[] getChildren(Object parentElement) dot.gif{
InBlock.gif            Itree node 
= (Itree)parentElement;
InBlock.gif            List list 
= node.getChildren();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if(list == null)dot.gif{
InBlock.gif                
return new Object[0];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return list.toArray();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public boolean hasChildren(Object element) dot.gif{
InBlock.gif            Itree node 
= (Itree)element;
InBlock.gif            List list 
= node.getChildren();
InBlock.gif            
return !(list == null || list.isEmpty());
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//以下三个函数根据需要填充
ExpandedSubBlockStart.gifContractedSubBlock.gif
        public Object getParent(Object element) dot.gif{
InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) dot.gif{
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public void dispose() dot.gif{
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
2,标签器:
None.gif import  model.Itree;
None.gif
None.gif
import  org.eclipse.jface.viewers.ILabelProvider;
None.gif
import  org.eclipse.jface.viewers.LabelProvider;
None.gif
import  org.eclipse.swt.graphics.Image;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  TreeLabelProvider  extends  LabelProvider  implements  ILabelProvider  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public String getText(Object element) dot.gif{
InBlock.gif            Itree node 
= (Itree)element;
InBlock.gif            
return node.getName();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public Image getImage(Object element) dot.gif{
InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
三、好了,准备工作做好了,把上边的内容利用起来就好了。
None.gif import  model.Factory;
None.gif
None.gif
import  org.eclipse.jface.viewers.TreeViewer;
None.gif
import  org.eclipse.swt.SWT;
None.gif
import  org.eclipse.swt.layout.FillLayout;
None.gif
import  org.eclipse.swt.widgets.Display;
None.gif
import  org.eclipse.swt.widgets.Shell;
None.gif
import  org.eclipse.swt.widgets.Tree;
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  TestTreeViewer  dot.gif {
InBlock.gif    
InBlock.gif    
private static Tree tree;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
InBlock.gif        
final Display display = Display.getDefault();
InBlock.gif        
final Shell shell = new Shell();
InBlock.gif        shell.setSize(
500375);
InBlock.gif        shell.setText(
"SWT Application");
InBlock.gif        
//
InBlock.gif
        final TreeViewer treeViewer = new TreeViewer(shell, SWT.BORDER|SWT.H_SCROLL);
InBlock.gif        tree 
= treeViewer.getTree();
InBlock.gif        tree.setBounds(
8375264185);
InBlock.gif        
InBlock.gif        treeViewer.setLabelProvider(
new TreeLabelProvider());
InBlock.gif        treeViewer.setContentProvider(
new TreeContentProvider());
InBlock.gif        treeViewer.setInput(Factory.createTree());
InBlock.gif        
InBlock.gif        shell.open();
InBlock.gif        shell.setLayout(
new FillLayout());
InBlock.gif        shell.layout();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while (!shell.isDisposed()) dot.gif{
InBlock.gif            
if (!display.readAndDispatch())
InBlock.gif                display.sleep();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

看看是不是已经实现了treeViewer
四、上边是简单的treeViewer,如果我们需要带checkbox的treeViewer,简单,只需要更改
None.gif final  TreeViewer treeViewer  =   new  TreeViewer(shell, SWT.BORDER | SWT.H_SCROLL);
None.gif
把TreeViewer换成CheckboxTreeViewer。这就是mvc的好处了。
但现实中我们多用ContainerCheckedTreeViewer代替CheckboxTreeViewer,因为这个提供了更多的功能。
CheckboxTreeViewer是TreeViewer的子类,  ContainerCheckedTreeViewer是CheckboxTreeViewer的子类,所以可以随便替换。
替换后:
None.gif final  ContainerCheckedTreeViewer treeViewer  =   new  ContainerCheckedTreeViewer(shell, SWT.BORDER | SWT.H_SCROLL);
五、treeviewer和tableviewer都介绍过了,考虑一下把两个结合起来是不是我们经常需要的情况。尝试一下吧。
六、souce下载: http://www.blogjava.net/Files/dreamstone/jface-6.rar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值