View Javadoc

1   /**
2    * This file Copyright (c) 2012-2014 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.contentapp.detail;
35  
36  import info.magnolia.i18nsystem.SimpleTranslator;
37  import info.magnolia.ui.actionbar.ActionbarPresenter;
38  import info.magnolia.ui.actionbar.ActionbarView;
39  import info.magnolia.ui.api.action.ActionExecutionException;
40  import info.magnolia.ui.api.action.ActionExecutor;
41  import info.magnolia.ui.api.app.AppContext;
42  import info.magnolia.ui.api.app.SubAppContext;
43  import info.magnolia.ui.api.message.Message;
44  import info.magnolia.ui.api.message.MessageType;
45  import info.magnolia.ui.api.overlay.ConfirmationCallback;
46  import info.magnolia.ui.api.view.View;
47  import info.magnolia.ui.contentapp.definition.EditorDefinition;
48  import info.magnolia.ui.vaadin.dialog.BaseDialog;
49  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
50  import info.magnolia.ui.vaadin.integration.contentconnector.SupportsCreation;
51  import info.magnolia.ui.vaadin.integration.contentconnector.SupportsVersions;
52  import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
53  
54  import java.util.HashMap;
55  
56  import javax.inject.Inject;
57  
58  import org.apache.commons.lang3.StringUtils;
59  import org.slf4j.Logger;
60  import org.slf4j.LoggerFactory;
61  
62  import com.vaadin.event.ShortcutAction.KeyCode;
63  import com.vaadin.event.ShortcutListener;
64  import com.vaadin.ui.Panel;
65  
66  /**
67   * Presenter for the workbench displayed in the {@link info.magnolia.ui.contentapp.detail.DetailSubApp}.
68   * Contains the {@link ActionbarPresenter} for handling action events and the {@link DetailPresenter} for displaying the actual item.
69   */
70  public class DetailEditorPresenter implements DetailEditorView.Listener, ActionbarPresenter.Listener {
71  
72      private static final Logger log = LoggerFactory.getLogger(DetailEditorPresenter.class);
73  
74      private final ActionExecutor actionExecutor;
75      private final AppContext appContext;
76      private final SubAppContext subAppContext;
77      private final DetailEditorView view;
78      private final DetailPresenter detailPresenter;
79      private final ActionbarPresenter actionbarPresenter;
80      private final DetailSubAppDescriptor subAppDescriptor;
81      private final EditorDefinition editorDefinition;
82      private final SimpleTranslator i18n;
83      private String nodePath;
84      private ContentConnector contentConnector;
85  
86      @Inject
87      public DetailEditorPresenter(final ActionExecutor actionExecutor, final SubAppContext subAppContext, final DetailEditorView view, final DetailPresenter detailPresenter, final ActionbarPresenter actionbarPresenter, final SimpleTranslator i18n) {
88          this.actionExecutor = actionExecutor;
89          this.view = view;
90          this.detailPresenter = detailPresenter;
91          this.actionbarPresenter = actionbarPresenter;
92          this.appContext = subAppContext.getAppContext();
93          this.subAppContext = subAppContext;
94          this.subAppDescriptor = (DetailSubAppDescriptor) subAppContext.getSubAppDescriptor();
95          this.editorDefinition = subAppDescriptor.getEditor();
96          this.i18n = i18n;
97      }
98  
99      public View start(String nodePath, DetailView.ViewType viewType, ContentConnector contentConnector) {
100 
101         return start(nodePath, viewType, contentConnector, null);
102     }
103 
104     public View start(String nodePath, DetailView.ViewType viewType, ContentConnector contentConnector, String versionName) {
105         this.contentConnector = contentConnector;
106         this.nodePath = nodePath;
107 
108         view.setListener(this);
109         Object itemId = contentConnector.getItemIdByUrlFragment(nodePath);
110 
111         if (contentConnector.canHandleItem(itemId)) {
112             if (StringUtils.isNotEmpty(versionName) && DetailView.ViewType.VIEW.equals(viewType) && contentConnector instanceof SupportsVersions) {
113                 itemId = ((SupportsVersions) contentConnector).getItemVersion(itemId, versionName);
114             }
115         } else {
116             if (contentConnector instanceof SupportsCreation) {
117                 Object parentId = contentConnector.getItemIdByUrlFragment(StringUtils.substringBeforeLast(nodePath, "/"));
118                 itemId = ((SupportsCreation) contentConnector).getNewItemId(parentId, editorDefinition.getNodeType().getName());
119             }
120         }
121 
122         DetailView itemView = detailPresenter.start(editorDefinition, viewType, itemId);
123 
124         view.setItemView(itemView);
125         actionbarPresenter.setListener(this);
126         ActionbarView actionbar = actionbarPresenter.start(subAppDescriptor.getActionbar(), subAppDescriptor.getActions());
127 
128         view.setActionbarView(actionbar);
129 
130         detailPresenter.addShortcut(new CloseEditorAfterConfirmationShortcutListener(KeyCode.ESCAPE, itemView));
131         detailPresenter.addShortcut(new CommitDialogShortcutListener(KeyCode.ENTER));
132 
133         if (editorDefinition.isWide()){
134             itemView.setWide(true);
135         }
136         return view;
137     }
138 
139     public View update(DetailLocation location) {
140         return this.start(location.getNodePath(), location.getViewType(), contentConnector, location.getVersion());
141     }
142 
143     public String getNodePath() {
144         return nodePath;
145     }
146 
147     public ActionbarPresenter getActionbarPresenter() {
148         return actionbarPresenter;
149     }
150 
151     @Override
152     public void onViewTypeChanged(final DetailView.ViewType viewType) {
153     }
154 
155     @Override
156     public void onActionbarItemClicked(String actionName) {
157         try {
158            actionExecutor.execute(actionName, detailPresenter.getItem());
159         }
160         catch (ActionExecutionException e) {
161             Message error = new Message(MessageType.ERROR, i18n.translate("ui-contentapp.error.action.execution"), e.getMessage());
162             appContext.sendLocalMessage(error);
163         }
164     }
165 
166     /**
167      * A shortcut listener which opens a confirmation to confirm closing the DetailEditor.
168      */
169     protected class CloseEditorAfterConfirmationShortcutListener extends ShortcutListener {
170 
171         private final View itemView;
172 
173         public CloseEditorAfterConfirmationShortcutListener(int keyCode, View itemView, int... modifierKey) {
174             super("", keyCode, modifierKey);
175             this.itemView = itemView;
176         }
177 
178         @Override
179         public void handleAction(Object sender, Object target) {
180             subAppContext.openConfirmation(
181                     MessageStyleTypeEnum.WARNING, i18n.translate("ui-contentapp.detailEditorPresenter.closeConfirmation.title"), i18n.translate("ui-dialog.closeConfirmation.body"), i18n.translate("ui-dialog.closeConfirmation.confirmButton"), i18n.translate("ui-dialog.cancelButton"), false,
182                     new ConfirmationCallback() {
183                         @Override
184                         public void onSuccess() {
185                             detailPresenter.onActionFired(BaseDialog.CANCEL_ACTION_NAME, new HashMap<String, Object>());
186                         }
187 
188                         @Override
189                         public void onCancel() {
190                             if (itemView.asVaadinComponent() instanceof Panel) {
191                                 ((Panel) itemView.asVaadinComponent()).focus();
192                             }
193                         }
194                     });
195         }
196     }
197 
198     /**
199      * A shortcut listener used to commit the DetailEditor if a text area does not have focus.
200      */
201     protected class CommitDialogShortcutListener extends ShortcutListener {
202 
203         public CommitDialogShortcutListener(int keyCode, int... modifierKey) {
204             super("", keyCode, modifierKey);
205         }
206 
207         @Override
208         public void handleAction(Object sender, Object target) {
209             // textareas are excluded on the client-side, see 'EnterFriendlyShortcutActionHandler', used in PanelConnector
210             detailPresenter.onActionFired(BaseDialog.COMMIT_ACTION_NAME, new HashMap<String, Object>());
211         }
212     }
213 
214 }