/*
 * @(#)Refresh.java    1.0  8 December 2004
 *
 * Copyright 2004
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.gui;

import java.awt.*;
import javax.swing.*;
import java.util.*;

/**
 * <P>Class <CODE>Refresh</CODE> encapsulates methods for graphics refresh.</P>
 *
 * <P>Class <CODE>Refresh</CODE> cannot be instantiated.</P>
 *
 * @author  Richard Rasala
 * @version 2.3.3
 * @since   2.3
 */
public class Refresh {

    /** Private constructor to prevent instantiation. */
    private Refresh() {}
    
    
    /**
     * The hash table to collect windows being pack to prevent recursive
     * calls to packParentWindow for the same window.
     */
    private static Hashtable windowHashtable = new Hashtable();
    
    
    /**
     * <p>Revalidates the given component, packs its parent window, and then
     * repaints the component.</p>
     *
     * <p>As of 2.3.3, prevents indirect recursive calls to this method that
     * attempt to pack the same window object.</p>
     *
     * @param component the component whose parent window should be packed
     */
    public static void packParentWindow(JComponent component) {
        if (component == null)
            return;
        
        component.revalidate();
        
        JRootPane pane = component.getRootPane();
        
        if (pane != null) {
            Object parent = ((JRootPane) pane).getParent();
            
            if (parent instanceof Window) {
                
                synchronized(windowHashtable) {
                    Window window = (Window) parent;
                    
                    if (! windowHashtable.containsKey(window)) {
                        windowHashtable.put(window, window);
                        window.pack();
                        windowHashtable.remove(window);
                    }
                }
            }
        }
        
        component.repaint();
    }
    
}

