//package com.apple.spell.ui

import javax.swing.border.AbstractBorder;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;

/**
 * <p>This should be used to help Java applications on OSX create areas
 * of the interface that look like the "drop zones" or "trays" that
 * are part of the Aqua interface.</p>
 * <p>This is only slightly changed from the com.apple.spell.ui.ImageBorder
 * class included in Apple's Spelling API examples. I have included a
 * better image of the drawer (so the corners are transparent) and
 * provided a simple static method to generate a panel around a
 * component. The original file was copyright 2002, Apple Computer and
 * authored by SL.  Reading their license, it looks like I don't have
 * to include it here once modifications are made, so I removed it.</p>
 *
 * @author  Stephen Lewallen (original), Rahul Bhargava (changes)
 */
public class ImageBorder extends AbstractBorder implements ImageObserver {

  private String fURL = null;
  //private Insets fInsets = new Insets(8,8,8,8);
  //changed by rahulb
  private Insets fInsets = new Insets(10,10,10,10);

  //// Image components
  private Image fImage;
  private int fWidth, fHeight;

  public ImageBorder() {
  }

  public ImageBorder(String url) {
    setImageSource(url);
  }

  public String getImageSource() {
    return fURL;
  }

  public void setImageSource(String url) {
    fURL = url;
    init(true);
  }

  public void setImageURL(String url) {
    fURL = url;
    init(false);
  }

  public Insets getBorderInsets(Component c) {
    return fInsets;
  }

  public Insets getBorderInsets() {
    return fInsets;
  }

  public void setBorderInsets(Insets insets) {
    fInsets = insets;
  }

  public boolean imageUpdate(Image p0, int p1, int p2, int p3, int p4, int p5) {
    return false;
  }

  private void init(boolean relative) {
    try {
      URL url = null;
      if (relative) {
        Class c = getClass();
        ClassLoader l = c.getClassLoader();
        System.out.println("trying to load with "+fURL);
        url = l.getResource(fURL);
      }
      else
        url = new URL(fURL);

      ImageIcon icon = new ImageIcon(url);
      fImage = icon.getImage();

      // Get master image and info
      fWidth = icon.getIconWidth();
      fHeight = icon.getIconHeight();
    }
    catch(Throwable t) {
      t.printStackTrace();
      fImage = null;
    }
  }

  public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {

    if (fImage == null)
      return;

    // NW
    g.drawImage(fImage,0,0,fInsets.left,fInsets.top,
      0,0,fInsets.left,fInsets.top,
      null);

    // NE
    g.drawImage(fImage,width-fInsets.right,0,width,fInsets.top,
      fWidth-fInsets.right,0,fWidth,fInsets.top,
      null);

    // SW
    g.drawImage(fImage,0,height-fInsets.bottom,fInsets.left,height,
      0,fHeight-fInsets.bottom,fInsets.left,fHeight,
      null);

    // SE
    g.drawImage(fImage,width-fInsets.left,height-fInsets.bottom,width,height,
      fWidth-fInsets.left,fHeight-fInsets.bottom,fWidth,fHeight,
      null);

    // TOP
    g.drawImage(fImage,fInsets.left,0,width-fInsets.right,fInsets.top,
      fInsets.left,0,fWidth-fInsets.right,fInsets.top,
      null);

    // BOTTOM
    g.drawImage(fImage,fInsets.left,height-fInsets.bottom,width-fInsets.right,height,
      fInsets.left,fHeight-fInsets.bottom,fWidth-fInsets.right,fHeight,
      null);

    // LEFT
    g.drawImage(fImage,0,fInsets.top,fInsets.left,height-fInsets.bottom,
      0,fInsets.top,fInsets.left,fHeight-fInsets.bottom,
      null);

    // RIGHT
    g.drawImage(fImage,width-fInsets.right,fInsets.top,width,height-fInsets.bottom,
      fWidth-fInsets.right,fInsets.top,fWidth,fHeight-fInsets.bottom,
      null);
  }

	/**
	 * Return a JPanel that looks like an OSX drop zone, a widget in the
	 * Aqua interface.
	 *	@param	c	the component to put inside the drop zone
	 *	@return a new JPanel with your component inside of it
	 */
	public static JPanel getOSXTrayPanel(JComponent c){
		c.setBackground( new Color(242,242,242) );
		c.setOpaque(true);
 	  	ImageBorder ib = new ImageBorder();
 	  	ib.setImageURL("file:osx-dropzone-light.gif");
		JPanel myPanel = new JPanel(new BorderLayout());
		myPanel.add(c,BorderLayout.CENTER);
		myPanel.setBorder(ib);
		myPanel.setOpaque(false);	
		return myPanel;
	}

	/**
	* Test this out by popping up a test frame...
	*/
    public static void main(String s[]) {
        JFrame myFrame = new JFrame("OSX Tray Tester");
        myFrame.getContentPane().setLayout(null);
		JPanel tempPanel = new JPanel();
			tempPanel.add(new JLabel("some random text") );
			tempPanel.add(new JButton("a button") );
			tempPanel.add(new JLabel("some random text") );
			tempPanel.add(new JButton("a button") );
			tempPanel.add(new JLabel("some random text") );
			tempPanel.add(new JButton("a button") );
        JPanel trayPanel = ImageBorder.getOSXTrayPanel(tempPanel);
        trayPanel.setBounds(50,35,200,200);
        myFrame.getContentPane().add(trayPanel);
        myFrame.setBounds(0,0,300,300);
        myFrame.setResizable(false);
        myFrame.setVisible(true);
    }

}
