View Javadoc

1   /**
2    * This file Copyright (c) 2012 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.templating.editor.client.widget;
35  
36  
37  import static info.magnolia.templating.editor.client.jsni.JavascriptUtils.getI18nMessage;
38  
39  import com.google.gwt.core.client.GWT;
40  import com.google.gwt.event.dom.client.ClickEvent;
41  import com.google.gwt.event.dom.client.ClickHandler;
42  import com.google.gwt.event.dom.client.HasClickHandlers;
43  import com.google.gwt.event.dom.client.KeyCodes;
44  import com.google.gwt.event.dom.client.LoadEvent;
45  import com.google.gwt.event.dom.client.LoadHandler;
46  import com.google.gwt.event.shared.HandlerRegistration;
47  import com.google.gwt.user.client.Event;
48  import com.google.gwt.user.client.Event.NativePreviewEvent;
49  import com.google.gwt.user.client.ui.Frame;
50  import com.google.gwt.user.client.ui.PopupPanel;
51  
52  /**
53   * PreviewChannelWidget.
54   * TODO javadoc bien sure!
55   * TODO extract base class with common functionality. Make this a MobilePreviewChannelWidget extending an AbstractPreviewChannelWidget.
56   */
57  public class PreviewChannel extends PopupPanel implements ClickHandler, HasClickHandlers {
58  
59      private String landscapeCssStyleSuffix = "Landscape";
60      private String portraitCssStyleSuffix = "Portrait";
61      private String deviceType = "smartphone";
62  
63      /**
64       * Orientation modes for this widget.
65       */
66      public enum Orientation {
67          PORTRAIT, LANDSCAPE
68      }
69  
70      private Orientation currentOrientation = Orientation.LANDSCAPE;
71  
72      public PreviewChannel(final String url, final Orientation orientation, final String deviceType) {
73          this.deviceType = deviceType;
74          this.currentOrientation = orientation;
75  
76          setStylePrimaryName("mobilePreview");
77          //TODO have a look at GWT add dependent style mechanism instead of doing it yourself.
78          addStyleName(orientation == Orientation.LANDSCAPE ? deviceType + landscapeCssStyleSuffix : deviceType + portraitCssStyleSuffix);
79  
80          setAnimationEnabled(true);
81          setAutoHideEnabled(true);
82          setModal(true);
83          setGlassEnabled(true);
84          setGlassStyleName("mgnlEditorPreviewBackground");
85  
86          getElement().setTitle(getI18nMessage("editor.preview.rotate.js"));
87  
88          addClickHandler(this);
89  
90          final Frame iframe = new Frame(url);
91          iframe.setStylePrimaryName("mobilePreviewIframe");
92          iframe.addLoadHandler(new LoadHandler() {
93  
94              @Override
95              public void onLoad(LoadEvent event) {
96                 //TODO nice animation before displaying the page preview?
97                  //Window.alert("iframe onload");
98              }
99          });
100 
101         add(iframe);
102     }
103 
104     public Orientation getOrientation() {
105         return currentOrientation;
106     }
107 
108     public String getDeviceType() {
109         return deviceType;
110     }
111 
112     public String getLandscapeCssStyleSuffix() {
113         return landscapeCssStyleSuffix;
114     }
115 
116     public String getPortraitCssStyleSuffix() {
117         return portraitCssStyleSuffix;
118     }
119 
120     @Override
121     public void onClick(ClickEvent event) {
122         //change orientation
123         GWT.log("currentOrientation is "+currentOrientation.toString());
124 
125         if(currentOrientation == Orientation.LANDSCAPE) {
126             currentOrientation = Orientation.PORTRAIT;
127             removeStyleName(getDeviceType() + landscapeCssStyleSuffix);
128             addStyleName(getDeviceType() + portraitCssStyleSuffix);
129         } else {
130             currentOrientation = Orientation.LANDSCAPE;
131             removeStyleName(getDeviceType() + portraitCssStyleSuffix);
132             addStyleName(getDeviceType() + landscapeCssStyleSuffix);
133         }
134         center();
135     }
136 
137     @Override
138     public HandlerRegistration addClickHandler(ClickHandler handler) {
139         return addDomHandler(handler, ClickEvent.getType());
140     }
141 
142     @Override
143     //key press or key down handlers have issues, that's why we had to resort to this. See also http://code.google.com/p/google-web-toolkit/issues/detail?id=5558.
144     protected void onPreviewNativeEvent(NativePreviewEvent event) {
145         super.onPreviewNativeEvent(event);
146         if (event.getTypeInt() == Event.ONKEYDOWN && event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ESCAPE) {
147             hide();
148         }
149     }
150 }