2012-11-16 55 views
0

我有一個對象Supply,它可以是ElecSupplyGasSupplysee related question)。GWT編輯器 - 如何添加基於集合的同一類型的N個子編輯器

無論正在編輯哪個子類,它們都有一個列表BillingPeriod s。

我現在需要基於該列表的內容來實例化N個BillingPeriodEditor s,並且對於如何執行該操作我感到非常困惑。

我正在使用GWTP。這裏是SupplyEditor我剛纔得到了工作代碼:

public class SupplyEditor extends Composite implements ValueAwareEditor<Supply> 
{ 
    private static SupplyEditorUiBinder uiBinder = GWT.create(SupplyEditorUiBinder.class); 

    interface SupplyEditorUiBinder extends UiBinder<Widget, SupplyEditor> 
    { 
    } 

    @Ignore 
    final ElecSupplyEditor elecSupplyEditor = new ElecSupplyEditor(); 

    @Path("") 
    final AbstractSubTypeEditor<Supply, ElecSupply, ElecSupplyEditor> elecSupplyEditorWrapper = new AbstractSubTypeEditor<Supply, ElecSupply, ElecSupplyEditor>(
      elecSupplyEditor) 
    { 
     @Override 
     public void setValue(final Supply value) 
     { 
      setValue(value, value instanceof ElecSupply); 
      if(!(value instanceof ElecSupply)) 
      { 
       showGasFields(); 
      } 
      else 
      { 
       showElecFields(); 
      } 
     } 
    }; 

    @Ignore 
    final GasSupplyEditor gasSupplyEditor = new GasSupplyEditor(); 

    @Path("") 
    final AbstractSubTypeEditor<Supply, GasSupply, GasSupplyEditor> gasSupplyEditorWrapper = new AbstractSubTypeEditor<Supply, GasSupply, GasSupplyEditor>(
      gasSupplyEditor) 
    { 
     @Override 
     public void setValue(final Supply value) 
     { 
      setValue(value, value instanceof GasSupply); 
      if(!(value instanceof GasSupply)) 
      { 
       showElecFields(); 
      } 
      else 
      { 
       showGasFields(); 
      } 
     } 
    }; 

    @UiField 
    Panel elecPanel, gasPanel, unitSection; 

    public SupplyEditor() 
    { 
     initWidget(uiBinder.createAndBindUi(this)); 

     gasPanel.add(gasSupplyEditor); 
     elecPanel.add(elecSupplyEditor); 
    } 

    // functions to show and hide depending on which type... 

    @Override 
    public void setValue(Supply value) 
    { 
     if(value instanceof ElecSupply) 
     { 
      showElecFields(); 
     } 
     else if(value instanceof GasSupply) 
     { 
      showGasFields(); 
     } 
     else 
     { 
      showNeither(); 
     } 
    } 
} 

現在,作爲BillingPeriods的列表是任何供應的一部分,我相信這樣做的邏輯應該是在SupplyEditor。

我在線程How to access PresenterWidget fields when added dynamically上得到了一些非常好的幫助,但那是在我完全實現了編輯器框架之前,所以我認爲邏輯在錯誤的地方。

任何幫助非常感謝。我可以發佈更多代碼(Presenter和View),但我不想讓它太難以閱讀,他們所做的只是從數據存儲獲取「供應」並在視圖上調用edit()。

我看過一些examples of ListEditor,但我並沒有真正明白!

回答

3

你需要一個ListEditor

這取決於你想如何展示他們實際的觀點,但同樣的想法適用於:

public class BillingPeriodListEditor implements isEditor<ListEditor<BillingPeriod,BillingPeriodEditor>>, HasRequestContext{ 
    private class BillingPeriodEditorSource extends EditorSource<BillingPeriodEditor>{ 
     @Override 
     public EmailsItemEditor create(final int index) { 
     // called each time u add or retrive new object on the list 
     // of the @ManyToOne or @ManyToMany 
     } 
     @Override 
     public void dispose(EmailsItemEditor subEditor) { 
     // called each time you remove the object from the list 
     } 
     @Override 
     public void setIndex(EmailsItemEditor editor, int index) { 
     // i would suggest track the index of the subeditor. 
     } 
    } 

    private ListEditor<BillingPeriod, BillingPeriodEditor> listEditor = ListEditor.of(new BillingPeriodEditorSource()); 

    // on add new one ... 
    // apply or request factory 
    // you must implement the HasRequestContext to 
    // call the create.(Proxy.class) 
    public void createNewBillingPeriod(){ 
     // create a new one then add to the list 
     listEditor.getList().add(...) 
    } 
} 

public class BillingPeriodEditor implements Editor<BillingPeriod>{ 
    // edit you BillingPeriod object 
} 

然後,在你實際的編輯器編輯的是路徑示例getBillingPeriods();

BillingPeriodListEditor billingPeriods = new BillingPeriodListEditor(); 


// latter on the clickhandler 
billingPeriods.createNewBillingPeriod() 

你現在就完成了。

+0

感謝您的回答,但我不確定您最後兩行的含義。我試圖在SupplyEditor中實例化BillingPeriodsListEditor,但我很掙扎。我的供應對象有一個getBillingPeriods()函數,但是我在哪裏調用它? – slugmandrew

+0

您的BillingPeriodListEditor需要知道什麼時候創建一個新的BillingPeriod,在視圖上您可以添加一個添加按鈕,然後在列出的監視器中調用createNewBillPeriod。 –

+0

我只需要命名我的BillingPeriodsListEditor billingPeriodsEditor,然後驅動程序識別Supply的getBillingPeriods()方法。謝謝! – slugmandrew