3.5.5.2.7. ExcludeAction

ExcludeAction 是 列表操作 设计用来从 UI 的数据容器中移除实体实例。与 RemoveAction 不同,ExcludeAction 不会从数据库删除选中的实例。在处理多对多集合的时候,需要该操作。

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

下列参数可以通过 XML 或 Java 的方式设置:

  • confirmation - boolean 值,配置是否需要在移除选中实体之前展示确认对话框。默认为 true。

  • confirmationMessage - 确认窗口的消息。默认从主消息包的 dialogs.Confirmation.Remove 键值获取。

  • confirmationTitle - 确认窗口标题。默认从主消息包的 dialogs.Confirmation 键值获取。

比如,希望展示特殊的确认消息,可以在 XML 中如下配置:

<action id="exclude" type="exclude">
    <properties>
        <property name="confirmation" value="true"/>
        <property name="confirmationTitle" value="Removing customer..."/>
        <property name="confirmationMessage" value="Do you really want to remove the customer from the list?"/>
    </properties>
</action>

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

@Named("customersTable.exclude")
private ExcludeAction customersTableExclude;

@Subscribe
public void onInit(InitEvent event) {
    customersTableExclude.setConfirmation(true);
    customersTableExclude.setConfirmationTitle("Removing customer...");
    customersTableExclude.setConfirmationMessage("Do you really want to remove the customer from the list?");
}

现在我们看看那些只能用 Java 代码配置的参数。如果要为这些参数生成带正确注解的方法桩代码,可以用 Studio 中 Component Inspector 工具窗口的 Handlers 标签页功能。

  • afterActionPerformedHandler - 在选中实体移除之后调用的处理器。接收事件对象,可以用来获取那些选中要移除的实体。示例:

    @Install(to = "customersTable.exclude", subject = "afterActionPerformedHandler")
    private void customersTableExcludeAfterActionPerformedHandler(RemoveOperation.AfterActionPerformedEvent<Customer> event) {
        System.out.println("Removed " + event.getItems());
    }
  • actionCancelledHandler - 当用户在确认窗口取消移除操作时会调用的方法。接收事件对象,可以用来获取那些选中要移除的实体。示例:

    @Install(to = "customersTable.exclude", subject = "actionCancelledHandler")
    private void customersTableExcludeActionCancelledHandler(RemoveOperation.ActionCancelledEvent<Customer> event) {
        System.out.println("Cancelled");
    }

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

@Named("customersTable.exclude")
private ExcludeAction customersTableExclude;

@Subscribe("customersTable.exclude")
public void onCustomersTableExclude(Action.ActionPerformedEvent event) {
    customersTableExclude.setConfirmation(false);
    dialogs.createOptionDialog()
            .withCaption("My fancy confirm dialog")
            .withMessage("Do you really want to remove the customer from the list?")
            .withActions(
                    new DialogAction(DialogAction.Type.YES)
                            .withHandler(e -> customersTableExclude.execute()), // execute action
                    new DialogAction(DialogAction.Type.NO)
            )
            .show();
}

另外,还可以先订阅 ActionPerformedEvent,但是不调用操作的 execute() 方法,而是直接使用 RemoveOperation API 移除选中的实体。此时,会忽略所有的操作参数和行为,只能用其通用参数,比如 caption, icon 等。示例:

@Inject
private RemoveOperation removeOperation;

@Subscribe("customersTable.exclude")
public void onCustomersTableExclude(Action.ActionPerformedEvent event) {
    removeOperation.builder(customersTable)
            .withConfirmationMessage("Do you really want to remove the customer from the list?")
            .withConfirmationTitle("Removing customer...")
            .exclude();
}