import com.informagen.MacClipboard;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.Frame;
import java.awt.Event;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;


import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;



public class ClipboardTest extends Frame implements ClipboardOwner {


	public static void main(String[] args) {
	
		ClipboardTest cbt = new ClipboardTest();
		
		cbt.resize(400, 350);
		cbt.show();
		
	}




	private Clipboard clipboard;
	
	private final TextArea    copyTo  = new TextArea(3, 25);
	private final ImageComponent theImageComponent = new ImageComponent();
	private final Button   pasteBtn  = new Button("Paste From System Clipboard");


	ClipboardTest() {
	
		clipboard = getToolkit().getSystemClipboard();

		buildUI();
		wireUI();		

	}




	void buildUI() {
	
		setLayout(new BorderLayout());
		
		add(new Label("Use Shift-Command-4, then hold the Control to get a PICT onto the clipboard"), BorderLayout.NORTH);
		
		add(copyTo, BorderLayout.WEST);
		add(theImageComponent, BorderLayout.EAST);

		add(pasteBtn, BorderLayout.SOUTH);
	
	}


	void wireUI() {
	
		// Paste button listener
		
		pasteBtn.addActionListener(new PasteListener());
		
		
		// Window listeners, ie Close box

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				dispose();
			}
		
		});
	}



	public void lostOwnership(Clipboard clip, Transferable transferable) {}
	
		
	class PasteListener implements ActionListener {
	
		public void actionPerformed(ActionEvent event) {
		
			Transferable contents = clipboard.getContents(this);
			

			// Determine if data is available in java.lang.String flavor
			// NB: if there is a PICT on the clipboard 'contents' will be null.
			
			if ( ( contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor) ) {
			
				try {
					String string = (String) contents.getTransferData(DataFlavor.stringFlavor);
					copyTo.setText(string);
				} catch(Exception e) {
					e.printStackTrace();
				}
				
			} else {
				if ( MacClipboard.isMacintosh() ) {
						Image anImage = MacClipboard.getImage();
						if (anImage != null)
							theImageComponent.setImage(anImage);
					}
			
			}
		}
	}



}



class ImageComponent extends Component {

	Image theImage;

	ImageComponent() {}

	public void setImage(Image inImage) {
		theImage = inImage;
		repaint();
	}

	public Dimension getPreferredSize() {
		return new Dimension(200,0);
	}
	
	public void paint(Graphics g) {
	
		Dimension size = getSize();

		if ( theImage != null)
			g.drawImage(theImage, (size.width-theImage.getWidth(this))/2, 
			                      (size.height-theImage.getHeight(this))/2, this  );
	
	}

}