AddAction

AddAction is a list action designed to add existing entity instances to a data container by selecting them in a lookup screen. For example, you can use it to fill a many-to-many collection.

The action is implemented by the io.jmix.ui.action.list.AddAction class and should be defined in XML using type="add" 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 AddAction class.

Properties

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

  • openMode - the lookup screen opening mode as a value of the OpenMode enum: NEW_TAB, DIALOG, etc. By default, AddAction opens the lookup screen in THIS_TAB mode.

  • screenId - string id of the lookup screen to use. By default, AddAction uses either a screen, annotated with @PrimaryLookupScreen, or having identifier in the format of <entity_name>.browse, for example, demo_Order.browse.

  • screenClass - Java class of the lookup screen controller to use. It has a higher priority than screenId.

For example, if you want to open a specific lookup screen as a dialog, you can configure the action in XML:

<action id="add" type="add">
    <properties>
        <property name="openMode" value="DIALOG"/>
        <property name="screenClass" value="ui.ex1.screen.entity.brand.BrandBrowse"/>
    </properties>
</action>

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

@Named("brandsTable.add")
private AddAction<Brand> addAction;

@Subscribe
public void onInitEntity(InitEntityEvent<Customer> event) {
    addAction.setOpenMode(OpenMode.DIALOG);
    addAction.setScreenClass(BrandBrowse.class);
}

Handlers

Now let’s consider parameters that can be configured only in Java code. To generate correctly annotated method stubs for these parameters, use Studio.

screenOptionsSupplier

It is a handler that returns the ScreenOptions object to be passed to the opened lookup screen. For example:

@Install(to = "brandsTable.add", subject = "screenOptionsSupplier")
private ScreenOptions brandsTableAddScreenOptionsSupplier() {
    return new MapScreenOptions(ParamsMap.of("someParameter", 10));
}

The returned ScreenOptions object will be available in the InitEvent of the opened screen.

screenConfigurer

It is a handler that accepts the lookup screen and can initialize it before opening. For example:

@Install(to = "brandsTable.add", subject = "screenConfigurer")
private void brandsTableAddScreenConfigurer(Screen screen) {
    ((BrandBrowse) screen).setSomeParameter(10);
}

Note that screen configurer comes into play when the screen is already initialized but not yet shown, that is, after its InitEvent and AfterInitEvent and before BeforeShowEvent are sent.

selectValidator

It is a handler that is invoked when the user clicks Select in the lookup screen. It accepts the object that contains the selected entities. You can use this handler to check if the selection matches some criteria. The handler must return true to proceed and close the lookup screen. For example:

@Install(to = "brandsTable.add", subject = "selectValidator")
private boolean brandsTableAddSelectValidator(LookupScreen.ValidationContext<Brand> validationContext) {
    boolean valid = checkBrands(validationContext.getSelectedItems());
    if (!valid) {
        notifications.create().withCaption("Selection is not valid").show();
    }
    return valid;
}

transformation

It is a handler that is invoked after entities are selected and validated in the lookup screen. It accepts the collection of selected entities. You can use this handler to transform the selection before adding entities to the receiving data container. For example:

@Install(to = "brandsTable.add", subject = "transformation")
private Collection<Brand> brandsTableAddTransformation(Collection<Brand> collection) {
    return reloadBrands(collection);
}

afterCloseHandler

It is a handler that is invoked after the lookup screen is closed. AfterCloseEvent is passed to the handler. For example:

@Install(to = "brandsTable.add", subject = "afterCloseHandler")
private void brandsTableAddAfterCloseHandler(AfterCloseEvent afterCloseEvent) {
    if (afterCloseEvent.closedWith(StandardOutcome.SELECT)) {
        System.out.println("Selected");
    }
}

Using ActionPerformedEvent

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 the 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 confirmation dialog before executing the action:

@Subscribe("brandsTable.add")
public void onBrandsTableAdd(Action.ActionPerformedEvent event) {
    dialogs.createOptionDialog()
            .withCaption("Please confirm")
            .withMessage("Do you really want to add a brand?")
            .withActions(
                    new DialogAction(DialogAction.Type.YES)
                            .withHandler(e -> addAction.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 ScreenBuilders API directly to open the lookup screen. In this case, you are ignoring all specific action parameters and behavior and using only its common parameters like caption, icon, etc. For example:

@Subscribe("brandsTable.addAction")
public void onBrandsTableAddAction(Action.ActionPerformedEvent event) {
    screenBuilders.lookup(brandsTable)
            .withOpenMode(OpenMode.DIALOG)
            .withScreenClass(BrandBrowse.class)
            .withSelectValidator(customerValidationContext -> {
                boolean valid = checkBrands(customerValidationContext.getSelectedItems());
                if (!valid) {
                    notifications.create().withCaption("Selection is not valid").show();
                }
                return valid;

            })
            .build()
            .show();
}