home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &n…he Search for Life DVD 2 / DVD-ROM.iso / install / jre1_3 / lib / rt.jar / javax / swing / DefaultPopupFactory.class (.txt) < prev    next >
Encoding:
Java Class File  |  1979-12-31  |  6.1 KB  |  350 lines

  1. package javax.swing;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.Dialog;
  6. import java.awt.Frame;
  7. import java.awt.Point;
  8. import java.awt.Rectangle;
  9. import java.awt.Window;
  10. import java.util.Hashtable;
  11. import java.util.Vector;
  12.  
  13. class DefaultPopupFactory implements PopupFactory {
  14.    private transient Component component;
  15.    protected transient Component invoker;
  16.    private transient Popup popup;
  17.    private transient Frame frame;
  18.    private int desiredLocationX;
  19.    private int desiredLocationY;
  20.    private int lastPopupType = 0;
  21.    private static final Object heavyPopupCacheKey = new StringBuffer("PopupFactory.heavyPopupCache");
  22.    private static final Object lightPopupCacheKey = new StringBuffer("PopupFactory.lightPopupCache");
  23.    private static final Object mediumPopupCacheKey = new StringBuffer("PopupFactory.mediumPopupCache");
  24.    private static final Object defaultLWPopupEnabledKey = new StringBuffer("PopupFactory.defaultLWPopupEnabledKey");
  25.    private static final int MAX_CACHE_SIZE = 5;
  26.    private boolean lightWeightPopupEnabled = true;
  27.    private static final int LIGHT_WEIGHT_POPUP = 0;
  28.    private static final int MEDIUM_WEIGHT_POPUP = 1;
  29.    private static final int HEAVY_WEIGHT_POPUP = 2;
  30.    private static final Object classLock = new Object();
  31.  
  32.    public static void setDefaultLightWeightPopupEnabled(boolean var0) {
  33.       SwingUtilities.appContextPut(defaultLWPopupEnabledKey, new Boolean(var0));
  34.    }
  35.  
  36.    public static boolean getDefaultLightWeightPopupEnabled() {
  37.       Boolean var0 = (Boolean)SwingUtilities.appContextGet(defaultLWPopupEnabledKey);
  38.       if (var0 == null) {
  39.          SwingUtilities.appContextPut(defaultLWPopupEnabledKey, Boolean.TRUE);
  40.          return true;
  41.       } else {
  42.          return var0;
  43.       }
  44.    }
  45.  
  46.    public void setLightWeightPopupEnabled(boolean var1) {
  47.       this.lightWeightPopupEnabled = var1;
  48.    }
  49.  
  50.    public boolean isLightWeightPopupEnabled() {
  51.       return this.lightWeightPopupEnabled;
  52.    }
  53.  
  54.    private static Hashtable getHeavyPopupCache() {
  55.       Hashtable var0 = (Hashtable)SwingUtilities.appContextGet(heavyPopupCacheKey);
  56.       if (var0 == null) {
  57.          var0 = new Hashtable(2);
  58.          SwingUtilities.appContextPut(heavyPopupCacheKey, var0);
  59.       }
  60.  
  61.       return var0;
  62.    }
  63.  
  64.    private static Vector getLightPopupCache() {
  65.       Vector var0 = (Vector)SwingUtilities.appContextGet(lightPopupCacheKey);
  66.       if (var0 == null) {
  67.          var0 = new Vector();
  68.          SwingUtilities.appContextPut(lightPopupCacheKey, var0);
  69.       }
  70.  
  71.       return var0;
  72.    }
  73.  
  74.    private static Vector getMediumPopupCache() {
  75.       Vector var0 = (Vector)SwingUtilities.appContextGet(mediumPopupCacheKey);
  76.       if (var0 == null) {
  77.          var0 = new Vector();
  78.          SwingUtilities.appContextPut(mediumPopupCacheKey, var0);
  79.       }
  80.  
  81.       return var0;
  82.    }
  83.  
  84.    static void recycleHeavyPopup(Popup var0) {
  85.       Object var1 = classLock;
  86.       synchronized(var1) {
  87.          Window var3 = SwingUtilities.getWindowAncestor((Component)var0);
  88.          Hashtable var4 = getHeavyPopupCache();
  89.          Vector var2;
  90.          if (var4.containsKey(var3)) {
  91.             var2 = (Vector)var4.get(var3);
  92.          } else {
  93.             var2 = new Vector();
  94.             var4.put(var3, var2);
  95.             var3.addWindowListener(new 1(var3));
  96.          }
  97.  
  98.          if (var2.size() < 5) {
  99.             var2.addElement(var0);
  100.          }
  101.  
  102.       }
  103.    }
  104.  
  105.    static Popup getRecycledHeavyPopup(Window var0) {
  106.       Object var1 = classLock;
  107.       synchronized(var1) {
  108.          Hashtable var3 = getHeavyPopupCache();
  109.          if (var3.containsKey(var0)) {
  110.             Vector var2 = (Vector)var3.get(var0);
  111.             if (var2.size() > 0) {
  112.                Popup var9 = (Popup)var2.elementAt(0);
  113.                var2.removeElementAt(0);
  114.                return var9;
  115.             } else {
  116.                Object var5 = null;
  117.                return (Popup)var5;
  118.             }
  119.          } else {
  120.             Object var4 = null;
  121.             return (Popup)var4;
  122.          }
  123.       }
  124.    }
  125.  
  126.    static void recycleLightPopup(Popup var0) {
  127.       Object var1 = classLock;
  128.       synchronized(var1) {
  129.          Vector var2 = getLightPopupCache();
  130.          if (var2.size() < 5) {
  131.             var2.addElement(var0);
  132.          }
  133.  
  134.       }
  135.    }
  136.  
  137.    static Popup getRecycledLightPopup() {
  138.       Object var0 = classLock;
  139.       synchronized(var0) {
  140.          Vector var1 = getLightPopupCache();
  141.          if (var1.size() > 0) {
  142.             Popup var7 = (Popup)var1.elementAt(0);
  143.             var1.removeElementAt(0);
  144.             return var7;
  145.          } else {
  146.             Object var3 = null;
  147.             return (Popup)var3;
  148.          }
  149.       }
  150.    }
  151.  
  152.    static void recycleMediumPopup(Popup var0) {
  153.       Object var1 = classLock;
  154.       synchronized(var1) {
  155.          Vector var2 = getMediumPopupCache();
  156.          if (var2.size() < 5) {
  157.             var2.addElement(var0);
  158.          }
  159.  
  160.       }
  161.    }
  162.  
  163.    static Popup getRecycledMediumPopup() {
  164.       Object var0 = classLock;
  165.       synchronized(var0) {
  166.          Vector var1 = getMediumPopupCache();
  167.          if (var1.size() > 0) {
  168.             Popup var7 = (Popup)var1.elementAt(0);
  169.             var1.removeElementAt(0);
  170.             return var7;
  171.          } else {
  172.             Object var3 = null;
  173.             return (Popup)var3;
  174.          }
  175.       }
  176.    }
  177.  
  178.    static void recyclePopup(Popup var0) {
  179.       if (var0 instanceof JPanelPopup) {
  180.          recycleLightPopup(var0);
  181.       } else if (var0 instanceof WindowPopup) {
  182.          recycleHeavyPopup(var0);
  183.       } else if (var0 instanceof PanelPopup) {
  184.          recycleMediumPopup(var0);
  185.       }
  186.  
  187.    }
  188.  
  189.    protected Popup createLightWeightPopup() {
  190.       Object var1 = getRecycledLightPopup();
  191.       if (var1 == null) {
  192.          var1 = new JPanelPopup(this);
  193.       }
  194.  
  195.       return (Popup)var1;
  196.    }
  197.  
  198.    protected Popup createMediumWeightPopup() {
  199.       Object var1 = getRecycledMediumPopup();
  200.       if (var1 == null) {
  201.          var1 = new PanelPopup(this);
  202.       }
  203.  
  204.       return (Popup)var1;
  205.    }
  206.  
  207.    protected Popup createHeavyWeightPopup() {
  208.       Object var1 = this.invoker != null ? SwingUtilities.getWindowAncestor(this.invoker) : null;
  209.       if (var1 != null) {
  210.          this.popup = getRecycledHeavyPopup((Window)var1);
  211.       } else {
  212.          var1 = new Frame();
  213.       }
  214.  
  215.       if (this.popup == null) {
  216.          this.popup = new WindowPopup(this, (Window)var1);
  217.       }
  218.  
  219.       return this.popup;
  220.    }
  221.  
  222.    private boolean popupFit(Rectangle var1) {
  223.       if (this.invoker != null) {
  224.          for(Container var2 = this.invoker.getParent(); var2 != null; var2 = ((Component)var2).getParent()) {
  225.             if (var2 instanceof JFrame || var2 instanceof JDialog || var2 instanceof JWindow) {
  226.                return SwingUtilities.isRectangleContainingRectangle(((Component)var2).getBounds(), var1);
  227.             }
  228.  
  229.             if (var2 instanceof JApplet) {
  230.                Rectangle var3 = ((Component)var2).getBounds();
  231.                Point var4 = ((Component)var2).getLocationOnScreen();
  232.                var3.x = var4.x;
  233.                var3.y = var4.y;
  234.                return SwingUtilities.isRectangleContainingRectangle(var3, var1);
  235.             }
  236.  
  237.             if (var2 instanceof Frame) {
  238.                return SwingUtilities.isRectangleContainingRectangle(((Component)var2).getBounds(), var1);
  239.             }
  240.          }
  241.       }
  242.  
  243.       return false;
  244.    }
  245.  
  246.    private boolean ancestorIsModalDialog(Component var1) {
  247.       Object var2 = null;
  248.       if (var1 != null) {
  249.          for(Container var3 = var1.getParent(); var3 != null; var3 = ((Component)var3).getParent()) {
  250.             if (var3 instanceof Dialog && ((Dialog)var3).isModal()) {
  251.                return true;
  252.             }
  253.          }
  254.       }
  255.  
  256.       return false;
  257.    }
  258.  
  259.    private void replacePopup(int var1) {
  260.       this.popup.removeComponent(this.component);
  261.       recyclePopup(this.popup);
  262.       this.popup = null;
  263.       switch (var1) {
  264.          case 0:
  265.             this.popup = this.createLightWeightPopup();
  266.             break;
  267.          case 1:
  268.             this.popup = this.createMediumWeightPopup();
  269.             break;
  270.          case 2:
  271.             this.popup = this.createHeavyWeightPopup();
  272.       }
  273.  
  274.       this.popup.setLocationOnScreen(this.desiredLocationX, this.desiredLocationY);
  275.       this.popup.addComponent(this.component, "Center");
  276.       this.component.invalidate();
  277.       this.popup.pack();
  278.    }
  279.  
  280.    private boolean invokerInHeavyWeightPopup(Component var1) {
  281.       if (var1 != null) {
  282.          for(Container var2 = var1.getParent(); var2 != null; var2 = ((Component)var2).getParent()) {
  283.             if (var2 instanceof WindowPopup) {
  284.                return true;
  285.             }
  286.  
  287.             if (var2 instanceof PanelPopup || var2 instanceof JPanelPopup) {
  288.                break;
  289.             }
  290.          }
  291.       }
  292.  
  293.       return false;
  294.    }
  295.  
  296.    public Popup getPopup(Component var1, Component var2, int var3, int var4) {
  297.       this.invoker = var2;
  298.       this.component = var1;
  299.       this.desiredLocationX = var3;
  300.       this.desiredLocationY = var4;
  301.       switch (this.lastPopupType) {
  302.          case 0:
  303.             this.popup = this.createLightWeightPopup();
  304.             break;
  305.          case 1:
  306.             this.popup = this.createMediumWeightPopup();
  307.             break;
  308.          case 2:
  309.             this.popup = this.createHeavyWeightPopup();
  310.       }
  311.  
  312.       int var5 = this.lastPopupType;
  313.       this.popup.setLocationOnScreen(this.desiredLocationX, this.desiredLocationY);
  314.       this.popup.addComponent(this.component, "Center");
  315.       this.popup.pack();
  316.       Rectangle var7 = new Rectangle(this.desiredLocationX, this.desiredLocationY, this.popup.getWidth(), this.popup.getHeight());
  317.       byte var6;
  318.       if (this.popupFit(var7)) {
  319.          if ((var1 instanceof JToolTip || var1 instanceof JPopupMenu && ((JPopupMenu)var1).isLightWeightPopupEnabled()) && this.lightWeightPopupEnabled) {
  320.             var6 = 0;
  321.          } else {
  322.             var6 = 1;
  323.          }
  324.       } else {
  325.          var6 = 2;
  326.       }
  327.  
  328.       if (this.invokerInHeavyWeightPopup(var2)) {
  329.          var6 = 2;
  330.       }
  331.  
  332.       if (var2 == null) {
  333.          var6 = 2;
  334.       }
  335.  
  336.       if (var6 != var5) {
  337.          this.replacePopup(var6);
  338.          var5 = var6;
  339.       }
  340.  
  341.       this.lastPopupType = var5;
  342.       return this.popup;
  343.    }
  344.  
  345.    // $FF: synthetic method
  346.    static Hashtable access$000() {
  347.       return getHeavyPopupCache();
  348.    }
  349. }
  350.