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