3.5.5.2.7. ExcludeAction

ExcludeAction is a list action designed to remove entity instances from a data container in UI. Unlike RemoveAction, ExcludeAction does not remove the selected instance from the database. It is required, for example, when working with a many-to-many collection.

The action is implemented by com.haulmont.cuba.gui.actions.list.ExcludeAction class and should be defined in XML using type="exclude" action’s attribute. You can configure common action parameters using XML attributes of the action element, see Declarative Actions for details. Below we describe parameters specific to the ExcludeAction class.

The following parameters can be set both in XML and in Java:

  • confirmation - boolean value specifying whether to show confirmation dialog before removing the selected entities. True by default.

  • confirmationMessage - confirmation dialog message. By default, it is taken from the main message pack using the dialogs.Confirmation.Remove key.

  • confirmationTitle - confirmation dialog title. By default, it is taken from the main message pack using the dialogs.Confirmation key.

For example, if you want to show a specific confirmation message, you can configure the action in 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>

Alternatively, you can inject the action into the screen controller and configure it using setters:

@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?");
}

Now let’s consider parameters that can be configured only in Java code. In order to generate correctly annotated method stubs for these parameters, use Handlers tab of the Component Inspector tool window in Studio.

  • afterActionPerformedHandler - a handler that is invoked after selected entities are removed. It accepts the event object that can be used to get the entities selected for removal. For example:

    @Install(to = "customersTable.exclude", subject = "afterActionPerformedHandler")
    private void customersTableExcludeAfterActionPerformedHandler(RemoveOperation.AfterActionPerformedEvent<Customer> event) {
        System.out.println("Removed " + event.getItems());
    }
  • actionCancelledHandler - a handler that is invoked when the remove operation is cancelled by user in the confirmation dialog. It accepts the event object that can be used to get the entities selected for removal. For example:

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

If you want to perform some checks or interact with the user before the action is executed, subscribe to the action’s ActionPerformedEvent and invoke execute() method of the action when needed. The action will be invoked with all parameters that you defined for it. In the example below, we show a custom confirmation dialog before executing the action:

@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();
}

You can also subscribe to ActionPerformedEvent and instead of invoking the action’s execute() method, use RemoveOperation API directly to remove the selected entities. In this case, you are ignoring all specific action parameters and behavior and using only its common parameters like caption, icon, etc. For example:

@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();
}