View Javadoc
1   package info.magnolia.ui.framework.layout;
2   
3   import info.magnolia.ui.field.EditorPropertyDefinition;
4   import info.magnolia.ui.field.FormRowOutliner;
5   import info.magnolia.ui.framework.layout.field.FieldComponentDecorator;
6   import info.magnolia.ui.vaadin.extension.FocusFieldCaption;
7   
8   import java.util.Map;
9   
10  import javax.inject.Inject;
11  
12  import com.vaadin.ui.Component;
13  import com.vaadin.ui.VerticalLayout;
14  
15  /**
16   * Lays out the form fields in a vertical layout. Unlike {@link FormLayoutProducer} this producer
17   * puts the field captions above the respective components allocating more horizontal space for the
18   * latter. This strategy is optimal for the the complex sub-editors, especially when they are recursively
19   * nested.
20   */
21  public class StackedLayoutProducer implements FieldLayoutProducer<StackedLayoutProducer.Definition> {
22  
23      private FieldComponentDecorator fieldComponentDecorator;
24  
25      @Inject
26      public StackedLayoutProducer(FieldComponentDecorator fieldComponentDecorator) {
27          this.fieldComponentDecorator = fieldComponentDecorator;
28      }
29  
30      @Override
31      public Component createLayout(StackedLayoutProducer.Definition definition, Map<EditorPropertyDefinition, Component> mappings) {
32          VerticalLayout layout = new VerticalLayout();
33          layout.addStyleName("stacked-form-layout");
34          if (mappings.size() > 1) {
35              layout.addStyleName("with-multiple-entries");
36          }
37  
38          layout.setSpacing(true);
39          layout.setMargin(false);
40  
41          mappings.forEach((propertyDefinition, component) -> {
42              Component wrappedField = createFieldComponentLayout(component, propertyDefinition);
43              layout.addStyleName(propertyDefinition.getStyleName());
44              layout.addComponent(wrappedField);
45  
46              FormRowOutliner.considerOutlining(wrappedField, propertyDefinition);
47          });
48  
49          FocusFieldCaption.focusCaptionOf(layout);
50          return layout;
51      }
52  
53      @LayoutType("stacked")
54      public static class Definition extends ConfiguredFieldLayoutDefinition<StackedLayoutProducer> {
55          public Definition() {
56              setImplementationClass(StackedLayoutProducer.class);
57          }
58      }
59  
60      private Component createFieldComponentLayout(Component component, EditorPropertyDefinition editorPropertyDefinition) {
61          return fieldComponentDecorator.decorateField(component, editorPropertyDefinition);
62      }
63  }