Search in sources :

Example 1 with BooleanDatatype

use of com.haulmont.chile.core.datatypes.impl.BooleanDatatype in project cuba by cuba-platform.

the class AppPropertiesEdit method init.

@Override
public void init(Map<String, Object> params) {
    cannotEditValueLabel.setVisible(item.getOverridden());
    Datatype datatype = Datatypes.get(item.getDataTypeName());
    fieldGroup.addCustomField("currentValue", (datasource, propertyId) -> {
        if (item.getOverridden()) {
            TextField textField = componentsFactory.createComponent(TextField.class);
            textField.setValue(item.getDisplayedCurrentValue());
            textField.setEditable(false);
            return textField;
        }
        if (item.getEnumValues() != null) {
            return createLookupField(Arrays.asList(item.getEnumValues().split(",")), item.getCurrentValue());
        } else {
            if (datatype instanceof BooleanDatatype) {
                return createLookupField(Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()), item.getCurrentValue());
            } else {
                if (Boolean.TRUE.equals(item.getSecret())) {
                    PasswordField passwordField = componentsFactory.createComponent(PasswordField.class);
                    passwordField.setValue(item.getCurrentValue());
                    passwordField.addValueChangeListener(e -> {
                        appPropertyDs.getItem().setCurrentValue(e.getValue() == null ? null : e.getValue().toString());
                    });
                    return passwordField;
                } else {
                    TextField textField = componentsFactory.createComponent(TextField.class);
                    textField.setValue(item.getCurrentValue());
                    try {
                        datatype.parse(item.getCurrentValue(), userSessionSource.getLocale());
                        textField.setDatatype(datatype);
                    } catch (ParseException e) {
                        // do not assign datatype then
                        log.trace("Localized parsing by datatype cannot be used for value {}", item.getCurrentValue());
                    }
                    textField.addValueChangeListener(e -> {
                        appPropertyDs.getItem().setCurrentValue(e.getValue() == null ? null : e.getValue().toString());
                    });
                    return textField;
                }
            }
        }
    });
    final Formatter<String> defaultValueFormatter = (value) -> {
        if (datatype instanceof BooleanDatatype) {
            return value;
        }
        try {
            Object parsedDefaultValue = datatype.parse(value);
            return datatype.format(parsedDefaultValue, userSessionSource.getLocale());
        } catch (ParseException e) {
            log.trace("Localized parsing by datatype cannot be used for value {}", value, e);
        }
        return value;
    };
    displayedDefaultValueField.setFormatter(defaultValueFormatter);
    appPropertyDs.setItem(metadata.getTools().copy(item));
}
Also used : Arrays(java.util.Arrays) Datasource(com.haulmont.cuba.gui.data.Datasource) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Datatypes(com.haulmont.chile.core.datatypes.Datatypes) BooleanDatatype(com.haulmont.chile.core.datatypes.impl.BooleanDatatype) ConfigStorageService(com.haulmont.cuba.core.app.ConfigStorageService) AppBeans(com.haulmont.cuba.core.global.AppBeans) UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) Metadata(com.haulmont.cuba.core.global.Metadata) Inject(javax.inject.Inject) ConfigurationClientImpl(com.haulmont.cuba.client.sys.ConfigurationClientImpl) List(java.util.List) Datatype(com.haulmont.chile.core.datatypes.Datatype) AppPropertyEntity(com.haulmont.cuba.core.config.AppPropertyEntity) ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) Configuration(com.haulmont.cuba.core.global.Configuration) WindowParam(com.haulmont.cuba.gui.WindowParam) Map(java.util.Map) Named(javax.inject.Named) ParseException(java.text.ParseException) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) BooleanDatatype(com.haulmont.chile.core.datatypes.impl.BooleanDatatype) ParseException(java.text.ParseException) BooleanDatatype(com.haulmont.chile.core.datatypes.impl.BooleanDatatype) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Aggregations

Datatype (com.haulmont.chile.core.datatypes.Datatype)1 Datatypes (com.haulmont.chile.core.datatypes.Datatypes)1 BooleanDatatype (com.haulmont.chile.core.datatypes.impl.BooleanDatatype)1 ConfigurationClientImpl (com.haulmont.cuba.client.sys.ConfigurationClientImpl)1 ConfigStorageService (com.haulmont.cuba.core.app.ConfigStorageService)1 AppPropertyEntity (com.haulmont.cuba.core.config.AppPropertyEntity)1 AppBeans (com.haulmont.cuba.core.global.AppBeans)1 Configuration (com.haulmont.cuba.core.global.Configuration)1 Metadata (com.haulmont.cuba.core.global.Metadata)1 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)1 WindowParam (com.haulmont.cuba.gui.WindowParam)1 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)1 Datasource (com.haulmont.cuba.gui.data.Datasource)1 ComponentsFactory (com.haulmont.cuba.gui.xml.layout.ComponentsFactory)1 ParseException (java.text.ParseException)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Inject (javax.inject.Inject)1 Named (javax.inject.Named)1