GWT编辑器 - 如何根据集合添加相同类型的N个子编辑器

时间:2012-11-16 15:45:55

标签: java gwt gwt-editors

我有一个Supply对象,可以是ElecSupplyGasSupplysee related question)。

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

我现在需要根据该列表的内容实例化N个BillingPeriodEditor,并且对于我应该如何做而感到困惑。

我正在使用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 dynamically上得到了一些非常好的帮助,但那是在我实现编辑框架之前,所以我认为逻辑是在错误的地方。

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

我看了一些examples of ListEditor,但我真的不明白!

1 个答案:

答案 0 :(得分: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()

你现在完成了。