import java.awt.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

/**
 * A demonstration of a Component that provides drag-and-drop
 * and cut/copy/paste support for Image objects.
 * 
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class ImagePanelDemo implements Runnable
{
  /**
   * Construct and display the GUI.
   * 
   * @param args  The command line arguments (which are ignored)
   * @throws Exception If something goes wrong
   */
  public static void main(String[] args) throws Exception
  {
    SwingUtilities.invokeAndWait(new ImagePanelDemo());
  }
  
  /**
   * The code to run in the event dispatch thread.
   */
  public void run()
  {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(640, 480);
    
    JPanel cp = (JPanel)f.getContentPane();
    cp.setLayout(new GridLayout(1,2));
    
    ImagePanel left = new ImagePanel("Left");
    left.setBorder(BorderFactory.createTitledBorder("Left"));
    left.setDragEnabled(true);
    cp.add(left);
    
    ImagePanel right = new ImagePanel("Right");
    right.setBorder(BorderFactory.createTitledBorder("Right"));
    right.setDragEnabled(true);
    cp.add(right);
    
    Image image;
    try
    {
      image = ImageIO.read(new File("WilsonHall.png"));
    }
    catch (IOException ioe)
    {
      image = null;
    }
    left.setImage(image);


    //[CCP
    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    ActionForwarder forwarder = new ActionForwarder();
    manager.addPropertyChangeListener("permanentFocusOwner", forwarder);
    
    // Enable the GUI components to indicate when they have the focus
    manager.addPropertyChangeListener("permanentFocusOwner", left);
    manager.addPropertyChangeListener("permanentFocusOwner", right);
    //]CCP

  f.setVisible(true);
}
}
