View Javadoc

1   /**
2    * This file Copyright (c) 2012-2013 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.gwt.client.widget;
35  
36  import info.magnolia.ui.vaadin.gwt.client.editor.jsni.AbstractFrameEventHandler;
37  
38  import com.google.gwt.core.client.GWT;
39  import com.google.gwt.dom.client.Element;
40  import com.google.gwt.dom.client.Style;
41  import com.google.gwt.user.client.ui.Composite;
42  import com.google.gwt.user.client.ui.SimplePanel;
43  import com.google.gwt.user.client.ui.Widget;
44  import com.google.web.bindery.event.shared.EventBus;
45  import com.vaadin.client.BrowserInfo;
46  import com.vaadin.client.ComputedStyle;
47  
48  /**
49   * GWT implementation of MagnoliaShell client side (the view part basically).
50   */
51  public class PageEditorViewImpl extends Composite implements PageEditorView {
52  
53      public static final String PAGE_EDITOR_CLASS_NAME = "pageEditor";
54  
55      private Listener listener;
56  
57      private PageEditorFrame iframe = new PageEditorFrame();
58  
59      private String url;
60  
61      private SimplePanel content;
62  
63      private AbstractFrameEventHandler handler;
64      private int lastScrollPosition;
65  
66      public PageEditorViewImpl(EventBus eventBus) {
67          super();
68          this.handler = GWT.create(AbstractFrameEventHandler.class);
69          this.content = new SimplePanel();
70          handler.setView(this);
71          handler.setEventBus(eventBus);
72          content.setWidget(iframe);
73          initWidget(content);
74          setStyleName(PAGE_EDITOR_CLASS_NAME);
75  
76          final Element iframeElement = iframe.getElement();
77          iframeElement.setAttribute("width", "100%");
78          iframeElement.setAttribute("height", "100%");
79          iframeElement.setAttribute("allowTransparency", "true");
80          iframeElement.setAttribute("frameborder", "0");
81  
82          handler.init();
83  
84      }
85  
86      @Override
87      public PageEditorFrame getFrame() {
88          return iframe;
89      }
90  
91      @Override
92      public Widget getContent() {
93          return content;
94      }
95  
96      @Override
97      public void setListener(Listener listener) {
98          this.listener = listener;
99      }
100 
101     @Override
102     public void setUrl(String url) {
103         getFrame().setUrl(url);
104         this.url = url;
105         handler.notifyUrlChange();
106     }
107 
108     @Override
109     public void reload() {
110         handler.reloadIFrame(iframe.getElement());
111         handler.notifyUrlChange();
112     }
113 
114     @Override
115     public void setLastScrollPosition(int lastScrollPosition) {
116         this.lastScrollPosition = lastScrollPosition;
117     }
118 
119     @Override
120     public void resetScrollTop() {
121         if (BrowserInfo.get().isTouchDevice()) {
122             getFrame().getElement().getStyle().setHeight(getFrame().getBody().getOffsetHeight(), Style.Unit.PX);
123             new ComputedStyle(getFrame().getElement());
124         }
125         content.getElement().setScrollTop(lastScrollPosition);
126     }
127 
128     @Override
129     public void initDomEventListeners() {
130         if (BrowserInfo.get().isTouchDevice()) {
131             handler.initNativeTouchSelectionListener(iframe.getBody(), listener);
132         } else {
133             handler.initNativeMouseSelectionListener(iframe.getBody(), listener);
134         }
135         handler.initNativeKeyListener(iframe.getElement());
136         handler.initScrollListener(content.getElement());
137     }
138 
139     @Override
140     public void initKeyEventListeners() {
141         handler.initNativeKeyListener(iframe.getElement());
142     }
143 
144 }