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.poc;
35
36 import static com.vaadin.server.Sizeable.Unit.*;
37
38 import info.magnolia.icons.MagnoliaIcons;
39 import info.magnolia.poc.extensions.StringFieldValidationExtension;
40 import info.magnolia.ui.incub.FieldCaptionActiveExtension;
41
42 import java.util.stream.StreamSupport;
43
44 import javax.servlet.annotation.WebServlet;
45
46 import org.vaadin.hene.expandingtextarea.ExpandingTextArea;
47 import org.vaadin.jonatan.contexthelp.ContextHelp;
48
49 import com.vaadin.annotations.Theme;
50 import com.vaadin.annotations.Title;
51 import com.vaadin.annotations.VaadinServletConfiguration;
52 import com.vaadin.annotations.Widgetset;
53 import com.vaadin.data.validator.EmailValidator;
54 import com.vaadin.data.validator.StringLengthValidator;
55 import com.vaadin.server.VaadinRequest;
56 import com.vaadin.server.VaadinServlet;
57 import com.vaadin.ui.Button;
58 import com.vaadin.ui.FormLayout;
59 import com.vaadin.ui.HorizontalLayout;
60 import com.vaadin.ui.TextArea;
61 import com.vaadin.ui.TextField;
62 import com.vaadin.ui.UI;
63
64 @Theme("poctheme")
65 @Title("Magnolia 6 Resurface - Text Components")
66 @Widgetset("info.magnolia.poc.Widgetset")
67 public class TextComponentUI extends UI {
68 protected static final String TEXT_SHORT = "Nullam quis risus eget urna mollis ornare vel eu leo";
69 protected static final String TEXT_ONELINE = TEXT_SHORT + ". Sed posuere consec tetur est at lobortis.";
70 protected static final String TEXT_2_LINES = TEXT_ONELINE + " Cras justo odio, dapibus ac facilisis in, egestas eget quam.";
71 protected static final String TEXT_3_LINES = TEXT_2_LINES + " Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.";
72 protected static final String TEXT_FULL = TEXT_3_LINES + " Etiam porta sem malesuada magna mollis euismod.";
73
74 @Override
75 protected void init(VaadinRequest vaadinRequest) {
76
77 FormLayout layout = new FormLayout();
78 layout.setMargin(true);
79 layout.setWidth(926, PIXELS);
80
81 ContextHelp contextHelp = new ContextHelp();
82 contextHelp.extend(UI.getCurrent());
83 contextHelp.setHelpKey(-1);
84
85 TextField textField = new TextField();
86 textField.setValue(TEXT_SHORT);
87
88 Button helpButton = new Button(MagnoliaIcons.HELP);
89 helpButton.addStyleName("field-description");
90
91 contextHelp.addHelpForComponent(helpButton, "The text field allows you to write many, many, many, many, many things.");
92
93 helpButton.addClickListener(event -> {
94 if (contextHelp.getState().hidden) {
95 contextHelp.showHelpFor(helpButton);
96 } else {
97 contextHelp.hideHelp();
98 }
99 });
100
101 HorizontalLayout textFieldLayout = new HorizontalLayout();
102 textFieldLayout.setCaption("Text field");
103 textFieldLayout.addComponents(textField, helpButton);
104 textField.setWidth("100%");
105 textFieldLayout.setExpandRatio(textField, 1.0f);
106
107 TextField textFieldHover = new TextField("Text field hover");
108 textFieldHover.setValue(TEXT_SHORT);
109 textFieldHover.setDescription("Hover content");
110
111 TextField textFieldFocus = new TextField("Text field focus");
112 textFieldFocus.setValue("Nullam ");
113
114 TextField disabledTextField = new TextField("Text field disabled");
115 disabledTextField.setValue(TEXT_SHORT);
116 disabledTextField.setEnabled(false);
117
118 TextField textFieldValidation = new TextField("Text field validation");
119 final EmailValidator emailValidator = new EmailValidator("Enter a valid email address.");
120 final StringLengthValidator lengthValidator = new StringLengthValidator("Length errors", 1, 10);
121 StringFieldValidationExtension.extendValidationFor(textFieldValidation, emailValidator, lengthValidator);
122 textFieldValidation.setValue(TEXT_SHORT);
123
124 TextField textFieldWithPlaceHolder = new TextField();
125 textFieldWithPlaceHolder.setPlaceholder("Add description here...");
126
127 Button helpButton2 = new Button(MagnoliaIcons.HELP);
128 helpButton2.addStyleName("field-description");
129
130 contextHelp.addHelpForComponent(helpButton2, "The text field with placeholder.");
131
132 helpButton2.addClickListener(event -> {
133 if (contextHelp.getState().hidden) {
134 contextHelp.showHelpFor(helpButton2);
135 } else {
136 contextHelp.hideHelp();
137 }
138 });
139
140 HorizontalLayout textFieldWithPlaceHolderLayout = new HorizontalLayout();
141 textFieldWithPlaceHolderLayout.setCaption("Placeholder field");
142 textFieldWithPlaceHolderLayout.addComponents(textFieldWithPlaceHolder, helpButton2);
143 textFieldWithPlaceHolder.setWidth("100%");
144 textFieldWithPlaceHolderLayout.setExpandRatio(textFieldWithPlaceHolder, 1.0f);
145
146 TextArea expandable1 = new TextArea("Expandable 1");
147 expandable1.setValue(TEXT_ONELINE);
148 expandable1.setRows(1);
149
150 TextArea expandable2 = new TextArea("Expandable 2");
151 expandable2.setValue(TEXT_2_LINES);
152 expandable2.setRows(2);
153
154 TextArea expandable3 = new TextArea("Expandable 3");
155 expandable3.setValue(TEXT_3_LINES);
156 expandable3.setRows(3);
157
158 TextArea expandable4 = new TextArea("Expandable 4 scroll");
159 expandable4.setValue(TEXT_3_LINES + ". " + TEXT_3_LINES);
160 expandable4.setRows(3);
161
162 ExpandingTextArea expandingTextArea = new ExpandingTextArea("Expanding text area");
163 expandingTextArea.setRows(2);
164 expandingTextArea.setMaxRows(3);
165
166 TextArea staticField = new TextArea("Static field");
167 staticField.setValue(TEXT_3_LINES);
168 staticField.setReadOnly(true);
169 staticField.setTabIndex(-1);
170
171 layout.addComponents(textFieldLayout,
172 textFieldHover,
173 textFieldFocus,
174 disabledTextField,
175 textFieldValidation,
176 textFieldWithPlaceHolderLayout,
177 expandable1,
178 expandable2,
179 expandable3,
180 expandable4,
181 expandingTextArea,
182 staticField);
183
184 StreamSupport.stream(layout.spliterator(), false)
185 .filter(component -> !(component instanceof Button))
186 .forEach(component -> component.setWidth(100, PERCENTAGE));
187
188 setContent(layout);
189 setFocusedComponent(textFieldFocus);
190
191 FieldCaptionActiveExtension.focusCaptionOf(layout);
192 }
193
194 @WebServlet(value = "/text/*", asyncSupported = true)
195 @VaadinServletConfiguration(productionMode = false, ui = TextComponentUI.class)
196 public static class Servlet extends VaadinServlet {
197 }
198 }