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.ui.field.factory;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.stream.Collectors;
39  import java.util.stream.Stream;
40  
41  import com.explicatis.ext_token_field.ExtTokenField;
42  import com.explicatis.ext_token_field.SimpleTokenizable;
43  import com.explicatis.ext_token_field.Tokenizable;
44  import com.vaadin.shared.Registration;
45  import com.vaadin.ui.ComboBox;
46  import com.vaadin.ui.Component;
47  import com.vaadin.ui.CustomField;
48  
49  import lombok.RequiredArgsConstructor;
50  
51  
52  /**
53   * Creates and initializes an token field based on a field definition.
54   *
55   * @param <T> item type.
56   */
57  @RequiredArgsConstructor
58  public class TokenFieldWrapper<T> extends CustomField<List<String>> {
59  
60      private final TokenField tokenField = new TokenField();
61      private final ComboBox<T> comboBox;
62  
63      @Override
64      public List<String> getValue() {
65          return tokenField.getValue().stream()
66                  .map(Tokenizable::getStringValue)
67                  .collect(Collectors.toList());
68      }
69  
70      //TODO value is not stored when inline editing grid
71      @Override
72      protected void doSetValue(List strings) {
73          if (strings == null) {
74              tokenField.doSetValue(new ArrayList());
75          } else {
76              tokenField.doSetValue(toTokenizables(strings.stream()));
77          }
78      }
79  
80      @Override
81      protected Component initContent() {
82          comboBox.addValueChangeListener((ValueChangeListener<T>) event -> {
83              if (event.getValue() != null) {
84                  //for now, we use caption as value to not introduce any JCR specific logic here
85                  tokenField.addTokenizable(new ToStringTokenizable(comboBox.getItemCaptionGenerator().apply(event.getValue())));
86                  event.getSource().setValue(null);
87              }
88          });
89          tokenField.setTokenDragDropEnabled(true);
90          tokenField.setEnableDefaultDeleteTokenAction(true);
91          tokenField.setInputField(comboBox);
92          return tokenField;
93      }
94  
95      @Override
96      public Registration addValueChangeListener(ValueChangeListener listener) {
97          tokenField.addValueChangeListener(listener);
98          return super.addValueChangeListener(listener);
99      }
100 
101     @Override
102     public void setReadOnly(boolean readOnly) {
103         super.setReadOnly(readOnly);
104         tokenField.setReadOnly(readOnly);
105     }
106 
107     private List<Tokenizable> toTokenizables(Stream<?> strings) {
108         return strings
109                 .map(ToStringTokenizable::new)
110                 .collect(Collectors.toList());
111     }
112 
113     private static class ToStringTokenizable extends SimpleTokenizable {
114 
115         private ToStringTokenizable(Object stringValue) {
116             super(stringValue.hashCode(), String.valueOf(stringValue));
117         }
118 
119         @Override
120         public String toString() {
121             return getStringValue();
122         }
123     }
124 
125     private static class TokenField extends ExtTokenField {
126 
127         @Override
128         protected void doSetValue(List<Tokenizable> value) {
129             super.doSetValue(value);
130         }
131     }
132 }