View Javadoc
1   /**
2    * This file Copyright (c) 2012-2015 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.context.UiContext;
42  import info.magnolia.ui.api.overlay.OverlayCloser;
43  import info.magnolia.ui.dialog.BaseDialogPresenter;
44  import info.magnolia.ui.dialog.Dialog;
45  import info.magnolia.ui.dialog.DialogCloseHandler;
46  import info.magnolia.ui.dialog.DialogView;
47  import info.magnolia.ui.dialog.actionarea.DialogActionExecutor;
48  import info.magnolia.ui.dialog.definition.FormDialogDefinition;
49  import info.magnolia.ui.dialog.registry.DialogDefinitionRegistry;
50  import info.magnolia.ui.form.EditorCallback;
51  import info.magnolia.ui.form.EditorValidator;
52  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
53  
54  import java.util.LinkedList;
55  import java.util.List;
56  
57  import javax.inject.Inject;
58  
59  import org.apache.commons.lang.StringUtils;
60  
61  import com.vaadin.data.Item;
62  
63  /**
64   * Presenter for forms opened inside dialogs.
65   */
66  public class FormDialogPresenterImpl extends BaseDialogPresenter implements FormDialogPresenter, EditorValidator {
67  
68      private EditorCallback callback;
69  
70      private DialogDefinitionRegistry dialogDefinitionRegistry;
71  
72      private FormBuilder formBuilder;
73  
74      private FormView formView;
75  
76      private Item item;
77  
78      @Inject
79      public FormDialogPresenterImpl(final DialogDefinitionRegistry dialogDefinitionRegistry, FormBuilder formBuilder, ComponentProvider componentProvider, DialogActionExecutor executor, FormView view, I18nizer i18nizer, SimpleTranslator i18n) {
80          super(componentProvider, executor, view, i18nizer, i18n);
81          this.dialogDefinitionRegistry = dialogDefinitionRegistry;
82          this.formBuilder = formBuilder;
83          this.componentProvider = componentProvider;
84          this.formView = view;
85      }
86  
87      @Override
88      public DialogView start(final Item item, String dialogId, final UiContext uiContext, EditorCallback callback) {
89          try {
90              FormDialogDefinition dialogDefinition = dialogDefinitionRegistry.getDialogDefinition(dialogId);
91              return start(item, dialogDefinition, uiContext, callback);
92          } catch (RegistrationException e) {
93              throw new RuntimeException("No dialogDefinition found for " + dialogId, e);
94          }
95      }
96  
97      /**
98       * Returns a {@link DialogView} containing {@link FormView} as content.
99       * <ul>
100      * <li>Sets the created {@link FormView} as content of the created {@link DialogView}.</li>
101      * </ul>
102      *
103      * @param item passed on to{@link info.magnolia.ui.dialog.formdialog.FormDialogPresenter}
104      * @param dialogDefinition
105      * @param uiContext
106      */
107     @Override
108     public DialogView start(final Item item, FormDialogDefinition dialogDefinition, final UiContext uiContext, EditorCallback callback) {
109         start(dialogDefinition, uiContext);
110         this.callback = callback;
111         this.item = item;
112 
113         getExecutor().setDialogDefinition(getDefinition());
114 
115         buildView((FormDialogDefinition) getDefinition());
116 
117         final OverlayCloser overlayCloser = uiContext.openOverlay(getView());
118         getView().addDialogCloseHandler(new DialogCloseHandler() {
119             @Override
120             public void onDialogClose(DialogView dialogView) {
121                 overlayCloser.close();
122             }
123         });
124         getView().setClosable(true);
125         return getView();
126     }
127 
128     private void buildView(FormDialogDefinition dialogDefinition) {
129         final Dialog dialog = new Dialog(dialogDefinition);
130 
131         formBuilder.buildForm(getView(), dialogDefinition.getForm(), item, dialog);
132 
133         final String description = dialogDefinition.getDescription();
134         final String label = dialogDefinition.getLabel();
135 
136         if (StringUtils.isNotBlank(description) && !isMessageKey(description)) {
137             getView().setDescription(description);
138         }
139 
140         if (StringUtils.isNotBlank(label) && !isMessageKey(label)) {
141             getView().setCaption(label);
142         }
143     }
144 
145     @Override
146     public FormView getView() {
147         return formView;
148     }
149 
150     @Override
151     public void showValidation(boolean visible) {
152         getView().showValidation(visible);
153     }
154 
155     @Override
156     public boolean isValid() {
157         return getView().isValid();
158     }
159 
160     @Override
161     protected DialogActionExecutor getExecutor() {
162         return (DialogActionExecutor) super.getExecutor();
163     }
164 
165     @Override
166     protected Iterable<ActionDefinition> filterActions() {
167         List<ActionDefinition> result = new LinkedList<ActionDefinition>();
168         boolean isJcrItemAdapter = (item instanceof JcrItemAdapter);
169         for (ActionDefinition action : getDefinition().getActions().values()) {
170             if (!isJcrItemAdapter || getExecutor().isAvailable(action.getName(), ((JcrItemAdapter) item).getJcrItem())) {
171                 result.add(action);
172             }
173         }
174         return result;
175     }
176 
177     @Override
178     protected Object[] getActionParameters(String actionName) {
179         return new Object[] { this, item, callback };
180     }
181 
182     /**
183      * @deprecated is a hack and should not be used. See MGNLUI-2207.
184      */
185     private boolean isMessageKey(final String text) {
186         return !text.contains(" ") && text.contains(".") && !text.endsWith(".");
187     }
188 
189 }