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