FileDocCategorySizeDatePackage
PopupFactory.javaAPI DocJava SE 6 API32506Tue Jun 10 00:26:40 BST 2008javax.swing

PopupFactory

public class PopupFactory extends Object
PopupFactory, as the name implies, is used to obtain instances of Popups. Popups are used to display a Component above all other Components in a particular containment hierarchy. The general contract is that once you have obtained a Popup from a PopupFactory, you must invoke hide on the Popup. The typical usage is:
PopupFactory factory = PopupFactory.getSharedInstance();
Popup popup = factory.getPopup(owner, contents, x, y);
popup.show();
...
popup.hide();
see
Popup
version
1.26 02/22/05
since
1.4

Fields Summary
private static final Object
SharedInstanceKey
The shared instanceof PopupFactory is per AppContext. This is the key used in the AppContext to locate the PopupFactory.
private static final int
MAX_CACHE_SIZE
Max number of items to store in any one particular cache.
static final int
LIGHT_WEIGHT_POPUP
Key used to indicate a light weight popup should be used.
static final int
MEDIUM_WEIGHT_POPUP
Key used to indicate a medium weight Popup should be used.
static final int
HEAVY_WEIGHT_POPUP
private int
popupType
Default type of Popup to create.
static final StringBuffer
forceHeavyWeightPopupKey
Key used for client property to force heavy weight popups for a component.
Constructors Summary
Methods Summary
private javax.swing.PopupgetHeadlessPopup(java.awt.Component owner, java.awt.Component contents, int ownerX, int ownerY)
Creates a headless popup

        return HeadlessPopup.getHeadlessPopup(owner, contents, ownerX, ownerY);
    
private javax.swing.PopupgetHeavyWeightPopup(java.awt.Component owner, java.awt.Component contents, int ownerX, int ownerY)
Creates a heavy weight popup.

        if (GraphicsEnvironment.isHeadless()) {
            return getMediumWeightPopup(owner, contents, ownerX, ownerY);
        }
        return HeavyWeightPopup.getHeavyWeightPopup(owner, contents, ownerX,
                                                    ownerY);
    
private javax.swing.PopupgetLightWeightPopup(java.awt.Component owner, java.awt.Component contents, int ownerX, int ownerY)
Creates a light weight popup.

        return LightWeightPopup.getLightWeightPopup(owner, contents, ownerX,
                                                    ownerY);
    
private javax.swing.PopupgetMediumWeightPopup(java.awt.Component owner, java.awt.Component contents, int ownerX, int ownerY)
Creates a medium weight popup.

        return MediumWeightPopup.getMediumWeightPopup(owner, contents,
                                                      ownerX, ownerY);
    
public javax.swing.PopupgetPopup(java.awt.Component owner, java.awt.Component contents, int x, int y)
Creates a Popup for the Component owner containing the Component contents. owner is used to determine which Window the new Popup will parent the Component the Popup creates to. A null owner implies there is no valid parent. x and y specify the preferred initial location to place the Popup at. Based on screen size, or other paramaters, the Popup may not display at x and y.

param
owner Component mouse coordinates are relative to, may be null
param
contents Contents of the Popup
param
x Initial x screen coordinate
param
y Initial y screen coordinate
exception
IllegalArgumentException if contents is null
return
Popup containing Contents

        if (contents == null) {
            throw new IllegalArgumentException(
                          "Popup.getPopup must be passed non-null contents");
        }

	int popupType = getPopupType(owner, contents, x, y);
        Popup popup = getPopup(owner, contents, x, y, popupType);

        if (popup == null) {
            // Didn't fit, force to heavy.
            popup = getPopup(owner, contents, x, y, HEAVY_WEIGHT_POPUP);
        }
        return popup;
    
private javax.swing.PopupgetPopup(java.awt.Component owner, java.awt.Component contents, int ownerX, int ownerY, int popupType)
Obtains the appropriate Popup based on popupType.

        if (GraphicsEnvironment.isHeadless()) {
            return getHeadlessPopup(owner, contents, ownerX, ownerY);
        }

	switch(popupType) {
	case LIGHT_WEIGHT_POPUP:
	    return getLightWeightPopup(owner, contents, ownerX, ownerY);
	case MEDIUM_WEIGHT_POPUP:
	    return getMediumWeightPopup(owner, contents, ownerX, ownerY);
	case HEAVY_WEIGHT_POPUP:
	    return getHeavyWeightPopup(owner, contents, ownerX, ownerY);
	}
        return null;
    
intgetPopupType()
Returns the preferred type of Popup to create.

        return popupType;
    
private intgetPopupType(java.awt.Component owner, java.awt.Component contents, int ownerX, int ownerY)
Returns the popup type to use for the specified parameters.

        int popupType = getPopupType();

	if (owner == null || invokerInHeavyWeightPopup(owner)) {
	    popupType = HEAVY_WEIGHT_POPUP;
	}
        else if (popupType == LIGHT_WEIGHT_POPUP &&
                 !(contents instanceof JToolTip) &&
                 !(contents instanceof JPopupMenu)) {
            popupType = MEDIUM_WEIGHT_POPUP;
        }

        // Check if the parent component is an option pane.  If so we need to
        // force a heavy weight popup in order to have event dispatching work
        // correctly.
        Component c = owner;
        while (c != null) {
            if (c instanceof JComponent) {
                if (((JComponent)c).getClientProperty(
                            forceHeavyWeightPopupKey) == Boolean.TRUE) {
                    popupType = HEAVY_WEIGHT_POPUP;
                    break;
                }
            }
            c = c.getParent();
        }

	return popupType;
    
public static javax.swing.PopupFactorygetSharedInstance()
Returns the shared PopupFactory which can be used to obtain Popups.

return
Shared PopupFactory

        PopupFactory factory = (PopupFactory)SwingUtilities.appContextGet(
                         SharedInstanceKey);

        if (factory == null) {
            factory = new PopupFactory(); 
            setSharedInstance(factory);
        }
        return factory;
    
private booleaninvokerInHeavyWeightPopup(java.awt.Component i)
Returns true if the Component i inside a heavy weight Popup.

	if (i != null) {
	    Container parent;
	    for(parent = i.getParent() ; parent != null ; parent =
		    parent.getParent()) {
		if (parent instanceof Popup.HeavyWeightWindow) {
		    return true;
                }
	    }
	}
        return false;
    
voidsetPopupType(int type)
Provides a hint as to the type of Popup that should be created.

        popupType = type;
    
public static voidsetSharedInstance(javax.swing.PopupFactory factory)
Sets the PopupFactory that will be used to obtain Popups. This will throw an IllegalArgumentException if factory is null.

param
factory Shared PopupFactory
exception
IllegalArgumentException if factory is null
see
#getPopup



                                        
         
        if (factory == null) {
            throw new IllegalArgumentException("PopupFactory can not be null");
        }
        SwingUtilities.appContextPut(SharedInstanceKey, factory);