import util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.io.*;

public class IconButton extends JButton implements DragSourceListener, DragGestureListener, Transferable {

    DragSource dragSource;
    DataFlavor[] supportedFlavors;

    public IconButton() {
	// Set up appearance.
	setBorderPainted(false);
	setContentAreaFilled(false);
	setFocusPainted(false);
	setSize(32,32);
	setMargin(new Insets(0,0,0,0));
	setIcon(new ImageIcon("folder.gif"));
	setPressedIcon(new ImageIcon("selectedfolder.gif"));
  	setSelectedIcon(new ImageIcon("selectedfolder.gif")); // not used yet.
  
	// Set up DND
	dragSource = DragSource.getDefaultDragSource();
	dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_MOVE, this);
	try {
	    supportedFlavors = new DataFlavor[3];
	    supportedFlavors[0] = DataFlavor.stringFlavor;
	    supportedFlavors[1] = DataFlavor.plainTextFlavor;
	    //    supportedFlavors[2] = new DataFlavor(Class.forName("IconButton"),"IconButton");
	    // This line seemed like it should be changed.  I only want to allow drag and drop within the local JVM in any case.  Also, with the above, it attempts to serialize the data being transfered, instead of just passing it by reference.  This causes problems with Swing components, which don't serialize properly. 
	    supportedFlavors[2] = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType);
	} catch (ClassNotFoundException cnfe) {
	    cnfe.printStackTrace();
	    System.exit(1);
	}
    }
    
    /** Interface DragGestureListener */
    public void dragGestureRecognized(DragGestureEvent dge) {
	Debug.print("Drag gesture recognized");
	dge.startDrag(DragSource.DefaultMoveDrop,  // initial cursor
		      this, // Transferable to pass (changed from v1.1)
		      this); // DragSourceListener
    }

    /** Interface DragSourceListener (5 methods) */
    public void dragDropEnd(DragSourceDropEvent DragSourceDropEvent){}
    public void dragEnter(DragSourceDragEvent DragSourceDragEvent){}
    public void dragExit(DragSourceEvent DragSourceEvent){}
    public void dragOver(DragSourceDragEvent DragSourceDragEvent){}
    public void dropActionChanged(DragSourceDragEvent DragSourceDragEvent){}
    
    /** Interface Transferable (3 methods) */
        
    public Object getTransferData(DataFlavor flavor) 
	throws UnsupportedFlavorException, IOException {
	if (!isDataFlavorSupported(flavor))
	    throw new UnsupportedFlavorException(flavor);
	if (flavor.equals(DataFlavor.stringFlavor))
	    return "Reposition me";
	if (flavor.equals(DataFlavor.plainTextFlavor))
	    return new StringReader("Reposition me");
	else return this;
    }
    
    public DataFlavor[] getTransferDataFlavors() {
	return supportedFlavors;
    }
    
    public boolean isDataFlavorSupported(DataFlavor flavor) {
	for (int i=0; i<supportedFlavors.length; i++) 
	    if (flavor.equals(supportedFlavors[i]))
		return true;
	return false;
    }
}

