EntityLookupAction

EntityLookupAction is an entity picker action designed to select an entity instance from a lookup screen and set it to the entity picker.

The action is implemented by the io.jmix.ui.action.entitypicker.EntityLookupAction class and should be defined in XML using type="entity_lookup" 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 EntityLookupAction 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, EntityLookupAction opens the screen in THIS_TAB mode.

  • screenId - string id of the lookup screen to use. By default, EntityLookupAction uses either a screen, annotated with @PrimaryLookupScreen, or having identifier in the format of <entity_name>.browse, for example, demo_Customer.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="lookup" type="entity_lookup">
    <properties>
        <property name="openMode" value="DIALOG"/>
        <property name="screenClass" value="ui.ex1.screen.entity.customer.CustomerBrowse"/>
    </properties>
</action>

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

@Named("cEntityField.lookup")
private EntityLookupAction<Customer> entityLookupAction;

@Subscribe
public void onInit(InitEvent event) {
    entityLookupAction.setOpenMode(OpenMode.DIALOG);
    entityLookupAction.setScreenClass(CustomerBrowse.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 ScreenOptions object to be passed to the opened lookup screen. For example:

@Install(to = "cEntityField.lookup", subject = "screenOptionsSupplier")
private ScreenOptions cEntityFieldLookupScreenOptionsSupplier() {
    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 = "cEntityField.lookup", subject = "screenConfigurer")
private void cEntityFieldLookupScreenConfigurer(Screen screen) {
    ((CustomerBrowse) 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 collection of selected entities. The first item of the collection is set to the field. 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 = "cEntityField.lookup", subject = "selectValidator")
private boolean cEntityFieldLookupSelectValidator(LookupScreen.ValidationContext<Customer> validationContext) {
    boolean valid = validationContext.getSelectedItems().size() == 1;
    if (!valid) {
        notifications.create().withCaption("Select a single customer").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. The first item of the collection is set to the field. You can use this handler to transform the selection before setting the entity to the field. For example:

@Install(to = "cEntityField.lookup", subject = "transformation")
private Collection<Customer> cEntityFieldLookupTransformation(Collection<Customer> collection) {
    return reloadCustomers(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 = "cEntityField.lookup", subject = "afterCloseHandler")
private void cEntityFieldLookupAfterCloseHandler(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("cEntityField.lookup")
public void onCEntityFieldLookup(Action.ActionPerformedEvent event) {
    dialogs.createOptionDialog()
            .withCaption("Please confirm")
            .withMessage("Do you really want to select a customer?")
            .withActions(
                    new DialogAction(DialogAction.Type.YES)
                            .withHandler(e -> entityLookupAction.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:

@Autowired
private EntityPicker<Customer> cEntityField;

@Autowired
private ScreenBuilders screenBuilders;

@Subscribe("cEntityField.lookupAction")
public void onCEntityFieldLookupAction(Action.ActionPerformedEvent event) {
    screenBuilders.lookup(cEntityField)
            .withOpenMode(OpenMode.DIALOG)
            .withScreenClass(CustomerBrowse.class)
            .withSelectValidator(customerValidationContext -> {
                boolean valid = customerValidationContext.getSelectedItems().size() == 1;
                if (!valid) {
                    notifications.create().withCaption("Select a single customer").show();
                }
                return valid;

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