View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.i18nsystem.I18nizer;
37  import info.magnolia.i18nsystem.SimpleTranslator;
38  import info.magnolia.objectfactory.ComponentProvider;
39  import info.magnolia.registry.RegistrationException;
40  import info.magnolia.ui.api.action.ActionDefinition;
41  import info.magnolia.ui.api.availability.AvailabilityChecker;
42  import info.magnolia.ui.api.context.UiContext;
43  import info.magnolia.ui.api.overlay.OverlayCloser;
44  import info.magnolia.ui.dialog.BaseDialogPresenter;
45  import info.magnolia.ui.dialog.Dialog;
46  import info.magnolia.ui.dialog.DialogCloseHandler;
47  import info.magnolia.ui.dialog.DialogView;
48  import info.magnolia.ui.dialog.actionarea.DialogActionExecutor;
49  import info.magnolia.ui.dialog.definition.FormDialogDefinition;
50  import info.magnolia.ui.dialog.registry.DialogDefinitionRegistry;
51  import info.magnolia.ui.form.EditorCallback;
52  import info.magnolia.ui.form.EditorValidator;
53  import info.magnolia.ui.form.FormPresenter;
54  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
55  
56  import java.util.ArrayList;
57  import java.util.Arrays;
58  import java.util.Iterator;
59  import java.util.List;
60  
61  import javax.inject.Inject;
62  
63  import org.apache.commons.lang3.StringUtils;
64  import org.slf4j.Logger;
65  import org.slf4j.LoggerFactory;
66  
67  import com.vaadin.v7.data.Item;
68  
69  /**
70   * Presenter for forms opened inside dialogs.
71   */
72  public class FormDialogPresenterImpl extends BaseDialogPresenter implements FormDialogPresenter, EditorValidator {
73  
74      private static final Logger log = LoggerFactory.getLogger(FormDialogPresenterImpl.class);
75  
76      private EditorCallback callback;
77  
78      private DialogDefinitionRegistry dialogDefinitionRegistry;
79  
80      private ContentConnector contentConnector;
81  
82      private AvailabilityChecker checker;
83  
84      private FormPresenter formPresenter;
85  
86      private FormView formView;
87  
88      private Item item;
89  
90      @Inject
91      public FormDialogPresenterImpl(final DialogDefinitionRegistry dialogDefinitionRegistry, ComponentProvider componentProvider, DialogActionExecutor executor, FormView view, I18nizer i18nizer, SimpleTranslator i18n, AvailabilityChecker checker, ContentConnector contentConnector, FormPresenter formPresenter) {
92          super(componentProvider, executor, view, i18nizer, i18n);
93          this.dialogDefinitionRegistry = dialogDefinitionRegistry;
94          this.checker = checker;
95          this.contentConnector = contentConnector;
96          this.formPresenter = formPresenter;
97          this.componentProvider = componentProvider;
98          this.formView = view;
99      }
100 
101     /**
102      * Constructor backwards compatible with pre-5.3 versions.
103      *
104      * @deprecated since version 5.3.1, use {@link #FormDialogPresenterImpl(DialogDefinitionRegistry, ComponentProvider, DialogActionExecutor, FormView, I18nizer, SimpleTranslator, AvailabilityChecker, ContentConnector, FormPresenter)} instead.
105      */
106     @Deprecated
107     public FormDialogPresenterImpl(DialogDefinitionRegistry dialogDefinitionRegistry, FormBuilder formBuilder, ComponentProvider componentProvider, DialogActionExecutor executor, FormView view, I18nizer i18nizer, SimpleTranslator i18n) {
108         this(dialogDefinitionRegistry, componentProvider, executor, view, i18nizer, i18n, componentProvider.getComponent(AvailabilityChecker.class), componentProvider.getComponent(ContentConnector.class), componentProvider.getComponent(FormPresenter.class));
109     }
110 
111     /**
112      * @deprecated since 5.4.2 - use {@link #FormDialogPresenterImpl(DialogDefinitionRegistry, ComponentProvider, DialogActionExecutor, FormView, I18nizer, SimpleTranslator, AvailabilityChecker, ContentConnector, FormPresenter)} instead.
113      */
114     @Deprecated
115     public FormDialogPresenterImpl(final DialogDefinitionRegistry dialogDefinitionRegistry, FormBuilder formBuilder, ComponentProvider componentProvider, DialogActionExecutor executor, FormView view, I18nizer i18nizer, SimpleTranslator i18n, AvailabilityChecker checker, ContentConnector contentConnector) {
116         this(dialogDefinitionRegistry, componentProvider, executor, view, i18nizer, i18n, checker, contentConnector, componentProvider.getComponent(FormPresenter.class));
117     }
118 
119     @Override
120     public DialogView start(Item item, FormDialogDefinition dialogDefinition, UiContext uiContext, EditorCallback callback, ContentConnector contentConnector) {
121         this.contentConnector = contentConnector;
122         return start(item, dialogDefinition, uiContext, callback);
123     }
124 
125     @Override
126     public DialogView start(Item item, String dialogId, final UiContext uiContext, EditorCallback callback) {
127         try {
128             FormDialogDefinition dialogDefinition = dialogDefinitionRegistry.getDialogDefinition(dialogId);
129             return start(item, dialogDefinition, uiContext, callback);
130         } catch (RegistrationException e) {
131             throw new RuntimeException("No dialogDefinition found for " + dialogId, e);
132         }
133     }
134 
135     /**
136      * Returns a {@link DialogView} containing {@link FormView} as content.
137      * <ul>
138      * <li>Sets the created {@link FormView} as content of the created {@link DialogView}.</li>
139      * </ul>
140      *
141      * @param item
142      * @param dialogDefinition
143      * @param uiContext
144      */
145     @Override
146     public DialogView start(Item item, FormDialogDefinition dialogDefinition, final UiContext uiContext, EditorCallback callback) {
147         this.callback = callback;
148         this.item = item;
149 
150         super.start(dialogDefinition, uiContext);
151         getExecutor().setDialogDefinition(getDefinition());
152         buildView(getDefinition());
153 
154         final OverlayCloser overlayCloser = uiContext.openOverlay(getView(), getView().getModalityLevel());
155         getView().addDialogCloseHandler(new DialogCloseHandler() {
156             @Override
157             public void onDialogClose(DialogView dialogView) {
158                 overlayCloser.close();
159             }
160         });
161         getView().setClosable(true);
162         return getView();
163     }
164 
165     private void buildView(FormDialogDefinition dialogDefinition) {
166         final Dialog dialog = new Dialog(dialogDefinition);
167         formPresenter.presentView(formView, dialogDefinition.getForm(), item, dialog);
168 
169         final String description = dialogDefinition.getDescription();
170         final String label = dialogDefinition.getLabel();
171 
172         if (StringUtils.isNotBlank(description) && !isMessageKey(description)) {
173             getView().setDescription(description);
174         }
175 
176         if (StringUtils.isNotBlank(label) && !isMessageKey(label)) {
177             getView().setCaption(label);
178         }
179     }
180 
181     @Override
182     public FormView getView() {
183         return formView;
184     }
185 
186     @Override
187     public void showValidation(boolean visible) {
188         getView().showValidation(visible);
189     }
190 
191     @Override
192     public boolean isValid() {
193         return formPresenter.isValid();
194     }
195 
196     @Override
197     protected DialogActionExecutor getExecutor() {
198         return (DialogActionExecutor) super.getExecutor();
199     }
200 
201     @Override
202     protected Iterable<ActionDefinition> filterActions() {
203         List<ActionDefinition> actions = new ArrayList<ActionDefinition>(getDefinition().getActions().values());
204         Iterator<ActionDefinition> it = actions.iterator();
205         Object itemId = contentConnector.getItemId(item);
206         if (itemId != null) {
207             while (it.hasNext()) {
208                 ActionDefinition action = it.next();
209                 if (!checker.isAvailable(action.getAvailability(), Arrays.asList(itemId))) {
210                     it.remove();
211                 }
212             }
213         } else {
214             log.info("Could not resolve itemId for item {}:{}, will not restrict availability of dialog actions.", item.getClass().getName(), item);
215         }
216         return actions;
217     }
218 
219     @Override
220     protected Object[] getActionParameters(String actionName) {
221         return new Object[] { this, item, callback };
222     }
223 
224     @Override
225     protected FormDialogDefinition getDefinition() {
226         return (FormDialogDefinition) super.getDefinition();
227     }
228 
229     /**
230      * @deprecated is a hack and should not be used. See MGNLUI-2207.
231      */
232     private boolean isMessageKey(final String text) {
233         return !text.contains(" ") && text.contains(".") && !text.endsWith(".");
234     }
235 
236     @Override
237     protected void onCancel () {
238         if (callback != null) {
239             callback.onCancel();
240         }
241     }
242 
243     @Override
244     public Item getRelatedItem() {
245         return item;
246     }
247 }