
package uk.co.wingpath.gui;

import java.util.*;
import java.net.*;
import java.awt.*;
import java.util.List;
import javax.swing.*;

/**
* This class is just a {@link JFrame} that is declared as implementing the
* {@link WWindow} interface.
* <p>There are no constructors for this class. Use the {@link #create create}
* method to create instances.
*/
public class WFrame
    extends JFrame
    implements WWindow
{
    private WFrame ()
    {
    }

    /**
    * Creates a {@code WFrame} with the specified "owner".
    * The icon images are set from the owner's icon images - the owner is not
    * used for any other purpose.
    * The intention is that you can set the icon images of the main frame
    * of a program, and have the icon images propagate through the owner links
    * to all other frames of the program.
    * @param owner {@code WWindow} to copy the icon images from.
    * May be {@code null} if you don't want to copy the icon images.
    */
    public static WFrame create (WWindow owner)
    {
        WFrame frame = new WFrame ();
        if (owner != null)
            frame.setIconImages (owner.getIconImages ());
        return frame;
    }

    /**
    * Sets the icon images from resources.
    * @param resource base name of the image resources. For example, if
    * resource is "image/wings", this method will attempt to set the icon
    * from the resources "image/wings16.png", "image/wings32.png",
    * "image/wings64.png", "image/wings128.png", "image/wings256.png", and
    * "image/wings.png".
    */
    public void setIconImages (String resource)
    {
        String [] suffixes =
        {
            "16.png",
            "32.png",
            "64.png",
            "128.png",
            "256.png",
            ".png"
        };

        ClassLoader loader = WFrame.class.getClassLoader ();
        List<Image> icons = new ArrayList<Image> ();

        for (int i = 0 ; i < suffixes.length ; ++i)
        {
            String suffix = suffixes [i];
            URL iconUrl = loader.getResource (resource + suffix);
            if (iconUrl == null)
                continue;
            Image iconImage = new ImageIcon (iconUrl).getImage ();
            icons.add (iconImage);
        }

        setIconImages (icons);
    }
}

