1.创建方式
2.事件
3.事件详情
viewer = new CheckboxTreeViewer(container/*, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER*/);
viewer.setContentProvider(new ViewContentProvider());//实现IStructuredContentProvider,ITreeContentProvider
viewer.setLabelProvider(new ViewLabelProvider());//extends LabelProvider
viewer.setInput(dataList);
2.事件
viewer.getTree().addSelectionListener(new TreeSelectionListener());//extends SelectionAdapter 右键菜单
viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
viewer.addDoubleClickListener(new IDoubleClickListener(){});//双击事件
viewer.addCheckStateListener(new TreeCheckStateListener());//implements ICheckStateListener 父子极联选中
3.事件详情
a.TreeCheckStateListener
Object obj = event.getElement();
if (!(obj instanceof TreeObject)) {
return;
}
TreeObject element = (TreeObject) obj;
boolean check = viewer.getChecked(obj);
switch(element.getLevel()){}
常用方法:viewer.getChecked(Objectxxx)
viewer.expandToLevel(parentElement, 2);
viewer.setChecked(child, check);
b.TreeSelectionListener
TreeItem item = (TreeItem) e.item;
if (item == null) {
return;
}
Object obj = item.getData();
MenuManager menuManager = new MenuManager();
if(!(obj instanceof TreeObject)){
return ;
}
TreeObject treeObj = (TreeObject)obj;
if(2 == treeObj.getLevel()){
menuManager.add(deleteAction);
}else if(1 == treeObj.getLevel()){
menuManager.add(newAction);
menuManager.add(deleteAction);
}else{
return ;
}
Menu menu = menuManager.createContextMenu(viewer.getControl());
viewer.getControl().setMenu(menu);
c.IDoubleClickListener
IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
if (selection == null){
return null;
}
Object obj = selection.getFirstElement();
TreeObject element = null;
if (obj instanceof TreeObject){
element = (TreeObject)obj;
}
//next open xxx handle