View Javadoc
1   /**
2    * This file Copyright (c) 2011-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.vaadin.editor;
35  
36  import info.magnolia.ui.vaadin.gwt.client.connector.PageEditorState;
37  import info.magnolia.ui.vaadin.gwt.client.rpc.PageEditorClientRpc;
38  import info.magnolia.ui.vaadin.gwt.client.rpc.PageEditorServerRpc;
39  import info.magnolia.ui.vaadin.gwt.client.shared.AreaElement;
40  import info.magnolia.ui.vaadin.gwt.client.shared.ComponentElement;
41  import info.magnolia.ui.vaadin.gwt.client.shared.ErrorType;
42  import info.magnolia.ui.vaadin.gwt.client.shared.PageEditorParameters;
43  import info.magnolia.ui.vaadin.gwt.client.shared.PageElement;
44  
45  import java.util.Map;
46  
47  import com.vaadin.ui.AbstractComponent;
48  
49  /**
50   * PageEditor widget server side implementation.
51   * Uses the {@link PageEditorListener} interface to call back to page editor presenter.
52   */
53  public class PageEditor extends AbstractComponent {
54  
55      private PageEditorListener listener;
56  
57      public PageEditor() {
58          setSizeFull();
59  
60          registerRpc(new PageEditorServerRpc() {
61  
62              @Override
63              public void selectPage(PageElement element) {
64                  listener.onElementSelect(element);
65              }
66  
67              @Override
68              public void selectArea(AreaElement element) {
69                  listener.onElementSelect(element);
70              }
71  
72              @Override
73              public void selectComponent(ComponentElement element) {
74                  listener.onElementSelect(element);
75              }
76  
77              @Override
78              public void selectExternalPage() {
79                  listener.onExternalPageSelect();
80              }
81  
82              @Override
83              public void newComponent(AreaElement areaElement) {
84                  // make sure pageEditorPresenter's selection is in sync for further use
85                  listener.onElementSelect(areaElement);
86                  listener.onAction(PageEditorListener.ACTION_ADD_COMPONENT, areaElement);
87              }
88  
89              @Override
90              public void sortComponent(AreaElement areaElement) {
91                  listener.onAction(PageEditorListener.ACTION_SORT_COMPONENT, areaElement);
92              }
93  
94              @Override
95              public void newArea(AreaElement areaElement) {
96                  listener.onAction(PageEditorListener.ACTION_ADD_AREA, areaElement);
97              }
98  
99              @Override
100             public void editComponent(ComponentElement element) {
101                 listener.onAction(PageEditorListener.ACTION_EDIT_COMPONENT, element);
102             }
103 
104             @Override
105             public void editArea(AreaElement element) {
106                 listener.onAction(PageEditorListener.ACTION_EDIT_ELEMENT, element);
107             }
108 
109             @Override
110             public void startMoveComponent() {
111                 listener.onAction(PageEditorListener.ACTION_START_MOVE_COMPONENT);
112             }
113 
114             @Override
115             public void stopMoveComponent() {
116                 listener.onAction(PageEditorListener.ACTION_STOP_MOVE_COMPONENT);
117             }
118 
119             @Override
120             public void onError(ErrorType errorType, String... parameters) {
121                 listener.onError(errorType, parameters);
122             }
123 
124             @Override
125             public void onClientAction(ComponentElement element, String actionName, Map<String, String> parameters) {
126                 listener.onAction(actionName, element, parameters);
127             }
128 
129             @Override
130             public void onNavigation(String url) {
131                 listener.onNavigation(url);
132             }
133         });
134     }
135 
136     /**
137      * Load the page editor with the parameters sent to client by state.
138      */
139     public void load(PageEditorParameters parameters) {
140         populateState(parameters, true);
141     }
142 
143     /**
144      * Silently update the parameters in the state.
145      */
146     public void update(PageEditorParameters parameters) {
147         populateState(parameters, false);
148     }
149 
150     private void populateState(PageEditorParameters parameters, boolean markAsDirty) {
151         PageEditorState state = getState(markAsDirty);
152         state.setUrl(parameters.getUrl());
153         state.setNodePath(parameters.getNodePath());
154         state.setSelectedElement(parameters.getSelectedElement());
155         state.setContextPath(parameters.getContextPath());
156         state.setPlatformType(parameters.getPlatformType());
157         state.setPreview(parameters.isPreview());
158         state.setI18nKeys(parameters.getI18nKeys());
159     }
160 
161     @Override
162     protected PageEditorState getState() {
163         return (PageEditorState) super.getState();
164     }
165 
166     @Override
167     protected PageEditorState getState(boolean markAsDirty) {
168         return (PageEditorState) super.getState(markAsDirty);
169     }
170 
171     public void refresh() {
172         getRpcProxy(PageEditorClientRpc.class).refresh();
173     }
174 
175     public void startMoveComponent() {
176         getRpcProxy(PageEditorClientRpc.class).startMoveComponent();
177     }
178 
179     public void cancelMoveComponent() {
180         getRpcProxy(PageEditorClientRpc.class).cancelMoveComponent();
181     }
182 
183     public void setListener(PageEditorListener listener) {
184         this.listener = listener;
185     }
186 }