/// Evolver Copyright 2000, Morgan McGuire
/// matrix@alum.mit.edu   http://graphics3d.com/matrix
import java.awt.*;
import javax.swing.*;
import java.net.*;

/**
 * A panel that surrounds its children with a border
 * made of images.
 */
public class FatBorderPanel extends JPanel
{
  /**
   * The internal panel.
   */
  JPanel panel = null;
  
  boolean initialized = false;

  public FatBorderPanel(String imageBase, String imageExtension, Color background)
  {
    super(new BorderLayout());

    panel = new JPanel();

    panel.setBackground(background);
    setBackground(background);

    ImageIcon topLeft = null;
    ImageIcon topCenter = null;
    ImageIcon topRight = null;
    ImageIcon leftCenter = null;
    ImageIcon rightCenter = null;
    ImageIcon bottomLeft = null;
    ImageIcon bottomCenter = null;
    ImageIcon bottomRight = null;
    
    try {
        topLeft = new ImageIcon(new URL(imageBase + "7" + imageExtension));
        topCenter = new ImageIcon(new URL(imageBase + "8" + imageExtension));
        topRight = new ImageIcon(new URL(imageBase + "9" + imageExtension));
        leftCenter = new ImageIcon(new URL(imageBase + "4" + imageExtension));
        rightCenter = new ImageIcon(new URL(imageBase + "6" + imageExtension));
        bottomLeft = new ImageIcon(new URL(imageBase + "1" + imageExtension));
        bottomCenter = new ImageIcon(new URL(imageBase + "2" + imageExtension));
        bottomRight = new ImageIcon(new URL(imageBase + "3" + imageExtension));
    } catch (MalformedURLException e) {
      System.out.println("Warning: " + e);
    }

    add(panel, BorderLayout.CENTER);
    add(new HBorderBar(topLeft, topCenter, topRight), BorderLayout.NORTH);
    add(new VBorderBar(leftCenter), BorderLayout.WEST);
    add(new HBorderBar(bottomLeft, bottomCenter, bottomRight), BorderLayout.SOUTH);
    add(new VBorderBar(rightCenter), BorderLayout.EAST);

    initialized = true;
  }

  protected void addImpl(Component comp, Object constraints, int index) 
  {
    if (! initialized) {
      // During construction, we have to forward the calls to our
      // superclass to make sure our own components are added to the
      // right place.
      super.addImpl(comp, constraints, index);
    } else {
      panel.add(comp, constraints, index);
    }
  }

  public boolean isAncestorOf(Component c) 
  {
    return panel.isAncestorOf(c);
  }

  public void remove(Component comp) 
  {
    panel.remove(comp);
  }
  
  public void remove(int index) 
  {
    panel.remove(index);
  }

  public void removeAll() 
  {
    panel.removeAll();
  }

  public void setLayout(LayoutManager mgr) 
  {
    if (panel == null) {
      // JPanel's constructor will call setLayout on us, so we have to
      // pass the call back to the base class during construction.
      super.setLayout(mgr);
    } else {
      panel.setLayout(mgr);
    }
  }
}

// DESIGN NOTE: Rather than repeat the logic of ImageIcon for loading
// images using Toolkit.getImage, we take advantage of the provided
// implementation.

/**
 * A component responsible for the left and right of the fat 
 * border.  
 */
class VBorderBar extends JPanel
{
  ImageIcon image;

  /**
   * Left and right images are drawn normally, center is stretched.
   * Images must all have the same dimensions.  Square images are
   * recommended.
   */
  public VBorderBar(ImageIcon image)
  {
    this.image = image;

    int width = -1, height = -1;
    width = image.getIconWidth();
    height = image.getIconHeight();

    Dimension dim = new Dimension(width, height);
    setPreferredSize(dim);
    setMinimumSize(dim);
    setMaximumSize(dim);
    invalidate();
  }

  public void paintComponent(Graphics g)
  {
	Rectangle bounds = getBounds();
    
    int width = image.getIconWidth();
    int height = image.getIconHeight();

	g.drawImage(image.getImage(), 0, 0, width, bounds.height, 0, 0, width, height, null);
  }
}

/**
 * A component responsible for the top and bottom of the fat 
 * border.  */
class HBorderBar extends JPanel
{
  ImageIcon left, center, right;

  /**
   * Left and right images are drawn normally, center is stretched.
   * Images must all have the same dimensions.  Square images are
   * recommended.
   */
  public HBorderBar(ImageIcon left, ImageIcon center, ImageIcon right)
  {
    this.left = left;
    this.center = center;
    this.right = right;

    int width = left.getIconWidth();
    int height = left.getIconHeight();

    Dimension dim = new Dimension(width, height);
    setPreferredSize(dim);
    setMinimumSize(dim);
    setMaximumSize(dim);
    invalidate();

  }

  public void paintComponent(Graphics g)
  {
	Rectangle bounds = getBounds();
    
    int width = left.getIconWidth();
    int height = left.getIconHeight();

	g.drawImage(left.getImage(), 0, 0, null);
	g.drawImage(center.getImage(), width, 0, bounds.width - width, height, 0, 0, width, height, null);
	g.drawImage(right.getImage(), bounds.width - width, 0, null);

  }
}

