src/share/classes/javax/swing/PopupFactory.java

Print this page

        

@@ -23,10 +23,12 @@
  * questions.
  */
 
 package javax.swing;
 
+import sun.swing.SwingAccessor;
+
 import java.applet.Applet;
 import java.awt.*;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import java.util.ArrayList;

@@ -292,10 +294,12 @@
      */
     private static class HeavyWeightPopup extends Popup {
         private static final Object heavyWeightPopupCacheKey =
                  new StringBuffer("PopupFactory.heavyWeightPopupCache");
 
+        private volatile boolean isCacheEnabled = true;
+
         /**
          * Returns either a new or recycled <code>Popup</code> containing
          * the specified children.
          */
         static Popup getHeavyWeightPopup(Component owner, Component contents,

@@ -446,16 +450,27 @@
                     popup._dispose();
                 }
             }
         }
 
+        /**
+         * Enables or disables cache for current object.
+         */
+        void setCacheEnabled(boolean enable) {
+            isCacheEnabled = enable;
+        }
+
         //
         // Popup methods
         //
         public void hide() {
             super.hide();
+            if (isCacheEnabled) {
             recycleHeavyWeightPopup(this);
+            } else {
+                this._dispose();
+            }
         }
 
         /**
          * As we recycle the <code>Window</code>, we don't want to dispose it,
          * thus this method does nothing, instead use <code>_dipose</code>

@@ -947,6 +962,16 @@
             MediumWeightComponent() {
                 super(new BorderLayout());
             }
         }
     }
+
+    static {
+        SwingAccessor.setPopupFactoryAccessor(new SwingAccessor.PopupFactoryAccessor() {
+            public void setHeavyWeightPopupCacheEnabled(Popup popup, boolean enable) {
+                if (popup instanceof HeavyWeightPopup) {
+                    ((HeavyWeightPopup)popup).setCacheEnabled(enable);
+                }
+            }
+        });
+    }
 }