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.form.field;
35
36 import info.magnolia.icons.MagnoliaIcons;
37 import info.magnolia.ui.vaadin.gwt.client.form.field.FieldLayoutState;
38
39 import java.util.function.Consumer;
40
41 import org.apache.commons.lang3.StringUtils;
42 import org.vaadin.jonatan.contexthelp.ContextHelp;
43
44 import com.google.common.base.Preconditions;
45 import com.vaadin.shared.ui.ContentMode;
46 import com.vaadin.ui.AbstractField;
47 import com.vaadin.ui.Button;
48 import com.vaadin.ui.Component;
49 import com.vaadin.ui.HorizontalLayout;
50 import com.vaadin.ui.Label;
51 import com.vaadin.ui.TabSheet;
52 import com.vaadin.ui.UI;
53 import com.vaadin.ui.VerticalLayout;
54
55
56
57
58
59
60 @Deprecated
61 public class FieldLayout extends VerticalLayout implements Component.Focusable {
62
63 private final ContextHelp contextHelp;
64 private final boolean isRequired;
65
66 private Label statusLabel;
67 private final Component field;
68 private final String description;
69
70 public static FieldLayout of(Component field, String description) {
71 return of(field, description, false);
72 }
73
74 public static FieldLayout of(Component field, String description, boolean isRequired) {
75 return new FieldLayout(field, description, isRequired);
76 }
77
78 private FieldLayout(Component field, String description, boolean isRequired) {
79 this.isRequired = isRequired;
80 Preconditions.checkNotNull(field);
81 this.field = field;
82 this.description = description;
83 this.contextHelp = UI.getCurrent().getExtensions().stream()
84 .filter(ContextHelp.class::isInstance)
85 .findFirst()
86 .map(ContextHelp.class::cast)
87 .orElse(null);
88 initContent();
89 }
90
91 private void initContent() {
92 setMargin(false);
93 setVisible(field.isVisible());
94
95 statusLabel = new Label(null, ContentMode.HTML);
96 statusLabel.setVisible(false);
97 statusLabel.addStyleName("validation-display");
98
99 Component horizontalLayout = field;
100 if (contextHelp != null && StringUtils.isNotEmpty(description)) {
101 final Button helpButton = new Button(MagnoliaIcons.HELP_MARK);
102 helpButton.addStyleName("help-button v-button-icon icon-button");
103
104 contextHelp.extend(UI.getCurrent());
105 contextHelp.addHelpForComponent(helpButton, description);
106
107 helpButton.addClickListener(event -> {
108 contextHelp.showHelpFor(helpButton);
109 });
110 HorizontalLayout descriptionLayout = new HorizontalLayout(field, helpButton);
111 descriptionLayout.addStyleName("help-button-field-layout");
112 descriptionLayout.setSizeFull();
113 descriptionLayout.setExpandRatio(field, 1);
114 descriptionLayout.setExpandRatio(helpButton, 0);
115 descriptionLayout.setSpacing(false);
116 horizontalLayout = descriptionLayout;
117 }
118
119 setCaption(field.getCaption());
120 setRequiredIndicatorVisible(isRequired);
121
122 addComponents(horizontalLayout, statusLabel);
123 }
124
125 @Override
126 public void setCaption(String caption) {
127 super.setCaption(caption);
128 }
129
130 @Override
131 public void setRequiredIndicatorVisible(boolean visible) {
132 if (field instanceof AbstractField) {
133 ((AbstractField) field).setRequiredIndicatorVisible(false);
134 }
135 getState().required = visible;
136 }
137
138 @Override
139 protected boolean isRequiredIndicatorVisible() {
140 return getState().required;
141 }
142
143 @Override
144 protected FieldLayoutState getState() {
145 return (FieldLayoutState) super.getState();
146 }
147
148 @Override
149 public void focus() {
150 performTabSelection(field);
151 if (field instanceof Focusable) {
152 ((Focusable) field).focus();
153 }
154 }
155
156 @Override
157 public int getTabIndex() {
158 return 0;
159 }
160
161 @Override
162 public void setTabIndex(int tabIndex) {
163 }
164
165 public Consumer<String> getValidationStatusHandler() {
166 return errorMessage -> {
167 statusLabel.setValue(errorMessage);
168 if (errorMessage.isEmpty()) {
169 field.removeStyleName("error");
170 statusLabel.setVisible(false);
171 } else {
172 field.addStyleName("error");
173
174 statusLabel.setVisible(true);
175 }
176 };
177 }
178
179 public Component getField() {
180 return field;
181 }
182
183
184
185
186 private void performTabSelection(Component f) {
187 Component t = f;
188 do {
189 f = t;
190 t = f.getParent();
191 } while (!(t == null || t instanceof TabSheet));
192 if (t != null) {
193 ((TabSheet) t).setSelectedTab(f);
194 }
195 }
196 }