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;
35  
36  import info.magnolia.i18nsystem.I18nParentable;
37  import info.magnolia.i18nsystem.I18nizer;
38  import info.magnolia.i18nsystem.SimpleTranslator;
39  import info.magnolia.objectfactory.ComponentProvider;
40  import info.magnolia.ui.api.action.ActionDefinition;
41  import info.magnolia.ui.api.action.ActionExecutionException;
42  import info.magnolia.ui.api.action.ActionExecutor;
43  import info.magnolia.ui.api.app.AppContext;
44  import info.magnolia.ui.api.app.SubAppContext;
45  import info.magnolia.ui.api.context.UiContext;
46  import info.magnolia.ui.api.message.Message;
47  import info.magnolia.ui.api.message.MessageType;
48  import info.magnolia.ui.api.overlay.ConfirmationCallback;
49  import info.magnolia.ui.api.overlay.OverlayLayer.ModalityLevel;
50  import info.magnolia.ui.dialog.actionarea.ActionAreaPresenter;
51  import info.magnolia.ui.dialog.actionarea.ActionListener;
52  import info.magnolia.ui.dialog.actionarea.EditorActionAreaPresenter;
53  import info.magnolia.ui.dialog.actionarea.view.EditorActionAreaView;
54  import info.magnolia.ui.dialog.definition.DialogDefinition;
55  import info.magnolia.ui.framework.ioc.AdmincentralFlavour;
56  import info.magnolia.ui.vaadin.dialog.BaseDialog;
57  import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
58  
59  import javax.inject.Inject;
60  
61  import org.slf4j.Logger;
62  import org.slf4j.LoggerFactory;
63  
64  import com.vaadin.event.ShortcutAction.KeyCode;
65  import com.vaadin.event.ShortcutAction.ModifierKey;
66  import com.vaadin.event.ShortcutListener;
67  import com.vaadin.server.WebBrowser;
68  import com.vaadin.ui.Panel;
69  import com.vaadin.ui.UI;
70  
71  /**
72   * Base implementation of {@link DialogPresenter}.
73   */
74  public class BaseDialogPresenter implements DialogPresenter, ActionListener {
75  
76      private Logger log = LoggerFactory.getLogger(getClass());
77  
78      private DialogView view;
79  
80      protected ComponentProvider componentProvider;
81  
82      private ActionExecutor executor;
83  
84      private EditorActionAreaPresenter editorActionAreaPresenter;
85  
86      private final I18nizer i18nizer;
87  
88      private final SimpleTranslator i18n;
89  
90      private UiContext uiContext;
91  
92      private DialogDefinition definition;
93  
94      private boolean isExecutingAction;
95  
96      @Inject
97      public BaseDialogPresenter(ComponentProvider componentProvider, ActionExecutor executor, DialogView view, I18nizer i18nizer, SimpleTranslator i18n) {
98          this.componentProvider = componentProvider;
99          this.executor = executor;
100         this.view = view;
101         this.i18nizer = i18nizer;
102         this.i18n = i18n;
103     }
104 
105     @Override
106     public DialogView getView() {
107         return view;
108     }
109 
110     @Override
111     public ActionAreaPresenter getActionArea() {
112         return editorActionAreaPresenter;
113     }
114 
115     @Override
116     public void closeDialog() {
117         view.close();
118     }
119 
120     @Override
121     public void addShortcut(final String actionName, final int keyCode, final int... modifiers) {
122 
123         view.addShortcut(new ShortcutListener(actionName, keyCode, modifiers) {
124             @Override
125             public void handleAction(Object sender, Object target) {
126                 executeAction(actionName, new Object[0]);
127             }
128         });
129     }
130 
131     @Override
132     public DialogView start(DialogDefinition dialogDefinition, UiContext uiContext) {
133         // Additional close handler to check for cases when the dialog isn't closed from an action (shortcut, dialog close icon), and process callback accordingly
134         getView().addDialogCloseHandler(new DialogCloseHandler() {
135 
136             @Override
137             public void onDialogClose(DialogView dialogView) {
138                 if (!isExecutingAction) {
139                     onCancel();
140                 }
141             }
142         });
143 
144         this.uiContext = uiContext;
145         // ChooseDialogDefinition is already enhanced as it is obtained via ContentAppDescriptor.getChooseDialog() at ContentApp.openChooseDialog(..)
146         if (dialogDefinition instanceof I18nParentable) {
147             this.definition = dialogDefinition;
148         } else {
149             this.definition = i18nizer.decorate(dialogDefinition);
150         }
151 
152         this.editorActionAreaPresenter = componentProvider.newInstance(definition.getActionArea().getPresenterClass());
153         EditorActionAreaView editorActionAreaView = editorActionAreaPresenter.start(filterActions(), definition.getActionArea(), this, uiContext);
154 
155         // Set modifier key based on OS.
156         int osSpecificModifierKey;
157         UI ui = UI.getCurrent();
158         if (ui != null) {
159             WebBrowser browser = ui.getPage().getWebBrowser();
160             if (browser.isWindows()) {
161                 osSpecificModifierKey = ModifierKey.CTRL;
162             } else {
163                 // osx and linux
164                 osSpecificModifierKey = ModifierKey.META;
165             }
166 
167             if (definition.getActions().containsKey(BaseDialog.COMMIT_ACTION_NAME)) {
168                 addShortcut(BaseDialog.COMMIT_ACTION_NAME, KeyCode.S, osSpecificModifierKey);
169             }
170             if (definition.getActions().containsKey(BaseDialog.CANCEL_ACTION_NAME)) {
171                 addShortcut(BaseDialog.CANCEL_ACTION_NAME, KeyCode.W, osSpecificModifierKey);
172             }
173             if (definition.getModalityLevel() == ModalityLevel.LIGHT) {
174                 view.addShortcut(new CloseDialogShortcutListener(KeyCode.ESCAPE));
175             }
176 
177         } else {
178             log.warn("The current Vaadin UI was null when starting {}, as a result dialog keyboard shortcuts will not work.", this);
179         }
180 
181         view.addShortcut(new CloseDialogAfterConfirmationShortcutListener(KeyCode.ESCAPE));
182         view.addShortcut(new CommitDialogShortcutListener(KeyCode.ENTER));
183 
184         this.view.setActionAreaView(editorActionAreaView);
185         this.view.setModalityLevel(definition.getModalityLevel());
186         // wide dialogs are unsupported since 6.0
187         if (definition.isWide() && AdmincentralFlavour.get().isM5()){
188             this.view.setWide(true);
189         }
190         return this.view;
191     }
192 
193     protected Iterable<ActionDefinition> filterActions() {
194         return getDefinition().getActions().values();
195     }
196 
197     protected Object[] getActionParameters(String actionName) {
198         return new Object[] { this };
199     }
200 
201     @Override
202     public void onActionFired(String actionName, Object... actionContextParams) {
203         executeAction(actionName, actionContextParams);
204     }
205 
206     protected void executeAction(String actionName, Object[] actionContextParams) {
207         isExecutingAction = true;
208         Object[] providedParameters = getActionParameters(actionName);
209         Object[] combinedParameters = new Object[providedParameters.length + actionContextParams.length];
210         System.arraycopy(providedParameters, 0, combinedParameters, 0, providedParameters.length);
211         System.arraycopy(actionContextParams, 0, combinedParameters, providedParameters.length, actionContextParams.length);
212         try {
213             executor.execute(actionName, combinedParameters);
214         } catch (ActionExecutionException e) {
215             String exceptionStatement = i18n.translate("ui-dialog.actionexecutionerror.basemessage");
216             Message error = new Message(MessageType.ERROR, exceptionStatement, e.getMessage());
217             log.error(exceptionStatement, e);
218             if (uiContext instanceof AppContext) {
219                 ((AppContext) uiContext).sendLocalMessage(error);
220             } else if (uiContext instanceof SubAppContext) {
221                 ((SubAppContext) uiContext).getAppContext().sendLocalMessage(error);
222             }
223         } finally {
224             isExecutingAction = false;
225         }
226     }
227 
228     protected DialogDefinition getDefinition() {
229         return definition;
230     }
231 
232     protected ActionExecutor getExecutor() {
233         return executor;
234     }
235 
236     protected I18nizer getI18nizer() {
237         return i18nizer;
238     }
239 
240     /**
241      * A shortcut listener used to close the dialog.
242      */
243     protected class CloseDialogShortcutListener extends ShortcutListener {
244 
245         public CloseDialogShortcutListener(int keyCode, int... modifierKey) {
246             super("", keyCode, modifierKey);
247         }
248 
249         @Override
250         public void handleAction(Object sender, Object target) {
251             closeDialog();
252         }
253     }
254 
255     /**
256      * A shortcut listener which opens a confirmation to confirm closing the dialog.
257      */
258     protected class CloseDialogAfterConfirmationShortcutListener extends ShortcutListener {
259 
260         public CloseDialogAfterConfirmationShortcutListener(int keyCode, int... modifierKey) {
261             super("", keyCode, modifierKey);
262         }
263 
264         @Override
265         public void handleAction(Object sender, Object target) {
266             uiContext.openConfirmation(
267                     MessageStyleTypeEnum.WARNING, i18n.translate("ui-dialog.closeConfirmation.title"), i18n.translate("ui-dialog.closeConfirmation.body"), i18n.translate("ui-dialog.closeConfirmation.confirmButton"), i18n.translate("ui-dialog.cancelButton"), false,
268                     new ConfirmationCallback() {
269                         @Override
270                         public void onSuccess() {
271                             closeDialog();
272                         }
273 
274                         @Override
275                         public void onCancel() {
276                             if (getView() instanceof Panel) {
277                                 ((Panel) getView()).focus();
278                             }
279                         }
280                     });
281         }
282     }
283 
284     /**
285      * A shortcut listener used to commit the dialog.
286      */
287     protected class CommitDialogShortcutListener extends ShortcutListener {
288 
289         public CommitDialogShortcutListener(int keyCode, int... modifierKey) {
290             super("", keyCode, modifierKey);
291         }
292 
293         @Override
294         public void handleAction(Object sender, Object target) {
295             // textareas are excluded on the client-side, see 'EnterFriendlyShortcutActionHandler', used in PanelConnector
296             executeAction(BaseDialog.COMMIT_ACTION_NAME, new Object[0]);
297         }
298     }
299 
300     protected void onCancel() {
301     }
302 }