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.data.Property;
44 import com.vaadin.data.Property.ValueChangeEvent;
45 import com.vaadin.data.Property.ValueChangeListener;
46 import com.vaadin.data.util.ObjectProperty;
47 import com.vaadin.data.util.converter.Converter;
48 import com.vaadin.shared.ui.label.ContentMode;
49 import com.vaadin.ui.Component;
50 import com.vaadin.ui.CssLayout;
51 import com.vaadin.ui.Label;
52 import com.vaadin.ui.VerticalLayout;
53
54
55
56
57 public class SmallAppLayout extends VerticalLayout {
58
59 private Property<String> description = new ObjectProperty<String>("");
60
61 private VerticalLayout root = this;
62
63 private CssLayout sectionsLayout = new CssLayout();
64
65 private Label descriptionLabel = new Label();
66
67 public SmallAppLayout() {
68 root.addStyleName("smallapp");
69 root.setSizeFull();
70
71 descriptionLabel.addStyleName("description");
72 descriptionLabel.setContentMode(ContentMode.HTML);
73 descriptionLabel.setConverter(new StringToHtmlConverter());
74 descriptionLabel.setPropertyDataSource(description);
75 descriptionLabel.addValueChangeListener(new ValueChangeListener() {
76
77 @Override
78 public void valueChange(ValueChangeEvent event) {
79 if (StringUtils.isNotBlank((String) event.getProperty().getValue())) {
80 descriptionLabel.setVisible(true);
81 } else {
82 descriptionLabel.setVisible(false);
83 }
84 }
85 });
86
87 root.addComponent(descriptionLabel);
88 root.setExpandRatio(descriptionLabel, 0);
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.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.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 }