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.contentapp.configuration.column.component;
35  
36  import info.magnolia.ui.contentapp.configuration.column.ColumnEditorDefinition;
37  import info.magnolia.ui.contentapp.configuration.column.ConfiguredColumnDefinition;
38  import info.magnolia.ui.field.FieldDefinition;
39  import info.magnolia.ui.field.factory.FormFieldFactory;
40  
41  import java.util.Locale;
42  import java.util.Optional;
43  
44  import com.vaadin.data.Converter;
45  import com.vaadin.data.HasValue;
46  import com.vaadin.data.Result;
47  import com.vaadin.data.ValueContext;
48  import com.vaadin.data.ValueProvider;
49  import com.vaadin.ui.AbstractField;
50  import com.vaadin.ui.AbstractSingleSelect;
51  import com.vaadin.ui.Component;
52  import com.vaadin.ui.renderers.ComponentRenderer;
53  
54  import lombok.AllArgsConstructor;
55  
56  /**
57   * Defines a column that displays a component.
58   */
59  public class ColumnComponentDefinition extends ConfiguredColumnDefinition {
60  
61      public ColumnComponentDefinition() {
62          setRenderer(ComponentRenderer.class);
63          setValueProvider(ComponentValueProvider.class);
64      }
65  
66      /**
67       * Provides a value for a given column component.
68       * @param <S> source value type.
69       */
70      @AllArgsConstructor
71      public static class ComponentValueProvider<S> implements ValueProvider<S, Component> {
72  
73          private final ColumnComponentDefinition componentDefinition;
74          private final FormFieldFactory formFieldFactory;
75          private final ValueProvider wrapped;
76  
77          @Override
78          public Component apply(S t) {
79              return Optional.ofNullable(componentDefinition.getEditor()).map(ColumnEditorDefinition::getField).map(fieldDefinition -> {
80                  final Component component = formFieldFactory.createField((FieldDefinition<?>) fieldDefinition, Locale.getDefault());
81                  if (component instanceof AbstractField) {
82                      ((AbstractField) component).setReadOnly(true);
83                  } else if (component instanceof AbstractSingleSelect) {
84                      ((AbstractSingleSelect) component).setReadOnly(true);
85                  }
86                  Object value = wrapped.apply(t);
87                  if (value != null) {
88                      ((HasValue) component).setValue(value);
89                  }
90                  return component;
91              }).orElse(null);
92          }
93  
94          /**
95           * Vaadin converter implementation which translates between a pre-set component
96           * and an arbitrary value.
97           */
98          @AllArgsConstructor
99          public static class ComponentConverter implements Converter<Object, Object> {
100 
101             private HasValue value;
102 
103             @Override
104             public Result<Object> convertToModel(Object value, ValueContext context) {
105                 return Result.ok(this.value);
106             }
107 
108             @Override
109             public Object convertToPresentation(Object value, ValueContext context) {
110                 return value;
111             }
112         }
113     }
114 }