Author: yradtsevich
Date: 2011-11-09 12:23:47 -0500 (Wed, 09 Nov 2011)
New Revision: 36251
Modified:
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/BrowserSim.java
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/DevicesList.java
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/DevicesManager.java
Log:
https://issues.jboss.org/browse/JBIDE-9539 : Browsersim app for testing mobile/desktop web
apps
- implemented load/save devices list to a file (now it is just API, not connected with the
UI)
Modified:
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/BrowserSim.java
===================================================================
---
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/BrowserSim.java 2011-11-09
15:00:39 UTC (rev 36250)
+++
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/BrowserSim.java 2011-11-09
17:23:47 UTC (rev 36251)
@@ -323,7 +323,7 @@
if (selectedDeviceIndex < 0) {
selectedDeviceIndex = 0;
}
- devicesList.setCurrentDeviceIndex(selectedDeviceIndex);
+ devicesList.setSelectedDeviceIndex(selectedDeviceIndex);
devicesList.notifyObservers();
}
};
Modified:
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/DevicesList.java
===================================================================
---
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/DevicesList.java 2011-11-09
15:00:39 UTC (rev 36250)
+++
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/DevicesList.java 2011-11-09
17:23:47 UTC (rev 36251)
@@ -14,17 +14,17 @@
import java.util.Observable;
/**
- * An unmodifiable list of {@link Device}s, with modifiable {@link #currentDeviceIndex}.
+ * An unmodifiable list of {@link Device}s, with modifiable {@link
#selectedDeviceIndex}.
*
* @author Yahor Radtsevich (yradtsevich)
*/
public class DevicesList extends Observable {
private List<Device> devices;
- private int currentDeviceIndex;
+ private int selectedDeviceIndex;
- public DevicesList(List<Device> devices, int currentDeviceIndex) {
+ public DevicesList(List<Device> devices, int selectedDeviceIndex) {
this.devices = devices;
- this.currentDeviceIndex = currentDeviceIndex;
+ this.selectedDeviceIndex = selectedDeviceIndex;
}
public List<Device> getDevices() {
@@ -32,12 +32,12 @@
}
public int getSelectedDeviceIndex() {
- return currentDeviceIndex;
+ return selectedDeviceIndex;
}
- public void setCurrentDeviceIndex(int currentDeviceIndex) {
- if (this.currentDeviceIndex != currentDeviceIndex) {
- this.currentDeviceIndex = currentDeviceIndex;
+ public void setSelectedDeviceIndex(int currentDeviceIndex) {
+ if (this.selectedDeviceIndex != currentDeviceIndex) {
+ this.selectedDeviceIndex = currentDeviceIndex;
setChanged();
}
}
Modified:
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/DevicesManager.java
===================================================================
---
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/DevicesManager.java 2011-11-09
15:00:39 UTC (rev 36250)
+++
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/DevicesManager.java 2011-11-09
17:23:47 UTC (rev 36251)
@@ -10,9 +10,17 @@
******************************************************************************/
package org.jboss.tools.browsersim;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
@@ -53,4 +61,92 @@
return new DevicesList(devices, 0);
}
+
+ public static String encode(String string) {
+ return string.replace("\\", "\\\\").replace("\n",
"\\n").replace("\t", "\\t");
+ }
+
+ public static String decode(String string) {
+ StringBuilder result = new StringBuilder();
+
+ int i = 0;
+ while (i < string.length() - 1) {
+ char c0 = string.charAt(i);
+ if (c0 == '\\') {
+ char c1 = string.charAt(i + 1);
+ switch (c1) {
+ case '\\':
+ result.append('\\');
+ i++;
+ break;
+ case 'n':
+ result.append('\n');
+ i++;
+ break;
+ case 't':
+ result.append('\t');
+ i++;
+ break;
+ default:
+ result.append(c0);
+ break;
+ }
+ }
+ i++;
+ }
+
+ if (i < string.length()) {
+ result.append(string.charAt(i));
+ }
+
+ return result.toString();
+ }
+
+ public static void saveDevicesList(DevicesList devicesList, File file) throws
IOException {
+ BufferedWriter writer = new BufferedWriter(new FileWriter(file));
+
+ writer.write(String.valueOf(devicesList.getSelectedDeviceIndex()));
+ writer.write('\n');
+
+ for (Device device : devicesList.getDevices()) {
+ writer.write( encode(device.getName() ));
+ writer.write('\t');
+ writer.write(encode( String.valueOf(device.getWidth()) ));
+ writer.write('\t');
+ writer.write(encode( String.valueOf(device.getHeight()) ));
+ writer.write('\t');
+ writer.write( encode(device.getUserAgent() ));
+ writer.write('\n');
+ }
+
+ writer.close();
+ }
+
+ public static DevicesList loadDevicesList(File file) throws IOException {
+ BufferedReader reader = new BufferedReader(new FileReader(file));
+
+ String nextLine = reader.readLine();
+ int selectedDeviceIndex = 0;
+ if (nextLine != null) {
+ selectedDeviceIndex = Integer.parseInt(nextLine);
+ }
+ Pattern devicePattern =
Pattern.compile("(.*)\\t([0-9]*)\\t([0-9]*)\\t(.*)");
+
+ List<Device> devices = new ArrayList<Device>();
+ while ((nextLine = reader.readLine()) != null) {
+ Matcher deviceMatcher = devicePattern.matcher(nextLine);
+ if (deviceMatcher.matches()) {
+ devices.add(new Device(
+ decode(deviceMatcher.group(1)),
+ Integer.parseInt(deviceMatcher.group(2)),
+ Integer.parseInt(deviceMatcher.group(3)),
+ decode(deviceMatcher.group(4))
+ ));
+ }
+ }
+
+ reader.close();
+
+ return new DevicesList(devices, selectedDeviceIndex);
+ }
}