/*******************************************************************************
* Copyright (c)                                                                *
*           2005   Tristan Allwood,                                            *
*           2005   Daniel Burke,                                               *
*           2005   Marc Hull,                                                  *
*           2005   Ekaterina Itskova,                                          *
*           2005   Steve Zymler                                                *
*                                                                              *
* Permission is hereby granted, free of charge, to any person obtaining a      *
* copy of this software and associated documentation files (the                *
* "Software"), to deal in the Software without restriction, including          *
* without limitation the rights to use, copy, modify, merge, publish,          *
* distribute, sublicense, and/or sell copies of the Software, and to           *
* permit persons to whom the Software is furnished to do so, subject to        *
* the following conditions:                                                    *
*                                                                              *
* The above copyright notice and this permission notice shall be included      *
* in all copies or substantial portions of the Software.                       *
*                                                                              *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS      *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF                   *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.       *
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY         *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,         *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE            *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                       *
*******************************************************************************/
/*
 * Created on 23.10.2004
 *
 */
package uk.ac.ic.doc.kevlar.gui.internal.dragdrop;

import org.eclipse.swt.graphics.Point;

import uk.ac.ic.doc.kevlar.gui.internal.AMouseListenerObject;
import uk.ac.ic.doc.kevlar.gui.internal.pipeline.PipeLineManager;
import uk.ac.ic.doc.kevlar.gui.internal.pipeline.Program;


/**
 * @author ei02
 *
 */
public class DragDropManager extends AMouseListenerObject{

	private ADraggableObject dragObject;
	private Point offset;
    
	public void mouseMoving(Point position){
		
 		if (dragObject != null && offset != null){
			
 		    dragObject.redraw();
			dragObject.setPosition(new Point(position.x-offset.x, position.y-offset.y));
			applyRedraw();
		
		}
 		
	}
			
	public void onDrop(ADraggableObject drag, int x, int y)
	{

	    PipeLineManager.getSingleton().addChild(drag);
	    drag.setPosition(PipeLineManager.getSingleton().getContents().absoluteToLocal(drag.getPosition()));
		drag.dropped(null);
	    applyRedraw();
		
	}
		
	public void mouseRelease(Point position, int mouseButton){
		
		if (dragObject!=null) {
		    
//			drop(dragObject, position.x, position.y);
		    IDropTarget target = getDropTarget(position);
		    
		    if (target != null) {
		        
		        if (target.acceptsDragObject(dragObject)) {
		       
		            Point localPos = target.absoluteToLocal(new Point(position.x-offset.x, position.y-offset.y));
		            target.dropDragObject(dragObject, localPos);
		            dragObject.dropped(target);
		            
		        } else {
		            
		            dragObject.dropped(null);
		            
		        }
		        
		    }  else {
		        
		        dragObject.dropped(null);
		        
		    }
		    
			unsetDragObject();
			
		}
		
	}
	
	public boolean hasDragObject() {
	    
	    return dragObject != null;
	    
	}
	
	public void setDragObject(ADraggableObject o, Point offset) {
	    
		if (dragObject==null) {
		    
		    this.offset = offset;
		    dragObject = o;
		    dragObject.redraw();
		    addChild(dragObject);
		    getMouseFocus();
		    
		}
		
	}
	
	public void unsetDragObject() {
	    
	    if (dragObject != null) {
	    
	        dragObject.redraw();
	        offset = null;
	        removeChild(dragObject);
	        dragObject = null;
	        releaseMouseFocus();
	        
	    }
		
	}
    
}
