Author: yradtsevich
Date: 2011-11-08 09:10:32 -0500 (Tue, 08 Nov 2011)
New Revision: 36217
Added:
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/EditDeviceDialog.java
Removed:
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/EditDevicesDialog.java
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
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/ManageDevicesDialog.java
Log:
https://issues.jboss.org/browse/JBIDE-9539 : Browsersim app for testing mobile/desktop web
apps
- wired model and dialogs
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-08
10:11:07 UTC (rev 36216)
+++
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/BrowserSim.java 2011-11-08
14:10:32 UTC (rev 36217)
@@ -15,6 +15,8 @@
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
+import java.util.Observable;
+import java.util.Observer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
@@ -58,8 +60,9 @@
private Text locationText;
private Label statusLabel;
private ProgressBar progressBar;
- private List<MenuItem> deviceMenuItems = new ArrayList<MenuItem>();
private String initialUrl;
+ private Menu devicesMenu;
+ private DevicesManager devicesManager;
public static void main(String[] args) {
String initialUrl;
@@ -266,36 +269,68 @@
};
});
- Menu devicesMenu = createDropDownMenu(appMenuBar, "Devices");
- for (Device device : DevicesManager.getInstance().getDevicesList().getDevices()) {
- MenuItem deviceMenuItem = new MenuItem(devicesMenu, SWT.RADIO);
+ devicesMenu = createDropDownMenu(appMenuBar, "Devices");
+ devicesManager = DevicesManager.getInstance();
+ devicesManager.addObserver(new Observer() {
+ public void update(Observable o, Object arg) {
+ DevicesManager devicesManager = (DevicesManager) o;
+ DevicesList devicesList = devicesManager.getDevicesList();
+ setDevicesListForMenu(devicesList);
+ setDevice(devicesList.getDevices().get(devicesList.getSelectedDeviceIndex()));
+ devicesList.addObserver(new Observer() {
+ public void update(Observable o, Object arg) {
+ setDevice(((DevicesList)o).getDevices().get(((DevicesList)o).getSelectedDeviceIndex()));
+ }
+ });
+ }
+ });
+ devicesManager.setDevicesList(DevicesManager.getDefaultDevicesList());
+ devicesManager.notifyObservers();
+
+ new MenuItem(devicesMenu, SWT.BAR);
+ MenuItem manageDevicesMenuItem = new MenuItem(devicesMenu, SWT.PUSH);
+ manageDevicesMenuItem.setText("Manage Devices...");
+ manageDevicesMenuItem.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e) {
+ DevicesList newDevicesList = new ManageDevicesDialog(shell, SWT.APPLICATION_MODAL |
SWT.SHELL_TRIM,
+ devicesManager.getDevicesList()).open();
+ if (newDevicesList != null) {
+ devicesManager.setDevicesList(newDevicesList);
+ devicesManager.notifyObservers();
+ }
+ }
+ });
+ }
+
+ private void setDevicesListForMenu(DevicesList devicesList) {
+ for (MenuItem item : devicesMenu.getItems()) {
+ if (item.getData() instanceof Device) {
+ item.dispose();
+ }
+ }
+
+ int currentIndex = 0;
+ for (Device device : devicesList.getDevices()) {
+ MenuItem deviceMenuItem = new MenuItem(devicesMenu, SWT.RADIO, currentIndex);
deviceMenuItem.setText(device.getName());
deviceMenuItem.setData(device);
deviceMenuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
MenuItem menuItem = (MenuItem)e.widget;
if (menuItem.getSelection()) {
- setDevice((Device) menuItem.getData());
+ DevicesList devicesList = devicesManager.getDevicesList();
+ int selectedDeviceIndex = devicesList.getDevices().indexOf(menuItem.getData());
+ if (selectedDeviceIndex < 0) {
+ selectedDeviceIndex = 0;
+ }
+ devicesList.setCurrentDeviceIndex(selectedDeviceIndex);
+ devicesList.notifyObservers();
}
};
});
- deviceMenuItems.add(deviceMenuItem);
- deviceMenuItem.addDisposeListener(new DisposeListener() {
- public void widgetDisposed(DisposeEvent e) {
- deviceMenuItems.remove(e.widget);
- }
- });
+ currentIndex++;
}
-
- new MenuItem(devicesMenu, SWT.BAR);
- MenuItem manageDevicesMenuItem = new MenuItem(devicesMenu, SWT.PUSH);
- manageDevicesMenuItem.setText("Manage Devices...");
- manageDevicesMenuItem.addSelectionListener(new SelectionAdapter() {
- public void widgetSelected(SelectionEvent e) {
- new ManageDevicesDialog(shell, SWT.APPLICATION_MODAL | SWT.SHELL_TRIM).open();
- }
- });
}
private Menu createDropDownMenu(Menu menuBar, String name) {
@@ -307,10 +342,12 @@
}
public void setDevice(final Device device) {
- for (MenuItem deviceMenuItem : deviceMenuItems) {
- deviceMenuItem.setSelection(deviceMenuItem.getData() == device);
+ for (MenuItem menuItem : devicesMenu.getItems()) {
+ if (menuItem.getData() instanceof Device) {
+ menuItem.setSelection(menuItem.getData() == device);
+ }
}
-
+
browser.setDefaultUserAgent(device.getUserAgent());
GridData data = (GridData) browser.getLayoutData();
data.widthHint = device.getWidth();
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-08
10:11:07 UTC (rev 36216)
+++
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/DevicesList.java 2011-11-08
14:10:32 UTC (rev 36217)
@@ -31,7 +31,7 @@
return devices;
}
- public int getCurrentDeviceIndex() {
+ public int getSelectedDeviceIndex() {
return currentDeviceIndex;
}
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-08
10:11:07 UTC (rev 36216)
+++
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/DevicesManager.java 2011-11-08
14:10:32 UTC (rev 36217)
@@ -34,16 +34,6 @@
}
public DevicesList getDevicesList() {
- if (devicesList == null) {
- List<Device> devices = new ArrayList<Device>();
- devices.add(new Device("Apple iPhone 3", 320, 480,
IPHONE_OS_4_0_USER_AGENT));
- devices.add(new Device("Apple iPhone 4", 640, 960,
IPHONE_OS_4_0_USER_AGENT));
- devices.add(new Device("BlackBerry Bold Touch 9900", 640, 480,
"Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en-US) AppleWebKit/534.1+ (KHTML, like
Gecko) Version/6.0.0.246 Mobile Safari/534.1+"));
- devices.add(new Device("Samsung Galaxy S II", 640, 800, "Mozilla/5.0
(Linux; U; Android 2.3; en-us; GT-I9100 Build/GRH78) AppleWebKit/533.1 (KHTML, like Gecko)
Version/4.0 Mobile Safari/533.1"));
-
- devicesList = new DevicesList(devices, 0);
- }
-
return devicesList;
}
@@ -53,4 +43,14 @@
setChanged();
}
}
+
+ public static DevicesList getDefaultDevicesList() {
+ List<Device> devices = new ArrayList<Device>();
+ devices.add(new Device("Apple iPhone 3", 320, 480,
IPHONE_OS_4_0_USER_AGENT));
+ devices.add(new Device("Apple iPhone 4", 640, 960,
IPHONE_OS_4_0_USER_AGENT));
+ devices.add(new Device("BlackBerry Bold Touch 9900", 640, 480,
"Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en-US) AppleWebKit/534.1+ (KHTML, like
Gecko) Version/6.0.0.246 Mobile Safari/534.1+"));
+ devices.add(new Device("Samsung Galaxy S II", 640, 800, "Mozilla/5.0
(Linux; U; Android 2.3; en-us; GT-I9100 Build/GRH78) AppleWebKit/533.1 (KHTML, like Gecko)
Version/4.0 Mobile Safari/533.1"));
+
+ return new DevicesList(devices, 0);
+ }
}
Copied:
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/EditDeviceDialog.java
(from rev 36170,
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/EditDevicesDialog.java)
===================================================================
---
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/EditDeviceDialog.java
(rev 0)
+++
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/EditDeviceDialog.java 2011-11-08
14:10:32 UTC (rev 36217)
@@ -0,0 +1,162 @@
+package org.jboss.tools.browsersim;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.FocusAdapter;
+import org.eclipse.swt.events.FocusEvent;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.VerifyEvent;
+import org.eclipse.swt.events.VerifyListener;
+import org.eclipse.swt.layout.FillLayout;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Dialog;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Text;
+
+public class EditDeviceDialog extends Dialog {
+ protected Device resultDevice;
+ protected Device initialDevice;
+ protected Shell shell;
+ private Text textName;
+ private Text textWidth;
+ private Text textHeight;
+ private Text textUserAgent;
+
+ /**
+ * Create the dialog.
+ * @param parent
+ * @param style
+ */
+ public EditDeviceDialog(Shell parent, int style, Device initialDevice) {
+ super(parent, style);
+ setText("SWT Dialog");
+ this.initialDevice = initialDevice;
+ }
+
+ /**
+ * Open the dialog.
+ * @return the newDevicesList
+ */
+ public Device open() {
+ createContents();
+ shell.open();
+ shell.layout();
+ shell.setSize(shell.computeSize(300, SWT.DEFAULT));
+ Display display = getParent().getDisplay();
+ while (!shell.isDisposed()) {
+ if (!display.readAndDispatch()) {
+ display.sleep();
+ }
+ }
+
+ return resultDevice;
+ }
+
+ /**
+ * Create contents of the dialog.
+ */
+ private void createContents() {
+ shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.MIN | SWT.MAX);
+ shell.setSize(450, 300);
+ shell.setText("Edit Device");
+ shell.setLayout(new GridLayout(2, false));
+
+ Label labelName = new Label(shell, SWT.NONE);
+ labelName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+ labelName.setText("Name:");
+
+ textName = new Text(shell, SWT.BORDER);
+ textName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
+ textName.addFocusListener(new FocusGainedTextListener());
+ textName.setText(initialDevice.getName());
+
+ Label labelWidth = new Label(shell, SWT.NONE);
+ labelWidth.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+ labelWidth.setText("Width:");
+
+ textWidth = new Text(shell, SWT.BORDER);
+ textWidth.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
+ textWidth.setTextLimit(4);
+ textWidth.addVerifyListener(new VerifyDigitsListener());
+ textWidth.addFocusListener(new FocusLostDigitsListener());
+ textWidth.addFocusListener(new FocusGainedTextListener());
+ textWidth.setText(String.valueOf(initialDevice.getWidth()));
+
+ Label labelHeight = new Label(shell, SWT.NONE);
+ labelHeight.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+ labelHeight.setText("Height:");
+
+ textHeight = new Text(shell, SWT.BORDER);
+ textHeight.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
+ textHeight.setTextLimit(4);
+ textHeight.addVerifyListener(new VerifyDigitsListener());
+ textHeight.addFocusListener(new FocusLostDigitsListener());
+ textHeight.addFocusListener(new FocusGainedTextListener());
+ textHeight.setText(String.valueOf(initialDevice.getHeight()));
+
+ Label labelUseragent = new Label(shell, SWT.NONE);
+ labelUseragent.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+ labelUseragent.setText("User-Agent:");
+
+ textUserAgent = new Text(shell, SWT.BORDER);
+ textUserAgent.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
+ textUserAgent.addFocusListener(new FocusGainedTextListener());
+ textUserAgent.setText(initialDevice.getUserAgent());
+
+ Composite composite = new Composite(shell, SWT.NONE);
+ composite.setLayout(new FillLayout(SWT.HORIZONTAL));
+ composite.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, true, true, 2, 1));
+
+ Button buttonOk = new Button(composite, SWT.NONE);
+ buttonOk.setText("OK");
+ buttonOk.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e) {
+ resultDevice = new Device(textName.getText(), Integer.valueOf(textWidth.getText()),
+ Integer.valueOf(textHeight.getText()), textUserAgent.getText());
+ shell.close();
+ }
+ });
+ shell.setDefaultButton(buttonOk);
+
+ Button buttonCancel = new Button(composite, SWT.NONE);
+ buttonCancel.setText("Cancel");
+ buttonCancel.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e) {
+ resultDevice = null;
+ shell.close();
+ }
+ });
+ }
+}
+
+final class VerifyDigitsListener implements VerifyListener {
+ public void verifyText(VerifyEvent e) {
+ for (char c : e.text.toCharArray()) {
+ if (!('0' <= c && c <= '9')) {
+ e.doit = false;
+ return;
+ }
+ }
+ }
+}
+
+final class FocusLostDigitsListener extends FocusAdapter {
+ public void focusLost(FocusEvent e) {
+ Text text = ((Text) e.widget);
+ if (text.getText().trim().isEmpty()) {
+ text.setText("0");
+ }
+ }
+}
+
+final class FocusGainedTextListener extends FocusAdapter {
+ public void focusGained(FocusEvent e) {
+ Text text = ((Text) e.widget);
+ text.setSelection(0, text.getText().length());
+ }
+}
Deleted:
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/EditDevicesDialog.java
===================================================================
---
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/EditDevicesDialog.java 2011-11-08
10:11:07 UTC (rev 36216)
+++
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/EditDevicesDialog.java 2011-11-08
14:10:32 UTC (rev 36217)
@@ -1,156 +0,0 @@
-package org.jboss.tools.browsersim;
-
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.FocusAdapter;
-import org.eclipse.swt.events.FocusEvent;
-import org.eclipse.swt.events.SelectionAdapter;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.events.VerifyEvent;
-import org.eclipse.swt.events.VerifyListener;
-import org.eclipse.swt.layout.FillLayout;
-import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Button;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Dialog;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Label;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.swt.widgets.Text;
-
-public class EditDevicesDialog extends Dialog {
- protected Object result;
- protected Shell shell;
- private Text textName;
- private Text textWidth;
- private Text textHeight;
- private Text textUserAgent;
-
- /**
- * Create the dialog.
- * @param parent
- * @param style
- */
- public EditDevicesDialog(Shell parent, int style) {
- super(parent, style);
- setText("SWT Dialog");
- }
-
- /**
- * Open the dialog.
- * @return the result
- */
- public Object open() {
- createContents();
- shell.open();
- shell.layout();
- shell.setSize(shell.computeSize(300, SWT.DEFAULT));
- Display display = getParent().getDisplay();
- while (!shell.isDisposed()) {
- if (!display.readAndDispatch()) {
- display.sleep();
- }
- }
-
- return result;
- }
-
- /**
- * Create contents of the dialog.
- */
- private void createContents() {
- shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.MIN | SWT.MAX);
- shell.setSize(450, 300);
- shell.setText("Edit Device");
- shell.setLayout(new GridLayout(2, false));
-
- Label labelName = new Label(shell, SWT.NONE);
- labelName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
- labelName.setText("Name:");
-
- textName = new Text(shell, SWT.BORDER);
- textName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
- textName.addFocusListener(new FocusGainedTextListener());
-
- Label labelWidth = new Label(shell, SWT.NONE);
- labelWidth.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
- labelWidth.setText("Width:");
-
- textWidth = new Text(shell, SWT.BORDER);
- textWidth.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
- textWidth.setTextLimit(4);
- textWidth.addVerifyListener(new VerifyDigitsListener());
- textWidth.addFocusListener(new FocusLostDigitsListener());
- textWidth.addFocusListener(new FocusGainedTextListener());
-
- Label labelHeight = new Label(shell, SWT.NONE);
- labelHeight.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
- labelHeight.setText("Height:");
-
- textHeight = new Text(shell, SWT.BORDER);
- textHeight.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
- textHeight.setTextLimit(4);
- textHeight.addVerifyListener(new VerifyDigitsListener());
- textHeight.addFocusListener(new FocusLostDigitsListener());
- textHeight.addFocusListener(new FocusGainedTextListener());
-
- Label labelUseragent = new Label(shell, SWT.NONE);
- labelUseragent.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
- labelUseragent.setText("User-Agent:");
-
- textUserAgent = new Text(shell, SWT.BORDER);
- textUserAgent.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
- textUserAgent.addFocusListener(new FocusGainedTextListener());
-
- Composite composite = new Composite(shell, SWT.NONE);
- composite.setLayout(new FillLayout(SWT.HORIZONTAL));
- composite.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, true, true, 2, 1));
-
- Button buttonOk = new Button(composite, SWT.NONE);
- buttonOk.setText("OK");
- buttonOk.addSelectionListener(new SelectionAdapter() {
- public void widgetSelected(SelectionEvent e) {
- result = new Device(textName.getText(), Integer.valueOf(textWidth.getText()),
- Integer.valueOf(textHeight.getText()), textUserAgent.getText());
- shell.close();
- }
- });
- shell.setDefaultButton(buttonOk);
-
- Button buttonCancel = new Button(composite, SWT.NONE);
- buttonCancel.setText("Cancel");
- buttonCancel.addSelectionListener(new SelectionAdapter() {
- public void widgetSelected(SelectionEvent e) {
- result = null;
- shell.close();
- }
- });
- }
-}
-
-final class VerifyDigitsListener implements VerifyListener {
- public void verifyText(VerifyEvent e) {
- for (char c : e.text.toCharArray()) {
- if (!('0' <= c && c <= '9')) {
- e.doit = false;
- return;
- }
- }
- }
-}
-
-final class FocusLostDigitsListener extends FocusAdapter {
- public void focusLost(FocusEvent e) {
- Text text = ((Text) e.widget);
- if (text.getText().trim().isEmpty()) {
- text.setText("0");
- }
- }
-}
-
-final class FocusGainedTextListener extends FocusAdapter {
- public void focusGained(FocusEvent e) {
- Text text = ((Text) e.widget);
- text.setSelection(0, text.getText().length());
- }
-}
Modified:
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/ManageDevicesDialog.java
===================================================================
---
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/ManageDevicesDialog.java 2011-11-08
10:11:07 UTC (rev 36216)
+++
workspace/yradtsevich/browsersim/swt-webkit-browsersim/org.jboss.tools.browsersim/src/org/jboss/tools/browsersim/ManageDevicesDialog.java 2011-11-08
14:10:32 UTC (rev 36217)
@@ -9,10 +9,12 @@
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package org.jboss.tools.browsersim;
+import java.util.ArrayList;
+import java.util.List;
+
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
@@ -30,25 +32,32 @@
*/
public class ManageDevicesDialog extends Dialog {
- protected Object result;
+ protected DevicesList oldDevicesList;
+ protected List<Device> devices;
+ protected int selectedDeviceIndex;
protected Shell shell;
private Table table;
+ private DevicesList resultDevicesList;
/**
* Create the dialog.
* @param parent
* @param style
+ * @param oldDevicesList
*/
- public ManageDevicesDialog(Shell parent, int style) {
+ public ManageDevicesDialog(Shell parent, int style, DevicesList oldDevicesList) {
super(parent, style);
setText("Devices");
- }
+ this.oldDevicesList = oldDevicesList;
+ this.devices = new ArrayList<Device>(oldDevicesList.getDevices());
+ this.selectedDeviceIndex = oldDevicesList.getSelectedDeviceIndex();
+ }
/**
* Open the dialog.
- * @return the result
+ * @return the newDevicesList
*/
- public Object open() {
+ public DevicesList open() {
createContents();
shell.open();
shell.layout();
@@ -58,7 +67,7 @@
display.sleep();
}
}
- return result;
+ return resultDevicesList;
}
/**
@@ -74,6 +83,11 @@
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
table.setHeaderVisible(true);
table.setLinesVisible(true);
+ table.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e) {
+ selectedDeviceIndex = ((Table)e.widget).getSelectionIndex();
+ }
+ });
TableColumn tableColumnName = new TableColumn(table, SWT.NONE);
tableColumnName.setWidth(100);
@@ -98,11 +112,12 @@
Button buttonAdd = new Button(compositeControls, SWT.NONE);
buttonAdd.setText("Add");
buttonAdd.addSelectionListener(new SelectionAdapter() {
- @Override
public void widgetSelected(SelectionEvent e) {
- Device newDevice = (Device) new EditDevicesDialog(shell, SWT.APPLICATION_MODAL |
SWT.SHELL_TRIM).open();
+ Device newDevice = new EditDeviceDialog(shell, SWT.APPLICATION_MODAL |
SWT.SHELL_TRIM,
+ new Device("Some Device", 480, 800, "Some User
Agent")).open();
if (newDevice != null) {
- DevicesManager.getInstance().getDevicesList().getDevices().add(newDevice);
+ devices.add(newDevice);
+ selectedDeviceIndex = devices.size() - 1;
updateDevices();
}
}
@@ -110,12 +125,42 @@
Button buttonEdit = new Button(compositeControls, SWT.NONE);
buttonEdit.setText("Edit");
+ buttonEdit.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e) {
+ Device newDevice = new EditDeviceDialog(shell, SWT.APPLICATION_MODAL |
SWT.SHELL_TRIM,
+ devices.get(selectedDeviceIndex)).open();
+ if (newDevice != null) {
+ devices.remove(selectedDeviceIndex);
+ devices.add(selectedDeviceIndex, newDevice);
+ updateDevices();
+ }
+ }
+ });
Button buttonRemove = new Button(compositeControls, SWT.NONE);
buttonRemove.setText("Remove");
+ buttonRemove.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e) {
+ System.out.println(devices.size());
+ if (devices.size() > 1) {
+ devices.remove(selectedDeviceIndex);
+ if (selectedDeviceIndex >= devices.size()) {
+ selectedDeviceIndex = devices.size() - 1;
+ }
+ updateDevices();
+ }
+ }
+ });
Button buttonReset = new Button(compositeControls, SWT.NONE);
buttonReset.setText("Reset");
+ buttonReset.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e) {
+ devices = new ArrayList<Device>(oldDevicesList.getDevices());
+ selectedDeviceIndex = oldDevicesList.getSelectedDeviceIndex();
+ updateDevices();
+ }
+ });
Composite compositeOkCancel = new Composite(shell, SWT.NONE);
compositeOkCancel.setLayout(new FillLayout(SWT.HORIZONTAL));
@@ -124,24 +169,32 @@
Button buttonOk = new Button(compositeOkCancel, SWT.NONE);
buttonOk.setText("OK");
shell.setDefaultButton(buttonOk);
+ buttonOk.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e) {
+ resultDevicesList = new DevicesList(devices, selectedDeviceIndex);
+ shell.close();
+ }
+ });
Button buttonCancel = new Button(compositeOkCancel, SWT.NONE);
buttonCancel.setText("Cancel");
buttonCancel.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
+ resultDevicesList = null;
shell.close();
}
});
+
updateDevices();
- updateDevices();
}
public void updateDevices() {
table.removeAll();
- for (Device device : DevicesManager.getInstance().getDevicesList().getDevices()) {
+ for (Device device : devices) {
TableItem tableItem = new TableItem(table, SWT.NONE);
tableItem.setText(new String[] {device.getName(), String.valueOf(device.getWidth()),
String.valueOf(device.getHeight()), device.getUserAgent()});
}
+ table.setSelection(selectedDeviceIndex);
}
}