涉及到的几个实体类:
基本节点接口:
public interface ITreeNode { public String getName() ; public void setName(String name); public abstract List getChild() ; public abstract void setChild(List child); }
大学实体University,他的孩子节点是学院:
public class University implements ITreeNode{ public String name; public List child = new ArrayList(); public List getChild() { return child; } public void setChild(List child) { this.child = child; } @Override public String getName() { // TODO Auto-generated method stub return name; } @Override public void setName(String name) { // TODO Auto-generated method stub this.name = name; } }
学院类 , 他的孩子是班级:
public class Collage implements ITreeNode { private String name; private List child = new ArrayList(); @Override public String getName() { // TODO Auto-generated method stub return name; } @Override public void setName(String name) { // TODO Auto-generated method stub this.name = name; } @Override public List getChild() { // TODO Auto-generated method stub return child; } @Override public void setChild(List child) { // TODO Auto-generated method stub this.child = child; } }
班级类:
public class CustomClass implements ITreeNode{ private String name; private List child = new ArrayList(); @Override public String getName() { // TODO Auto-generated method stub return name; } @Override public void setName(String name) { // TODO Auto-generated method stub this.name = name; } @Override public List getChild() { // TODO Auto-generated method stub return null; } @Override public void setChild(List child) { // TODO Auto-generated method stub this.child = child; } }
TreeView的contentProvider和labelProvider:
TreeViewerContentProvider
public class TreeViewerContentProvider implements ITreeContentProvider{ public TreeViewerContentProvider() { } public Object [] getChildren(Object parentElement) { if (parentElement instanceof University){ // System.out.println(((University) parentElement).getName()); List childList = ((University) parentElement).getChild(); return childList.toArray(); } if (parentElement instanceof Collage){ System.out.println(((Collage) parentElement).getName()); List childList = ((Collage) parentElement).getChild(); return childList.toArray(); } return new Object[0]; } public Object getParent(Object element) { return null; } public boolean hasChildren(Object element) { if (element instanceof University){ List childList = ((University) element).getChild(); if (childList.size() > 0) return true; else return false; } if (element instanceof Collage){ List childList = ((Collage) element).getChild(); if (childList.size() > 0) return true; else return false; } return false; } public Object[] getElements(Object inputElement) { if(inputElement instanceof List) { List list =(List)inputElement; return list.toArray(); } else return new Object[]{inputElement}; } public void dispose(){} public void inputChanged(Viewer viewer,Object oldInput,Object newInput){ } }
TreeViewerLabelProvider
public class TreeViewerLabelProvider implements ILabelProvider{ public TreeViewerLabelProvider(){ } public Image getImage(Object element) { return null; } public String getText(Object element) { if (element instanceof University){ return ((University) element).getName(); } if (element instanceof Collage){ return ((Collage) element).getName(); } if (element instanceof CustomClass){ return ((CustomClass) element).getName(); } return ""; } public void addListener(ILabelProviderListener listener) { } public void dispose() { } public boolean isLabelProperty(Object element,String property) { return false; } public void removeListener(ILabelProviderListener listener) { } }
tableTreeView的contentProvider和labelProvier
TableViewContentProvider
public class TableViewContentProvider implements ITreeContentProvider{ @Override public void dispose() { // TODO Auto-generated method stub } @Override public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { // TODO Auto-generated method stub } @Override public Object[] getElements(Object inputElement) { // TODO Auto-generated method stub if(inputElement instanceof List) return ((List)inputElement).toArray(); else return new Object[]{inputElement}; } @Override public Object[] getChildren(Object parentElement) { // TODO Auto-generated method stub if(parentElement instanceof University){ List list = new ArrayList(); list.addAll(((University) parentElement).getChild()); return list.toArray(); } if(parentElement instanceof Collage) { List list = new ArrayList(); list.addAll(((Collage) parentElement).getChild()); return list.toArray(); } return new Object[0]; } @Override public Object getParent(Object element) { // TODO Auto-generated method stub return null; } @Override public boolean hasChildren(Object element) { if(element instanceof University) { if(((University) element).getChild().size() > 0) return true; return false; } if(element instanceof Collage){ if (((Collage) element).getChild().size() > 0) return true; return false; } return false; } }
TableViewLabelProvider
public class TableViewLabelProvider implements ITableLabelProvider { @Override public void addListener(ILabelProviderListener listener) { // TODO Auto-generated method stub } @Override public void dispose() { // TODO Auto-generated method stub } @Override public boolean isLabelProperty(Object element, String property) { // TODO Auto-generated method stub return false; } @Override public void removeListener(ILabelProviderListener listener) { // TODO Auto-generated method stub } @Override public Image getColumnImage(Object element, int columnIndex) { // TODO Auto-generated method stub return null; } @Override public String getColumnText(Object element, int columnIndex) { if(element instanceof University) { switch (columnIndex) { case 0: return ((University) element).getName(); default: return ""; } } if(element instanceof Collage) { switch (columnIndex) { case 1: return ((Collage) element).getName(); default: return ""; } } if(element instanceof CustomClass){ if(columnIndex==2) return ((CustomClass) element).getName(); else return ""; } return ""; } }
产生数据的工厂:
public class DataFactory { public static List getList(){ University ui = new University(); ui.setName("南京航空航天大学"); Collage infoCollage = new Collage(); infoCollage.setName("信息科学与技术学院"); Collage compuCollage = new Collage(); compuCollage.setName("计算机科学与技术学院"); ui.getChild().add(infoCollage); ui.getChild().add(compuCollage); CustomClass class201 = new CustomClass(); class201.setName("201班"); CustomClass class301 = new CustomClass(); class301.setName("301班"); CustomClass class401 = new CustomClass(); class401.setName("401班"); CustomClass class501 = new CustomClass(); class501.setName("501班"); infoCollage.getChild().add(class201); infoCollage.getChild().add(class301); compuCollage.getChild().add(class401); compuCollage.getChild().add(class501); List unverList = new ArrayList(); unverList.add(ui); return unverList; } }
对话框类:
import java.util.ArrayList; public class TableTreeDemoDlg extends Dialog { protected Object result; protected Shell shlSwt; private List tableTreeViewInputList = new ArrayList(); /** * Create the dialog. * @param parent * @param style */ public TableTreeDemoDlg(Shell parent, int style) { super(parent, style); setText("SWT Dialog"); } /** * Open the dialog. * @return the result */ public Object open() { createContents(); shlSwt.open(); shlSwt.layout(); Display display = getParent().getDisplay(); while (!shlSwt.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } return result; } /** * Create contents of the dialog. */ private void createContents() { shlSwt = new Shell(getParent(), SWT.CLOSE | SWT.RESIZE | SWT.TITLE | SWT.APPLICATION_MODAL); shlSwt.setSize(727, 535); shlSwt.setText("SWT\u6F14\u793A"); shlSwt.setLayout(null); final TableTreeViewer tableTreeViewer = new TableTreeViewer(shlSwt, SWT.BORDER | SWT.FULL_SELECTION); TableTree tableTreeView = tableTreeViewer.getTableTree(); tableTreeView.getTable().setLinesVisible(true); tableTreeView.getTable().setHeaderVisible(true); tableTreeView.setBounds(240, 10, 458, 421); TableColumn tblclmnNewColumn = new TableColumn(tableTreeView.getTable(), SWT.CENTER); tblclmnNewColumn.setWidth(90); tblclmnNewColumn.setText("University"); TableColumn tblCollage = new TableColumn(tableTreeView.getTable(), SWT.CENTER); tblCollage.setWidth(114); tblCollage.setText("Collage"); TableColumn tblClass = new TableColumn(tableTreeView.getTable(), SWT.CENTER); tblClass.setWidth(112); tblClass.setText("Class"); TableColumn tblStuNo = new TableColumn(tableTreeView.getTable(), SWT.CENTER); tblStuNo.setText("StuNo"); tblStuNo.setWidth(138); final TreeViewer treeViewer = new TreeViewer(shlSwt, SWT.BORDER); Tree treeView = treeViewer.getTree(); treeView.setBounds(10, 10, 211, 426); Button btnNewButton = new Button(shlSwt, SWT.NONE); btnNewButton.setBounds(64, 456, 72, 22); btnNewButton.setText("New Button"); Button btnNewButton_1 = new Button(shlSwt, SWT.NONE); btnNewButton_1.setBounds(538, 456, 72, 22); btnNewButton_1.setText("New Button"); List dataList = DataFactory.getList(); tableTreeViewer.setContentProvider(new TableViewContentProvider()); tableTreeViewer.setLabelProvider(new TableViewLabelProvider()); tableTreeViewer.setInput(tableTreeViewInputList); treeViewer.setContentProvider(new TreeViewerContentProvider()); treeViewer.setLabelProvider(new TreeViewerLabelProvider()); treeViewer.setInput(dataList); treeViewer.getTree().addSelectionListener(new SelectionAdapter(){ public void widgetSelected(SelectionEvent e) { TreeItem[] selected = treeViewer.getTree().getSelection(); Object obj = selected[0].getData(); tableTreeViewInputList.clear(); tableTreeViewInputList.add(obj); tableTreeViewer.refresh(); } } ); } }