package slides;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


/**
 * A container for a slide presentation
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class SlidePresentation extends    JPanel
                               implements ActionListener
{
    protected ButtonPanel       buttonP;
    protected FlipChartLayout   layout;
    protected Panel             slidePanel;
    protected TitlePanel        titleP;


    public static final Color         BACKGROUND = Color.black;
    public static final Color         FOREGROUND = Color.white;
    public static final Color         TITLE      = Color.yellow;



    /**
     * Explicit value constructor
     *
     * @param slides   The array of slides
     */
    public SlidePresentation(Component[]  slides)
    {
	super();

	slidePanel = new Panel();
	buttonP = new ButtonPanel();
	layout = new FlipChartLayout();
	titleP = new TitlePanel("         ");

	performLayout(slides);

	buttonP.addActionListener(this);
	showTitle();
    }



    /**
     * Handle actionPerformed events (required by ActionListener)
     *
     * @param evt    The event
     */
    public void actionPerformed(ActionEvent evt)
    {
	Object      source;
	String      ac;



	ac     = evt.getActionCommand();

	if      (ac.equals("forward"))  forward();
	else if (ac.equals("backward")) backward();

	showTitle();
    }



    /**
     * Move one slide backward
     */
    protected void backward()
    {
	layout.backward(slidePanel);
	showTitle();
    }


    /**
     * Move one slide forward
     */
    protected void forward()
    {
	layout.forward(slidePanel);
	showTitle();
    }


    /**
     * Layout this component
     */
    protected void performLayout(Component[] slides)
    {
	ImageIcon   icon;
	int         i;
	JPanel      bottomP, centerP, tmpP;
	JLabel      keyboardP;

	setBackground(BACKGROUND);
	setForeground(FOREGROUND);

	titleP.setHighlight(TITLE);
	titleP.setForeground(BACKGROUND);
	titleP.setBackground(BACKGROUND);




	setLayout(new BorderLayout());

	slidePanel.setLayout(layout);

	     for (i=0; i < slides.length; i++) 
             {
		 slidePanel.add(slides[i], "Slide"+i);
	     }

	add(slidePanel, BorderLayout.CENTER);

	bottomP = new JPanel();
	    bottomP.setLayout(new BorderLayout());

	        tmpP = new JPanel();
		tmpP.setBackground(BACKGROUND);
		tmpP.setLayout(new GridLayout(2,1));
		tmpP.add(new JLabel(" "));
		tmpP.add(buttonP);

	    bottomP.add(tmpP, BorderLayout.WEST);

	    icon = new ImageIcon("keyboard.gif");
	    keyboardP = new JLabel(icon);
	    bottomP.add(keyboardP, BorderLayout.EAST);

	    bottomP.add(new JLabel(" "), BorderLayout.CENTER);

	    bottomP.setBackground(Color.black);

	add(bottomP, BorderLayout.SOUTH);

	    titleP.setFont(new Font("Sans Serif", Font.BOLD, 24));

	add(titleP, BorderLayout.NORTH);
    }



    /**
     * Show the title associated with the first/front
     * slide
     */
    protected void showTitle()
    {
	Slide         first;

	first = (Slide)layout.getFirst(slidePanel);
	if (first != null) 
        {
	    titleP.setText(first.getName());
	}
    }
}
