0

我有一个对象,Supply可以是ElecSupplyGasSupply请参阅相关问题)。

无论正在编辑哪个子类,它们都有一个BillingPeriods 列表。

我现在需要BillingPeriodEditor根据该列表的内容实例化 N 个 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 列表是任何 Supply 的一部分,我认为其逻辑应该在 SupplyEditor 中。

我在线程How to access PresenterWidget fields when added dynamic上得到了一些非常好的帮助,但那是在我完全实现编辑器框架之前,所以我认为逻辑在错误的地方。

非常感谢任何帮助。我可以发布更多代码(Presenter 和 View),但我不想让它太难阅读,他们所做的就是从数据存储中获取 Supply 并在 View 上调用 edit()。

我看过一些ListEditor 的例子,但我真的不明白!

4

1 回答 1

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
}

然后在你的实际编辑器中编辑路径 Example getBillingPeriods();

BillingPeriodListEditor billingPeriods = new BillingPeriodListEditor ();


// latter on the clickhandler
billingPeriods.createNewBillingPeriod()

你现在完成了。

于 2012-11-21T04:27:12.133 回答