Author: yradtsevich
Date: 2011-12-02 13:58:34 -0500 (Fri, 02 Dec 2011)
New Revision: 36897
Added:
trunk/vpe/plugins/org.jboss.tools.vpe.browsersim/src/org/jboss/tools/vpe/browsersim/model/DeviceOrientation.java
trunk/vpe/plugins/org.jboss.tools.vpe.browsersim/src/org/jboss/tools/vpe/browsersim/resources/icons/rotate_clockwise.png
trunk/vpe/plugins/org.jboss.tools.vpe.browsersim/src/org/jboss/tools/vpe/browsersim/resources/icons/rotate_counterclockwise.png
Modified:
trunk/vpe/plugins/org.jboss.tools.vpe.browsersim/src/org/jboss/tools/vpe/browsersim/ui/BrowserSim.java
Log:
https://issues.jboss.org/browse/JBIDE-10280 : BrowserSim: simulate orientation change
- added buttons for rotating of browser
- added simulation of javascript orientationchange event
Added:
trunk/vpe/plugins/org.jboss.tools.vpe.browsersim/src/org/jboss/tools/vpe/browsersim/model/DeviceOrientation.java
===================================================================
---
trunk/vpe/plugins/org.jboss.tools.vpe.browsersim/src/org/jboss/tools/vpe/browsersim/model/DeviceOrientation.java
(rev 0)
+++
trunk/vpe/plugins/org.jboss.tools.vpe.browsersim/src/org/jboss/tools/vpe/browsersim/model/DeviceOrientation.java 2011-12-02
18:58:34 UTC (rev 36897)
@@ -0,0 +1,47 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2011 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.vpe.browsersim.model;
+
+import java.util.Observable;
+
+/**
+ * Holds device orientation and can be turned.
+ */
+public class DeviceOrientation extends Observable {
+ public static final int PORTRAIT = 0;
+ public static final int LANDSCAPE = 90;
+ public static final int PORTRAIT_INVERTED = 180;
+ public static final int LANDSCAPE_INVERTED = -90;
+
+ private int orientationAngle;
+
+ public DeviceOrientation(int orientationAngle) {
+ this.orientationAngle = orientationAngle;
+ }
+
+ public void turnDevice(boolean counterclockwise) {
+ if (counterclockwise) {
+ orientationAngle+= 90;
+ } else {
+ orientationAngle-= 90;
+ }
+
+ // normalize angle to be in [-90; 180]
+ orientationAngle = ((orientationAngle - 180) % 360) + 180;
+ orientationAngle = ((orientationAngle + 90) % 360) - 90;
+
+ this.setChanged();
+ }
+
+ public int getOrientationAngle() {
+ return orientationAngle;
+ }
+}
Added:
trunk/vpe/plugins/org.jboss.tools.vpe.browsersim/src/org/jboss/tools/vpe/browsersim/resources/icons/rotate_clockwise.png
===================================================================
(Binary files differ)
Property changes on:
trunk/vpe/plugins/org.jboss.tools.vpe.browsersim/src/org/jboss/tools/vpe/browsersim/resources/icons/rotate_clockwise.png
___________________________________________________________________
Added: svn:mime-type
+ application/octet-stream
Added:
trunk/vpe/plugins/org.jboss.tools.vpe.browsersim/src/org/jboss/tools/vpe/browsersim/resources/icons/rotate_counterclockwise.png
===================================================================
(Binary files differ)
Property changes on:
trunk/vpe/plugins/org.jboss.tools.vpe.browsersim/src/org/jboss/tools/vpe/browsersim/resources/icons/rotate_counterclockwise.png
___________________________________________________________________
Added: svn:mime-type
+ application/octet-stream
Modified:
trunk/vpe/plugins/org.jboss.tools.vpe.browsersim/src/org/jboss/tools/vpe/browsersim/ui/BrowserSim.java
===================================================================
---
trunk/vpe/plugins/org.jboss.tools.vpe.browsersim/src/org/jboss/tools/vpe/browsersim/ui/BrowserSim.java 2011-12-02
18:50:35 UTC (rev 36896)
+++
trunk/vpe/plugins/org.jboss.tools.vpe.browsersim/src/org/jboss/tools/vpe/browsersim/ui/BrowserSim.java 2011-12-02
18:58:34 UTC (rev 36897)
@@ -47,6 +47,7 @@
import org.jboss.tools.vpe.browsersim.browser.AbstractWebKitBrowser;
import org.jboss.tools.vpe.browsersim.browser.WebKitBrowserFactory;
import org.jboss.tools.vpe.browsersim.model.Device;
+import org.jboss.tools.vpe.browsersim.model.DeviceOrientation;
import org.jboss.tools.vpe.browsersim.model.DevicesList;
import org.jboss.tools.vpe.browsersim.model.DevicesListHolder;
import org.jboss.tools.vpe.browsersim.model.DevicesListStorage;
@@ -66,6 +67,7 @@
private String initialUrl;
private Menu devicesMenu;
private DevicesListHolder devicesListHolder;
+ private DeviceOrientation deviceOrientation;
public static void main(String[] args) {
String initialUrl;
@@ -175,7 +177,10 @@
});
browser.addLocationListener(new LocationListener() {
public void changed(LocationEvent event) {
- if (event.top) locationText.setText(event.location);
+ if (event.top) {
+ locationText.setText(event.location);
+ }
+ initOrientation(deviceOrientation.getOrientationAngle());
}
public void changing(LocationEvent event) {
}
@@ -247,17 +252,38 @@
}
});
+ ToolItem itemRotateCounterclockwise = new ToolItem(toolbar, SWT.PUSH);
+// itemGo.setText("Rotate Counterclockwise");
+ itemRotateCounterclockwise.addListener(SWT.Selection, new Listener() {
+ public void handleEvent(Event event) {
+ rotateDevice(true);
+ }
+ });
+
+ ToolItem itemRotateClockwise = new ToolItem(toolbar, SWT.PUSH);
+// itemGo.setText("Rotate Clockwise");
+ itemRotateClockwise.addListener(SWT.Selection, new Listener() {
+ public void handleEvent(Event event) {
+ rotateDevice(false);
+ }
+ });
+
final Image imageBack = new Image(display,
ResourcesUtil.getResourceAsStream("/org/jboss/tools/vpe/browsersim/resources/icons/nav_backward.gif"));
//$NON-NLS-1$
final Image imageForward = new Image(display,
ResourcesUtil.getResourceAsStream("/org/jboss/tools/vpe/browsersim/resources/icons/nav_forward.gif"));
//$NON-NLS-1$
final Image imageStop = new Image(display,
ResourcesUtil.getResourceAsStream("/org/jboss/tools/vpe/browsersim/resources/icons/nav_stop.gif"));
//$NON-NLS-1$
final Image imageRefresh = new Image(display,
ResourcesUtil.getResourceAsStream("/org/jboss/tools/vpe/browsersim/resources/icons/nav_refresh.gif"));
//$NON-NLS-1$
final Image imageGo = new Image(display,
ResourcesUtil.getResourceAsStream("/org/jboss/tools/vpe/browsersim/resources/icons/nav_go.gif"));
//$NON-NLS-1$
+ final Image imageRotateClockwise = new Image(display,
ResourcesUtil.getResourceAsStream("/org/jboss/tools/vpe/browsersim/resources/icons/rotate_clockwise.png"));
//$NON-NLS-1$
+ final Image imageRotateCounterclockwise = new Image(display,
ResourcesUtil.getResourceAsStream("/org/jboss/tools/vpe/browsersim/resources/icons/rotate_counterclockwise.png"));
//$NON-NLS-1$
itemBack.setImage(imageBack);
itemForward.setImage(imageForward);
itemStop.setImage(imageStop);
itemRefresh.setImage(imageRefresh);
itemGo.setImage(imageGo);
+ itemRotateClockwise.setImage(imageRotateClockwise);
+ itemRotateCounterclockwise.setImage(imageRotateCounterclockwise);
+
shell.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
imageBack.dispose();
@@ -265,12 +291,11 @@
imageStop.dispose();
imageRefresh.dispose();
imageGo.dispose();
+ imageRotateClockwise.dispose();
+ imageRotateCounterclockwise.dispose();
}
});
-
- itemForward.setImage(imageForward);
-
return toolbar;
}
@@ -369,20 +394,46 @@
menuItem.setSelection(menuItem.getData() == device);
}
}
+
+ setBrowserSize(device.getWidth(), device.getHeight());
+ deviceOrientation = new DeviceOrientation(device.getWidth() < device.getHeight()
+ ? DeviceOrientation.PORTRAIT
+ : DeviceOrientation.LANDSCAPE);
+ deviceOrientation.addObserver(new Observer() {
+ public void update(Observable o, Object arg) {
+ int orientationAngle = ((DeviceOrientation) o).getOrientationAngle();
+
+ int minSize = Math.min(device.getWidth(), device.getHeight());
+ int maxSize = Math.max(device.getWidth(), device.getHeight());
+
+ if (orientationAngle == DeviceOrientation.LANDSCAPE
+ || orientationAngle == DeviceOrientation.LANDSCAPE_INVERTED) {
+ setBrowserSize(maxSize, minSize);
+ } else {
+ setBrowserSize(minSize, maxSize);
+ }
+
+ fireOrientationChangeEvent(orientationAngle);
+ }
+ });
browser.setDefaultUserAgent(device.getUserAgent());
+ browser.refresh();
+ }
+
+ private void setBrowserSize(int width, int height) {
GridData data = (GridData) browser.getLayoutData();
Rectangle clientArea = display.getClientArea();
int shellWidthHint = SWT.DEFAULT;
- if (device.getWidth() != Device.DEFAULT_SIZE) {
- data.widthHint = device.getWidth();
+ if (width != Device.DEFAULT_SIZE) {
+ data.widthHint = width;
} else if (data.widthHint == SWT.DEFAULT) {
shellWidthHint = clientArea.width;
}
int shellHeightHint = SWT.DEFAULT;
- if (device.getHeight() != Device.DEFAULT_SIZE) {
- data.heightHint = device.getHeight();
+ if (height != Device.DEFAULT_SIZE) {
+ data.heightHint = height;
} else if (data.heightHint == SWT.DEFAULT) {
shellHeightHint = clientArea.height;
}
@@ -411,6 +462,28 @@
}
shell.setBounds(shellBounds);
- browser.refresh();
}
+
+ private void initOrientation(int orientation) {
+ browser.execute("window.onorientationchange = null;"
+ + "window.orientation = " + orientation + ";");
+ }
+
+ private void fireOrientationChangeEvent(int orientation) {
+ browser.execute("window.orientation = " + orientation + ";"
+ + "(function(){"
+ + "var event = document.createEvent('Event');"
+ + "event.initEvent('orientationchange', false, false);" //
http://jsbin.com/azefow/6 https://developer.mozilla.org/en/DOM/document.createEvent
+ + "window.dispatchEvent(event);"
+ + "if (typeof window.onorientationchange === 'function') {"
+ + "window.onorientationchange(event);"
+ + "}"
+ + "})();"
+ );
+ }
+
+ protected void rotateDevice(boolean counterclockwise) {
+ deviceOrientation.turnDevice(counterclockwise);
+ deviceOrientation.notifyObservers();
+ }
}