View Javadoc
1   /**
2    * This file Copyright (c) 2013-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 info.magnolia.context.MgnlContext;
37  import info.magnolia.i18nsystem.SimpleTranslator;
38  import info.magnolia.ui.dialog.BaseDialogViewImpl;
39  import info.magnolia.ui.vaadin.dialog.FormDialog;
40  import info.magnolia.ui.vaadin.form.Form;
41  import info.magnolia.ui.vaadin.form.FormSection;
42  import info.magnolia.ui.vaadin.lightcombobox.LightComboBox;
43  
44  import java.util.Collection;
45  import java.util.List;
46  import java.util.Locale;
47  
48  import javax.inject.Inject;
49  
50  import com.vaadin.v7.data.Item;
51  import com.vaadin.v7.data.Property;
52  import com.vaadin.v7.ui.ComboBox;
53  import com.vaadin.v7.ui.Field;
54  
55  /**
56   * Owns a Form and Dialog and connects them.
57   */
58  public class ItemFormView extends BaseDialogViewImpl implements FormView {
59  
60      private ComboBox languageSelector;
61  
62      private final Form form = new Form();
63  
64      private final SimpleTranslator i18n;
65  
66      @Inject
67      public ItemFormView(SimpleTranslator i18n) {
68          super(new FormDialog());
69          this.i18n = i18n;
70  
71          form.setErrorLabels(i18n.translate("validation.message.errors"), i18n.translate("validation.message.nextError"));
72  
73          getDialog().setContent(form);
74          getDialog().addDescriptionVisibilityHandler(event -> form.setDescriptionVisibility(event.isVisible()));
75  
76          createLocaleSelector();
77      }
78  
79      @Override
80      public Item getItemDataSource() {
81          return form.getItemDataSource();
82      }
83  
84      @Override
85      public void setItemDataSource(Item newDataSource) {
86          form.setItemDataSource(newDataSource);
87      }
88  
89      @Override
90      public void addField(Field<?> field) {
91          form.addField(field);
92      }
93  
94      @Override
95      public void setDescriptionVisibility(boolean isVisible) {
96          form.setDescriptionVisibility(isVisible);
97      }
98  
99      @Override
100     public void setCaption(String caption) {
101         getDialog().setCaption(caption);
102     }
103 
104     @Override
105     public void addFormSection(String tabName, FormSection inputFields) {
106         form.addFormSection(tabName, inputFields);
107     }
108 
109     @Override
110     public void showValidation(boolean isVisible) {
111         form.showValidation(isVisible);
112     }
113 
114     @Override
115     public void setShowAllEnabled(boolean enabled) {
116         form.getContent().showAllTab(enabled, i18n.translate("form.tabs.all"));
117     }
118 
119     @Override
120     public boolean isValid() {
121         return form.isValid();
122     }
123 
124     @Override
125     @Deprecated
126     public Collection<Field<?>> getFields() {
127         return form.getFields();
128     }
129 
130     @Override
131     public List<FormSection> getFormSections() {
132         return form.getFormSections();
133     }
134 
135     @Override
136     public void setCurrentLocale(Locale locale) {
137         this.languageSelector.setValue(locale);
138         // Prevent updating locale in the first opening.
139         if (languageSelector.getListeners(Property.ValueChangeEvent.class).isEmpty()) {
140             form.setLocale(locale);
141             languageSelector.addValueChangeListener(event -> updateLocale((Locale) event.getProperty().getValue()));
142         }
143     }
144 
145     @Override
146     public void setAvailableLocales(List<Locale> locales) {
147         if (locales != null && !locales.isEmpty()) {
148             languageSelector.removeAllItems();
149             for (Locale locale : locales) {
150                 String label = locale.getDisplayLanguage(MgnlContext.getLocale());
151                 if (!locale.getDisplayCountry(MgnlContext.getLocale()).isEmpty()) {
152                     label += " (" + locale.getDisplayCountry(MgnlContext.getLocale()) + ")";
153                 }
154                 languageSelector.addItem(locale);
155                 languageSelector.setItemCaption(locale, label);
156             }
157             getActionAreaView().setToolbarComponent(languageSelector);
158         }
159     }
160 
161     @Override
162     public void setListener(FormView.Listener listener) {
163         form.setListener(listener);
164     }
165 
166     private void createLocaleSelector() {
167         languageSelector = new LightComboBox();
168         // Language selector must have width 125px (include padding right 10px)
169         languageSelector.setWidth(115, Unit.PIXELS);
170         languageSelector.setDescription(i18n.translate("languageSelector.description"));
171     }
172 
173     protected void updateLocale(Locale locale) {
174         boolean isValid = isValid();
175         form.showValidation(!isValid);
176         if (isValid) {
177             form.setLocale(locale);
178         } else {
179             setCurrentLocale(form.getLocale());
180         }
181     }
182 }