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