您绝对正确,屏幕应该考虑实体继承以消除代码重复。我在这里分叉了示例项目,以演示如何使用框架来完成它。
customer-frame.xml
包含基本实体的字段和它的数据源:
<window xmlns="http://schemas.haulmont.com/cuba/window.xsd"
caption="msg://editCaption"
class="com.company.entityinheritance.gui.customer.CustomerFrame"
focusComponent="fieldGroup"
messagesPack="com.company.entityinheritance.gui.customer">
<dsContext>
<datasource id="customerDs"
class="com.company.entityinheritance.entity.Customer"
view="_local"/>
</dsContext>
<layout spacing="true">
<fieldGroup id="fieldGroup"
datasource="customerDs">
<column width="250px">
<field id="name"/>
<field id="email"/>
</column>
</fieldGroup>
</layout>
</window>
在CustomerFrame
控制器中有一个公共方法来为数据源设置一个实例:
public class CustomerFrame extends AbstractFrame {
@Inject
private Datasource<Customer> customerDs;
public void setCustomer(Customer customer) {
customerDs.setItem(customer);
}
}
公司编辑器company-edit.xml
包括框架而不是客户字段:
<window xmlns="http://schemas.haulmont.com/cuba/window.xsd"
caption="msg://editCaption"
class="com.company.entityinheritance.gui.company.CompanyEdit"
datasource="companyDs"
focusComponent="customerFrame"
messagesPack="com.company.entityinheritance.gui.company">
<dsContext>
<datasource id="companyDs"
class="com.company.entityinheritance.entity.Company"
view="_local"/>
</dsContext>
<layout expand="windowActions"
spacing="true">
<frame id="customerFrame"
screen="demo$Customer.frame"/>
<fieldGroup id="fieldGroup"
datasource="companyDs">
<column width="250px">
<field id="industry"/>
</column>
</fieldGroup>
<frame id="windowActions"
screen="editWindowActions"/>
</layout>
</window>
在 Company 编辑器控制器中,框架被注入并且一个已编辑的实例被传递给它:
public class CompanyEdit extends AbstractEditor<Company> {
@Inject
private CustomerFrame customerFrame;
@Override
protected void postInit() {
customerFrame.setCustomer(getItem());
}
}