3.5.5.2.8. LookupAction

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

The action is implemented by com.haulmont.cuba.gui.actions.picker.LookupAction class and should be defined in XML using type="picker_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 LookupAction class.

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, LookupAction opens the screen in THIS_TAB mode.

  • screenId - string id of the lookup screen to use. By default, LookupAction uses either a screen, annotated with @PrimaryLookupScreen, or having identifier in the format of <entity_name>.lookup or <entity_name>.browse, e.g. demo_Customer.browse.

  • screenClass - Java class of the lookup screen controller to use. It has 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="picker_lookup">
    <properties>
        <property name="openMode" value="DIALOG"/>
        <property name="screenClass" value="com.company.sales.web.customer.CustomerBrowse"/>
    </properties>
</action>

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

@Named("customerField.lookup")
private LookupAction customerFieldLookup;

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

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.

  • screenOptionsSupplier - a handler that returns ScreenOptions object to be passed to the opened lookup screen. For example:

    @Install(to = "customerField.lookup", subject = "screenOptionsSupplier")
    private ScreenOptions customerFieldLookupScreenOptionsSupplier() {
        return new MapScreenOptions(ParamsMap.of("someParameter", 10));
    }

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

  • screenConfigurer - a handler that accepts the lookup screen and can initialize it before opening. For example:

    @Install(to = "customerField.lookup", subject = "screenConfigurer")
    private void customerFieldLookupScreenConfigurer(Screen screen) {
        ((CustomerBrowse) screen).setSomeParameter(10);
    }

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

  • selectValidator - 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 = "customerField.lookup", subject = "selectValidator")
    private boolean customerFieldLookupSelectValidator(LookupScreen.ValidationContext<Customer> validationContext) {
        boolean valid = validationContext.getSelectedItems().size() == 1;
        if (!valid) {
            notifications.create().withCaption("Select a single customer").show();
        }
        return valid;
    }
  • transformation - 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 = "customerField.lookup", subject = "transformation")
    private Collection<Customer> customerFieldLookupTransformation(Collection<Customer> collection) {
        return reloadCustomers(collection);
    }
  • afterCloseHandler - a handler that is invoked after the lookup screen is closed. AfterCloseEvent is passed to the handler. For example:

    @Install(to = "customerField.lookup", subject = "afterCloseHandler")
    private void customerFieldLookupAfterCloseHandler(AfterCloseEvent event) {
        if (event.closedWith(StandardOutcome.SELECT)) {
            System.out.println("Selected");
        }
    }

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

@Named("customerField.lookup")
private LookupAction customerFieldLookup;

@Subscribe("customerField.lookup")
public void onCustomerFieldLookup(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 -> customerFieldLookup.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:

@Inject
private ScreenBuilders screenBuilders;
@Inject
private LookupPickerField<Customer> customerField;

@Subscribe("customerField.lookup")
public void onCustomerFieldLookup(Action.ActionPerformedEvent event) {
    screenBuilders.lookup(customerField)
            .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();
}