我正在制作一个使用我自己的向导页面(扩展MBSCustomPage)的Eclipse(3.6.2)CDT(7.0.2)插件。该向导页面显示一个表,其中填充了一些TableItem,可以通过单击它们来选中或取消选中它们(通常)。问题在于,当选中(或未选中)TableItem复选框时,我总是会收到两个事件!在收到的第一个事件中,即使首先检查了TableItem,也有一个(SelectedEvent)e.detail == SWT.CHECK,而在第二个事件中,我有一个(SelectedEvent)e.detail == 0!。所以我没有办法知道TableItem是否真的被检查过。
这是我的代码(有点):

final Table table = new Table( composite, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION | SWT.CHECK );
table.setHeaderVisible(true);
table.setLinesVisible(false);

(...)

table.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent e) {
        CheckThatOnlyOneItemCanBeCheckedAtTime(e.item);
        //If someone check on me, save the item data value in a "container"
        if( e.detail == SWT.CHECK ) {
            MBSCustomPageManager.addPageProperty(PAGE_ID, "SDK_Path", ((ISdk)((TableItem)e.item).getData()).getPath() );
        } else { //Otherwise, unset the saved value
            MBSCustomPageManager.addPageProperty(PAGE_ID, "SDK_Path", "" );
        }
    }
});


当我单击TableItem的复选框时,为什么两次调用widgetSelected()?
我测试了即使在widgetSelected()方法内没有代码的情况下,也会触发事件。
我没有发现任何东西在谷歌搜索或在Eclipse Bugzilla数据库中寻找……对我来说真的很奇怪,但是我不是一个经验丰富的Eclipse插件编码器(甚至不是Java):)

谢谢!

最佳答案

table.addListener(SWT.Selection, new Listener() {
  public void handleEvent(Event arg0) {
    String string = arg0.detail == SWT.CHECK ? "Checked" : "Selected";
    if (arg0.detail == SWT.CHECK) {
      System.out.println(arg0.item + " " + string+ ":" +
                         ((Table)arg0.widget).getSelection()[0].getChecked());
     }
     else {
       System.out.println(arg0.item + " " + string);
     }
   }
});

08-06 15:29