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