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.pages.app.editor;
35  
36  import info.magnolia.event.EventBus;
37  import info.magnolia.i18nsystem.SimpleTranslator;
38  import info.magnolia.pages.app.editor.event.ComponentMoveEvent;
39  import info.magnolia.pages.app.editor.event.NodeSelectedEvent;
40  import info.magnolia.ui.api.action.ActionExecutionException;
41  import info.magnolia.ui.api.action.ActionExecutor;
42  import info.magnolia.ui.api.app.SubAppContext;
43  import info.magnolia.ui.api.app.SubAppEventBus;
44  import info.magnolia.ui.api.event.ContentChangedEvent;
45  import info.magnolia.ui.api.message.Message;
46  import info.magnolia.ui.api.message.MessageType;
47  import info.magnolia.ui.vaadin.editor.PageEditorListener;
48  import info.magnolia.ui.vaadin.editor.PageEditorView;
49  import info.magnolia.ui.vaadin.gwt.client.shared.AbstractElement;
50  import info.magnolia.ui.vaadin.gwt.client.shared.PageEditorParameters;
51  
52  import javax.inject.Inject;
53  import javax.inject.Named;
54  import javax.inject.Singleton;
55  
56  import org.slf4j.Logger;
57  import org.slf4j.LoggerFactory;
58  
59  /**
60   * Presenter for the server side {@link PageEditorView}.
61   * Serves multiple methods for actions triggered from the page editor.
62   */
63  @Singleton
64  public class PageEditorPresenter implements PageEditorListener {
65  
66      private static final Logger log = LoggerFactory.getLogger(PageEditorPresenter.class);
67  
68      private final ActionExecutor actionExecutor;
69      private final PageEditorView view;
70      private final EventBus subAppEventBus;
71      private final SubAppContext subAppContext;
72      private final SimpleTranslator i18n;
73  
74      private AbstractElement selectedElement;
75      private boolean moving = false;
76      private Listener listener;
77  
78      @Inject
79      public PageEditorPresenter(final ActionExecutor actionExecutor, PageEditorView view, final @Named(SubAppEventBus.NAME) EventBus subAppEventBus,
80              SubAppContext subAppContext, SimpleTranslator i18n) {
81          this.actionExecutor = actionExecutor;
82          this.view = view;
83          this.subAppEventBus = subAppEventBus;
84          this.subAppContext = subAppContext;
85          this.i18n = i18n;
86          registerHandlers();
87      }
88  
89      public PageEditorView start() {
90          view.setListener(this);
91          return view;
92      }
93  
94      private void registerHandlers() {
95          subAppEventBus.addHandler(ContentChangedEvent.class, new ContentChangedEvent.Handler() {
96  
97              @Override
98              public void onContentChanged(ContentChangedEvent event) {
99                  view.refresh();
100             }
101         });
102         subAppEventBus.addHandler(ComponentMoveEvent.class, new ComponentMoveEvent.Handler() {
103             @Override
104             public void onMove(ComponentMoveEvent event) {
105                 moving = event.isStart();
106                 if (moving) {
107                     view.startMoveComponent();
108                 } else if (event.isServerSide()) {
109                     view.cancelMoveComponent();
110                 }
111 
112                 listener.onMove();
113             }
114         });
115 
116     }
117 
118     @Override
119     public void onElementSelect(AbstractElement selectedElement) {
120         this.selectedElement = selectedElement;
121         subAppEventBus.fireEvent(new NodeSelectedEvent(selectedElement));
122     }
123 
124     @Override
125     public void onAction(String actionName, Object... args) {
126         try {
127             actionExecutor.execute(actionName, args);
128         } catch (ActionExecutionException e) {
129             Message error = new Message(MessageType.ERROR, i18n.translate("pages.pageEditorPresenter.actionExecutionError.message"), e.getMessage());
130             log.error("An error occurred while executing action [{}]", actionName, e);
131             subAppContext.getAppContext().sendLocalMessage(error);
132         }
133     }
134 
135     public AbstractElement getSelectedElement() {
136         return selectedElement;
137     }
138 
139     public void loadPageEditor(PageEditorParameters parameters) {
140         view.load(parameters);
141     }
142 
143     public void updateParameters(PageEditorParameters parameters) {
144         view.update(parameters);
145     }
146 
147     public boolean isMoving() {
148         return moving;
149     }
150 
151     public void setListener(Listener listener) {
152         this.listener = listener;
153     }
154 
155     /**
156      * Listener interface to call {@link PageEditorPresenter}.
157      */
158     interface Listener {
159         void onMove();
160     }
161 }