JFace TreeViewer Demo
这是一个三级Tree,因此我们先定义三级对象,它们应该都继承于同一个接口,
【ITreeEntity】
【PeopleEntity】
-
package demo.treeviewer;
-
-
import java.util.Date;
-
import java.util.List;
-
-
/**
-
* 本类包含五个不同数据类型的变量,分别对应数据库中的五个字段。
-
*
-
* @author YOYO
-
*
-
*/
-
public class PeopleEntity implements ITreeEntry {
-
-
private Long id;
-
private String name;
-
private boolean sex;
-
private int age;
-
private Date createDate;
-
-
public PeopleEntity(){}
-
-
return id;
-
}
-
this.id = id;
-
}
-
return name;
-
}
-
this.name = name;
-
}
-
public boolean isSex() {
-
return sex;
-
}
-
public void setSex(boolean sex) {
-
this.sex = sex;
-
}
-
public int getAge() {
-
return age;
-
}
-
public void setAge(int age) {
-
this.age = age;
-
}
-
return createDate;
-
}
-
this.createDate = createDate;
-
}
-
return null;
-
}
-
}
-
-
}
【CityEntity】
-
package demo.treeviewer;
-
-
import java.util.List;
-
-
/**
-
* 城市的实体类
-
* @author YOYO
-
*
-
*/
-
public class CityEntity implements ITreeEntry {
-
-
private Long id;
-
-
private String name;
-
-
private List peoples;
-
-
public CityEntity() {
-
super();
-
}
-
-
super();
-
this.name = name;
-
}
-
-
return id;
-
}
-
-
this.id = id;
-
}
-
-
return name;
-
}
-
-
this.name = name;
-
}
-
-
return peoples;
-
}
-
-
this.peoples = peoples;
-
}
-
-
return this.getPeoples();
-
}
-
-
this.setPeoples(children);
-
}
-
-
}
【CountryEntity】
-
package demo.treeviewer;
-
-
import java.util.List;
-
-
/**
-
* 国家的实体类
-
* @author YOYO
-
*
-
*/
-
public class CountryEntity implements ITreeEntry {
-
-
private Long id;
-
private String name;
-
private List cities;
-
public CountryEntity() {
-
super();
-
}
-
super();
-
this.name = name;
-
}
-
return id;
-
}
-
this.id = id;
-
}
-
return name;
-
}
-
this.name = name;
-
}
-
return cities;
-
}
-
this.cities = cities;
-
}
-
return this.getCities();
-
}
-
this.setCities(children);
-
}
-
-
}
TreeViewer的数据是由List带入的,因此我们创建一个类用来生成数据。(Country本来应该放国家,我这里放了“省内”和“省外”,囧)
-
package demo.treeviewer;
-
-
import java.util.ArrayList;
-
-
public class DataFactory {
-
-
PeopleEntity p1 = new PeopleEntity("Yahreso Lin");
-
PeopleEntity p2 = new PeopleEntity("Queeny Lee");
-
PeopleEntity p3 = new PeopleEntity("Skittles");
-
PeopleEntity p4 = new PeopleEntity("Cyin");
-
PeopleEntity p5 = new PeopleEntity("Lienxq");
-
PeopleEntity p6 = new PeopleEntity("Mini Yang");
-
PeopleEntity p7 = new PeopleEntity("ChenST");
-
PeopleEntity p8 = new PeopleEntity("ZhengC");
-
PeopleEntity p9 = new PeopleEntity("BlackBeetle");
-
-
CityEntity city1 = new CityEntity("龙岩");
-
CityEntity city2 = new CityEntity("郑州");
-
CityEntity city3 = new CityEntity("福州");
-
CityEntity city4 = new CityEntity("厦门");
-
CityEntity city5 = new CityEntity("天津");
-
-
CountryEntity c1 = new CountryEntity("省内");
-
CountryEntity c2 = new CountryEntity("省外");
-
-
{
-
list.add(p1);
-
list.add(p2);
-
city1.setChildren(list);
-
}
-
{
-
list.add(p3);
-
city2.setChildren(list);
-
}
-
{
-
list.add(p4);
-
list.add(p7);
-
list.add(p8);
-
city3.setChildren(list);
-
}
-
{
-
list.add(p5);
-
list.add(p9);
-
city4.setChildren(list);
-
}
-
{
-
list.add(p6);
-
city5.setChildren(list);
-
}
-
-
-
{
-
list.add(city1);
-
list.add(city3);
-
list.add(city4);
-
c1.setChildren(list);
-
}
-
{
-
list.add(city2);
-
list.add(city5);
-
c2.setChildren(list);
-
}
-
-
{
-
list.add(c1);
-
list.add(c2);
-
return list;
-
}
-
}
-
-
}
然后一个TreeViewer还需要一个ContentProvider和一个LabelProvider,
【TreeViewContentProvider】
-
package demo.treeviewer;
-
-
import java.util.List;
-
-
import org.eclipse.jface.viewers.ITreeContentProvider;
-
import org.eclipse.jface.viewers.Viewer;
-
-
/**
-
* 内容器:由它决定哪些对象记录应该输出在TreeViewer里显示
-
* @author YOYO
-
*
-
*/
-
public class TreeViewContentProvider implements ITreeContentProvider {
-
-
/**
-
* 由这种方法决定父节点应该显示哪些子结点
-
*/
-
ITreeEntry entry = (ITreeEntry) parentElement;
-
if( list == null || list.isEmpty() ) {
-
}else{
-
return list.toArray();
-
}
-
}
-
-
return null;
-
}
-
-
/**
-
* 判断某节点是否有子结点
-
*/
-
ITreeEntry entry = (ITreeEntry) element;
-
if(list==null || list.isEmpty()) {
-
return false;
-
}else{
-
return true;
-
}
-
}
-
-
/**
-
* 由这种方法决定树的一级显示哪些对象
-
*/
-
return list.toArray();
-
}else{
-
}
-
}
-
-
public void dispose() {
-
-
}
-
-
}
-
-
}
【TreeViewLabelProvider】
-
package demo.treeviewer;
-
-
import org.eclipse.jface.viewers.ILabelProvider;
-
import org.eclipse.jface.viewers.ILabelProviderListener;
-
import org.eclipse.swt.graphics.Image;
-
-
/**
-
* 标签提供器:控制记录在树中显示的文字和图像等
-
*
-
* @author YOYO
-
*
-
*/
-
public class TreeViewerLableProvider implements ILabelProvider {
-
-
/**
-
* 记录显示的图像,可以返回null值
-
*/
-
return null;
-
}
-
-
/**
-
* 记录显示的文字,不能返回null
-
*/
-
ITreeEntry entry = (ITreeEntry) element;
-
return entry.getName();
-
}
-
-
public void addListener(ILabelProviderListener listener) {
-
}
-
-
public void dispose() {
-
}
-
-
return false;
-
}
-
-
public void removeListener(ILabelProviderListener listener) {
-
}
-
-
}
这样就完成一个简单的TreeViewer了,代码如下:
【TreeViewer1】
-
package demo.treeviewer;
-
-
import org.eclipse.jface.viewers.TreeViewer;
-
-
public class TreeViewer1 {
-
-
TreeViewer1 window = new TreeViewer1();
-
window.open();
-
}
-
-
public void open(){
-
final Display display = new Display();
-
final Shell shell = new Shell();
-
shell.setSize(200, 300);
-
-
shell.setLayout(new FillLayout());
-
c.setLayout(new FillLayout());
-
TreeViewer tv = new TreeViewer(c, SWT.BORDER);
-
tv.setContentProvider(new TreeViewContentProvider());
-
tv.setLabelProvider(new TreeViewerLableProvider());
-
-
tv.setInput(inputObj);
-
-
shell.open();
-
while(!shell.isDisposed()) {
-
if(!display.readAndDispatch()) {
-
display.sleep();
-
}
-
}
-
}
-
-
}
但这只是个简单的显示TreeViewer的操作,如果要为它加上右键动作的话,我们需要做一下处理:
【MyActionGroup】
-
package demo.treeviewer;
-
-
import org.eclipse.jface.action.Action;
-
-
public class MyActionGroup extends ActionGroup {
-
-
private TreeViewer tv;
-
-
public MyActionGroup(TreeViewer treeViewer) {
-
this.tv = treeViewer;
-
}
-
-
@Override
-
public void fillContextMenu(IMenuManager mgr) {
-
MenuManager menuManager = (MenuManager) mgr;
-
menuManager.add(new OpenAction());
-
menuManager.add(new RefreshAction());
-
-
menuManager.add(new ExpandAction());
-
menuManager.add(new CollapseAction());
-
menuManager.add(new AddEntryAction());
-
menuManager.add(new RemoveEntryAction());
-
menuManager.add(new ModifyEntryAction());
-
-
Tree tree = tv.getTree();
-
tree.setMenu(menu);
-
}
-
-
public OpenAction() {
-
setText("打开");
-
}
-
-
public void run() {
-
IStructuredSelection selection = (IStructuredSelection) tv.getSelection();
-
ITreeEntry obj = (ITreeEntry) (selection.getFirstElement());
-
if(obj != null) {
-
MessageDialog.openInformation(null, null, obj.getName());
-
}
-
}
-
}
-
-
public RefreshAction() {
-
setText("刷新");
-
}
-
-
public void run() {
-
tv.refresh();
-
}
-
}
-
-
/**
-
* 展开当前节点的Action类
-
* @author YOYO
-
*
-
*/
-
public ExpandAction() {
-
setText("展开");
-
}
-
-
public void run() {
-
ITreeEntry obj = getSelTreeEntry();
-
if(obj != null) {
-
tv.expandToLevel(obj, 1); // 只展开一层,设置可以超过实际树的层数
-
}
-
}
-
-
}
-
-
/**
-
* 收缩当前节点的Action类
-
* @author YOYO
-
*
-
*/
-
public CollapseAction() {
-
setText("收缩");
-
}
-
-
public void run() {
-
ITreeEntry obj = getSelTreeEntry();
-
if(obj != null) {
-
tv.expandToLevel(obj, -1); // -1为将当前节点的所有子结点收缩
-
}
-
}
-
}
-
-
/**
-
* 给当前节点添加一个子结点的Action类
-
* @author YOYO
-
*
-
*/
-
public AddEntryAction() {
-
setText("增加");
-
}
-
-
public void run() {
-
ITreeEntry obj = getSelTreeEntry();
-
if(obj == null || obj instanceof PeopleEntity ) {
-
return;
-
}
-
InputDialog dialog = new InputDialog(null, "给当前节点增加一个子结点", "输入名称", "abc", null);
-
if(dialog.open() == InputDialog.OK ) {
-
-
ITreeEntry newEntry = null;
-
if(obj instanceof CountryEntity) {
-
newEntry = new CityEntity(entryName);
-
}else if(obj instanceof CityEntity) {
-
newEntry = new PeopleEntity(entryName);
-
}
-
-
// 在增加子结点之前把父节点展开
-
if(!tv.getExpandedState(obj)) {
-
tv.expandToLevel(obj, 1);
-
}
-
-
tv.add(obj, newEntry);
-
}
-
}
-
}
-
-
/**
-
* 删除节点的Action类
-
* @author YOYO
-
*
-
*/
-
public RemoveEntryAction() {
-
setText("删除");
-
}
-
-
public void run() {
-
ITreeEntry obj = getSelTreeEntry();
-
if(obj == null) {
-
return;
-
}
-
tv.remove(obj);
-
}
-
}
-
-
/**
-
* 修改节点名称的Action类
-
* @author YOYO
-
*
-
*/
-
public ModifyEntryAction() {
-
setText("修改");
-
}
-
-
public void run() {
-
ITreeEntry obj = getSelTreeEntry();
-
if(obj == null) {
-
return;
-
}
-
InputDialog dialog = new InputDialog(null, "修改节点", "输入新名称", obj.getName(), null);
-
if(dialog.open() == InputDialog.OK ) {
-
obj.setName(entryName);
-
tv.refresh(obj);
-
}
-
}
-
}
-
-
/**
-
* 自定义方法:取得当前选择的节点
-
* @return
-
*/
-
private ITreeEntry getSelTreeEntry() {
-
IStructuredSelection selection = (IStructuredSelection) tv.getSelection();
-
ITreeEntry entry = (ITreeEntry) (selection.getFirstElement());
-
return entry;
-
}
-
-
}
【TreeViewer2】修改原先的类,增加注入菜单的
-
package demo.treeviewer;
-
-
import org.eclipse.jface.action.MenuManager;
-
-
public class TreeViewer2 {
-
-
TreeViewer2 window = new TreeViewer2();
-
window.open();
-
}
-
-
public void open(){
-
final Display display = new Display();
-
final Shell shell = new Shell();
-
shell.setText("JFace TreeViewer Demo");
-
shell.setSize(278, 300);
-
-
shell.setLayout(new FillLayout());
-
c.setLayout(new FillLayout());
-
TreeViewer tv = new TreeViewer(c, SWT.BORDER);
-
tv.setContentProvider(new TreeViewContentProvider());
-
tv.setLabelProvider(new TreeViewerLableProvider());
-
-
/////////////START
-
MyActionGroup actionGroup = new MyActionGroup(tv);
-
actionGroup.fillContextMenu(new MenuManager());
-
/////////////END
-
-
tv.setInput(inputObj);
-
-
shell.open();
-
while(!shell.isDisposed()) {
-
if(!display.readAndDispatch()) {
-
display.sleep();
-
}
-
}
-
}
-
-
}
- 无匹配
2010年7月27日 01:13
博主,你好!这个例子很好,可是我想知道在treeViewer树上进行修改怎么做啊,你这个程序不全吧!我很急,希望看到我的留言能给我联系好吗?谢谢!!
2010年7月27日 22:42
这个应该是全的吧 = =。。已经发送邮件过去。。
2020年7月30日 17:28
亲测有用!非常感谢,帮了我的大忙。