use of com.haulmont.cuba.gui.xml.layout.LayoutLoader 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());
}
}
}
use of com.haulmont.cuba.gui.xml.layout.LayoutLoader 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.getFullFrameId());
}
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)) {
DsContext dsContext = getContext().getFrame().getDsContext();
optionsDs = findDatasourceRecursively(dsContext, optDsName);
if (optionsDs == null) {
throw new GuiDevelopmentException(String.format("Options datasource %s not found for field %s", optDsName, id), context.getFullFrameId());
}
}
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 = Dom4j.elements(element);
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.getCurrentFrameId());
}
if (customField) {
throw new GuiDevelopmentException(String.format("FieldGroup field %s cannot use both custom/generator attribute and inline component definition", id), context.getCurrentFrameId());
}
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.getFullFrameId());
}
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 = metadataTools.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.getFullFrameId());
}
}
}
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);
} else {
loadCaption(field, element);
if (field.getCaption() == null) {
field.setCaption(getDefaultCaption(field, targetDs));
}
}
loadDescription(field, element);
loadContextHelp(field, element);
field.setXmlDescriptor(element);
com.haulmont.cuba.gui.components.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 = new LayoutLoader(context, factory, layoutLoaderConfig);
loader.setLocale(getLocale());
loader.setMessagesPack(getMessagesPack());
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 com.haulmont.cuba.gui.xml.layout.LayoutLoader in project cuba by cuba-platform.
the class GridLayoutLoader method createSubComponents.
protected void createSubComponents(GridLayout gridLayout, Element element, int row) {
LayoutLoader loader = new LayoutLoader(context, factory, layoutLoaderConfig);
loader.setLocale(getLocale());
loader.setMessagesPack(getMessagesPack());
int col = 0;
// noinspection unchecked
for (Element subElement : (Collection<Element>) element.elements()) {
ComponentLoader componentLoader = loader.createComponent(subElement);
pendingLoadComponents.add(componentLoader);
Component subComponent = componentLoader.getResultComponent();
String colspan = subElement.attributeValue("colspan");
String rowspan = subElement.attributeValue("rowspan");
if (col >= spanMatrix.length) {
Map<String, Object> params = new HashMap<>();
params.put("Grid ID", gridLayout.getId());
String rowId = element.attributeValue("id");
if (StringUtils.isNotEmpty(rowId)) {
params.put("Row ID", rowId);
} else {
params.put("Row Index", row);
}
throw new GuiDevelopmentException("Grid column count is less than number of components in grid row", context.getFullFrameId(), params);
}
while (spanMatrix[col][row]) {
col++;
}
if (StringUtils.isEmpty(colspan) && StringUtils.isEmpty(rowspan)) {
addSubComponent(gridLayout, subComponent, col, row, col, row);
} else {
int cspan = 1;
int rspan = 1;
if (StringUtils.isNotEmpty(colspan)) {
cspan = Integer.parseInt(colspan);
if (cspan < 1) {
throw new GuiDevelopmentException("GridLayout colspan can not be less than 1", context.getFullFrameId(), "colspan", cspan);
}
if (cspan == 1) {
LoggerFactory.getLogger(GridLayoutLoader.class).warn("Do not use colspan=\"1\", it will have no effect");
}
}
if (StringUtils.isNotEmpty(rowspan)) {
rspan = Integer.parseInt(rowspan);
if (rspan < 1) {
throw new GuiDevelopmentException("GridLayout rowspan can not be less than 1", context.getFullFrameId(), "rowspan", rspan);
}
if (rspan == 1) {
LoggerFactory.getLogger(GridLayoutLoader.class).warn("Do not use rowspan=\"1\", it will have no effect");
}
}
fillSpanMatrix(col, row, cspan, rspan);
int endColumn = col + cspan - 1;
int endRow = row + rspan - 1;
addSubComponent(gridLayout, subComponent, col, row, endColumn, endRow);
}
col++;
}
}
use of com.haulmont.cuba.gui.xml.layout.LayoutLoader in project cuba by cuba-platform.
the class PopupViewLoader method createContent.
protected void createContent() {
if (element != null) {
LayoutLoader loader = new LayoutLoader(context, factory, layoutLoaderConfig);
loader.setLocale(getLocale());
loader.setMessagesPack(getMessagesPack());
List<Element> elements = Dom4j.elements(element);
if (elements.size() != 0) {
Element innerElement = elements.get(0);
if (innerElement != null) {
popupComponentLoader = loader.createComponent(innerElement);
resultComponent.setPopupContent(popupComponentLoader.getResultComponent());
}
}
}
}
use of com.haulmont.cuba.gui.xml.layout.LayoutLoader 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;
}
Aggregations