import java.awt.Cursor;
import java.awt.datatransfer.StringSelection;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.util.ArrayList;


public class DragSourceLabel extends MyLabel{
	
	public DragSourceLabel(){
		super();
		new DragSource().createDefaultDragGestureRecognizer(this,DnDConstants.ACTION_COPY_OR_MOVE,dgl);
	}
	
	//private ArrayList<DragSourceLabelListener> listenerList=new ArrayList<DragSourceLabelListener>();
	private ArrayList listenerList=new ArrayList();
	public void addListener(DragSourceLabelListener l){
		listenerList.add(l);
	}
	
	private DragGestureListener dgl=new DragGestureListener(){
		//@Override
		public void dragGestureRecognized(DragGestureEvent e) {
			
			// 1) cursor
			Cursor dragCursor=DragSource.DefaultMoveDrop;

			// 2) transfer data
			String text=getText();
			StringSelection transferable=new StringSelection(text);
			
			// 3) drag start
			e.startDrag(dragCursor, transferable,dsl);
		}
	};
	
	
	private DragSourceListener dsl=new DragSourceListener(){
		//@Override
		public void dragDropEnd(DragSourceDropEvent dsde) {
			
			// notify
			if( dsde.getDropSuccess() ){
				/*
				for(DragSourceLabelListener l:listenerList){
					l.requestRemove(DragSourceLabel.this);
				}
				*/

				for(int i=0; i<listenerList.size(); i++){
					DragSourceLabelListener l=(DragSourceLabelListener)listenerList.get(i);
					l.requestRemove(DragSourceLabel.this);
				}
			}
		}
		
		//@Override
		public void dragEnter(DragSourceDragEvent dsde) {
		}
		//@Override
		public void dragExit(DragSourceEvent dse) {
		}
		//@Override
		public void dragOver(DragSourceDragEvent dsde) {
		}
		//@Override
		public void dropActionChanged(DragSourceDragEvent dsde) {
		}
	};



}

