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.dialog.formdialog;
35  
36  import static com.vaadin.server.Sizeable.Unit.PERCENTAGE;
37  import static java.util.stream.Collectors.toList;
38  import static java.util.stream.StreamSupport.stream;
39  
40  import info.magnolia.i18nsystem.SimpleTranslator;
41  import info.magnolia.ui.dialog.ResurfaceDialogViewImpl;
42  import info.magnolia.ui.vaadin.form.FormSection;
43  import info.magnolia.ui.vaadin.form.FormViewReduced;
44  
45  import java.util.Collection;
46  import java.util.Iterator;
47  import java.util.List;
48  import java.util.Locale;
49  
50  import javax.inject.Inject;
51  
52  import com.vaadin.server.Sizeable;
53  import com.vaadin.ui.AbstractComponent;
54  import com.vaadin.ui.ComboBox;
55  import com.vaadin.ui.Component;
56  import com.vaadin.ui.ComponentContainer;
57  import com.vaadin.ui.FormLayout;
58  import com.vaadin.ui.TabSheet;
59  import com.vaadin.ui.themes.ValoTheme;
60  import com.vaadin.v7.data.Item;
61  import com.vaadin.v7.data.Validatable;
62  import com.vaadin.v7.ui.AbstractField;
63  import com.vaadin.v7.ui.Field;
64  
65  /**
66   * Resurface, GWT-free form that'll be passed to the DialogBuilder.
67   */
68  public class ResurfaceFormViewImpl extends ResurfaceDialogViewImpl implements FormView {
69  
70      private final SimpleTranslator i18n;
71      private Item itemDatasource;
72  
73      private String title;
74      private TabSheet tabSheet = new TabSheet();
75      private ComboBox<Locale> languageSelector;
76      private FormViewReduced.Listener listener;
77  
78      @Inject
79      public ResurfaceFormViewImpl(SimpleTranslator i18n) {
80          this.i18n = i18n;
81  
82          tabSheet.setSizeFull();
83          tabSheet.addStyleName("content");
84          tabSheet.addStyleName(ValoTheme.TABSHEET_FRAMED);
85  
86          createLocaleSelector();
87          this.setContent(() -> tabSheet);
88      }
89  
90      @Override
91      public void setCurrentLocale(Locale locale) {
92          this.languageSelector.setValue(locale);
93      }
94  
95      @Override
96      public void setAvailableLocales(List<Locale> locales) {
97          if (locales != null && !locales.isEmpty()) {
98              languageSelector.clear();
99              languageSelector.setItems(locales);
100             getActionAreaView().setToolbarComponent(languageSelector);
101         }
102     }
103 
104     @Override
105     public void setItemDataSource(Item newDataSource) {
106         this.itemDatasource = newDataSource;
107     }
108 
109     @Override
110     public Item getItemDataSource() {
111         return this.itemDatasource;
112     }
113 
114    @Override
115     public void setCaption(String caption) {
116        this.title = caption;
117     }
118 
119     @Override
120     public String getTitle() {
121         return this.title;
122     }
123 
124     @Override
125     public void addField(Field<?> field) {
126         // NOOP
127     }
128 
129     @Override
130     public void addFormSection(String tabName, FormSection inputFields) {
131         final FormLayout formLayout = new FormLayout(inputFields.getComponents().toArray(new Component[]{}));
132         final TabSheet.Tab tab = this.tabSheet.addTab(formLayout, tabName);
133         tab.setId(inputFields.getName());
134         formLayout.setWidth(95, PERCENTAGE);
135     }
136 
137     @Override
138     public void showValidation(boolean isVisible) {
139         getFields().forEach(field -> ((AbstractField) field).setValidationVisible(isVisible));
140     }
141 
142     @Override
143     public void setShowAllEnabled(boolean enabled) {
144         // NOOP
145     }
146 
147     @Override
148     public void setDescriptionVisibility(boolean isVisible) {
149         // NOOP
150     }
151 
152     @Override
153     public boolean isValid() {
154         return getFields().stream().allMatch(Validatable::isValid);
155     }
156 
157     @Override
158     public List<FormSection> getFormSections() {
159         return stream(tabSheet.spliterator(), false)
160                 .map(component -> new FormSectionAdapter((ComponentContainer) component, tabSheet.getTab(component).getId()))
161                 .collect(toList());
162     }
163 
164     @Override
165     public Collection<Field<?>> getFields() {
166         List<Field<?>> fields = getFormSections().stream()
167                 .flatMap(sections -> sections.getComponents().stream())
168                 .filter(component -> component instanceof Field)
169                 .map(component -> (Field<?>) component)
170                 .collect(toList());
171         return fields;
172     }
173 
174     @Override
175     public void setListener(FormViewReduced.Listener listener) {
176         this.listener = listener;
177     }
178 
179     @Override
180     public AbstractComponent asVaadinComponent() {
181 
182         /*
183          * When a {@code com.vaadin.ui.TabSheet} is provided, let's check how many
184          * {@code com.vaadin.ui.TabSheet.Tab} elements it contains. If only one, only show that.
185          */
186         if (tabSheet.getComponentCount() == 1) {
187             tabSheet.addStyleName("single-tab");
188         } else {
189             tabSheet.removeStyleName("single-tab");
190         }
191 
192         return tabSheet;
193     }
194 
195     private void createLocaleSelector() {
196         languageSelector = new ComboBox<>();
197         // Language selector must have width 125px (include padding right 10px)
198         languageSelector.setWidth(150, Sizeable.Unit.PIXELS);
199         languageSelector.setDescription(i18n.translate("languageSelector.description"));
200         languageSelector.setItemCaptionGenerator(Locale::getDisplayLanguage);
201         languageSelector.setEmptySelectionAllowed(false);
202         languageSelector.addValueChangeListener(event -> {
203             if (this.listener != null) {
204                 this.listener.localeChanged(event.getValue());
205             }
206         });
207     }
208 
209     void updateFormView() {
210         List<FormSection> sections = getFormSections();
211         tabSheet.removeAllComponents();
212         sections.forEach(section -> addFormSection(section.getName(), section));
213     }
214 
215     private final class FormSectionAdapter extends FormSection {
216 
217         private final ComponentContainer layout;
218 
219         FormSectionAdapter(ComponentContainer layout, String name) {
220             this.layout = layout;
221             setName(name);
222         }
223 
224         @Override
225         public void addComponent(Component c) {
226             accessTabLayout().addComponent(c);
227         }
228 
229         ComponentContainer accessTabLayout() {
230             return layout;
231         }
232 
233         @Override
234         public void removeAllComponents() {
235             accessTabLayout().removeAllComponents();
236         }
237 
238         @Override
239         public void removeComponent(Component c) {
240             accessTabLayout().removeComponent(c);
241         }
242 
243         @Override
244         public Iterator<Component> iterator() {
245             return accessTabLayout().iterator();
246         }
247 
248         @Override
249         public int getComponentCount() {
250             return accessTabLayout().getComponentCount();
251         }
252 
253         @Override
254         public List<Component> getComponents() {
255             return stream(accessTabLayout().spliterator(), false).collect(toList());
256         }
257     }
258 }