View Javadoc
1   /**
2    * This file Copyright (c) 2021 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.vaadin.editor;
35  
36  import com.vaadin.server.AbstractClientConnector;
37  import com.vaadin.server.AbstractExtension;
38  import info.magnolia.ui.vaadin.gwt.client.rpc.PageEditorClientRpc;
39  import info.magnolia.ui.vaadin.gwt.client.rpc.PageEditorServerRpc;
40  import info.magnolia.ui.vaadin.gwt.client.shared.AreaElement;
41  import info.magnolia.ui.vaadin.gwt.client.shared.ComponentElement;
42  import info.magnolia.ui.vaadin.gwt.client.shared.ErrorType;
43  import info.magnolia.ui.vaadin.gwt.client.shared.PageElement;
44  
45  import java.util.Map;
46  
47  /**
48   * An extension for listening to Post Messages in a Vaadin application and
49   * responding to them.
50   */
51  @SuppressWarnings("serial")
52  public class PostMessageExtension extends AbstractExtension {
53  
54      private PageEditorListener listener;
55  
56      private PageEditorServerRpcrpc/PageEditorServerRpc.html#PageEditorServerRpc">PageEditorServerRpc rpc = new PageEditorServerRpc() {
57          @Override
58          public void editComponent(ComponentElement element) {
59              listener.onAction(PageEditorListener.ACTION_EDIT_COMPONENT, element);
60          }
61  
62          @Override
63          public void editArea(AreaElement element) {
64              listener.onAction(PageEditorListener.ACTION_EDIT_ELEMENT, element);
65          }
66  
67          @Override
68          public void newComponent(AreaElement element) {
69              listener.onElementSelect(element);
70              listener.onAction(PageEditorListener.ACTION_ADD_COMPONENT, element);
71          }
72  
73          @Override
74          public void newArea(AreaElement element) {
75              listener.onAction(PageEditorListener.ACTION_ADD_AREA, element);
76          }
77  
78          @Override
79          public void selectExternalPage() {
80              listener.onExternalPageSelect();
81          }
82  
83          @Override
84          public void startMoveComponent(ComponentElement element) {
85              listener.onAction(PageEditorListener.ACTION_START_MOVE_COMPONENT, element);
86          }
87  
88          @Override
89          public void stopMoveComponent() {
90              listener.onAction(PageEditorListener.ACTION_STOP_MOVE_COMPONENT);
91          }
92  
93          @Override
94          public void sortComponent(AreaElement element) {
95              listener.onAction(PageEditorListener.ACTION_SORT_COMPONENT, element);
96          }
97  
98          @Override
99          public void onClientAction(ComponentElement element, String actionName, Map<String, String> parameters) {
100             listener.onAction(actionName, element, parameters);
101         }
102 
103         @Override
104         public void onNavigation(String url) {
105             listener.onNavigation(url);
106         }
107 
108         @Override
109         public void onFrameScroll(int scrollTopPosition) {
110             //TODO https://jira.magnolia-cms.com/browse/PAGES-475
111         }
112 
113         @Override
114         public void onError(ErrorType errorType, String... parameters) {
115             listener.onError(errorType, parameters);
116         }
117 
118         @Override
119         public void selectPage(PageElement element) {
120             listener.onElementSelect(element);
121         }
122 
123         @Override
124         public void selectComponent(ComponentElement element) {
125             listener.onElementSelect(element);
126         }
127 
128         @Override
129         public void selectArea(AreaElement element) {
130             listener.onElementSelect(element);
131         }
132     };
133 
134     public PostMessageExtension() {
135         registerRpc(rpc);
136     }
137 
138     public void refresh() {
139         getRpcProxy(PageEditorClientRpc.class).refresh();
140     }
141 
142     public void startMoveComponent(ComponentElement componentElement) {
143         getRpcProxy(PageEditorClientRpc.class).startMoveComponent(componentElement);
144     }
145 
146     public void cancelMoveComponent() {
147         getRpcProxy(PageEditorClientRpc.class).cancelMoveComponent();
148     }
149 
150     public void setListener(PageEditorListener listener) {
151         this.listener = listener;
152     }
153 
154     @Override
155     public void extend(AbstractClientConnector target) {
156         super.extend(target);
157     }
158 }