新建

CreateAction 是一个 列表操作,用来创建新的实体实例。能创建新实例并使用新创建的实例打开实体编辑界面。在编辑界面保存了实体实例之后,会将其添加至 UI 组件的数据容器。

该操作通过 io.jmix.ui.action.list.CreateAction 类实现,在 XML 中需要使用操作属性 type="create" 定义。可以用 action 元素的 XML 属性定义通用的操作参数,参阅 声明式操作 了解细节。下面我们介绍 CreateAction 类特有的参数。

属性

下列参数可以在 XML 或 Java 中设置:

  • openMode - 编辑界面的打开模式,是 OpenMode 枚举类型值:NEW_TABDIALOG 等。默认情况下,CreateActionTHIS_TAB 模式打开编辑界面。

  • screenId - 编辑界面的字符串 id。CreateAction 默认会使用带有 @PrimaryEditorScreen 注解的界面,或标识符为 <entity_name>.edit 格式的界面,例如,demo_Customer.edit

  • screenClass - 编辑界面控制器的 Java 类。比 screenId 有更高的优先级。

示例,需要以对话框方式打开一个特定的编辑界面,可以在 XML 中这样配置操作:

<action id="create" type="create">
    <properties>
        <property name="openMode" value="DIALOG"/>
        <property name="screenClass" value="ui.ex1.screen.entity.customer.CustomerEdit"/>
    </properties>
</action>

或者,可以在界面控制器注入该操作,然后用 setter 配置:

@Named("customersGroupTable.create")
private CreateAction<Customer> createAction;

@Subscribe
public void onInit(InitEvent event) {
    createAction.setOpenMode(OpenMode.DIALOG);
    createAction.setScreenClass(CustomerEdit.class);
}

处理器

下面了解这些只能用 Java 配置的参数。用 Studio 能为这些参数正确生成带注解的方法。

screenOptionsSupplier

返回 ScreenOptions 对象,返回值可以传递给打开的编辑界面。示例:

@Install(to = "custTable.create", subject = "screenOptionsSupplier")
private ScreenOptions custTableCreateScreenOptionsSupplier() {
    return new MapScreenOptions(ParamsMap.of("someParameter", 10));
}

返回的 ScreenOptions 对象可以在打开界面的 InitEvent 中使用。

screenConfigurer

接收编辑界面作为参数并能在打开之前初始化界面。示例:

@Install(to = "custTable.create", subject = "screenConfigurer")
private void custTableCreateScreenConfigurer(Screen screen) {
    ((CustomerEdit) screen).setSomeParameter(10);
}

注意,界面 configurer 会在界面已经初始化但是还未显示时生效,即在界面的 InitEventAfterInitEvent 事件之后,但是在 BeforeShowEvent 之前。

newEntitySupplier

返回新实体实例,用于在编辑界面展示。示例:

@Install(to = "custTable.create", subject = "newEntitySupplier")
private Customer custTableCreateNewEntitySupplier() {
    Customer customer = metadata.create(Customer.class);
    customer.setFirstName("Sean");
    customer.setLastName("White");
    return customer;
}

initializer

接收新实体实例作为参数并能在展示到编辑界面之前对其做初始化。示例:

@Install(to = "custTable.create", subject = "initializer")
private void custTableCreateInitializer(Customer customer) {
    customer.setFirstName("Abel");
    customer.setLastName("Higgins");
}

transformation

在编辑界面选择实体并验证之后调用。接收选中的实体作为参数。可以用这个处理器对选中的实体做一些处理,然后再传递给接收的数据容器。示例:

@Install(to = "custTable.create", subject = "transformation")
private Customer custTableCreateTransformation(Customer customer) {
    return reloadCustomer(customer);
}

afterCommitHandler

当实体实例在编辑界面提交之后会调用。接收创建的实体作为参数。示例:

@Install(to = "custTable.create", subject = "afterCommitHandler")
private void custTableCreateAfterCommitHandler(Customer customer) {
    System.out.println("Created " + customer);
}

afterCloseHandler

在编辑界面关闭后调用。AfterCloseEvent 事件会传递给该处理器。示例:

@Install(to = "custTable.create", subject = "afterCloseHandler")
private void custTableCreateAfterCloseHandler(AfterCloseEvent afterCloseEvent) {
    if (afterCloseEvent.closedWith(StandardOutcome.COMMIT)) {
        System.out.println("Committed");
    }
}

使用 ActionPerformedEvent

如果需要在该操作执行前做一些检查或者与用户做一些交互,可以订阅操作的 ActionPerformedEvent 事件并按需调用操作的 execute() 方法。操作会使用你为它定义的所有参数进行调用。下面的例子中,我们在执行操作前展示了一个确认对话框:

@Named("custTable.create")
private CreateAction<Customer> custTableCreate;

@Subscribe("custTable.create")
public void onCustTableCreate(Action.ActionPerformedEvent event) {
    dialogs.createOptionDialog()
            .withCaption("Please confirm")
            .withMessage("Do you really want to create new customer?")
            .withActions(
                    new DialogAction(DialogAction.Type.YES)
                            .withHandler(e -> custTableCreate.execute()), // execute action
                    new DialogAction(DialogAction.Type.NO)
            )
            .show();
}

另外,还可以先订阅 ActionPerformedEvent,但是不调用操作的 execute() 方法,而是使用 ScreenBuilders API 直接打开查找界面。此时,会忽略所有特殊的操作参数和行为,只能用其通用参数,比如 captionicon 等。示例:

@Subscribe("customersTable.create")
public void onCustomersTableCreate(Action.ActionPerformedEvent event) {
    screenBuilders.editor(customersTable)
            .newEntity()
            .withOpenMode(OpenMode.DIALOG)
            .withScreenClass(CustomerEdit.class)
            .withAfterCloseListener(afterScreenCloseEvent -> {
                if (afterScreenCloseEvent.closedWith(StandardOutcome.COMMIT)) {
                    Customer committedCustomer = (afterScreenCloseEvent.getSource()).getEditedEntity();
                    System.out.println("Created " + committedCustomer);
                }
            })
            .build()
            .show();
}