Editor ReadOnly Mode

I have been looking for this feature for a while now. Something similar to the Readonly role.
In my application I have a lot of entities that cannot be modified after they reached a certain “status”.
In CUBA 6 I just disabled the the fieldgroups, and all the detail(nested) table actions, which is a lot of work, because you also have disable the detail table edit screens.

Here is one simple universal solution for CUBA7:

Screen XML with standard window actions and one additional button: closeNoCommitBtn

...
<hbox id="editActions" spacing="true">
	<button id="windowCommitAndCloseBtn" action="windowCommitAndClose"/>
	<button id="windowCommitBtn" action="windowCommit"/>
	<button id="windowCloseBtn" action="windowClose"/>
	<button id="closeNoCommitBtn" caption="Close" icon="font-icon:CLOSE" invoke="onCloseNoCommitBtnClick"/>
</hbox>
...

Controller

...
@Inject
private Button windowCommitBtn, windowCloseBtn, windowCommitAndCloseBtn, closeNoCommitBtn;

@Subscribe
private void onBeforeShow(BeforeShowEvent event) {
	this.disableEdit(this.isUpdateAllowed());
}

/**
 * Close the window without changes
 */
@Subscribe
public void onCloseNoCommitBtnClick() {
	closeWithDiscard();
}

/**
 * Override preventUnsavedChanges. This prevents the framweork from asking to save changes.
 * This is necessary because, if the user uses the close X of the tab, the framework asks to save changes.
 * @param event
 */
@Override
protected void preventUnsavedChanges(BeforeCloseEvent event) {
	if (this.isUpdateAllowed()) { // Some function to check entity fields if update is allowed
		super.preventUnsavedChanges(event); // Call super method to work like normal
	}
	// else do nothing
}

/**
 * Enable disable edit based on the status
 */
private void disableEdit(boolean updateAllowed) {
	if (!updateAllowed) {
		// Hide the standard window buttons
		windowCommitAndCloseBtn.setVisible(false);
		windowCommitBtn.setVisible(false);
		windowCloseBtn.setVisible(false);
		// Disable the actions to prevent user from using shortcut CTRL+ENTER to save.
		this.getWindow().getAction(WINDOW_COMMIT_AND_CLOSE).setEnabled(false);
		this.getWindow().getAction(WINDOW_COMMIT).setEnabled(false);
	}
	// Disable/enable close without commit button
	closeNoCommitBtn.setVisible(!updateAllowed); // Only show this button when update is not allowed
}

I was looking into the code of preventUnsavedChanges and it calls a function isCheckForUnsavedChanges(). It would be nice if this was a setting you could turn on/off in the editor screen.
It would be even nicer if ReadOnly was a standard feature you could turn on for Editor Screens.

The reason I can’t do this wit security constraints, is there are still some actions you can do with the entity that are invoked through a button that does make ‘controlled’ changes to the entity.

Anyone have any other ideas?

Renato

1 Like

I agree, that having ReadOnly mode for editor screen out-of-the-box would be a good thing.

Comparing to your solution, I would like to have not only actions hidden and disabled, but also controls (fields, etc…) disabled.

Seems like an approach in MasterDetailScreen can be reused. I’ll try and post here later

Hi,

apparently this is on the roadmap for CUBA 7.2 as documented in this issue: Standard "View" action for tables · Issue #2270 · cuba-platform/cuba · GitHub

Implementation seems to be in progress / done already, so there is a chance this is covered in the next minor version.

Bye
Mario

1 Like

Looking forward to this feature !

Hi,

Since 7.2 is release, can anyone direct me to an example on how this can be used?

Thank you.

Hi,

you can look it up from the Petclinic example. It is implemented in the Owner Editor: https://demo10.cuba-platform.com/petclinic/#main/1/owners

owner-browse: cuba-petclinic/owner-browse.xml at master · cuba-platform/cuba-petclinic · GitHub

owner-edit: cuba-petclinic/owner-edit.xml at master · cuba-platform/cuba-petclinic · GitHub

Cheers
Mario

Hi Mario,

Thanks for the response.
I have taken a look at the examples that you have given.
However, it seems that the Edit screen is disabled by being triggered from the browse screen.

What if I wanted to directly go to an Edit screen disable it without a browse screen?
Do you have any experience with this case.

Once again, thank you

Hi Mario,

It’s ok. I resolve this using :
@Subscribe
public void onInit(InitEvent event) {
this.setReadOnly(true);
}
Thanks.