View action from browse

Hi

I need add to browse new action which show edit window in view mode.
In this mode all fields are no editable. That would be lovely to create this action to all browse. Is there any way to simple create this functionality.

Andrzej

Hi,

You can define an additional global action that extends EditAction, for example, something like this:

public class ReadOnlyViewAction extends EditAction {

    public static final String ACTION_ID = "readOnlyView";

    public ReadOnlyViewAction(ListComponent target) {
        super(target);
    }

    public ReadOnlyViewAction(ListComponent target, WindowManager.OpenType openType) {
        super(target, WindowManager.OpenType.DIALOG);
    }

    public ReadOnlyViewAction(ListComponent target, WindowManager.OpenType openType, String id) {
        super(target, WindowManager.OpenType.DIALOG, ACTION_ID);
    }

    @Override
    public void setCaption(String caption) {
        super.setCaption("Read-only View");
    }

    @Override
    public void actionPerform(Component component) {
        setWindowParams(ParamsMap.of("isReadOnly", true));
        super.actionPerform(component);
    }
}

Next, you can extend AbstractEditor and add some logic for disabling editable components, for example:

public class ReadOnlyOptionEditor<Y extends StandardEntity> extends AbstractEditor {

    private boolean isReadOnly = false;

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);
        if (params.get("isReadOnly") != null)
            isReadOnly = (boolean) params.get("isReadOnly");
    }

    @Override
    public void ready() {
        super.ready();
        if (isReadOnly) {
            for (Component component : getOwnComponents()) {
                if (component instanceof Editable) {
                    ((Editable) component).setEditable(false);
                }
            }
        }
    }
}

Now make the editor screens of your entities extend the new custom editor, for example:

public class CustomerEdit extends ReadOnlyOptionEditor<Customer> {
}

Finally, in the entity’s browser screen, add the new action to the table:

    @Override
    public void init(Map<String, Object> params) {
        customersTable.addAction(new ReadOnlyViewAction(customersTable, WindowManager.OpenType.DIALOG, ReadOnlyViewAction.ACTION_ID));
    }

Not very straightforward, but at least you don’t need to create an action in each screen controller individually.

demo.zip (86.9 KB)

2 Likes

Thanks to you for complet example. I done a little modification and set component enabled in edit controller by method setEnabled(false) like below

protected void postInit() {
	super.postInit();	
	if (viewmode)
	    setEnabled(false);
	}

Now I have other problem. The vertical scrollbar in inner browse is disable.

obraz

Is there any way change this feature ? I need enabled scrollbar in this component when component enabled parametr set false.

You cannot enable the scrollbar separately. In fact, you don’t even need to apply setEnabled(false) to the whole table. I would recommend to disable only table actions, thus the data will remain readable (not light-grey as in disabled mode), you will have your scrollbar, but the user will not be able to modify the data. Of course, this requires more complex logic than in my demo sample.

ok
I have last question. I add this action to browse action panel by code
below

Button viewButton = componentsFactory.createComponent(Button.class);
				ViewAction va = new ViewAction(table, OpenType.DIALOG);
				va.setCaption(messages.getMessage("com.haulmont.cuba.gui.components", "view"));
				viewButton.setAction(va);
				viewButton.setCaption(messages.getMessage("com.haulmont.cuba.gui.components", "view"));
				viewButton.setIconFromSet(CubaIcon.EYE);
				table.getButtonsPanel().add(viewButton);

Now I’d like add shortcut. I done it in ViewAction

@Override
	public void setShortcut(String shortcut) {
		super.setShortcut("CTRL-ENTER");
	}

but this method invoke doesn’t work in my case.

This method is not invoked by the framework. Call it explicitly like va.setShortcut("CTRL-ENTER") after creation of the action or in its constructor.

I invoke this method after create action, but this way doesn’t work.