use of io.jmix.ui.xml.layout.ComponentLoader in project jmix by jmix-framework.
the class ContainerLoader method createSubComponents.
protected void createSubComponents(ComponentContainer container, Element containerElement) {
LayoutLoader loader = getLayoutLoader();
for (Element subElement : containerElement.elements()) {
if (!isChildElementIgnored(subElement)) {
ComponentLoader componentLoader = loader.createComponent(subElement);
pendingLoadComponents.add(componentLoader);
container.add(componentLoader.getResultComponent());
}
}
}
use of io.jmix.ui.xml.layout.ComponentLoader 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;
}
use of io.jmix.ui.xml.layout.ComponentLoader in project jmix by jmix-framework.
the class InputDialogFacetProvider method loadField.
@Nullable
protected Field<?> loadField(String paramId, Element element, ComponentLoader.ComponentContext context) {
List<Element> elements = element.elements();
if (elements.size() == 0) {
return null;
} else if (elements.size() > 1) {
throw new GuiDevelopmentException(String.format("InputParameter '%s' element cannot contain " + "two or more custom field definitions", paramId), context);
}
Element customFieldElement = elements.get(0);
ComponentLoader loader = getLayoutLoader(context).createComponent(customFieldElement);
io.jmix.ui.component.Component component = loader.getResultComponent();
// Check field type before loading attributes
if (!(component instanceof Field)) {
throw new GuiDevelopmentException(String.format("InputParameter '%s' custom field must implement " + "io.jmix.ui.component.Field", paramId), context);
}
loader.loadComponent();
return ((Field<?>) component);
}
use of io.jmix.ui.xml.layout.ComponentLoader in project jmix by jmix-framework.
the class AccordionLoader method createComponent.
@Override
public void createComponent() {
resultComponent = createComponentInternal();
loadId(resultComponent, element);
LayoutLoader layoutLoader = getLayoutLoader();
List<Element> tabElements = element.elements("tab");
for (Element tabElement : tabElements) {
String name = tabElement.attributeValue("id");
boolean lazy = Boolean.parseBoolean(tabElement.attributeValue("lazy"));
ComponentLoader tabComponentLoader = layoutLoader.getLoader(tabElement, TabComponentLoader.class);
Accordion.Tab tab;
if (lazy) {
tab = resultComponent.addLazyTab(name, tabElement, tabComponentLoader);
} else {
tabComponentLoader.createComponent();
Component tabComponent = tabComponentLoader.getResultComponent();
tab = resultComponent.addTab(name, tabComponent);
pendingLoadComponents.add(tabComponentLoader);
}
pendingLoadTabs.put(tabElement, tab);
}
}
use of io.jmix.ui.xml.layout.ComponentLoader in project jmix by jmix-framework.
the class FormLoader method loadComponent.
protected ComponentPosition loadComponent(Element element, @Nullable String columnWidth, @Nullable Float flex) {
Component component;
if ("field".equals(element.getName())) {
component = loadField(element);
} else {
LayoutLoader loader = getLayoutLoader();
ComponentLoader childComponentLoader = loader.createComponent(element);
childComponentLoader.loadComponent();
component = childComponentLoader.getResultComponent();
}
// Set default width
String componentWidth = element.attributeValue("width");
if (Strings.isNullOrEmpty(componentWidth)) {
if (columnWidth != null) {
component.setWidth(columnWidth);
} else if (flex != null) {
component.setWidthFull();
}
}
// Set default caption
if (component instanceof HasValueSource && ((HasValueSource) component).getValueSource() instanceof EntityValueSource && component instanceof Component.HasCaption && ((Component.HasCaption) component).getCaption() == null) {
EntityValueSource valueSource = ((EntityValueSource) ((HasValueSource) component).getValueSource());
MetaPropertyPath metaPropertyPath = valueSource.getMetaPropertyPath();
String propertyName = metaPropertyPath != null ? metaPropertyPath.getMetaProperty().getName() : null;
if (metaPropertyPath != null) {
MetaClass propertyMetaClass = getMetadataTools().getPropertyEnclosingMetaClass(metaPropertyPath);
String propertyCaption = getMessageTools().getPropertyCaption(propertyMetaClass, propertyName);
((Component.HasCaption) component).setCaption(propertyCaption);
}
}
int colspan = loadSpan(element, "colspan");
int rowspan = loadSpan(element, "rowspan");
return new ComponentPosition(component, colspan, rowspan);
}
Aggregations