ViewAction
ViewAction
is a list action designed to view and edit entity instances. It opens the edit screen in the same way as EditAction, but it makes all fields non-editable and disables actions implementing interface ReadOnlyAwareScreen
. If you want to allow users to switch the screen to the editable mode, add a button and link it with the predefined enableEditing
action:
<hbox id="editActions" spacing="true">
<button id="commitAndCloseBtn" action="windowCommitAndClose"/>
<button id="closeBtn" action="windowClose"/>
<button action="enableEditing"/> (1)
</hbox>
1 | This button is shown only when the screen is in read-only mode. |
You can redefine the enableEditing
action caption in the main message pack using the actions.EnableEditing
key, or right in the screen by specifying the caption
attribute to the corresponding button.
ViewAction
is implemented by the io.jmix.ui.action.list.ViewAction
class and should be defined in XML using type="view"
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 ViewAction
class.
Properties
The following parameters can be set both in XML and in Java:
-
openMode
- the editor screen opening mode as a value of theOpenMode
enum:NEW_TAB
,DIALOG
, etc. By default,ViewAction
opens the editor inTHIS_TAB
mode. -
screenId
- string id of the editor screen to use. By default,ViewAction
uses either a screen, annotated with@PrimaryEditorScreen
, or having identifier in the format of<entity_name>.edit
, for example,demo_Customer.edit
. -
screenClass
- Java class of the editor screen controller to use. It has a higher priority thanscreenId
.
For example, if you want to open a specific editor screen as a dialog, you can configure the action in XML:
<action id="view" type="view">
<properties>
<property name="openMode" value="DIALOG"/>
<property name="screenClass" value="ui.ex1.screen.entity.customer.CustomerEdit"/>
</properties>
</action>
Alternatively, you can inject the action into the screen controller and configure it using setters:
@Named("custTable.view")
private ViewAction<Customer> viewAction;
@Subscribe
public void onInit(InitEvent event) {
viewAction.setOpenMode(OpenMode.DIALOG);
viewAction.setScreenClass(CustomerEdit.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 editor screen. For example:
@Install(to = "custTable.view", subject = "screenOptionsSupplier")
private ScreenOptions custTableViewScreenOptionsSupplier() {
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 editor screen and can initialize it before opening. For example:
@Install(to = "custTable.view", subject = "screenConfigurer")
private void custTableViewScreenConfigurer(Screen screen) {
((CustomerEdit) 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.
transformation
It is a handler that is invoked after the entity is selected and validated in the editor screen. It accepts the selected entity. You can use this handler to transform the committed entity (if enableEditing
action was executed) before setting it to the target data container. For example:
@Install(to = "custTable.view", subject = "transformation")
private Customer custTableViewTransformation(Customer customer) {
return reloadCustomer(customer);
}
afterCommitHandler
It is a handler that is invoked after the edited entity instance is committed in the editor screen if the user switched the screen to editable mode using enableEditing
action mentioned above. The handler method accepts the updated entity. For example:
@Install(to = "custTable.view", subject = "afterCommitHandler")
private void custTableViewAfterCommitHandler(Customer customer) {
System.out.println("Updated " + customer);
}
afterCloseHandler
It is a handler that is invoked after the editor screen is closed. AfterCloseEvent
is passed to the handler. For example:
@Install(to = "custTable.view", subject = "afterCloseHandler")
private void custTableViewAfterCloseHandler(AfterCloseEvent afterCloseEvent) {
if (afterCloseEvent.closedWith(StandardOutcome.COMMIT)) {
System.out.println("Enabled editing and then committed");
}
}
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:
@Autowired
private ScreenBuilders screenBuilders;
@Subscribe("custTable.view")
public void onCustTableView(Action.ActionPerformedEvent event) {
CustomerEdit customerEdit = screenBuilders.editor(custTable)
.withOpenMode(OpenMode.DIALOG)
.withScreenClass(CustomerEdit.class)
.withAfterCloseListener(afterScreenCloseEvent -> {
if (afterScreenCloseEvent.closedWith(StandardOutcome.COMMIT)) {
Customer committedCustomer = (afterScreenCloseEvent.getSource()).getEditedEntity();
System.out.println("Updated " + committedCustomer);
}
})
.build();
customerEdit.setReadOnly(true);
customerEdit.show();
}