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;
35  
36  import info.magnolia.ui.editor.ComplexPropertyDefinition;
37  import info.magnolia.ui.editor.EditorDefinition;
38  import info.magnolia.ui.ConfiguredViewDefinition;
39  import info.magnolia.ui.editor.ItemProviderDefinition;
40  import info.magnolia.ui.editor.ItemProviderStrategy;
41  import info.magnolia.ui.editor.EditorView;
42  import info.magnolia.ui.editor.FormView;
43  
44  import java.util.Optional;
45  
46  import lombok.Data;
47  import lombok.EqualsAndHashCode;
48  
49  /**
50   * {@link ComplexPropertyDefinition} implementation.
51   * @param <T> item type.
52   */
53  @EqualsAndHashCode(callSuper = true)
54  @Data
55  public class ConfiguredComplexPropertyDefinition<T> extends ConfiguredViewDefinition<EditorView<T>> implements ComplexPropertyDefinition<T> {
56      private String name;
57      private String label;
58      private String description;
59      private boolean i18n;
60      private String styleName;
61      private ItemProviderDefinition<T, T> itemProvider;
62  
63      public ConfiguredComplexPropertyDefinition() {
64          setImplementationClass((Class) FormView.class);
65      }
66  
67      @Override
68      public EditorDefinition<T> getEditorDefinition() {
69          if (this instanceof EditorDefinition) {
70              return (EditorDefinition<T>) this;
71          }
72          throw new IllegalStateException("Attempted to cast a non-editor property definition to EditorDefinition");
73      }
74  
75      /**
76       * Allows to use an external {@link EditorDefinition} to create a {@link ComplexPropertyDefinition}
77       * (by fusing it with some {@link ItemProviderStrategy}.
78       *
79       * @param <T>
80       *     data item type
81       */
82      public static class Wrapper<T> extends ConfiguredComplexPropertyDefinition<T> {
83  
84          private final EditorDefinition<T> delegate;
85  
86          public Wrapper(EditorDefinition<T> delegate) {
87              this.delegate = delegate;
88          }
89  
90          @Override
91          public EditorDefinition<T> getEditorDefinition() {
92              return delegate;
93          }
94  
95          @Override
96          public String getName() {
97              String name = super.getName();
98              if (name == null) {
99                  name = delegate.getName();
100             }
101             return name;
102         }
103 
104         @Override
105         public String getLabel() {
106             String label = super.getLabel();
107             if (label == null && delegate instanceof EditorPropertyDefinition) {
108                 label = ((EditorPropertyDefinition) delegate).getLabel();
109             }
110             return label;
111         }
112 
113         @Override
114         public String getDescription() {
115             String description = super.getDescription();
116             if (description == null && delegate instanceof EditorPropertyDefinition) {
117                 description = ((EditorPropertyDefinition) delegate).getDescription();
118             }
119             return description;
120         }
121 
122         @Override
123         public boolean isI18n() {
124             if (super.isI18n()) {
125                 return true;
126             } else if (delegate instanceof EditorPropertyDefinition) {
127                 return ((EditorPropertyDefinition) delegate).isI18n();
128             } else return false;
129         }
130 
131         @Override
132         public String getStyleName() {
133             String styleName = super.getStyleName();
134             if (styleName == null && delegate instanceof EditorPropertyDefinition) {
135                 styleName = ((EditorPropertyDefinition) delegate).getStyleName();
136             }
137             return styleName;
138         }
139     }
140 }