Search in sources :

Example 1 with ComponentLoader

use of com.haulmont.cuba.gui.xml.layout.ComponentLoader in project cuba by cuba-platform.

the class AbstractComponentLoader method getLoader.

protected ComponentLoader getLoader(Element element, Class<? extends ComponentLoader> loaderClass) {
    ComponentLoader loader;
    try {
        Constructor<? extends ComponentLoader> constructor = loaderClass.getConstructor();
        loader = constructor.newInstance();
        loader.setLocale(locale);
        loader.setMessagesPack(messagesPack);
        loader.setContext(context);
        loader.setLayoutLoaderConfig(layoutLoaderConfig);
        loader.setFactory(factory);
        loader.setElement(element);
    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
        throw new GuiDevelopmentException("Loader instatiation error: " + e, context.getFullFrameId());
    }
    return loader;
}
Also used : GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ComponentLoader(com.haulmont.cuba.gui.xml.layout.ComponentLoader)

Example 2 with ComponentLoader

use of com.haulmont.cuba.gui.xml.layout.ComponentLoader in project cuba by cuba-platform.

the class ContainerLoader method createSubComponents.

protected void createSubComponents(Component.Container container, Element containerElement) {
    LayoutLoader loader = new LayoutLoader(context, factory, layoutLoaderConfig);
    loader.setLocale(getLocale());
    loader.setMessagesPack(getMessagesPack());
    // noinspection unchecked
    for (Element subElement : (Collection<Element>) containerElement.elements()) {
        if (!isChildElementIgnored(subElement)) {
            ComponentLoader componentLoader = loader.createComponent(subElement);
            pendingLoadComponents.add(componentLoader);
            container.add(componentLoader.getResultComponent());
        }
    }
}
Also used : LayoutLoader(com.haulmont.cuba.gui.xml.layout.LayoutLoader) Element(org.dom4j.Element) Collection(java.util.Collection) ComponentLoader(com.haulmont.cuba.gui.xml.layout.ComponentLoader)

Example 3 with ComponentLoader

use of com.haulmont.cuba.gui.xml.layout.ComponentLoader in project cuba by cuba-platform.

the class WindowManager method createLayout.

protected ComponentLoader createLayout(WindowInfo windowInfo, Element rootElement, ComponentLoader.Context context, LayoutLoaderConfig layoutConfig) {
    String descriptorPath = windowInfo.getTemplate();
    LayoutLoader layoutLoader = new LayoutLoader(context, AppConfig.getFactory(), layoutConfig);
    layoutLoader.setLocale(getLocale());
    if (!StringUtils.isEmpty(descriptorPath)) {
        if (descriptorPath.contains("/")) {
            descriptorPath = StringUtils.substring(descriptorPath, 0, descriptorPath.lastIndexOf("/"));
        }
        String path = descriptorPath.replaceAll("/", ".");
        int start = path.startsWith(".") ? 1 : 0;
        path = path.substring(start);
        layoutLoader.setMessagesPack(path);
    }
    // noinspection UnnecessaryLocalVariable
    ComponentLoader windowLoader = layoutLoader.createWindow(rootElement, windowInfo.getId());
    return windowLoader;
}
Also used : LayoutLoader(com.haulmont.cuba.gui.xml.layout.LayoutLoader) ComponentLoader(com.haulmont.cuba.gui.xml.layout.ComponentLoader)

Example 4 with ComponentLoader

use of com.haulmont.cuba.gui.xml.layout.ComponentLoader in project cuba by cuba-platform.

the class WebTabSheet method addLazyTab.

@Override
public TabSheet.Tab addLazyTab(String name, Element descriptor, ComponentLoader loader) {
    // todo replace
    ComponentsFactory cf = AppBeans.get(ComponentsFactory.NAME);
    CssLayout tabContent = cf.createComponent(CssLayout.NAME);
    tabContent.setStyleName("c-tabsheet-lazytab");
    tabContent.setSizeFull();
    Tab tab = new Tab(name, tabContent);
    tabs.put(name, tab);
    com.vaadin.ui.Component tabComponent = tabContent.unwrapComposition(com.vaadin.ui.Component.class);
    tabMapping.put(tabComponent, new ComponentDescriptor(name, tabContent));
    com.vaadin.ui.TabSheet.Tab tabControl = this.component.addTab(tabComponent);
    getLazyTabs().add(tabComponent);
    this.component.addSelectedTabChangeListener(new LazyTabChangeListener(tabContent, descriptor, loader));
    context = loader.getContext();
    if (!postInitTaskAdded && context instanceof ComponentLoader.ComponentContext) {
        ((ComponentLoader.ComponentContext) context).addPostInitTask((c, w) -> initComponentTabChangeListener());
        postInitTaskAdded = true;
    }
    if (getDebugId() != null) {
        this.component.setTestId(tabControl, AppUI.getCurrent().getTestIdManager().getTestId(getDebugId() + "." + name));
    }
    if (AppUI.getCurrent().isTestMode()) {
        this.component.setCubaId(tabControl, name);
    }
    if (context instanceof ComponentLoader.ComponentContext) {
        tabContent.setFrame(((ComponentLoader.ComponentContext) context).getFrame());
    } else {
        throw new IllegalStateException("'context' must implement " + "com.haulmont.cuba.gui.xml.layout.ComponentLoader.ComponentContext");
    }
    return tab;
}
Also used : ComponentLoader(com.haulmont.cuba.gui.xml.layout.ComponentLoader) ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) CubaTabSheet(com.haulmont.cuba.web.widgets.CubaTabSheet)

Example 5 with ComponentLoader

use of com.haulmont.cuba.gui.xml.layout.ComponentLoader in project cuba by cuba-platform.

the class FieldGroupLoader method loadField.

protected FieldGroup.FieldConfig loadField(Element element, Datasource ds, String columnWidth) {
    String id = element.attributeValue("id");
    String property = element.attributeValue("property");
    if (Strings.isNullOrEmpty(id) && Strings.isNullOrEmpty(property)) {
        throw new GuiDevelopmentException(String.format("id/property is not defined for field of FieldGroup '%s'. " + "Set id or property attribute.", resultComponent.getId()), context);
    }
    if (Strings.isNullOrEmpty(property)) {
        property = id;
    } else if (Strings.isNullOrEmpty(id)) {
        id = property;
    }
    Datasource targetDs = ds;
    Datasource datasource = loadDatasource(element);
    if (datasource != null) {
        targetDs = datasource;
    }
    CollectionDatasource optionsDs = null;
    String optDsName = element.attributeValue("optionsDatasource");
    if (StringUtils.isNotBlank(optDsName)) {
        LegacyFrame frame = (LegacyFrame) getComponentContext().getFrame().getFrameOwner();
        DsContext dsContext = frame.getDsContext();
        optionsDs = findDatasourceRecursively(dsContext, optDsName);
        if (optionsDs == null) {
            throw new GuiDevelopmentException(String.format("Options datasource %s not found for field %s", optDsName, id), context);
        }
    }
    boolean customField = false;
    String custom = element.attributeValue("custom");
    if (StringUtils.isNotEmpty(custom)) {
        customField = Boolean.parseBoolean(custom);
    }
    if (StringUtils.isNotEmpty(element.attributeValue("generator"))) {
        customField = true;
    }
    List<Element> elements = element.elements();
    List<Element> customElements = elements.stream().filter(e -> !("formatter".equals(e.getName()) || "validator".equals(e.getName()))).collect(Collectors.toList());
    if (!customElements.isEmpty()) {
        if (customElements.size() > 1) {
            throw new GuiDevelopmentException(String.format("FieldGroup field %s element cannot contains two or more custom field definitions", id), context);
        }
        if (customField) {
            throw new GuiDevelopmentException(String.format("FieldGroup field %s cannot use both custom/generator attribute and inline component definition", id), context);
        }
        customField = true;
    }
    if (!customField && targetDs == null) {
        throw new GuiDevelopmentException(String.format("Datasource is not defined for FieldGroup field '%s'. " + "Only custom fields can have no datasource.", property), context);
    }
    FieldGroup.FieldConfig field = resultComponent.createField(id);
    if (property != null) {
        field.setProperty(property);
    }
    if (datasource != null) {
        field.setDatasource(datasource);
    }
    if (optionsDs != null) {
        field.setOptionsDatasource(optionsDs);
    }
    String stylename = element.attributeValue("stylename");
    if (StringUtils.isNotEmpty(stylename)) {
        field.setStyleName(stylename);
    }
    MetaPropertyPath metaPropertyPath = null;
    if (targetDs != null && property != null) {
        MetaClass metaClass = targetDs.getMetaClass();
        metaPropertyPath = getMetadataTools().resolveMetaPropertyPath(targetDs.getMetaClass(), property);
        if (metaPropertyPath == null) {
            if (!customField) {
                throw new GuiDevelopmentException(String.format("Property '%s' is not found in entity '%s'", property, metaClass.getName()), context);
            }
        }
    }
    String propertyName = metaPropertyPath != null ? metaPropertyPath.getMetaProperty().getName() : null;
    if (metaPropertyPath != null && DynamicAttributesUtils.isDynamicAttribute(metaPropertyPath.getMetaProperty())) {
        CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaPropertyPath.getMetaProperty());
        field.setCaption(categoryAttribute != null ? categoryAttribute.getLocaleName() : propertyName);
        field.setDescription(categoryAttribute != null ? categoryAttribute.getLocaleDescription() : null);
    } else {
        loadCaption(field, element);
        if (field.getCaption() == null) {
            field.setCaption(getDefaultCaption(field, targetDs));
        }
    }
    loadDescription(field, element);
    loadContextHelp(field, element);
    field.setXmlDescriptor(element);
    Function formatter = loadFormatter(element);
    if (formatter != null) {
        field.setFormatter(formatter);
    }
    String defaultWidth = element.attributeValue("width");
    if (StringUtils.isEmpty(defaultWidth)) {
        defaultWidth = columnWidth;
    }
    loadWidth(field, defaultWidth);
    if (customField) {
        field.setCustom(true);
    }
    String required = element.attributeValue("required");
    if (StringUtils.isNotEmpty(required)) {
        field.setRequired(Boolean.parseBoolean(required));
    }
    String requiredMsg = element.attributeValue("requiredMessage");
    if (requiredMsg != null) {
        requiredMsg = loadResourceString(requiredMsg);
        field.setRequiredMessage(requiredMsg);
    }
    String tabIndex = element.attributeValue("tabIndex");
    if (StringUtils.isNotEmpty(tabIndex)) {
        field.setTabIndex(Integer.parseInt(tabIndex));
    }
    loadInputPrompt(field, element);
    if (customElements.size() == 1) {
        // load nested component defined as inline
        Element customFieldElement = customElements.get(0);
        LayoutLoader loader = getLayoutLoader();
        ComponentLoader childComponentLoader = loader.createComponent(customFieldElement);
        childComponentLoader.loadComponent();
        Component customComponent = childComponentLoader.getResultComponent();
        String inlineAttachMode = element.attributeValue("inlineAttachMode");
        if (StringUtils.isNotEmpty(inlineAttachMode)) {
            field.setComponent(customComponent, FieldGroup.FieldAttachMode.valueOf(inlineAttachMode));
        } else {
            field.setComponent(customComponent);
        }
    }
    return field;
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) EmbeddedDatasource(com.haulmont.cuba.gui.data.EmbeddedDatasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Iterables(com.google.common.collect.Iterables) java.util(java.util) FieldGroupFieldFactory(com.haulmont.cuba.gui.components.FieldGroupFieldFactory) Datasource(com.haulmont.cuba.gui.data.Datasource) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) DynamicAttributesUtils(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils) BooleanUtils(org.apache.commons.lang3.BooleanUtils) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) MetaClass(com.haulmont.chile.core.model.MetaClass) Strings(com.google.common.base.Strings) MetadataTools(com.haulmont.cuba.core.global.MetadataTools) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) EmbeddedDatasource(com.haulmont.cuba.gui.data.EmbeddedDatasource) Preconditions.checkNotNullArgument(com.haulmont.bali.util.Preconditions.checkNotNullArgument) Component(com.haulmont.cuba.gui.components.Component) FieldGroup(com.haulmont.cuba.gui.components.FieldGroup) DsContext(com.haulmont.cuba.gui.data.DsContext) Nullable(javax.annotation.Nullable) LegacyFrame(com.haulmont.cuba.gui.screen.compatibility.LegacyFrame) LayoutLoader(com.haulmont.cuba.gui.xml.layout.LayoutLoader) MetaProperty(com.haulmont.chile.core.model.MetaProperty) DeclarativeFieldGenerator(com.haulmont.cuba.gui.xml.DeclarativeFieldGenerator) Collectors(java.util.stream.Collectors) ComponentLoader(com.haulmont.cuba.gui.xml.layout.ComponentLoader) Consumer(java.util.function.Consumer) EntityOp(com.haulmont.cuba.security.entity.EntityOp) EntityAttrAccess(com.haulmont.cuba.security.entity.EntityAttrAccess) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) FieldCaptionAlignment(com.haulmont.cuba.gui.components.FieldGroup.FieldCaptionAlignment) Element(org.dom4j.Element) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) MessageTools(com.haulmont.cuba.core.global.MessageTools) LayoutLoader(com.haulmont.cuba.gui.xml.layout.LayoutLoader) DsContext(com.haulmont.cuba.gui.data.DsContext) FieldGroup(com.haulmont.cuba.gui.components.FieldGroup) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Element(org.dom4j.Element) LegacyFrame(com.haulmont.cuba.gui.screen.compatibility.LegacyFrame) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) ComponentLoader(com.haulmont.cuba.gui.xml.layout.ComponentLoader) Function(java.util.function.Function) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) Component(com.haulmont.cuba.gui.components.Component)

Aggregations

ComponentLoader (com.haulmont.cuba.gui.xml.layout.ComponentLoader)14 LayoutLoader (com.haulmont.cuba.gui.xml.layout.LayoutLoader)10 Element (org.dom4j.Element)9 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)4 Component (com.haulmont.cuba.gui.components.Component)4 MetaClass (com.haulmont.chile.core.model.MetaClass)2 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)2 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)2 DsContext (com.haulmont.cuba.gui.data.DsContext)2 UIPerformanceLogger (com.haulmont.cuba.gui.logging.UIPerformanceLogger)2 ComponentsFactory (com.haulmont.cuba.gui.xml.layout.ComponentsFactory)2 ComponentLoaderContext (com.haulmont.cuba.gui.xml.layout.loaders.ComponentLoaderContext)2 StopWatch (org.perf4j.StopWatch)2 Slf4JStopWatch (org.perf4j.slf4j.Slf4JStopWatch)2 Strings (com.google.common.base.Strings)1 Iterables (com.google.common.collect.Iterables)1 Preconditions.checkNotNullArgument (com.haulmont.bali.util.Preconditions.checkNotNullArgument)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 DynamicAttributesUtils (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils)1 DynamicAttributesUtils.getCategoryAttribute (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils.getCategoryAttribute)1