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.poc.extensions.StringFieldValidationExtension;
39  
40  import java.util.stream.Collectors;
41  import java.util.stream.Stream;
42  import java.util.stream.StreamSupport;
43  
44  import javax.servlet.annotation.WebServlet;
45  
46  import org.vaadin.aceeditor.AceEditor;
47  import org.vaadin.aceeditor.AceMode;
48  
49  import com.explicatis.ext_token_field.ExtTokenField;
50  import com.explicatis.ext_token_field.SimpleTokenizable;
51  import com.vaadin.annotations.Theme;
52  import com.vaadin.annotations.Title;
53  import com.vaadin.annotations.VaadinServletConfiguration;
54  import com.vaadin.annotations.Widgetset;
55  import com.vaadin.data.validator.StringLengthValidator;
56  import com.vaadin.server.VaadinRequest;
57  import com.vaadin.server.VaadinServlet;
58  import com.vaadin.ui.Button;
59  import com.vaadin.ui.ComboBox;
60  import com.vaadin.ui.FormLayout;
61  import com.vaadin.ui.PasswordField;
62  import com.vaadin.ui.UI;
63  import com.vaadin.ui.VerticalLayout;
64  
65  @Theme("poctheme")
66  @Title("Magnolia 6 Resurface - Special Fields Components")
67  @Widgetset("info.magnolia.poc.Widgetset")
68  public class SpecialFieldsComponentUI extends UI {
69  
70      @Override
71      protected void init(VaadinRequest vaadinRequest) {
72  
73          FormLayout layout = new FormLayout();
74          layout.setMargin(true);
75          layout.setWidth(926, PIXELS);
76  
77          AceEditor codeField = new AceEditor();
78          codeField.setStyleName("textcodefield");
79          codeField.setUseWorker(false);
80          codeField.setCaption("Code field");
81          codeField.setValue("class Resurface {\n" +
82                  "\t{\n" +
83                  "\t\tSystem.out.println(\"Hello Resurface\");\n" +
84                  "\t}\n" +
85                  "}\n");
86  
87          String aceResourcePath = "/VAADIN/ace";
88          codeField.setModePath(aceResourcePath);
89          codeField.setWorkerPath(aceResourcePath);
90          codeField.setThemePath(aceResourcePath);
91          codeField.setMode(AceMode.java);
92  
93          VerticalLayout passwordFieldLayout = new VerticalLayout();
94          passwordFieldLayout.setCaption("Password field");
95  
96          PasswordField passwordField = new PasswordField("Please verify your entry");
97          passwordField.setValue("password");
98  
99          PasswordField invalidPasswordField = new PasswordField("Please verify your entry");
100         final StringLengthValidator lengthValidator = new StringLengthValidator("Sorry, incorrect password.", 10, 100);
101         StringFieldValidationExtension.extendValidationFor(invalidPasswordField, lengthValidator);
102         invalidPasswordField.setValue("password");
103 
104         passwordFieldLayout.addComponents(passwordField, invalidPasswordField);
105 
106         ExtTokenField tokenField = new ExtTokenField();
107         tokenField.setCaption("Tag field");
108         tokenField.setEnableDefaultDeleteTokenAction(true);
109         tokenField.setTokenDragDropEnabled(true);
110 
111         String[] tokens = {"adventure", "family", "kids", "asia", "travel", "active", "beach", "cultural", "off-path"};
112         ComboBox<SimpleTokenizable> comboBox = new ComboBox<>("", Stream.of(tokens)
113                 .sorted()
114 				.map(token -> new SimpleTokenizable(token.hashCode(), token))
115                 .collect(Collectors.toList()));
116 		comboBox.setItemCaptionGenerator(SimpleTokenizable::getStringValue);
117 		comboBox.setEmptySelectionAllowed(false);
118 		comboBox.addValueChangeListener(event -> {
119 		    if (event.getValue() != null) {
120                 tokenField.addTokenizable(event.getValue());
121                 event.getSource().setValue(null);
122             }
123         });
124         tokenField.setInputField(comboBox);
125         Stream.of(tokens)
126                 .limit(5)
127                 .map(token -> new SimpleTokenizable(token.hashCode(), token))
128                 .collect(Collectors.toList())
129                 .forEach(tokenField::addTokenizable);
130 
131         layout.addComponents(
132                 codeField,
133                 passwordFieldLayout,
134                 tokenField
135         );
136 
137         StreamSupport.stream(layout.spliterator(), false)
138                 .filter(component -> !(component instanceof Button))
139                 .forEach(component -> component.setWidth(100, PERCENTAGE));
140 
141         setContent(layout);
142     }
143 
144     @WebServlet(value = "/special/*", asyncSupported = true)
145     @VaadinServletConfiguration(productionMode = false, ui = SpecialFieldsComponentUI.class)
146     public static class Servlet extends VaadinServlet {
147     }
148 }