Example usage for com.vaadin.data HasValue setReadOnly

List of usage examples for com.vaadin.data HasValue setReadOnly

Introduction

In this page you can find the example usage for com.vaadin.data HasValue setReadOnly.

Prototype

public void setReadOnly(boolean readOnly);

Source Link

Document

Sets the read-only mode of this HasValue to given mode.

Usage

From source file:org.jpos.qi.QIEntityView.java

License:Open Source License

public Layout createForm(final Object entity, String[] params, boolean isNew) {
    VerticalLayout profileLayout = new VerticalLayout();
    profileLayout.setMargin(true);//from   w  ww . java 2  s.  c  o  m
    profileLayout.setSpacing(true);

    //Add Back Button
    if (params.length <= 1 || !"profile".equals(params[1])) {
        Button back = new Button(getApp().getMessage("back"));
        back.addStyleName("borderless-colored");
        back.setIcon(VaadinIcons.ARROW_LEFT);
        back.addClickListener(event -> app.getNavigator().navigateTo(getGeneralRoute()));
        profileLayout.addComponent(back);
        profileLayout.setComponentAlignment(back, Alignment.MIDDLE_LEFT);
    }

    binder = new Binder<T>(clazz) {
        @Override
        public void setReadOnly(boolean readOnly) {
            super.setReadOnly(readOnly);
            if (readOnlyFields != null) {
                for (String fieldId : readOnlyFields) {
                    if (binder.getBinding(fieldId).isPresent()) {
                        HasValue field = binder.getBinding(fieldId).get().getField();
                        if ((field != null && !field.isEmpty()) || !field.isRequiredIndicatorVisible()) {
                            field.setReadOnly(true);
                            binder.bind(field, fieldId);
                        }
                    }

                }
            }
        }
    };
    bean = (T) entity;
    final Layout formLayout = createLayout();
    getHelper().setOriginalEntity(bean);
    binder.readBean((T) entity);
    binder.setReadOnly(true);
    profileLayout.addComponent(formLayout);

    HorizontalLayout footer = new HorizontalLayout();
    footer.addStyleName("footer");
    footer.setMargin(new MarginInfo(true, false, false, false));
    footer.setSpacing(true);
    formLayout.addComponent(footer);

    //Add Save, Remove & Cancel Buttons
    editBtn = new Button(app.getMessage("edit"));
    removeBtn = new Button(app.getMessage("remove"));
    saveBtn = new Button(app.getMessage("save"));
    cancelBtn = new Button(app.getMessage("cancel"));

    editBtn.addClickListener(event -> editClick(event, formLayout));
    editBtn.addStyleName("icon-edit");

    saveBtn.addClickListener(event -> saveClick(event, formLayout));
    saveBtn.setVisible(false);
    saveBtn.setStyleName("icon-ok");
    saveBtn.setClickShortcut(ShortcutAction.KeyCode.ENTER);

    removeBtn.addClickListener(event -> app.addWindow(new ConfirmDialog(app.getMessage("confirmTitle"),
            app.getMessage("removeConfirmationMessage"), confirm -> {
                if (confirm) {
                    removeEntity();
                }
            })));
    removeBtn.addStyleName("icon-trash");

    cancelBtn.addClickListener(event -> {
        if (isNew) {
            app.getNavigator().navigateTo(getGeneralRoute());
        } else {
            cancelClick(event, formLayout);
        }
    });

    cancelBtn.setClickShortcut(ShortcutAction.KeyCode.ESCAPE);
    cancelBtn.setVisible(false);
    cancelBtn.addStyleName("icon-cancel");

    if (canEdit()) {
        footer.addComponent(editBtn);
        footer.addComponent(saveBtn);
        footer.addComponent(cancelBtn);
        footer.setComponentAlignment(editBtn, Alignment.MIDDLE_RIGHT);
        footer.setComponentAlignment(saveBtn, Alignment.MIDDLE_RIGHT);
        footer.setComponentAlignment(cancelBtn, Alignment.MIDDLE_RIGHT);
    }
    if (canRemove()) {
        footer.addComponent(removeBtn);
        footer.setComponentAlignment(removeBtn, Alignment.MIDDLE_RIGHT);
    }
    if (isNew) {
        editBtn.click();
    }
    errorLabel = new Label();
    errorLabel.setVisible(false);
    errorLabel.setStyleName(ValoTheme.LABEL_FAILURE);
    profileLayout.addComponent(errorLabel);
    return profileLayout;
}