package visual.dynamic.sampled;


import java.awt.*;
import java.util.*;
import javax.swing.*;

import visual.*;
import visual.statik.SimpleContent;


/**
 * The renderer for Screen objects
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class      ScreenRenderer 
       implements VisualizationRenderer
{
    private Composite               oldComposite;    
    private VisualizationRenderer   decorated;    


    /**
     * Explicit Value Constructor
     */
    public ScreenRenderer(VisualizationRenderer decorated)
    {
       this.decorated = decorated;
       
    }
    
    
//[postRendering.

    /**
     * Operations to perform after rendering
     *
     * @param g      The rendering engine
     * @param model  The Visualization containing the content
     * @param view   The component presenting the content
     */
    public void postRendering(Graphics          g,
                              Visualization     model,
                              VisualizationView view)
    {
       Graphics2D                g2;       
       int                       frameNumber;       
       Screen                    smodel;       
       Iterator<Transition>      transitions;
       Iterator<Superimposition> superimpositions;
       
       
       g2 = (Graphics2D)g;       
       g2.setComposite(oldComposite);       
       view.setDoubleBuffered(true);

       // Get information from the model
       smodel           = (Screen)model;
       frameNumber      = smodel.getFrameNumber();
       transitions      = smodel.getTransitions();
       superimpositions = smodel.getSuperimpositions();
       

       // Apply the transitions
       if (transitions != null)
       {
          while (transitions.hasNext())
          {
             transitions.next().postRendering(g, frameNumber);           
          }          
       }
       

       // Apply the superimpositions
       if (superimpositions != null)
       {
          while (superimpositions.hasNext())
          {
             superimpositions.next().postRendering(g, frameNumber);           
          }
       }
    }
//]postRendering.    
//[preRendering.

    /**
     * Operations to perform before rendering
     *
     * @param g      The rendering engine
     * @param model  The Visualization containing the content
     * @param view   The component presenting the content
     */
    public void preRendering(Graphics          g,
                             Visualization     model,
                             VisualizationView view)
    {
       Graphics2D            g2;       
       int                   frameNumber;       
       Screen                smodel;       
       Iterator<Transition>  transitions;
       Iterator<Superimposition>  superimpositions;
       

       g2 = (Graphics2D)g;       
       oldComposite = g2.getComposite();       
       view.setDoubleBuffered(true);

       // Get information from the model
       smodel           = (Screen)model;
       transitions      = smodel.getTransitions();
       superimpositions = smodel.getSuperimpositions();
       frameNumber      = smodel.getFrameNumber();

       // Apply the transitions
       if (transitions != null)
       {
          while (transitions.hasNext())
          {
             transitions.next().preRendering(g, frameNumber);           
          }          
       }
       

       // Apply the superimpositions
       if (superimpositions != null)
       {
          while (superimpositions.hasNext())
          {
             superimpositions.next().preRendering(g, frameNumber);           
          }
       }
    }
//]preRendering.
//[render.

    /**
     * Render the content contained in the model.
     *
     *
     * @param g      The rendering engine
     * @param model  The Visualization containing the content
     * @param view   The component presenting the content
     */
    public void render(Graphics          g,
                       Visualization     model,
                       VisualizationView view)
    {
       decorated.render(g, model, view);       
    }
//]render.


}
