/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */

package dnd;

import ilog.views.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;

/**
 * The <code>DropAdapter</code> class allows an <code>IlvManagerView</code>
 * to be a <code>DropTarget</code> for Java drag-and-drop mechanism by implementing Java <code>DropTargetListener</code>.
 * @see DragAdapter
 */
public class DropAdapter
  implements DropTargetListener
{
  Override
  public  void dragEnter (DropTargetDragEvent dtde) 
  {
    // We accept drag only of our type.
    dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
  }

  Override
  public  void dragOver (DropTargetDragEvent dtde) 
  {
    // We accept drag only of our type.
    dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
  }

  
  /**
   * It implements the <code>DropTargetListener</code> method to end a drag-and-drop operation.
   * @param dtde The triggered event.
   */
  Override
  public  void drop (DropTargetDropEvent dtde) 
  {
    // Gets the targeted view.
    IlvManagerView view = (IlvManagerView)(dtde.getDropTargetContext().getComponent());
    
    // Verifies the kind of action asked by the user.
    if ((dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0) {
      // Gets the location of mouse pointer
      IlvPoint p = new IlvPoint(dtde.getLocation().x, dtde.getLocation().y);
      view.getTransformer().inverse(p);

      // We parse <code>DataFlavor</code> supported by the Transferable object
      // to find one we can understand.
      Transferable trans = dtde.getTransferable();
      DataFlavor df[] = dtde.getCurrentDataFlavors();

      for (int i = 0; i < df.length; i++) {
        // <code>IlvGraphic.GetGraphicObject</code> recognizes <code>DataFlavor.stringFlavor</code>.
        if (DataFlavor.stringFlavor.equals(df[i])) {
          // We have found the right flavor, accepts the user action.
          dtde.acceptDrop(dtde.getDropAction());
          IlvGraphic object = null;
          try {
            // Asks for the corresponding <code>IlvGraphic</code>.
            object = IlvGraphic.GetGraphicObject(trans);

            // Everything is OK.
            if (object != null)
              dtde.getDropTargetContext().dropComplete(true);
            else {
              dtde.getDropTargetContext().dropComplete(false);
              return;
            }
          }
          catch (Exception ex) {
            System.err.println("Cannot perform drop action");
            dtde.getDropTargetContext().dropComplete(false);
            return;
          }
          
          // Getting the right property we will then move the
          // object to the right position and add it to manager.
          IlvPoint delta = ((DeltaProperty)object.getNamedProperty(DeltaProperty.NAME)).getDelta();   

  
          object.move(p.x - delta.x, p.y - delta.y);
          view.getManager().addObject(object, true);
        
          // Prevents from polluting memory space.
          object.removeNamedProperty(DeltaProperty.NAME);
          
          return;
        }
      }
      // Found no compatible DataFlavor: reject the drop.
      dtde.rejectDrop();
    }
    else dtde.rejectDrop();
  }
  
  // In this simple example we do not redefine the following methods:
  Override
  public  void dropActionChanged(DropTargetDragEvent dtde) 
  {
  }
  public void dragScroll(DropTargetDragEvent dtde) 
  {
  }
  Override
  public  void dragExit(DropTargetEvent dte) 
  {
  }
}