【问题标题】:GWT Editors - how to add N sub-editors of the same type based on a CollectionGWT Editors - 如何基于一个Collection添加N个相同类型的子编辑器
【发布时间】:2012-11-16 15:45:55
【问题描述】:

我有一个对象Supply,它可以是ElecSupplyGasSupply (see related question)。

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

我现在需要根据该列表的内容实例化 N 个 BillingPeriodEditors,但我不知道该怎么做。

我正在使用 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,但我真的不明白!

【问题讨论】:

    标签: java gwt gwt-editors


    【解决方案1】:

    你需要一个 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()
    

    你现在完成了。

    【讨论】:

    • 感谢您的回答,但我不确定您所说的最后两行是什么意思。我试图在 SupplyEditor 中实例化 BillingPeriodsListEditor 但我很挣扎。我的 Supply 对象有一个 getBillingPeriods() 函数,但我在哪里调用它?
    • 您的 BillingPeriodListEditor 需要知道何时创建一个新的 BillingPeriod,您可以在视图上放置一个添加按钮,然后在列表编辑器中调用 createNewBillPeriod。
    • 我只需要将我的 BillingPeriodsListEditor 命名为 billingPeriodsEditor,然后驱动程序就会识别出 Supply 的 getBillingPeriods() 方法。谢谢!
    • 还有一个带有@UiHandler 的按钮来添加新的,效果很好:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多