Use SwtMenuBar in HierarchyViewer application.

Change-Id: I5f61e0c238520d8582e9bef8fa69c354a1165475
diff --git a/hierarchyviewer2/app/.classpath b/hierarchyviewer2/app/.classpath
index d75889a..c5a657c 100644
--- a/hierarchyviewer2/app/.classpath
+++ b/hierarchyviewer2/app/.classpath
@@ -7,5 +7,6 @@
 	<classpathentry combineaccessrules="false" kind="src" path="/ddmlib"/>
 	<classpathentry combineaccessrules="false" kind="src" path="/ddmuilib"/>
 	<classpathentry combineaccessrules="false" kind="src" path="/SdkLib"/>
+	<classpathentry kind="var" path="ANDROID_OUT_FRAMEWORK/swtmenubar.jar" sourcepath="/ANDROID_SRC/sdk/swtmenubar/src"/>
 	<classpathentry kind="output" path="bin"/>
 </classpath>
diff --git a/hierarchyviewer2/app/Android.mk b/hierarchyviewer2/app/Android.mk
index d940403..0e00273 100644
--- a/hierarchyviewer2/app/Android.mk
+++ b/hierarchyviewer2/app/Android.mk
@@ -27,7 +27,8 @@
     swt \
     org.eclipse.jface_3.4.2.M20090107-0800 \
     org.eclipse.core.commands_3.4.0.I20080509-2000 \
-    sdklib
+    sdklib \
+    swtmenubar
 
 LOCAL_MODULE := hierarchyviewer2
 
diff --git a/hierarchyviewer2/app/README b/hierarchyviewer2/app/README
new file mode 100755
index 0000000..5d33255
--- /dev/null
+++ b/hierarchyviewer2/app/README
@@ -0,0 +1,69 @@
+Using the Eclipse project HierarchyViewer
+-----------------------------------------
+
+HierarchyViewer requires some external libraries to compile.
+If you build HierarchyViewer using the makefile, you have nothing
+to configure. However if you want to develop on HierarchyViewer
+using Eclipse, you need to perform the following configuration.
+
+
+-------
+1- Projects required in Eclipse
+-------
+
+To run HierarchyViewer from Eclipse, you need to import the following 5 projects:
+
+  - sdk/hierarchyviewer2/app
+  - sdk/hierarchyviewer2/libs/hierarchyviewerlib/
+  - sdk/ddms/libs/ddmlib
+  - sdk/ddms/libs/ddmuilib
+  - sdk/sdkmanager/libs/sdklib
+
+
+-------
+2- HierarchyViewer requires some SWT JARs to compile.
+-------
+
+SWT is available in the tree under prebuild/<platform>/swt
+
+Because the build path cannot contain relative path that are not inside
+the project directory, the .classpath file references a user library
+called ANDROID_SWT.
+
+In order to compile the project:
+- Open Preferences > Java > Build Path > User Libraries
+
+- Create a new user library named ANDROID_SWT
+- Add the following 4 JAR files:
+
+  - prebuild/<platform>/swt/swt.jar
+  - prebuilt/common/eclipse/org.eclipse.core.commands_3.*.jar
+  - prebuilt/common/eclipse/org.eclipse.equinox.common_3.*.jar
+  - prebuilt/common/eclipse/org.eclipse.jface_3.*.jar
+
+
+-------
+3- HierarchyViewer also requires the compiled SwtMenuBar library.
+-------
+
+Build the swtmenubar library:
+$ cd $TOP (top of Android tree)
+$ . build/envsetup.sh && lunch sdk-eng
+$ sdk/eclipse/scripts/create_sdkman_symlinks.sh
+
+Define a classpath variable in Eclipse:
+- Open Preferences > Java > Build Path > Classpath Variables
+- Create a new classpath variable named ANDROID_OUT_FRAMEWORK
+- Set its folder value to <Android tree>/out/host/<platform>/framework
+- Create a new classpath variable named ANDROID_SRC
+- Set its folder value to <Android tree>
+
+You might need to clean the ddms project (Project > Clean...) after
+you add the new classpath variable, otherwise previous errors might not
+go away automatically.
+
+The ANDROID_SRC part should be optional. It allows you to have access to
+the SwtMenuBar generic parts from the Java editor.
+
+--
+EOF
diff --git a/hierarchyviewer2/app/src/com/android/hierarchyviewer/HierarchyViewerApplication.java b/hierarchyviewer2/app/src/com/android/hierarchyviewer/HierarchyViewerApplication.java
index 3a0a0e9..54a5fd6 100644
--- a/hierarchyviewer2/app/src/com/android/hierarchyviewer/HierarchyViewerApplication.java
+++ b/hierarchyviewer2/app/src/com/android/hierarchyviewer/HierarchyViewerApplication.java
@@ -53,11 +53,15 @@
 import com.android.hierarchyviewerlib.ui.TreeView;
 import com.android.hierarchyviewerlib.ui.TreeViewControls;
 import com.android.hierarchyviewerlib.ui.TreeViewOverview;
+import com.android.menubar.IMenuBarEnhancer;
+import com.android.menubar.MenuBarEnhancer;
+import com.android.menubar.IMenuBarEnhancer.MenuBarMode;
 
 import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.action.Separator;
 import org.eclipse.jface.window.ApplicationWindow;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.SWTException;
 import org.eclipse.swt.custom.SashForm;
 import org.eclipse.swt.custom.StackLayout;
 import org.eclipse.swt.events.SelectionEvent;
@@ -81,6 +85,7 @@
 
 public class HierarchyViewerApplication extends ApplicationWindow {
 
+    private static final String APP_NAME = "Hierarchy Viewer";
     private static final int INITIAL_WIDTH = 1024;
     private static final int INITIAL_HEIGHT = 768;
 
@@ -148,7 +153,7 @@
     @Override
     protected void configureShell(Shell shell) {
         super.configureShell(shell);
-        shell.setText("Hierarchy Viewer");
+        shell.setText(APP_NAME);
         ImageLoader imageLoader = ImageLoader.getLoader(HierarchyViewerDirector.class);
         Image image = imageLoader.loadImage("sdk-hierarchyviewer-128.png", Display.getDefault()); //$NON-NLS-1$
         shell.setImage(image);
@@ -162,7 +167,14 @@
     public void run() {
         setBlockOnOpen(true);
 
-        open();
+        try {
+            open();
+        } catch (SWTException e) {
+         // Ignore "widget disposed" errors after we closed.
+            if (!getShell().isDisposed()) {
+                throw e;
+            }
+        }
 
         TreeViewModel.getModel().removeTreeChangeListener(mTreeChangeListener);
         PixelPerfectModel.getModel().removeImageChangeListener(mImageChangeListener);
@@ -581,12 +593,16 @@
         MenuManager mm = getMenuBarManager();
         mm.removeAll();
 
-        String os = System.getProperty("os.name"); //$NON-NLS-1$
-        if (os.startsWith("Mac OS") == false) { //$NON-NLS-1$
-            MenuManager file = new MenuManager("&File");
+        MenuManager file = new MenuManager("&File");
+        IMenuBarEnhancer enhancer = MenuBarEnhancer.setupMenuManager(
+                APP_NAME,
+                getShell().getDisplay(),
+                file,
+                AboutAction.getAction(getShell()),
+                null /*preferencesAction*/,
+                QuitAction.getAction());
+        if (enhancer.getMenuBarMode() == MenuBarMode.GENERIC) {
             mm.add(file);
-
-            file.add(QuitAction.getAction());
         }
 
         MenuManager device = new MenuManager("&Devices");
@@ -596,11 +612,6 @@
         device.add(LoadViewHierarchyAction.getAction());
         device.add(InspectScreenshotAction.getAction());
 
-        MenuManager help = new MenuManager("&Help");
-        mm.add(help);
-
-        help.add(AboutAction.getAction(getShell()));
-
         mm.updateAll(true);
 
         mDeviceViewButton.setSelection(true);
@@ -626,12 +637,16 @@
         MenuManager mm = getMenuBarManager();
         mm.removeAll();
 
-        String os = System.getProperty("os.name"); //$NON-NLS-1$
-        if (os.startsWith("Mac OS") == false) { //$NON-NLS-1$
-            MenuManager file = new MenuManager("&File");
+        MenuManager file = new MenuManager("&File");
+        IMenuBarEnhancer enhancer = MenuBarEnhancer.setupMenuManager(
+                APP_NAME,
+                getShell().getDisplay(),
+                file,
+                AboutAction.getAction(getShell()),
+                null /*preferencesAction*/,
+                QuitAction.getAction());
+        if (enhancer.getMenuBarMode() == MenuBarMode.GENERIC) {
             mm.add(file);
-
-            file.add(QuitAction.getAction());
         }
 
         MenuManager treeViewMenu = new MenuManager("&Tree View");
@@ -646,11 +661,6 @@
         treeViewMenu.add(InvalidateAction.getAction());
         treeViewMenu.add(RequestLayoutAction.getAction());
 
-        MenuManager help = new MenuManager("&Help");
-        mm.add(help);
-
-        help.add(AboutAction.getAction(getShell()));
-
         mm.updateAll(true);
 
         mDeviceViewButton.setSelection(false);
@@ -676,12 +686,16 @@
         MenuManager mm = getMenuBarManager();
         mm.removeAll();
 
-        String os = System.getProperty("os.name"); //$NON-NLS-1$
-        if (os.startsWith("Mac OS") == false) { //$NON-NLS-1$
-            MenuManager file = new MenuManager("&File");
+        MenuManager file = new MenuManager("&File");
+        IMenuBarEnhancer enhancer = MenuBarEnhancer.setupMenuManager(
+                APP_NAME,
+                getShell().getDisplay(),
+                file,
+                AboutAction.getAction(getShell()),
+                null /*preferencesAction*/,
+                QuitAction.getAction());
+        if (enhancer.getMenuBarMode() == MenuBarMode.GENERIC) {
             mm.add(file);
-
-            file.add(QuitAction.getAction());
         }
 
         MenuManager pixelPerfect = new MenuManager("&Pixel Perfect");
@@ -695,11 +709,6 @@
 
         mm.add(pixelPerfect);
 
-        MenuManager help = new MenuManager("&Help");
-        mm.add(help);
-
-        help.add(AboutAction.getAction(getShell()));
-
         mm.updateAll(true);
 
         mDeviceViewButton.setSelection(false);