`
sslaowan
  • 浏览: 373833 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

扩展swt combo

阅读更多

原文链接:http://blog.csdn.net/kentchenj/archive/2008/10/14/3074623.aspx

swt的combo有两个麻烦的问题:

  1. 如果选中上次选中的内容,它照样会触发addSelectionListener方法
  2. 如果使用setData(String key, Object value)时,必须设定key值。通常会使用index作为key值。但是如果删除了一项,它的index会自动-1,不能和key相对应了
为了解决这个问题,重新写了一个Combo类
要解决问题1,写了一个新的接口:
  1. public interface SelectionChangedListener {
  2.     /**
  3.      * 类似SelectionListener
  4.      * @param event
  5.      */
  6.     public void selectionChanged(SelectionEvent event);
  7. }
要解决问题2,定义一个类保存combo的状态
  1. public class ComboState {
  2.     private int selectionIndex = -1;
  3.     public int getSelectionIndex() {
  4.         return selectionIndex;
  5.     }
  6.     public void setSelectionIndex(int selectionIndex) {
  7.         this.selectionIndex = selectionIndex;
  8.     }
  9. }
Combo类如下:
  1. public class EasyCombo extends Combo {
  2.     private ComboState state;
  3.     private List<Object> indexData = new LinkedList<Object>();
  4.     private List<SelectionChangedListener> scls = new ArrayList<SelectionChangedListener>();
  5.     public EasyCombo(Composite parent, int style) {
  6.         super(parent, style);
  7.         this.setState(new ComboState());
  8.     }
  9.     /**
  10.      * 封装SelectionChangedListener事件
  11.      */
  12.     public void addSelectionChangedListener(SelectionChangedListener listener) {
  13.         if (scls.size() == 0) {// 第一次add,会创建addSelectionListener事件
  14.             addSelectionListener(new SelectionListener() {
  15.                 public void widgetDefaultSelected(SelectionEvent e) {
  16.                 }
  17.                 public void widgetSelected(SelectionEvent e) {
  18.                     if (getState().getSelectionIndex() != getSelectionIndex()) {
  19.                         getState().setSelectionIndex(getSelectionIndex());
  20.                         for (SelectionChangedListener listener : scls) {
  21.                             listener.selectionChanged(e);
  22.                         }
  23.                     }
  24.                 }
  25.             });
  26.         }
  27.         scls.add(listener);
  28.     }
  29.     public void add(String text) {
  30.         super.add(text);
  31.         indexData.add(null);
  32.     }
  33.     public void add(String text, int index) {
  34.         super.add(text, index);
  35.         if (index > indexData.size()) {
  36.             indexData.add(null);
  37.         } else {
  38.             indexData.set(index, null);
  39.         }
  40.     }
  41.     public void remove(int index) {
  42.         super.remove(index);
  43.         indexData.remove(index);
  44.     }
  45.     public void removeAll() {
  46.         super.removeAll();
  47.         indexData.clear();
  48.     }
  49.     /**
  50.      * 设置值,和setData(String,Object)不同的是,它是remove敏感的<br>
  51.      * 例如:0->A 1->B 2->C 如果删除1,那么 0->A 1->C<br>
  52.      * 所以可以使用一个链表存放object的值,当remove被调用时,此链表也被remove一个节点
  53.      * 
  54.      * @param index
  55.      * @param value
  56.      */
  57.     public void setIndexData(int index, Object value) {
  58.         indexData.set(index, value);
  59.     }
  60.     public Object getIndexData(int index) {
  61.         return indexData.get(index);
  62.     }
  63.     public void setState(ComboState state) {
  64.         this.state = state;
  65.     }
  66.     public ComboState getState() {
  67.         return this.state;
  68.     }
  69.     /**
  70.      * 注意,必须写一个空方法,不然会报exception
  71.      */
  72.     protected void checkSubclass() {
  73.     }
  74. }
最后是测试类:
  1. public class TestEasyCombo {
  2.     public static void main(String[] args) {
  3.         Display display = new Display();
  4.         Shell shell = new Shell(display);
  5.         shell.setText("测试窗口");
  6.         shell.setLayout(new FillLayout());
  7.         
  8.         EasyCombo combo = new EasyCombo(shell, SWT.READ_ONLY);
  9.         combo.add("test0");
  10.         combo.setIndexData(0"测试0");
  11.         combo.add("test1");
  12.         combo.setIndexData(1"测试1");
  13.         combo.add("test2");
  14.         combo.setIndexData(2"测试2");
  15.         
  16.         System.out.println("index: "+combo.getIndexData(1));
  17.         
  18.         combo.remove(1);
  19.         System.out.println("index: "+combo.getIndexData(1));
  20.         
  21.         
  22.         combo.addSelectionChangedListener(new SelectionChangedListener(){
  23.             public void selectionChanged(SelectionEvent event) {
  24.                 System.out.println("selection changed!");
  25.             }
  26.             
  27.         });
  28.         
  29.         shell.pack();
  30.         shell.open();
  31.         while (!shell.isDisposed()) {
  32.             if (!display.readAndDispatch())
  33.                 display.sleep();
  34.         }
  35.     }
  36. }
好了,新的类完成了。
它提供一个SelectionChangeEvent的事件,当你选中combo的一项,并且不是上次选择的那项时,SelectionChangeEvent才会被触发。此外,我可以根据index获得相相应的值了。

这么做只是解决问题的一种方法。如果不扩展Combo(eclipse建议不要继承swt widget),还有其它的解决方法。下文再写

 


 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics