network: (Mac OS) Add a guide dialog for bridge network.
authorminkee.lee <minkee.lee@samsung.com>
Fri, 25 Sep 2015 07:11:26 +0000 (16:11 +0900)
committerminkee.lee <minkee.lee@samsung.com>
Fri, 25 Sep 2015 07:38:10 +0000 (16:38 +0900)
- When user select bridge network for emulator,
  guide dialog is shown if Mac OS version is above 10.9.x and
  bridge is not ready in host.

Change-Id: If841c1e6719eafc05a22d463801b6277d77af2d7
Signed-off-by: minkee.lee <minkee.lee@samsung.com>
src/org/tizen/emulator/manager/tool/TapUtil.java
src/org/tizen/emulator/manager/ui/detail/item/property/NetConnectTypeViewItem.java
src/org/tizen/emulator/manager/ui/dialog/TapGuideDialogForMac.java [new file with mode: 0644]

index 3215cdb..9c30a80 100644 (file)
@@ -53,6 +53,7 @@ import org.tizen.emulator.manager.resources.FilePathResources;
 import org.tizen.emulator.manager.resources.StringResources;
 import org.tizen.emulator.manager.ui.detail.item.template.ComboViewItem;
 import org.tizen.emulator.manager.ui.dialog.MessageDialog;
+import org.tizen.emulator.manager.ui.dialog.TapGuideDialogForMac;
 import org.tizen.emulator.manager.ui.dialog.TapGuideDialogForWin;
 import org.tizen.emulator.manager.vms.helper.HelperClass;
 import org.tizen.emulator.manager.vms.helper.ProcessOutputReader;
@@ -744,6 +745,101 @@ public class TapUtil {
        }
 
 
+       private static Boolean MANUAL_BRIDGE = null;
+       public static void showGuideForMac() {
+               // check version
+               if (MANUAL_BRIDGE == null) {
+                       MANUAL_BRIDGE = checkMacOSVersionForBridge();
+               }
+
+               if (!MANUAL_BRIDGE) {
+                       return;
+               }
+
+               // check bridge is ready;
+               if (!getBridgeNameForMac().isEmpty()) {
+                       return;
+               }
+
+               // show guide
+               final MessageDialog resultDialog = new MessageDialog();
+               Display.getDefault().asyncExec(new Runnable() {
+                       @Override
+                       public void run() {
+                               int res = resultDialog
+                                               .openSelectionDialog(
+                                                               "You need some configuration for bridge network."
+                                                               + StringResources.NEW_LINE
+                                                               + "Would you like to see a guide?");
+                               if (res == SWT.OK) {
+                                       // Show bridge guide dialog
+                                       TapGuideDialogForMac.open();
+                               }
+                       }
+               });
+
+       }
+
+       // return true if a guide for manual bridge is needed.
+       private static boolean checkMacOSVersionForBridge() {
+               String newLine = StringResources.NEW_LINE;
+               String command = "#!/bin/sh"
+                               + newLine + "do_version_check() {"
+                               + newLine + "   if [ \"$1\" == \"$2\" ]; then"
+                               + newLine + "      echo \"same\""
+                               + newLine + "      exit 0"
+                               + newLine + "   fi"
+                               + newLine + "   ver1front=`echo $1 | cut -d \".\" -f -1`"
+                               + newLine + "   ver1back=`echo $1 | cut -d \".\" -f 2-`"
+                               + newLine + "   ver2front=`echo $2 | cut -d \".\" -f -1`"
+                               + newLine + "   ver2back=`echo $2 | cut -d \".\" -f 2-`"
+                               + newLine + "   if [ \"$ver1front\" != \"$1\" ] || [ \"$ver2front\" != \"$2\" ]; then"
+                               + newLine + "      if [ \"$ver1front\" -gt \"$ver2front\" ]; then"
+                               + newLine + "         echo \"greater\""
+                               + newLine + "         exit 1"
+                               + newLine + "      fi"
+                               + newLine + "      if [ \"$ver1front\" -lt \"$ver2front\" ]; then"
+                               + newLine + "         echo \"lesser\""
+                               + newLine + "         exit -1"
+                               + newLine + "      fi"
+                               + newLine + "      if [ \"$ver1front\" == \"$1\" ] || [ -z \"$ver1back\" ]; then"
+                               + newLine + "         ver1back=0"
+                               + newLine + "      fi"
+                               + newLine + "      if [ \"$ver2front\" == \"$2\" ] || [ -z \"$ver2back\" ]; then"
+                               + newLine + "         ver2back = 0"
+                               + newLine + "      fi"
+                               + newLine + "      do_version_check \"$ver1back\" \"$ver2back\""
+                               + newLine + "   elif [ \"$1\" -gt \"$2\" ]; then"
+                               + newLine + "      echo \"greater\""
+                               + newLine + "      exit 1"
+                               + newLine + "   else"
+                               + newLine + "      echo \"lesser\""
+                               + newLine + "      exit -1"
+                               + newLine + "   fi"
+                               + newLine + "}"
+                               + newLine + "MAC_VER=\"`sw_vers | grep ProductVersion | awk '{print $2}'`\""
+                               + newLine + "do_version_check \"$MAC_VER\" \"10.9\"";
+
+               File tmpFile = createTmpFileWithContent(command);
+
+               if (tmpFile == null || !tmpFile.exists()) {
+                       return true;
+               }
+
+               List<String> cmd = Arrays.asList("/bin/sh", tmpFile.getAbsolutePath());
+               ProcessResult res = HelperClass.runProcess(cmd);
+               if (!res.isSuccess()) {
+                       EMLogger.getLogger().info("Check os version for bridge fail : "
+                                       + res.getResultMessage());
+                       return true;
+               }
+
+               // return false is version is lesser than 10.9.x (exit value : -1)
+               return !(res.getExitValue() == -1);
+
+       }
+
+
        private static File TMP_SCRIPT_FILE = null;
        public static String getBridgeNameForMac() {
 
@@ -764,26 +860,8 @@ public class TapUtil {
                                        + newLine + "      fi"
                                        + newLine + "   fi"
                                        + newLine + "done";
-                       BufferedWriter writer = null;
-                       try {
-                               TMP_SCRIPT_FILE = File.createTempFile("tap", ".sh");
-                               TMP_SCRIPT_FILE.deleteOnExit();
-                               writer = new BufferedWriter(new FileWriter(TMP_SCRIPT_FILE));
-                               writer.append(command);
-                               writer.flush();
-
-                       } catch (IOException e) {
-                               e.printStackTrace();
 
-                       } finally {
-                               if (writer != null) {
-                                       try {
-                                               writer.close();
-                                       } catch (IOException e) {
-                                               e.printStackTrace();
-                                       }
-                               }
-                       }
+                       TMP_SCRIPT_FILE = createTmpFileWithContent(command);
                }
 
                if (TMP_SCRIPT_FILE == null || !TMP_SCRIPT_FILE.exists()) {
@@ -808,6 +886,33 @@ public class TapUtil {
                return result.get(0);
        }
 
+
+       private static File createTmpFileWithContent(String content) {
+               BufferedWriter writer = null;
+               File file = null;
+               try {
+                       file = File.createTempFile("tap", ".sh");
+                       file.deleteOnExit();
+                       writer = new BufferedWriter(new FileWriter(file));
+                       writer.append(content);
+                       writer.flush();
+
+               } catch (IOException e) {
+                       e.printStackTrace();
+
+               } finally {
+                       if (writer != null) {
+                               try {
+                                       writer.close();
+                               } catch (IOException e) {
+                                       e.printStackTrace();
+                               }
+                       }
+               }
+
+               return file;
+       }
+
        public static String getDnsFromTap(String tapName) {
                String dns = ""; //$NON-NLS-1$
                if (EmulatorManager.isLinux()) {
index 96ac710..a2bba8a 100644 (file)
@@ -32,6 +32,8 @@ package org.tizen.emulator.manager.ui.detail.item.property;
 
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
+import org.tizen.emulator.manager.EmulatorManager;
+import org.tizen.emulator.manager.tool.TapUtil;
 import org.tizen.emulator.manager.ui.detail.item.AdvancedViewItem;
 import org.tizen.emulator.manager.ui.detail.item.CommonItemListFactory;
 import org.tizen.emulator.manager.ui.detail.item.LineLabelViewItem;
@@ -67,6 +69,11 @@ public class NetConnectTypeViewItem extends ComboViewItem {
                                        ((NetTapDeviceViewItem) item).resetCombo(newValue);
                                }
 
+                               // (In Mac OS) check host bridge configuration
+                               if (EmulatorManager.isMac() && newValue.equals(VALUE_BRIDGE)) {
+                                       TapUtil.showGuideForMac();
+                               }
+
                                // Disable / enable following item - bridge name, ip info, dns
                                AdvancedViewItem item = lineLabelViewItem
                                                .getItem(CommonItemListFactory.ITEM_NET_IP_INFO);
@@ -100,6 +107,7 @@ public class NetConnectTypeViewItem extends ComboViewItem {
                });
        }
 
+       @Override
        public String getValue() {
                return newValue;
        }
diff --git a/src/org/tizen/emulator/manager/ui/dialog/TapGuideDialogForMac.java b/src/org/tizen/emulator/manager/ui/dialog/TapGuideDialogForMac.java
new file mode 100644 (file)
index 0000000..91a172e
--- /dev/null
@@ -0,0 +1,182 @@
+/* Emulator Manager
+ *
+ * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Minkee Lee <minkee.lee@samsung.com>
+ * SeokYeon Hwang <syeon.hwang@samsung.com>
+ * Sangho Park <sangho1206.park@samsung.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+
+package org.tizen.emulator.manager.ui.dialog;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.graphics.Rectangle;
+import org.eclipse.swt.layout.FormAttachment;
+import org.eclipse.swt.layout.FormData;
+import org.eclipse.swt.layout.FormLayout;
+import org.eclipse.swt.layout.RowData;
+import org.eclipse.swt.layout.RowLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
+import org.tizen.emulator.manager.resources.ColorResources;
+import org.tizen.emulator.manager.resources.FontResources;
+import org.tizen.emulator.manager.resources.StringResources;
+import org.tizen.emulator.manager.ui.MainDialog;
+
+public class TapGuideDialogForMac {
+
+       private static Shell dialog = null;
+       private static int WIDTH = 500;
+       private static int HEIGHT = 400;
+
+       private static int TITLE_INDENT = 7;
+       private static int CONTENT_INDENT = 15;
+
+       public static void open() {
+               makeDialog();
+               dialog.open();
+               while (!dialog.isDisposed()) {
+                       if (!Display.getCurrent().readAndDispatch()) {
+                               Display.getCurrent().sleep();
+                       }
+               }
+       }
+
+       private static void makeDialog() {
+               dialog = new Shell(MainDialog.getShell(), SWT.DIALOG_TRIM | SWT.NONE);
+               dialog.setText("Using Bridge Network");
+               dialog.setSize(WIDTH, HEIGHT);
+               dialog.setBackground(ColorResources.TAB_CONTENTS_BG_COLOR.getColor());
+               RowLayout rowLayout = new RowLayout();
+               rowLayout.type = 512;
+               rowLayout.marginTop = 15;
+               rowLayout.marginLeft = 10;
+               rowLayout.spacing = 3;
+               dialog.setLayout(rowLayout);
+               makeContent();
+
+               if (MainDialog.getShell() != null) {
+                       Point p = MainDialog.getShell().getLocation();
+                       Rectangle rect = MainDialog.getShell().getBounds();
+                       int posX = p.x + (rect.width - WIDTH) / 2;
+                       int posY = p.y + (rect.height - HEIGHT) / 2;
+                       dialog.setLocation(posX, posY);
+               }
+
+       }
+
+       private static void makeContent() {
+
+               Composite comp = new Composite(dialog, SWT.BORDER);
+               comp.setLayoutData(new RowData(WIDTH - 30, HEIGHT - 60));
+               comp.setLayout(new FormLayout());
+
+               String newLine = StringResources.NEW_LINE;
+               Label objective = new Label(comp, SWT.NONE);
+               objective.setFont(FontResources.DETAIL_TITLE_FONT.getFont());
+               objective.setText(
+                                               "To use bridge network in Tizen emulator, bridge connection should be"
+                                               + newLine
+                                               + "created with proper TCP/IP setting.");
+               FormData data = new FormData();
+               data.left = new FormAttachment(0, TITLE_INDENT);
+               data.top = new FormAttachment(0, 5);
+               data.width = WIDTH - 50;
+               data.height = 40;
+               objective.setLayoutData(data);
+
+               Label title1 = new Label(comp, SWT.NONE);
+               title1.setFont(FontResources.DETAIL_TOP_FONT.getFont());
+               title1.setForeground(ColorResources.TEAL.getColor());
+               title1.setText("Create a network bridge");
+               data = new FormData();
+               data.left = new FormAttachment(0, TITLE_INDENT);
+               data.top = new FormAttachment(objective, 3);
+               data.width = WIDTH - 50;
+               data.height = 20;
+               title1.setLayoutData(data);
+
+               Label content1 = new Label(comp, SWT.NONE);
+               String msg = "1. Choose Apple menu > System Preferences, then click working network."
+                               + newLine + newLine
+                               + "2. Click the action pop-up menu(serrated wheel icon), then choose "
+                               + newLine
+                               + "Manage Virtual Interfaces"
+                               + newLine + newLine
+                               + "3. Click Add(plus icon), choose New Bridge, "
+                               + newLine
+                               + "then select the Ethernet interfaces to include in the bridge.";
+
+               content1.setFont(FontResources.DETAIL_TITLE_FONT.getFont());
+               content1.setText(msg);
+               data = new FormData();
+               data.left = new FormAttachment(0, CONTENT_INDENT);
+               data.top = new FormAttachment(title1, 5);
+               data.width = WIDTH - 50;
+               data.height = 100;
+               content1.setLayoutData(data);
+
+               Label title2 = new Label(comp, SWT.NONE);
+               title2.setFont(FontResources.DETAIL_TOP_FONT.getFont());
+               title2.setForeground(ColorResources.TEAL.getColor());
+               title2.setText("TCP/IP setting for bridge connection");
+               data = new FormData();
+               data.left = new FormAttachment(0, TITLE_INDENT);
+               data.top = new FormAttachment(content1, 20);
+               data.width = WIDTH - 50;
+               data.height = 20;
+               title2.setLayoutData(data);
+
+               Label content2 = new Label(comp, SWT.NONE);
+               String msg2 = "Set TCP/IP configuration for bridge.(IP, DNS and so on)";
+
+               content2.setFont(FontResources.DETAIL_TITLE_FONT.getFont());
+               content2.setText(msg2);
+               data = new FormData();
+               data.left = new FormAttachment(0, CONTENT_INDENT);
+               data.top = new FormAttachment(title2, 5);
+               data.width = WIDTH - 50;
+               data.height = 50;
+               content2.setLayoutData(data);
+
+
+               Label end = new Label(comp, SWT.NONE);
+               String endMsg = "Finally, check if your host network works and continue launching VM."
+                               + newLine
+                               + "If host network doesn't work like before, there may be some problems in"
+                               + newLine
+                               + "bridge configuration.";
+               end.setFont(FontResources.DETAIL_TITLE_FONT.getFont());
+               end.setText(endMsg);
+               data = new FormData();
+               data.left = new FormAttachment(0, TITLE_INDENT);
+               data.top = new FormAttachment(content2, 0);
+               data.width = WIDTH - 50;
+               data.height = 50;
+               end.setLayoutData(data);
+       }
+
+}