2012-10-11 34 views
1

在我的主dailog中,我有一個JFace TableViewer。該表的最後一列是ComboBoxCellEditor。他們可以選擇否,是的,兩者。這一切都按設計工作。SWT - 在我的TableViewer中使用EditingSupport

但這是我的問題。

  1. 如果用戶選擇Both作爲值。
  2. 我需要運行將從陣列
  3. 變化來自於無
  4. 值獲取當前行數據的方法,使數據的副本,然後將值更改爲Yes
  5. 加兩回到Array
  6. 刷新表

表範例

從 -

1002 | 001 | sss | part | both(user changed from default) 

要 -

1002 | 001 | sss | part | No 

1002 | 001 | sss | part | Yes 

我想弄清楚這兩個選擇後如何運行做休息的方法。我假設它必須是某種傾聽者。請看看我的EditingSupport代碼,並告訴我在哪裏開始我的方法來完成剩下的工作。

public class OptionEditingSupport extends EditingSupport 
{ 
    private ComboBoxCellEditor cellEditor; 

    public OptionEditingSupport(ColumnViewer viewer) { 
     super(viewer); 
     cellEditor = new ComboBoxCellEditor(((TableViewer)viewer).getTable(), new String[]{"No", "Yes", "Both"}, SWT.DROP_DOWN); 
     //cellEditor.setValue(0); 
    } 
    protected CellEditor getCellEditor(Object element) { 
     return cellEditor; 
    } 
    protected boolean canEdit(Object element) { 
     return true; 
    } 
    protected Object getValue(Object element) { 
     return 0; 
    } 
    protected void setValue(Object element, Object value) 
    { 
     if((element instanceof AplotDatasetData) && (value instanceof Integer)) { 
      Integer choice = (Integer)value; 
      //String option = (choice == 0? "Yes":"No":"Both"); 
      String option = ((AplotDatasetData)element).getMarkupValue();; 
      if(choice == 0) { 
       option = "No"; 
      }  
      else if(choice == 1) { 
       option = "Yes"; 
      }  
      else { 
       option = "Both"; 
      }  
      ((AplotDatasetData)element).setMarkupValue(option); 
      getViewer().update(element, null); 
     } 
    } 
} 

回答

1

據我理解你的問題,你要複製你的對象之一,將它添加到你的模型,並刷新瀏覽器。

而這一切都應該發生在用戶選擇組合框中的"both"時。你已經知道,當發生這種情況。您將以setValue方法的else個案結束。然後你可以做你在那裏必須做的事情:

protected void setValue(Object element, Object value) 
{ 
    if((element instanceof AplotDatasetData) && (value instanceof Integer)) { 
     Integer choice = (Integer)value; 

     String option = ((AplotDatasetData)element).getMarkupValue(); 

     if(choice == 0) { 
      option = "No"; 
     }  
     else if(choice == 1) { 
      option = "Yes"; 
     }  
     else { 
      option = "Both"; 

      // create a copy of your element 
      // add it to your model 
      // update the viewer 
     } 

     getViewer().update(element, null); 
    } 

} 
+0

現在你明白地指出了這一點。巴茲非常感謝你的幫助。我從你的建議和解釋中學到了很多東西 – jkteater

+0

@jkteater不客氣。當我能幫忙時總是很高興。 – Baz

相關問題