Search in sources :

Example 56 with Component

use of io.jmix.ui.component.Component in project jmix by jmix-framework.

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().resolveMetaPropertyPathOrNull(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 && DynAttrUtils.isDynamicAttributeProperty(propertyName)) {
        String attributeCode = DynAttrUtils.getAttributeCodeFromProperty(propertyName);
        getDynAttrMetadata().getAttributes(metaPropertyPath.getMetaClass()).stream().filter(attr -> Objects.equals(attributeCode, attr.getCode())).findFirst().ifPresent(attr -> {
            field.setCaption(getMessageBundleTools().getLocalizedValue(attr.getNameMsgBundle(), attr.getName()));
            field.setDescription(getMessageBundleTools().getLocalizedValue(attr.getDescriptionsMsgBundle(), attr.getDescription()));
        });
    } else {
        loadCaption(field, element);
        if (field.getCaption() == null) {
            field.setCaption(getDefaultCaption(field, targetDs));
        }
    }
    loadDescription(field, element);
    loadContextHelp(field, element);
    field.setXmlDescriptor(element);
    Formatter 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) MetaClass(io.jmix.core.metamodel.model.MetaClass) 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) Formatter(io.jmix.ui.component.formatter.Formatter) ComponentLoader(io.jmix.ui.xml.layout.ComponentLoader) Security(com.haulmont.cuba.core.global.Security) ComponentLoaderHelper(com.haulmont.cuba.gui.xml.data.ComponentLoaderHelper) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) DynAttrMetadata(io.jmix.dynattr.DynAttrMetadata) Frame(io.jmix.ui.component.Frame) BooleanUtils(org.apache.commons.lang3.BooleanUtils) StringUtils(org.apache.commons.lang3.StringUtils) EntityOp(io.jmix.core.security.EntityOp) Strings(com.google.common.base.Strings) Component(io.jmix.ui.component.Component) LayoutLoader(io.jmix.ui.xml.layout.loader.LayoutLoader) EmbeddedDatasource(com.haulmont.cuba.gui.data.EmbeddedDatasource) MetadataTools(io.jmix.core.MetadataTools) 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) DynAttrUtils(io.jmix.dynattr.DynAttrUtils) Screen(io.jmix.ui.screen.Screen) DeclarativeFieldGenerator(com.haulmont.cuba.gui.xml.DeclarativeFieldGenerator) Collectors(java.util.stream.Collectors) UiControllerUtils(io.jmix.ui.screen.UiControllerUtils) Consumer(java.util.function.Consumer) GuiDevelopmentException(io.jmix.ui.GuiDevelopmentException) AttributeDefinition(io.jmix.dynattr.AttributeDefinition) MsgBundleTools(io.jmix.dynattr.MsgBundleTools) EntityAttrAccess(com.haulmont.cuba.security.entity.EntityAttrAccess) FieldCaptionAlignment(com.haulmont.cuba.gui.components.FieldGroup.FieldCaptionAlignment) Element(org.dom4j.Element) UiComponents(com.haulmont.cuba.gui.UiComponents) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) MessageTools(io.jmix.core.MessageTools) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) LayoutLoader(io.jmix.ui.xml.layout.loader.LayoutLoader) DsContext(com.haulmont.cuba.gui.data.DsContext) FieldGroup(com.haulmont.cuba.gui.components.FieldGroup) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Formatter(io.jmix.ui.component.formatter.Formatter) Element(org.dom4j.Element) LegacyFrame(com.haulmont.cuba.gui.screen.compatibility.LegacyFrame) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) ComponentLoader(io.jmix.ui.xml.layout.ComponentLoader) MetaClass(io.jmix.core.metamodel.model.MetaClass) GuiDevelopmentException(io.jmix.ui.GuiDevelopmentException) Component(io.jmix.ui.component.Component)

Example 57 with Component

use of io.jmix.ui.component.Component in project jmix by jmix-framework.

the class DeclarativeFieldGenerator method generateField.

@Override
public Component generateField(Datasource datasource, String propertyId) {
    Frame frame = fieldGroup.getFrame();
    if (frame == null) {
        throw new IllegalStateException("Table should be attached to frame");
    }
    FrameOwner controller = frame.getFrameOwner();
    if (controller instanceof LegacyFragmentAdapter) {
        controller = ((LegacyFragmentAdapter) controller).getRealScreen();
    }
    Class<? extends FrameOwner> cCls = controller.getClass();
    Method exactMethod = getAccessibleMethod(cCls, methodName, Datasource.class, String.class);
    if (exactMethod != null) {
        checkGeneratorMethodResultType(exactMethod, frame);
        try {
            return (Component) exactMethod.invoke(controller, datasource, propertyId);
        } catch (Exception e) {
            throw new RuntimeException("Exception in declarative FieldGroup Field generator " + methodName, e);
        }
    }
    Method dsMethod = getAccessibleMethod(cCls, methodName, Datasource.class);
    if (dsMethod != null) {
        checkGeneratorMethodResultType(dsMethod, frame);
        try {
            return (Component) dsMethod.invoke(controller, datasource);
        } catch (Exception e) {
            throw new RuntimeException("Exception in declarative FieldGroup Field generator " + methodName, e);
        }
    }
    Method parameterLessMethod = getAccessibleMethod(cCls, methodName);
    if (parameterLessMethod != null) {
        checkGeneratorMethodResultType(parameterLessMethod, frame);
        try {
            return (Component) parameterLessMethod.invoke(controller);
        } catch (Exception e) {
            throw new RuntimeException("Exception in declarative FieldGroup Field generator " + methodName, e);
        }
    }
    String fieldGroupId = fieldGroup.getId() == null ? "" : fieldGroup.getId();
    throw new IllegalStateException(String.format("No suitable method named %s for column generator of table %s", methodName, fieldGroupId));
}
Also used : Frame(io.jmix.ui.component.Frame) FrameOwner(io.jmix.ui.screen.FrameOwner) MethodUtils.getAccessibleMethod(org.apache.commons.lang3.reflect.MethodUtils.getAccessibleMethod) Method(java.lang.reflect.Method) LegacyFragmentAdapter(com.haulmont.cuba.gui.components.compatibility.LegacyFragmentAdapter) Component(io.jmix.ui.component.Component) GuiDevelopmentException(io.jmix.ui.GuiDevelopmentException)

Example 58 with Component

use of io.jmix.ui.component.Component in project jmix by jmix-framework.

the class AppLoginWindow method initLocales.

protected void initLocales() {
    localesSelect.setOptionsMap(messageTools.getAvailableLocalesMap());
    localesSelect.setValue(app.getLocale());
    boolean localeSelectVisible = cubaProperties.isLocaleSelectVisible();
    localesSelect.setVisible(localeSelectVisible);
    // if old layout is used
    Component localesSelectLabel = getComponent("localesSelectLabel");
    if (localesSelectLabel != null) {
        localesSelectLabel.setVisible(localeSelectVisible);
    }
    localesSelect.addValueChangeListener(e -> {
        Locale selectedLocale = e.getValue();
        app.setLocale(selectedLocale);
        authInfoThreadLocal.set(new AuthInfo(loginField.getValue(), passwordField.getValue(), rememberMeCheckBox.getValue()));
        try {
            app.createTopLevelWindow();
        } finally {
            authInfoThreadLocal.set(null);
        }
    });
}
Also used : Locale(java.util.Locale) AuthInfo(com.haulmont.cuba.web.security.AuthInfo) Component(io.jmix.ui.component.Component)

Example 59 with Component

use of io.jmix.ui.component.Component in project jmix by jmix-framework.

the class CubaUiComponents method create.

@SuppressWarnings("unchecked")
@Override
public <T extends Component> T create(String name) {
    Class<? extends Component> componentClass = classes.get(name);
    if (componentClass == null) {
        throw new IllegalStateException(String.format("Can't find component class for '%s'", name));
    }
    Constructor<? extends Component> constructor;
    try {
        constructor = componentClass.getConstructor();
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(String.format("Unable to get constructor for '%s' component", name), e);
    }
    try {
        Component instance = constructor.newInstance();
        autowireContext(instance);
        initCompositeComponent(instance, componentClass);
        return (T) instance;
    } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
        throw new RuntimeException(String.format("Error creating the '%s' component instance", name), e);
    }
}
Also used : Component(io.jmix.ui.component.Component) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 60 with Component

use of io.jmix.ui.component.Component in project jmix by jmix-framework.

the class CubaUiComponents method initCompositeComponent.

protected void initCompositeComponent(Component instance, Class<? extends Component> componentClass) {
    if (!(instance instanceof CompositeComponent)) {
        return;
    }
    CompositeComponent compositeComponent = (CompositeComponent) instance;
    CompositeDescriptor descriptor = componentClass.getAnnotation(CompositeDescriptor.class);
    if (descriptor != null) {
        String descriptorPath = descriptor.value();
        if (!descriptorPath.startsWith("/")) {
            String packageName = getPackage(componentClass);
            if (StringUtils.isNotEmpty(packageName)) {
                String relativePath = packageName.replace('.', '/');
                descriptorPath = "/" + relativePath + "/" + descriptorPath;
            }
        }
        Component root = processCompositeDescriptor(componentClass, descriptorPath);
        CompositeComponentUtils.setRoot(compositeComponent, root);
    }
    CompositeComponent.CreateEvent event = new CompositeComponent.CreateEvent(compositeComponent);
    CompositeComponentUtils.fireEvent(compositeComponent, CompositeComponent.CreateEvent.class, event);
}
Also used : Component(io.jmix.ui.component.Component)

Aggregations

Component (io.jmix.ui.component.Component)81 GuiDevelopmentException (io.jmix.ui.GuiDevelopmentException)16 Datasource (com.haulmont.cuba.gui.data.Datasource)12 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)11 Test (org.junit.jupiter.api.Test)11 Element (org.dom4j.Element)10 Consumer (java.util.function.Consumer)9 ListComponent (com.haulmont.cuba.gui.components.ListComponent)8 MetaClass (io.jmix.core.metamodel.model.MetaClass)7 Action (io.jmix.ui.action.Action)7 HasValue (io.jmix.ui.component.HasValue)7 FrameOwner (io.jmix.ui.screen.FrameOwner)7 ArrayList (java.util.ArrayList)7 DatasourceImpl (com.haulmont.cuba.gui.data.impl.DatasourceImpl)6 FetchPlan (io.jmix.core.FetchPlan)6 UUID (java.util.UUID)6 Nullable (javax.annotation.Nullable)6 User (com.haulmont.cuba.core.model.common.User)5 DsBuilder (com.haulmont.cuba.gui.data.DsBuilder)5 Entity (io.jmix.core.Entity)5