Search in sources :

Example 11 with GuiDevelopmentException

use of io.jmix.ui.GuiDevelopmentException in project jmix by jmix-framework.

the class ComponentLoaderHelper method loadValidator.

@SuppressWarnings("unchecked")
public static Consumer<?> loadValidator(Element validatorElement, ComponentLoader.Context context, ClassManager classManager) {
    String className = validatorElement.attributeValue("class");
    String scriptPath = validatorElement.attributeValue("script");
    String script = validatorElement.getText();
    Consumer<?> validator = null;
    if (StringUtils.isNotBlank(scriptPath) || StringUtils.isNotBlank(script)) {
        validator = new ScriptValidator(validatorElement, context.getMessageGroup());
    } else {
        Class aClass = classManager.findClass(className);
        if (aClass == null)
            throw new GuiDevelopmentException(String.format("Class %s is not found", className), context);
        if (!StringUtils.isBlank(context.getMessageGroup()))
            try {
                validator = (Consumer<?>) ReflectionHelper.newInstance(aClass, validatorElement, context.getMessageGroup());
            } catch (NoSuchMethodException e) {
            // 
            }
        if (validator == null) {
            try {
                validator = (Consumer<?>) ReflectionHelper.newInstance(aClass, validatorElement);
            } catch (NoSuchMethodException e) {
                try {
                    validator = (Consumer<?>) ReflectionHelper.newInstance(aClass);
                } catch (NoSuchMethodException e1) {
                // todo log warn
                }
            }
        }
        if (validator == null) {
            throw new GuiDevelopmentException(String.format("Validator class %s has no supported constructors", aClass), context);
        }
    }
    return validator;
}
Also used : Consumer(java.util.function.Consumer) GuiDevelopmentException(io.jmix.ui.GuiDevelopmentException)

Example 12 with GuiDevelopmentException

use of io.jmix.ui.GuiDevelopmentException in project jmix by jmix-framework.

the class ComponentLoaderHelper method loadFormatter.

@Nullable
public static Formatter<?> loadFormatter(Element element, ClassManager classManager, ComponentLoader.Context context) {
    Element formatterElement = element.element("formatter");
    if (formatterElement != null) {
        String className = formatterElement.attributeValue("class");
        if (StringUtils.isEmpty(className)) {
            throw new GuiDevelopmentException("Formatter's attribute 'class' is not specified", context);
        }
        Class<?> aClass = classManager.findClass(className);
        if (aClass == null) {
            throw new GuiDevelopmentException(String.format("Class %s is not found", className), context);
        }
        try {
            Constructor<?> constructor = aClass.getConstructor(Element.class);
            try {
                return (Formatter<?>) constructor.newInstance(formatterElement);
            } catch (Throwable e) {
                throw new GuiDevelopmentException(String.format("Unable to instantiate class %s: %s", className, e), context);
            }
        } catch (NoSuchMethodException e) {
            try {
                return (Formatter<?>) aClass.getDeclaredConstructor().newInstance();
            } catch (Exception e1) {
                throw new GuiDevelopmentException(String.format("Unable to instantiate class %s: %s", className, e1), context);
            }
        }
    } else {
        return null;
    }
}
Also used : Formatter(io.jmix.ui.component.formatter.Formatter) Element(org.dom4j.Element) GuiDevelopmentException(io.jmix.ui.GuiDevelopmentException) GuiDevelopmentException(io.jmix.ui.GuiDevelopmentException) Nullable(javax.annotation.Nullable)

Example 13 with GuiDevelopmentException

use of io.jmix.ui.GuiDevelopmentException in project jmix by jmix-framework.

the class DatasourceLoaderHelper method loadDatasource.

/**
 * Loads datasource with property and returns empty optional if datasource is not defined in the component or
 * it has already {@link ValueSource}
 *
 * @param componentId      component id
 * @param element          component descriptor
 * @param context          loader context
 * @param componentContext component loader context
 * @return optional with {@link DatasourceValueSource}
 */
public static Optional<DatasourceValueSource> loadDatasource(String componentId, Element element, ComponentLoader.Context context, ComponentLoaderContext componentContext) {
    String datasource = element.attributeValue("datasource");
    if (!StringUtils.isEmpty(datasource)) {
        if (componentContext.getDsContext() == null) {
            throw new IllegalStateException("'datasource' attribute can be used only in screens with 'dsContext' element. " + "In a screen with 'data' element use 'dataContainer' attribute.");
        }
        Datasource ds = componentContext.getDsContext().get(datasource);
        if (ds == null) {
            throw new GuiDevelopmentException(String.format("Datasource '%s' is not defined", datasource), context, "Component ID", componentId);
        }
        String property = loadProperty(componentId, element, context);
        return Optional.of(new DatasourceValueSource(ds, property));
    }
    return Optional.empty();
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) DatasourceValueSource(com.haulmont.cuba.gui.components.data.value.DatasourceValueSource) GuiDevelopmentException(io.jmix.ui.GuiDevelopmentException)

Example 14 with GuiDevelopmentException

use of io.jmix.ui.GuiDevelopmentException in project jmix by jmix-framework.

the class EmbeddedLoader method loadComponent.

@Override
public void loadComponent() {
    assignFrame(resultComponent);
    String relativeSrc = element.attributeValue("relativeSrc");
    if (StringUtils.isNotEmpty(relativeSrc)) {
        resultComponent.setRelativeSource(relativeSrc);
    }
    String srcAttr = element.attributeValue("src");
    if (srcAttr != null) {
        if (srcAttr.startsWith(URL_PREFIX)) {
            String src = srcAttr.substring(URL_PREFIX.length());
            URL targetUrl;
            try {
                targetUrl = new URL(src);
            } catch (MalformedURLException e) {
                throw new GuiDevelopmentException("Incorrect URL in Embedded src attribute", context, "src", srcAttr);
            }
            resultComponent.setType(Embedded.Type.BROWSER);
            resultComponent.setSource(targetUrl);
        } else if (srcAttr.startsWith(THEME_PREFIX)) {
            resultComponent.setSource(srcAttr);
        } else if (srcAttr.startsWith(FILE_PREFIX)) {
            String src = srcAttr.substring(FILE_PREFIX.length());
            resultComponent.setType(Embedded.Type.OBJECT);
            resultComponent.setSource(src);
        } else {
            throw new GuiDevelopmentException("Illegal src attribute value. 'url://' or 'file://' or theme:// prefix expected", context, "src", srcAttr);
        }
    }
    String typeAttribute = element.attributeValue("type");
    if (StringUtils.isNotEmpty(typeAttribute)) {
        Embedded.Type type = Embedded.Type.valueOf(typeAttribute);
        resultComponent.setType(type);
    }
    loadVisible(resultComponent, element);
    loadEnable(resultComponent, element);
    loadStyleName(resultComponent, element);
    loadHeight(resultComponent, element);
    loadWidth(resultComponent, element);
    loadAlign(resultComponent, element);
    loadCaption(resultComponent, element);
    loadDescription(resultComponent, element);
    loadContextHelp(resultComponent, element);
    loadIcon(resultComponent, element);
}
Also used : MalformedURLException(java.net.MalformedURLException) GuiDevelopmentException(io.jmix.ui.GuiDevelopmentException) Embedded(com.haulmont.cuba.gui.components.Embedded) URL(java.net.URL)

Example 15 with GuiDevelopmentException

use of io.jmix.ui.GuiDevelopmentException in project jmix by jmix-framework.

the class FieldGroupLoader method loadComponent.

@Override
public void loadComponent() {
    assignFrame(resultComponent);
    String fieldFactoryBean = element.attributeValue("fieldFactoryBean");
    if (StringUtils.isNotEmpty(fieldFactoryBean)) {
        FieldGroupFieldFactory fieldFactory = applicationContext.getBean(fieldFactoryBean, FieldGroupFieldFactory.class);
        resultComponent.setFieldFactory(fieldFactory);
    }
    assignXmlDescriptor(resultComponent, element);
    loadVisible(resultComponent, element);
    loadWidth(resultComponent, element);
    loadEditable(resultComponent, element);
    loadEnable(resultComponent, element);
    loadStyleName(resultComponent, element);
    loadCss(resultComponent, element);
    loadHtmlSanitizerEnabled(resultComponent, element);
    loadIcon(resultComponent, element);
    loadCaption(resultComponent, element);
    loadDescription(resultComponent, element);
    loadContextHelp(resultComponent, element);
    loadHeight(resultComponent, element);
    loadAlign(resultComponent, element);
    loadCaptionAlignment(resultComponent, element);
    loadFieldCaptionWidth(resultComponent, element);
    Datasource ds = loadDatasource(element);
    resultComponent.setDatasource(ds);
    if (element.elements("column").isEmpty()) {
        Iterable<FieldGroup.FieldConfig> rootFields = loadFields(resultComponent, element, ds, null);
        Iterable<FieldGroup.FieldConfig> dynamicAttributeFields = loadDynamicAttributeFields(ds);
        for (FieldGroup.FieldConfig field : dynamicAttributeFields) {
            if (resultComponent.getWidth() > 0 && field.getWidth() == null) {
                field.setWidth("100%");
            }
        }
        for (FieldGroup.FieldConfig field : Iterables.concat(rootFields, dynamicAttributeFields)) {
            resultComponent.addField(field);
        }
    } else {
        List<Element> columnElements = element.elements("column");
        List<Element> fieldElements = element.elements("field");
        if (fieldElements.size() > 0) {
            Map<String, Object> params = new HashMap<>();
            String fieldGroupId = resultComponent.getId();
            if (StringUtils.isNotEmpty(fieldGroupId)) {
                params.put("FieldGroup ID", fieldGroupId);
            }
            throw new GuiDevelopmentException("FieldGroup field elements should be placed within its column.", context, params);
        }
        resultComponent.setColumns(columnElements.size());
        int colIndex = 0;
        for (Element columnElement : columnElements) {
            String flex = columnElement.attributeValue("flex");
            if (StringUtils.isNotEmpty(flex)) {
                resultComponent.setColumnExpandRatio(colIndex, Float.parseFloat(flex));
            }
            String columnWidth = loadThemeString(columnElement.attributeValue("width"));
            Iterable<FieldGroup.FieldConfig> columnFields = loadFields(resultComponent, columnElement, ds, columnWidth);
            if (colIndex == 0) {
                columnFields = Iterables.concat(columnFields, loadDynamicAttributeFields(ds));
            }
            for (FieldGroup.FieldConfig field : columnFields) {
                resultComponent.addField(field, colIndex);
            }
            String columnFieldCaptionWidth = columnElement.attributeValue("fieldCaptionWidth");
            if (StringUtils.isNotEmpty(columnFieldCaptionWidth)) {
                if (columnFieldCaptionWidth.startsWith(MessageTools.MARK)) {
                    columnFieldCaptionWidth = loadResourceString(columnFieldCaptionWidth);
                }
                if (columnFieldCaptionWidth.endsWith("px")) {
                    columnFieldCaptionWidth = columnFieldCaptionWidth.substring(0, columnFieldCaptionWidth.indexOf("px"));
                }
                resultComponent.setFieldCaptionWidth(colIndex, Integer.parseInt(columnFieldCaptionWidth));
            }
            colIndex++;
        }
    }
    for (FieldGroup.FieldConfig field : resultComponent.getFields()) {
        if (!field.isCustom()) {
            if (!DynAttrUtils.isDynamicAttributeProperty(field.getProperty())) {
                // the following does not make sense for dynamic attributes
                loadValidators(resultComponent, field);
                loadRequired(resultComponent, field);
                loadEnable(resultComponent, field);
            }
            loadVisible(resultComponent, field);
            loadEditable(resultComponent, field);
        }
    }
    resultComponent.bind();
    ComponentContext componentContext = getComponentContext();
    for (FieldGroup.FieldConfig field : resultComponent.getFields()) {
        if (field.getXmlDescriptor() != null) {
            String generator = field.getXmlDescriptor().attributeValue("generator");
            if (generator != null) {
                componentContext.addInjectTask((boundContext, window) -> {
                    DeclarativeFieldGenerator fieldGenerator = new DeclarativeFieldGenerator(resultComponent, generator);
                    Component fieldComponent = fieldGenerator.generateField(field.getTargetDatasource(), field.getProperty());
                    field.setComponent(fieldComponent);
                });
            }
        }
    }
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) EmbeddedDatasource(com.haulmont.cuba.gui.data.EmbeddedDatasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) FieldGroup(com.haulmont.cuba.gui.components.FieldGroup) DeclarativeFieldGenerator(com.haulmont.cuba.gui.xml.DeclarativeFieldGenerator) Element(org.dom4j.Element) FieldGroupFieldFactory(com.haulmont.cuba.gui.components.FieldGroupFieldFactory) GuiDevelopmentException(io.jmix.ui.GuiDevelopmentException) Component(io.jmix.ui.component.Component)

Aggregations

GuiDevelopmentException (io.jmix.ui.GuiDevelopmentException)106 Element (org.dom4j.Element)42 FrameOwner (io.jmix.ui.screen.FrameOwner)16 Component (io.jmix.ui.component.Component)13 InstanceContainer (io.jmix.ui.model.InstanceContainer)11 ScreenData (io.jmix.ui.model.ScreenData)11 CollectionContainer (io.jmix.ui.model.CollectionContainer)9 MetaClass (io.jmix.core.metamodel.model.MetaClass)8 Action (io.jmix.ui.action.Action)7 BaseAction (io.jmix.ui.action.BaseAction)6 ParseException (java.text.ParseException)6 Date (java.util.Date)6 Nullable (javax.annotation.Nullable)6 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)5 ComponentLoader (io.jmix.ui.xml.layout.ComponentLoader)5 ArrayList (java.util.ArrayList)5 Datasource (com.haulmont.cuba.gui.data.Datasource)4 Formatter (io.jmix.ui.component.formatter.Formatter)4 JsonParser (com.google.gson.JsonParser)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)3