Java GWT编辑器-如何基于集合添加N个相同类型的子编辑器

Java GWT编辑器-如何基于集合添加N个相同类型的子编辑器,java,gwt,gwt-editors,Java,Gwt,Gwt Editors,我有一个对象,Supply,它可以是ElecSupply或GasSupply() 无论编辑哪个子类,它们都有一个BillingPeriods的列表 现在,我需要根据该列表的内容实例化N个billingperiodiditors,我很困惑该如何做 我正在使用GWTP。以下是我刚开始工作的供应编辑器的代码: public class SupplyEditor extends Composite implements ValueAwareEditor<Supply> { priva

我有一个对象,
Supply
,它可以是
ElecSupply
GasSupply
()

无论编辑哪个子类,它们都有一个
BillingPeriod
s的列表

现在,我需要根据该列表的内容实例化N个
billingperiodiditor
s,我很困惑该如何做

我正在使用GWTP。以下是我刚开始工作的
供应编辑器的代码:

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();
        }
    }
}
公共类SupplyEditor扩展复合实现ValueAwareEditor
{
私有静态SupplyEditorUiBinder uiBinder=GWT.create(SupplyEditorUiBinder.class);
接口SupplyEditorUiBinder扩展了UiBinder
{
}
@忽略
最终ElecSupplyEditor ElecSupplyEditor=新ElecSupplyEditor();
@路径(“”)
最终抽象子类型编辑器elecSupplyEditorWrapper=新的抽象子类型编辑器(
elecSupplyEditor)
{
@凌驾
公共无效设置值(最终供应值)
{
设置值(ElecSupply的值、值实例);
如果(!(ElecSupply的值实例))
{
showGasFields();
}
其他的
{
showlecfields();
}
}
};
@忽略
最终GasSupplyEditor GasSupplyEditor=新GasSupplyEditor();
@路径(“”)
最终抽象子类型编辑器gasSupplyEditorWrapper=新的抽象子类型编辑器(
gasSupplyEditor)
{
@凌驾
公共无效设置值(最终供应值)
{
设置值(气体供应的值、值实例);
如果(!(气体供应值实例))
{
showlecfields();
}
其他的
{
showGasFields();
}
}
};
@尤菲尔德
面板电气面板、gasPanel、unitSection;
公共供应编辑器()
{
initWidget(uiBinder.createAndBindUi(this));
gasPanel.add(gasSupplyEditor);
添加(elecSupplyEditor);
}
//根据类型显示和隐藏的函数。。。
@凌驾
公共无效设置值(供应值)
{
if(ElecSupply的值实例)
{
showlecfields();
}
else if(气体供应的值实例)
{
showGasFields();
}
其他的
{
shownother();
}
}
}
现在,由于BillingPeriods列表是任何供应的一部分,因此我认为其逻辑应该在SupplyEditor中

我在这个线程上得到了一些非常好的帮助,但那是在我实现编辑器框架之前,所以我认为逻辑是错误的

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

我看了一些,但我真的不明白

您需要一个监听器

这取决于您希望如何在实际视图中呈现它们,但同样的想法也适用于:

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
}
您现在完成了。

您需要一个监听器

这取决于您希望如何在实际视图中呈现它们,但同样的想法也适用于:

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
}

您现在完成了。

谢谢您的回答,但我不确定最后两行是什么意思。我曾尝试在SupplyEditor中实例化BillingPeriodsListEditor,但我仍在努力。我的Supply对象有一个getBillingPeriods()函数,但我在哪里调用它?您的BillingPeriods listeditor需要知道何时创建新的BillingPeriod,您可以在视图上单击add按钮,然后在listeditor中调用createNewBillPeriod。我只需要将我的BillingPeriods listeditor命名为BillingPeriods listeditor,然后,驱动程序识别了getBillingPeriods()供应方法。谢谢还有一个带有@UiHandler的按钮,可以添加新的,效果很好:)谢谢你的回答,但我不知道最后两行是什么意思。我曾尝试在SupplyEditor中实例化BillingPeriodsListEditor,但我仍在努力。我的Supply对象有一个getBillingPeriods()函数,但我在哪里调用它?您的BillingPeriods listeditor需要知道何时创建新的BillingPeriod,您可以在视图上单击add按钮,然后在listeditor中调用createNewBillPeriod。我只需要将我的BillingPeriods listeditor命名为BillingPeriods listeditor,然后,驱动程序识别了getBillingPeriods()供应方法。谢谢还有一个带有@UiHandler的按钮,可以添加新的,效果很好:)