Fixes Issue #1103.
Resolves an issue where some of a PopupPanel's instance state was
being stored in the singleton PopupImplMozilla, which caused issues
when more than one Popup was active in an application.

Patch by: bobv, scottb
Review by: knorton



git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@1166 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/ui/impl/PopupImplMozilla.java b/user/src/com/google/gwt/user/client/ui/impl/PopupImplMozilla.java
index 18fbe27..62510dd 100644
--- a/user/src/com/google/gwt/user/client/ui/impl/PopupImplMozilla.java
+++ b/user/src/com/google/gwt/user/client/ui/impl/PopupImplMozilla.java
@@ -45,12 +45,22 @@
  */
 public class PopupImplMozilla extends PopupImpl {
 
-  private Element containerElement = null;
+  /**
+   * Cache the value to avoid repeated calls.
+   */
+  private static boolean isMac = isMac();
+  
+  private static native boolean isMac() /*-{
+    if (navigator.userAgent.indexOf("Macintosh") != -1) {
+      return true;
+    }  
+    return false;
+  }-*/;
 
   public Element createElement() {
     final Element outerElem = DOM.createDiv();
 
-    if (isMac()) {
+    if (isMac) {
       // To solve the scrollbar rendering problem on the Mac, we have to make
       // the PopupPanel a 'heavyweight' element by setting a style of
       // 'overflow:auto' on the outermost div. This ensures that all of the
@@ -77,25 +87,12 @@
           DOM.setStyleAttribute(outerElem, "overflow", "auto");
         }
       });
-
-      // Keep a reference to the container element in order to avoid further
-      // calls to isMac() in getContainerElement().
-      containerElement = DOM.getFirstChild(outerElem);
-    } else {
-      containerElement = outerElem;
     }
 
     return outerElem;
   }
 
-  public Element getContainerElement(Element popup) {
-    return containerElement;
+  public Element getContainerElement(Element outerElem) {
+    return isMac ? DOM.getFirstChild(outerElem) : outerElem;
   }
-
-  private native boolean isMac() /*-{
-    if (navigator.userAgent.indexOf("Macintosh") != -1) {
-      return true;
-    }  
-    return false;
-  }-*/;
 }
diff --git a/user/test/com/google/gwt/user/client/ui/PopupTest.java b/user/test/com/google/gwt/user/client/ui/PopupTest.java
index 6549c90..8bcd81a 100644
--- a/user/test/com/google/gwt/user/client/ui/PopupTest.java
+++ b/user/test/com/google/gwt/user/client/ui/PopupTest.java
@@ -16,6 +16,8 @@
 package com.google.gwt.user.client.ui;
 
 import com.google.gwt.junit.client.GWTTestCase;
+import com.google.gwt.user.client.DOM;
+import com.google.gwt.user.client.Element;
 import com.google.gwt.user.client.Window;
 
 /**
@@ -23,6 +25,15 @@
  */
 public class PopupTest extends GWTTestCase {
 
+  /**
+   * Expose otherwise private or protected methods.
+   */
+  private class TestablePopupPanel extends PopupPanel {
+    public Element getContainerElement() {
+      return super.getContainerElement();
+    }
+  }
+
   public String getModuleName() {
     return "com.google.gwt.user.User";
   }
@@ -66,4 +77,13 @@
     });
     popup.hide();
   }
+  
+  public void testSeparateContainers() {
+    TestablePopupPanel p1 = new TestablePopupPanel();
+    TestablePopupPanel p2 = new TestablePopupPanel();
+    assertTrue(p1.getContainerElement() != null);
+    assertTrue(p2.getContainerElement() != null);
+    assertFalse(
+        DOM.compare(p1.getContainerElement(), p2.getContainerElement()));
+  }
 }