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