use of com.haulmont.cuba.gui.components.form.ComponentPosition in project cuba by cuba-platform.
the class WebForm method calculateComponentAreas.
protected List<ComponentArea> calculateComponentAreas() {
List<ComponentArea> componentAreas = new ArrayList<>();
// Inspired by GridLayoutLoader logic
boolean[][] spanMatrix = new boolean[this.component.getColumns()][this.component.getRows()];
for (int col = 0; col < columnComponentMapping.size(); col++) {
int row = 0;
List<ComponentPosition> columnFields = columnComponentMapping.get(col);
for (ComponentPosition componentPosition : columnFields) {
while (spanMatrix[col][row]) {
row++;
}
Component component = componentPosition.getComponent();
int colSpan = componentPosition.getColSpan();
int rowSpan = componentPosition.getRowSpan();
if (colSpan == 1 && rowSpan == 1) {
componentAreas.add(new ComponentArea(component, col, row, col, row));
} else {
fillSpanMatrix(spanMatrix, col, row, colSpan, rowSpan);
int endColumn = col + colSpan - 1;
int endRow = row + rowSpan - 1;
componentAreas.add(new ComponentArea(component, col, row, endColumn, endRow));
}
row++;
}
}
return componentAreas;
}
use of com.haulmont.cuba.gui.components.form.ComponentPosition in project cuba by cuba-platform.
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) {
if (isDynamicAttribute(metaPropertyPath.getMetaProperty())) {
CategoryAttribute categoryAttribute = getCategoryAttribute(metaPropertyPath.getMetaProperty());
((Component.HasCaption) component).setCaption(categoryAttribute != null ? categoryAttribute.getLocaleName() : propertyName);
((Component.HasCaption) component).setDescription(categoryAttribute != null ? categoryAttribute.getLocaleDescription() : null);
} else {
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);
}
use of com.haulmont.cuba.gui.components.form.ComponentPosition in project cuba by cuba-platform.
the class FormLoader method loadColumns.
protected void loadColumns(Form resultComponent, Element element) {
ValueSourceProvider valueSourceProvider = resultComponent.getValueSourceProvider();
if (element.elements("column").isEmpty()) {
Iterable<ComponentPosition> rootComponents = loadComponents(element, null, null);
Iterable<ComponentPosition> dynamicAttributeComponents = loadDynamicAttributeComponents(valueSourceProvider, null);
for (ComponentPosition component : Iterables.concat(rootComponents, dynamicAttributeComponents)) {
resultComponent.add(component.getComponent(), 0, component.getColSpan(), component.getRowSpan());
}
} else {
List<Element> columnElements = element.elements("column");
if (element.elements().size() > columnElements.size()) {
String fieldGroupId = resultComponent.getId();
Map<String, Object> params = Strings.isNullOrEmpty(fieldGroupId) ? Collections.emptyMap() : ParamsMap.of("Form ID", fieldGroupId);
throw new GuiDevelopmentException("Form component elements have to be placed within its column.", context, params);
}
resultComponent.setColumns(columnElements.size());
int colIndex = 0;
for (Element columnElement : columnElements) {
String columnWidth = loadThemeString(columnElement.attributeValue("width"));
String flexAttr = columnElement.attributeValue("flex");
Float flex = null;
if (!Strings.isNullOrEmpty(flexAttr)) {
flex = Float.parseFloat(flexAttr);
resultComponent.setColumnFlex(colIndex, flex);
}
Iterable<ComponentPosition> columnComponents = loadComponents(columnElement, columnWidth, flex);
if (colIndex == 0) {
columnComponents = Iterables.concat(columnComponents, loadDynamicAttributeComponents(valueSourceProvider, columnWidth));
}
for (ComponentPosition component : columnComponents) {
resultComponent.add(component.getComponent(), colIndex, component.getColSpan(), component.getRowSpan());
}
loadChildrenCaptionAlignment(resultComponent, columnElement, colIndex);
loadChildrenCaptionWidth(resultComponent, columnElement, colIndex);
colIndex++;
}
}
}
use of com.haulmont.cuba.gui.components.form.ComponentPosition in project cuba by cuba-platform.
the class FormLoader method loadDynamicAttributeComponents.
protected List<ComponentPosition> loadDynamicAttributeComponents(ValueSourceProvider provider, @Nullable String columnWidth) {
if (provider instanceof ContainerValueSourceProvider && getMetadataTools().isPersistent(((ContainerValueSourceProvider) provider).getContainer().getEntityMetaClass())) {
String windowId = getWindowId(context);
InstanceContainer instanceContainer = ((ContainerValueSourceProvider) provider).getContainer();
MetaClass metaClass = instanceContainer.getEntityMetaClass();
List<CategoryAttribute> attributesToShow = getDynamicAttributesGuiTools().getSortedAttributesToShowOnTheScreen(metaClass, windowId, resultComponent.getId());
if (!attributesToShow.isEmpty()) {
List<ComponentPosition> components = new ArrayList<>();
if (instanceContainer instanceof HasLoader) {
DataLoader dataLoader = ((HasLoader) instanceContainer).getLoader();
if (dataLoader instanceof InstanceLoader) {
((InstanceLoader) dataLoader).setLoadDynamicAttributes(true);
}
}
for (CategoryAttribute attribute : attributesToShow) {
String code = DynamicAttributesUtils.encodeAttributeCode(attribute.getCode());
ValueSource<?> valueSource = provider.getValueSource(code);
Component dynamicAttrComponent;
if (Boolean.TRUE.equals(attribute.getIsCollection())) {
dynamicAttrComponent = getDynamicAttributesComponentsGenerator().generateComponent(provider.getValueSource(code), attribute);
} else {
ComponentGenerationContext context = new ComponentGenerationContext(metaClass, code);
context.setValueSource(valueSource);
dynamicAttrComponent = getUiComponentsGenerator().generate(context);
}
if (dynamicAttrComponent instanceof Component.HasCaption) {
((Component.HasCaption) dynamicAttrComponent).setCaption(attribute.getLocaleName());
((Component.HasCaption) dynamicAttrComponent).setDescription(attribute.getLocaleDescription());
}
if (dynamicAttrComponent instanceof HasValueSource) {
// noinspection unchecked
((HasValueSource) dynamicAttrComponent).setValueSource(valueSource);
}
if (dynamicAttrComponent instanceof Component.Editable && Boolean.TRUE.equals(attribute.getConfiguration().isReadOnly())) {
((Component.Editable) dynamicAttrComponent).setEditable(false);
}
if (dynamicAttrComponent instanceof Field) {
((Field) dynamicAttrComponent).setRequired(attribute.getRequired());
((Field) dynamicAttrComponent).setRequiredMessage(getMessages().formatMainMessage("validation.required.defaultMsg", attribute.getLocaleName()));
}
String defaultWidth = Strings.isNullOrEmpty(attribute.getWidth()) ? columnWidth : attribute.getWidth();
loadWidth(dynamicAttrComponent, defaultWidth);
components.add(new ComponentPosition(dynamicAttrComponent, 1, 1));
}
return components;
}
}
return Collections.emptyList();
}
use of com.haulmont.cuba.gui.components.form.ComponentPosition in project cuba by cuba-platform.
the class FormLoader method loadComponents.
protected List<ComponentPosition> loadComponents(Element element, @Nullable String columnWidth, @Nullable Float flex) {
List<Element> elements = element.elements();
if (elements.isEmpty()) {
return Collections.emptyList();
} else {
List<ComponentPosition> components = new ArrayList<>(elements.size());
for (Element componentElement : elements) {
ComponentPosition component = loadComponent(componentElement, columnWidth, flex);
components.add(component);
}
return components;
}
}
Aggregations