需求:使用FilteredTree实现一个下面这样的Dialog,要求Check框单选,即只能选择一个:当选择新的时候,旧的不选。说明:FilteredTree自带一个文本输入框。
1、自己的类继承org.eclipse.jface.dialogs.Dialog
2、在createDialogArea方法中实现自己的窗体:
1 protected Control createDialogArea(Composite parent) { 2 Composite composite = new Composite(parent,SWT.NULL); 3 composite.getShell().setText("****选择"); 4 5 composite.setLayout(new GridLayout(1,false)); 6 GridData compositeGridData = new GridData(GridData.FILL_BOTH); 7 composite.setLayoutData(compositeGridData); 8 9 Label title = new Label(composite,SWT.NONE); 10 title.setAlignment(SWT.LEFT); 11 title.setText("可选**信息"); 12 title.setLayoutData(new GridData(SWT.LEFT,SWT.TOP,false,false,1,1)); 13 14 ChooseProjectEntryFilteredTree filteredTree = new ChooseProjectEntryFilteredTree( 15 composite, 16 SWT.BORDER, 17 new ChooseProjectEntryPatternFilter(), 18 true); 19 this.treeViewer = filteredTree.getViewer(); 20 //this.treeViewer.setUseHashlookup(true); 21 22 this.tree = this.treeViewer.getTree(); 23 this.tree.setToolTipText("请输入***的关键词"); 24 25 this.checkboxTreeViewer = new CheckboxTreeViewer(this.tree); 26 27 GridData gd = new GridData(SWT.FILL,SWT.FILL,true,false,1,1); 28 gd.heightHint = 250; 29 this.tree.setLayoutData(gd); 30 this.tree.setLinesVisible(true); 31 this.tree.setHeaderVisible(true); 32 33 TreeColumn funcDecl = new TreeColumn(this.tree,SWT.CENTER); 34 funcDecl.setText("****"); 35 funcDecl.setWidth(240); 36 funcDecl.addSelectionListener(new SelectionAdapter() { 37 boolean sortType = true; 38 39 @Override 40 public void widgetSelected(SelectionEvent e) { 41 sortType = !sortType; 42 treeViewer.setSorter(sortType ? ChooseProjectEntrySorter.sorter 43 : ChooseProjectEntrySorter.reverseSorter); 44 } 45 46 47 }); 48 49 TreeColumn file = new TreeColumn(this.tree,SWT.CENTER); 50 file.setText("****"); 51 file.setWidth(270); 52 53 TreeColumn line = new TreeColumn(this.tree,SWT.CENTER); 54 line.setText("***"); 55 line.setWidth(100); 56 57 treeViewer.setContentProvider(new ChooseProjectEntryContentProvider()); 58 treeViewer.setLabelProvider(new ChooseProjectEntryLabelProvider()); 59 treeViewer.setSorter(ChooseProjectEntrySorter.sorter); 60 treeViewer.setInput(manager); 61 62 this.selectedLabel = new Label(composite,SWT.NONE); 63 this.selectedLabel.setAlignment(SWT.LEFT); 64 GridData labelGd = new GridData(SWT.LEFT,SWT.TOP,true,false,1,1); 65 labelGd.widthHint = 380; 66 this.selectedLabel.setLayoutData(labelGd); 67 68 this.checkboxTreeViewer.addCheckStateListener(checkStateListener); 69 70 //this.treeViewer.addSelectionChangedListener(listener); 71 return null; 72 }
说明:
treeViewer.setContentProvider(new ChooseProjectEntryContentProvider()); treeViewer.setLabelProvider(new ChooseProjectEntryLabelProvider()); treeViewer.setSorter(ChooseProjectEntrySorter.sorter); treeViewer.setInput(manager);
这里设置的是TreeViewer的content provider和label provider、sorter,所以并不能正常使用checkboxTreeViewer.setChecked()和setSubtreeChecked(),并不能像这个链接中那样设置指定行的checked状态为false,所以需要使用下面的方法:
1)、checkboxTreeViewer监听事件:
1 /** 2 * 单选表格 3 */ 4 private ICheckStateListener checkStateListener = new ICheckStateListener(){ 5 @Override 6 public void checkStateChanged(CheckStateChangedEvent event) { 7 if (event.getChecked()) { 8 setAllCheckedFalse(); 9 //checkboxTreeViewer.setChecked(getTreeItem(event.getElement()), true); 10 checkFunc = (FuncItem)(event.getElement()); 11 TreeItem item = getTreeItem(checkFunc); 12 if(item != null){ 13 item.setChecked(true); 14 } 15 selectedLabel.setText("已选**:" + checkFunc.***); 16 }else{ 17 checkFunc = null; 18 } 19 20 treeViewer.refresh(); 21 } 22 23 };
2)、设置所有checked的行为unchecked状态:
1 /** 2 * 设置所有被checked的选项unchecked 3 */ 4 private void setAllCheckedFalse(){ 5 TreeItem[] items = this.tree.getItems(); 6 7 if(items.length > 0){ 8 for(int i = 0; i < items.length; i ++){ 9 TreeItem item = items[i]; 10 item.setChecked(false); 11 } 12 } 13 }
3)、getTreeItem函数:
1 private TreeItem getTreeItem(FuncItem element){ 2 TreeItem[] items = this.tree.getItems(); 3 4 if(items.length > 0){ 5 for(int i = 0; i < items.length; i ++){ 6 TreeItem item = items[i]; 7 if(element.equals((FuncItem)item.getData())){ 8 return item; 9 } 10 } 11 } 12 13 return null; 14 }
3、贴一下ChooseProjectEntryFilteredTree的代码
1 /** 2 * 自行导入需要的包 3 */ 4 public class ChooseProjectEntryFilteredTree extends FilteredTree { 5 6 public ChooseProjectEntryFilteredTree(Composite parent, int treeStyle, 7 PatternFilter filter, boolean useNewLook) { 8 super(parent, treeStyle, filter, useNewLook); 9 } 10 11 @Override 12 protected TreeViewer doCreateTreeViewer(Composite parent, int style) { 13 return super.doCreateTreeViewer(parent, SWT.CHECK | SWT.SINGLE | SWT.BORDER 14 | SWT.V_SCROLL | SWT.H_SCROLL); 15 } 16 17 18 }
4、贴一下ChooseProjectEntryPatternFilter的代码
1 public class ChooseProjectEntryPatternFilter extends PatternFilter { 2 3 @Override 4 protected boolean isLeafMatch(Viewer viewer, Object element) { 5 TreeViewer treeViewer = (TreeViewer) viewer; 6 7 int numberOfColumns = treeViewer.getTree().getColumnCount(); 8 9 ITableLabelProvider labelProvider = (ITableLabelProvider) treeViewer 10 .getLabelProvider(); 11 12 boolean isMatch = false; 13 14 for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) { 15 String labelText = labelProvider 16 .getColumnText(element, columnIndex); 17 isMatch |= wordMatches(labelText); 18 } 19 20 return isMatch; 21 } 22 23 }
ChooseProjectEntryContentProvider、ChooseProjectEntryLabelProvider、ChooseProjectEntrySorter跟一般的TreeViewer差不多,
这里再贴ChooseProjectEntryLabelProvider的部分代码:
1 public class ChooseProjectEntryLabelProvider extends LabelProvider implements ITableLabelProvider{ 2 3 @Override 4 public Image getColumnImage(Object element, int columnIndex) { 5 return null; 6 } 7 8 @Override 9 public String getColumnText(Object element, int columnIndex) { 10 11 if(element instanceof FuncItem){ 12 FuncItem func = (FuncItem)element; 13 switch(columnIndex){ 14 case 0: 15 return func.***; 16 case 1: 17 return func.***; 18 case 2: 19 return String.valueOf(func.***); 20 default: 21 return ""; 22 } 23 } 24 25 return getText(element); 26 } 27 28 29 }
注意这里实现的是ITableLabelProvider接口。
我自己用checkboxTreeViewer.setChecked(element, state)无法设置event.getElement()【checkStateChanged方法中】元素的状态,
但是使用checkboxTreeViewer.setAllChecked(state)可以设置所有元素的状态为checked或者unchecked,
没办法,我又需要使用FilteredTree,又无法使用setChecked办法设置指定元素状态(我单步调试进入setChecked方法里面看了看,应该是event.getElement()元素的问题),只好想出上面的馊办法,希望对读者有用。