View Javadoc

1   /**
2    * This file Copyright (c) 2012-2013 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.I18nizer;
37  import info.magnolia.i18nsystem.SimpleTranslator;
38  import info.magnolia.objectfactory.ComponentProvider;
39  import info.magnolia.ui.api.action.ActionDefinition;
40  import info.magnolia.ui.api.action.ActionExecutionException;
41  import info.magnolia.ui.api.action.ActionExecutor;
42  import info.magnolia.ui.api.app.AppContext;
43  import info.magnolia.ui.api.app.SubAppContext;
44  import info.magnolia.ui.api.context.UiContext;
45  import info.magnolia.ui.api.message.Message;
46  import info.magnolia.ui.api.message.MessageType;
47  import info.magnolia.ui.dialog.actionarea.ActionAreaPresenter;
48  import info.magnolia.ui.dialog.actionarea.ActionListener;
49  import info.magnolia.ui.dialog.actionarea.EditorActionAreaPresenter;
50  import info.magnolia.ui.dialog.actionarea.view.EditorActionAreaView;
51  import info.magnolia.ui.dialog.definition.DialogDefinition;
52  import info.magnolia.ui.vaadin.dialog.BaseDialog;
53  
54  import javax.inject.Inject;
55  
56  import net.sf.cglib.proxy.Enhancer;
57  
58  import org.slf4j.Logger;
59  import org.slf4j.LoggerFactory;
60  
61  import com.vaadin.event.ShortcutAction.KeyCode;
62  import com.vaadin.event.ShortcutAction.ModifierKey;
63  import com.vaadin.event.ShortcutListener;
64  import com.vaadin.server.WebBrowser;
65  import com.vaadin.ui.UI;
66  
67  /**
68   * Base implementation of {@link DialogPresenter}.
69   */
70  public class BaseDialogPresenter implements DialogPresenter, ActionListener {
71  
72      private Logger log = LoggerFactory.getLogger(getClass());
73  
74      private DialogView view;
75  
76      protected ComponentProvider componentProvider;
77  
78      private ActionExecutor executor;
79  
80      private EditorActionAreaPresenter editorActionAreaPresenter;
81  
82      private final I18nizer i18nizer;
83  
84      private final SimpleTranslator i18n;
85  
86      private UiContext uiContext;
87  
88      private DialogDefinition definition;
89  
90      @Inject
91      public BaseDialogPresenter(ComponentProvider componentProvider, ActionExecutor executor, DialogView view, I18nizer i18nizer, SimpleTranslator i18n) {
92          this.componentProvider = componentProvider;
93          this.executor = executor;
94          this.view = view;
95          this.i18nizer = i18nizer;
96          this.i18n = i18n;
97      }
98  
99      @Override
100     public DialogView getView() {
101         return view;
102     }
103 
104     @Override
105     public ActionAreaPresenter getActionArea() {
106         return editorActionAreaPresenter;
107     }
108 
109     @Override
110     public void closeDialog() {
111         view.close();
112     }
113 
114     @Override
115     public void addShortcut(final String actionName, final int keyCode, final int... modifiers) {
116         view.addShortcut(new ShortcutListener(actionName, keyCode, modifiers) {
117             @Override
118             public void handleAction(Object sender, Object target) {
119                 executeAction(actionName, new Object[0]);
120             }
121         });
122     }
123 
124     @Override
125     public DialogView start(DialogDefinition definition, UiContext uiContext) {
126         this.uiContext = uiContext;
127         // ChooseDialogDefinition is already enhanced as it is obtained via ContentAppDescriptor.getChooseDialog() at ContentApp.openChooseDialog(..)
128         if (Enhancer.isEnhanced(definition.getClass())) {
129             this.definition = definition;
130         } else {
131             this.definition = i18nizer.decorate(definition);
132         }
133         this.editorActionAreaPresenter = componentProvider.newInstance(definition.getActionArea().getPresenterClass());
134         EditorActionAreaView editorActionAreaView = editorActionAreaPresenter.start(filterActions(), definition.getActionArea(), this, uiContext);
135 
136         // Set modifier key based on OS.
137         int osSpecificModifierKey;
138         WebBrowser browser = UI.getCurrent().getSession().getBrowser();
139         if (browser.isWindows()) {
140             osSpecificModifierKey = ModifierKey.CTRL;
141         } else {
142             // osx and linux
143             osSpecificModifierKey = ModifierKey.META;
144         }
145 
146         if (definition.getActions().containsKey(BaseDialog.COMMIT_ACTION_NAME)) {
147             addShortcut(BaseDialog.COMMIT_ACTION_NAME, KeyCode.S, osSpecificModifierKey);
148         }
149         if (definition.getActions().containsKey(BaseDialog.CANCEL_ACTION_NAME)) {
150             addShortcut(BaseDialog.CANCEL_ACTION_NAME, KeyCode.W, osSpecificModifierKey);
151         }
152         this.view.setActionAreaView(editorActionAreaView);
153         return this.view;
154     }
155 
156     protected Iterable<ActionDefinition> filterActions() {
157         return getDefinition().getActions().values();
158     }
159 
160     protected Object[] getActionParameters(String actionName) {
161         return new Object[]{this};
162     }
163 
164     @Override
165     public void onActionFired(String actionName, Object... actionContextParams) {
166         executeAction(actionName, actionContextParams);
167     }
168 
169     protected void executeAction(String actionName, Object[] actionContextParams) {
170         Object[] providedParameters = getActionParameters(actionName);
171         Object[] combinedParameters = new Object[providedParameters.length + actionContextParams.length];
172         System.arraycopy(providedParameters, 0, combinedParameters, 0, providedParameters.length);
173         System.arraycopy(actionContextParams, 0, combinedParameters, providedParameters.length, actionContextParams.length);
174         try {
175             executor.execute(actionName, combinedParameters);
176         } catch (ActionExecutionException e) {
177             String exceptionStatement = i18n.translate("ui-dialog.actionexecutionerror.basemessage");
178             Message error = new Message(MessageType.ERROR, exceptionStatement, e.getMessage());
179             log.error(exceptionStatement, e);
180             if (uiContext instanceof AppContext) {
181                 ((AppContext) uiContext).broadcastMessage(error);
182             } else if (uiContext instanceof SubAppContext) {
183                 ((SubAppContext) uiContext).getAppContext().broadcastMessage(error);
184             }
185 
186         }
187     }
188 
189     protected DialogDefinition getDefinition() {
190         return definition;
191     }
192 
193     protected ActionExecutor getExecutor() {
194         return executor;
195     }
196 
197     protected I18nizer getI18nizer() {
198         return i18nizer;
199     }
200 }