CheckboxTreeviewer选中状态问题
来源:互联网 发布:守望先锋网络质量显示 编辑:程序博客网 时间:2024/05/19 01:58
在使用JFace的CheckboxTreeViewer做开发时,如果遇到如下问题:
- check状态点击出现问题,check A却让B的状态check了
- 置灰(setGrayed)某个元素,元素状态并没有置灰,而改成了checked
- 选中某个元素,在selection linstener中监听到的选中元素却不是选中的元素
- 。。。。
如果遇到上述一系列问题(或者其他比较反常的问题),那很有可能你的问题原因是:CheckboxTreeViewer的某一级树结构中含有重复元素。举例说明:
假如CheckboxTreeViewer的input为如下结构:
A
|__B //1
C
|__B //2
在选中 2 的时候,可能变化的是1的状态,或者出现其他异常问题。该问题出现根本原因是AbstractTreeViewer(CheckboxTreeViewer的父类)的一个Bug,有如下代码:
protected boolean isSameSelection(List items, Item[] current) { // If they are not the same size then they are not equivalent int n = items.size(); if (n != current.length) { return false; } CustomHashtable itemSet = newHashtable(n * 2 + 1); for (Iterator i = items.iterator(); i.hasNext();) { Item item = (Item) i.next(); Object element = item.getData(); itemSet.put(element, element); } // Go through the items of the current collection // If there is a mismatch return false for (int i = 0; i < current.length; i++) { if (current[i].getData() == null || !itemSet.containsKey(current[i].getData())) { return false; } } return true; }
这段代码用来比较树上的元素是否相等,在比较时,只考虑本级目录,而未考虑父目录是否相同,一种改进方案是(摘抄自stackoverflow):
protected boolean isSameSelection(List items, Item[] current) { // If they are not the same size then they are not equivalent int n = items.size(); if (n != current.length) { return false; } Set itemSet = new HashSet(n * 2 + 1); for (Iterator i = items.iterator(); i.hasNext();) { Item item = (Item) i.next(); itemSet.add(getTreePathFromItem(item)); } // Go through the items of the current collection // If there is a mismatch return false for (int i = 0; i < current.length; i++) { if (current[i].getData() == null || !itemSet.contains(getTreePathFromItem(current[i]))) { return false; } } return true; }
在改进的代码中,将 对数据的判断改为了对路径的判断,用于解决元素重复这个问题。
参考资料:http://stackoverflow.com/questions/6289049/treeviewer-setselection-does-not-select-the-correct-item-after-the-path-given
0 0
- CheckboxTreeviewer选中状态问题
- swt CheckboxTreeViewer 全选 反选 级联选中子节点
- RadioGroup选中状态的问题
- 解决UIButton 选中状态和未选中状态色差问题
- ListView+CheckBox 选中状态错乱问题
- 选中状态
- 关于BaseAdapter,SimpleAdapter的数据显示错乱问题,选中状态错乱问题,Checkbox选中状态错乱问题总结
- 关于CheckBoxList选中框改变状态的问题
- IE 下双击 checkbox 选中状态延迟问题
- 关于ListView中checkBox选中状态错乱问题的解决办法
- 解决checkbox在滑动时选中状态错乱的问题
- 关于RadioGroup中的radiobutton选中状态保存的问题!!
- 解决UITableViewCell选中状态下UILabel背景消失的问题
- SWT/jface 之 CheckBoxTreeViewer 选择父节点时 子节点全部选中。选择子节点时,父节点选中,并且是Grayed 样式(实心)
- Clistctrl控件选中状态
- UItableview 取消选中状态
- 导航栏目选中状态
- GridView选中状态
- git fetch 和 git pull 的区别
- 特洛伊木马后传 ——浅谈互联网创业的艰辛
- 2015杭电新生赛1003玩骰子【大型模拟】
- linux下centOS系统让tomcat以service方式运行
- UML总结
- CheckboxTreeviewer选中状态问题
- IOS--UIButton
- php获取post参数的几种方式
- 使用PHP生成二维码的两种方法(带logo图像)
- 代码块创建Label
- UIImagePickerView的应用 (李梦珂)
- 项目适配iOS9 遇到的坑坑娃娃
- THINKPHP限制今天提交的数据不超过几条
- VLD(Visual LeakDetector)内存泄露工具的使用