/*
 * @topic T11346 Spring 2016 Inheritance Demo
 * @brief class Window extends Component
*/
package inheritance_demo;

import java.util.ArrayList;

public class Window extends Component
{
    //--------------------------------------
    // data attributes
    //--------------------------------------
    private ArrayList< Gadget > gadgets =
        new ArrayList< Gadget >();

    //--------------------------------------
    // constructors
    //--------------------------------------
    public Window(int width, int height)
    {
        super( width, height );
    }//Window
    
    public Window(int topx, int topy, int width, int height)
    {
        super( topx, topy, width, height );
    }//Window
    
    //--------------------------------------
    // operations
    //--------------------------------------
    public void attach( Gadget gadget ) {
        gadgets.add(gadget);
    }//attach

    public void show()
    {
        System.out.println("Window:");
        for ( Gadget gadget : gadgets ) {
            gadget.show();
        }
        System.out.println();
    }//show

    public void userClicks( String caption ) {
        for ( Gadget gadget : gadgets ) {
            if ( gadget instanceof Button ) {
                Button button = (Button) gadget;
                if ( caption.equals( button.getCaption() ) ) {
                    IClick handler = button.getEventHandler();
                    if ( handler != null ) {
                        handler.click();
                        return;
                    }
                }
            }
        }
    }
    public void attachHandler( String caption, IClick handler ) {
        for ( Gadget gadget : gadgets ) {
            if ( gadget instanceof Button ) {
                Button button = (Button) gadget;
                if ( caption.equals( button.getCaption() ) ) {
                    button.setEventHandler( handler );
                }
            }
        }
    }
}//class Window