View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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());    // only needed once for each UI
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 }