3.5.3.4.2. 编程式创建数据组件

可以使用编程的方式在可视化组件中创建和使用数据组件。

下面的例子中,创建了跟前一章一样的编辑界面,使用了相同的数据和可视化组件,只不过是用纯 Java 实现的。

  1. package com.company.sales.web.order;
  2. import com.company.sales.entity.Customer;
  3. import com.company.sales.entity.Order;
  4. import com.company.sales.entity.OrderLine;
  5. import com.haulmont.cuba.core.global.View;
  6. import com.haulmont.cuba.gui.UiComponents;
  7. import com.haulmont.cuba.gui.components.*;
  8. import com.haulmont.cuba.gui.components.data.options.ContainerOptions;
  9. import com.haulmont.cuba.gui.components.data.table.ContainerTableItems;
  10. import com.haulmont.cuba.gui.components.data.value.ContainerValueSource;
  11. import com.haulmont.cuba.gui.model.*;
  12. import com.haulmont.cuba.gui.screen.PrimaryEditorScreen;
  13. import com.haulmont.cuba.gui.screen.StandardEditor;
  14. import com.haulmont.cuba.gui.screen.Subscribe;
  15. import com.haulmont.cuba.gui.screen.UiController;
  16. import javax.inject.Inject;
  17. import java.sql.Date;
  18. @UiController("sales_Order.edit")
  19. public class OrderEdit extends StandardEditor<Order> {
  20. @Inject
  21. private DataComponents dataComponents; (1)
  22. @Inject
  23. private UiComponents uiComponents;
  24. private InstanceContainer<Order> orderDc;
  25. private CollectionPropertyContainer<OrderLine> linesDc;
  26. private CollectionContainer<Customer> customersDc;
  27. private InstanceLoader<Order> orderDl;
  28. private CollectionLoader<Customer> customersDl;
  29. @Subscribe
  30. protected void onInit(InitEvent event) {
  31. createDataComponents();
  32. createUiComponents();
  33. }
  34. private void createDataComponents() {
  35. DataContext dataContext = dataComponents.createDataContext();
  36. getScreenData().setDataContext(dataContext); (2)
  37. orderDc = dataComponents.createInstanceContainer(Order.class);
  38. orderDl = dataComponents.createInstanceLoader();
  39. orderDl.setContainer(orderDc); (3)
  40. orderDl.setDataContext(dataContext); (4)
  41. orderDl.setView("order-edit");
  42. linesDc = dataComponents.createCollectionContainer(
  43. OrderLine.class, orderDc, "lines"); (5)
  44. customersDc = dataComponents.createCollectionContainer(Customer.class);
  45. customersDl = dataComponents.createCollectionLoader();
  46. customersDl.setContainer(customersDc);
  47. customersDl.setDataContext(dataContext);
  48. customersDl.setQuery("select e from sales_Customer e"); (6)
  49. customersDl.setView(View.MINIMAL);
  50. }
  51. private void createUiComponents() {
  52. DateField<Date> dateField = uiComponents.create(DateField.TYPE_DATE);
  53. getWindow().add(dateField);
  54. dateField.setValueSource(new ContainerValueSource<>(orderDc, "date")); (7)
  55. Form form = uiComponents.create(Form.class);
  56. getWindow().add(form);
  57. LookupPickerField<Customer> customerField = uiComponents.create(LookupField.of(Customer.class));
  58. form.add(customerField);
  59. customerField.setValueSource(new ContainerValueSource<>(orderDc, "customer"));
  60. customerField.setOptions(new ContainerOptions<>(customersDc)); (8)
  61. TextField<Integer> amountField = uiComponents.create(TextField.TYPE_INTEGER);
  62. amountField.setValueSource(new ContainerValueSource<>(orderDc, "amount"));
  63. Table<OrderLine> table = uiComponents.create(Table.of(OrderLine.class));
  64. getWindow().add(table);
  65. getWindow().expand(table);
  66. table.setItems(new ContainerTableItems<>(linesDc)); (9)
  67. Button okButton = uiComponents.create(Button.class);
  68. okButton.setAction(getWindow().getActionNN(WINDOW_COMMIT_AND_CLOSE));
  69. getWindow().add(okButton);
  70. Button cancelButton = uiComponents.create(Button.class);
  71. cancelButton.setAction(getWindow().getActionNN(WINDOW_CLOSE));
  72. getWindow().add(cancelButton);
  73. }
  74. @Override
  75. protected InstanceContainer<Order> getEditedEntityContainer() { (10)
  76. return orderDc;
  77. }
  78. @Subscribe
  79. protected void onBeforeShow(BeforeShowEvent event) { (11)
  80. orderDl.load();
  81. customersDl.load();
  82. }
  83. }
1DataComponents 是创建数据组件的工厂。
2DataContext 实例在界面注册,以便标准的提交动作能正常工作。
3orderDl 加载器会加载数据到 orderDc 容器。
4orderDl 加载器会合并加载的实体到数据上下文以便跟踪改动。
5linesDc 创建为属性容器。
6customersDl 加载器指定了一个查询语句。
7ContainerValueSource 用来绑定单一字段到容器。
8ContainerOptions 用来为查找控件提供选项。
9ContainerTableItems 用来绑定表格到容器。
10getEditedEntityContainer() 被重写了,用来指定容器,替代了 @EditedEntityContainer 注解。
11在界面展示前加载数据。平台会自动设置编辑实体的 id 到 orderDl