1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.ui.vaadin.layout;
35
36 import java.util.ArrayList;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.Locale;
40
41 import org.apache.commons.lang3.StringUtils;
42
43 import com.vaadin.shared.ui.MarginInfo;
44 import com.vaadin.ui.Component;
45 import com.vaadin.ui.CssLayout;
46 import com.vaadin.v7.data.Property;
47 import com.vaadin.v7.data.Property.ValueChangeEvent;
48 import com.vaadin.v7.data.Property.ValueChangeListener;
49 import com.vaadin.v7.data.util.ObjectProperty;
50 import com.vaadin.v7.data.util.converter.Converter;
51 import com.vaadin.v7.shared.ui.label.ContentMode;
52 import com.vaadin.v7.ui.Label;
53 import com.vaadin.v7.ui.VerticalLayout;
54
55
56
57
58 public class SmallAppLayout extends VerticalLayout {
59
60 private Property<String> description = new ObjectProperty<String>("");
61
62 private VerticalLayout root = this;
63
64 private CssLayout sectionsLayout = new CssLayout();
65
66 private Label descriptionLabel = new Label();
67
68 public SmallAppLayout() {
69 root.addStyleName("smallapp");
70 root.setSizeFull();
71 root.setMargin(new MarginInfo(true, false, false, false));
72
73 descriptionLabel.addStyleName("description");
74 descriptionLabel.setContentMode(ContentMode.HTML);
75 descriptionLabel.setConverter(new StringToHtmlConverter());
76 descriptionLabel.setPropertyDataSource(description);
77 descriptionLabel.addValueChangeListener(new ValueChangeListener() {
78
79 @Override
80 public void valueChange(ValueChangeEvent event) {
81 if (StringUtils.isNotBlank((String) event.getProperty().getValue())) {
82 descriptionLabel.setVisible(true);
83 } else {
84 descriptionLabel.setVisible(false);
85 }
86 }
87 });
88 root.addComponent(descriptionLabel);
89
90 sectionsLayout.addStyleName("smallapp-sections");
91 sectionsLayout.setSizeFull();
92 root.addComponent(sectionsLayout);
93 root.setExpandRatio(sectionsLayout, 1);
94 }
95
96 @Override
97 public String getDescription() {
98 return description.getValue();
99 }
100
101 @Override
102 public void setDescription(String description) {
103 this.description.setValue(description);
104 }
105
106 public List<Component> getSections() {
107 Iterator<Component> it = sectionsLayout.iterator();
108 List<Component> sections = new ArrayList<Component>();
109 while (it.hasNext()) {
110 sections.add(it.next());
111 }
112 return sections;
113 }
114
115 public void addSection(Component section) {
116 section.addStyleName("smallapp-section");
117 sectionsLayout.addComponent(section);
118 }
119
120 public void removeSection(Component section) {
121 sectionsLayout.removeComponent(section);
122 }
123
124
125
126
127 private class StringToHtmlConverter implements Converter<String, String> {
128
129 @Override
130 public String convertToModel(String value, Class<? extends String> targetType, Locale locale) throws com.vaadin.v7.data.util.converter.Converter.ConversionException {
131 return value.replaceAll("<br/>", "\n");
132 }
133
134 @Override
135 public String convertToPresentation(String value, Class<? extends String> targetType, Locale locale) throws com.vaadin.v7.data.util.converter.Converter.ConversionException {
136 if (value != null) {
137 return value.replaceAll("\n", "<br/>");
138 }
139 return null;
140 }
141
142 @Override
143 public Class<String> getModelType() {
144 return String.class;
145 }
146
147 @Override
148 public Class<String> getPresentationType() {
149 return String.class;
150 }
151
152 }
153
154 }