/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2015 
 * © 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 ilog.views.swing.*;
//import ilog.views.util.IlvProductUtil;
import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import javax.swing.*;
import java.util.ArrayList;

/*
 * This example launches a <code>JFrame</code> containing two <code>JInternalFrame</code>.
 * You can drag and drop <code>IlvGraphic</code> from one frame to another.
 */
public class DnDExample extends JFrame
{
  private IlvManager mg1;
  private IlvManager mg2;
  private boolean main = false;
  private static ArrayList desktops = new ArrayList();

  public static void main(String[] arg)
  {
    // This sample uses JViews Diagrammer features. When deploying an
    // application that includes this code, you need to be in possession
    // of a Rogue Wave JViews Diagrammer Deployment license.
//    IlvProductUtil.DeploymentLicenseRequired(
//        IlvProductUtil.JViews_Diagrammer_Deployment);

    // Sun recommends that to put the entire GUI initialization into the
    // AWT thread
    SwingUtilities.invokeLater(
      new Runnable() {
        public void run() {
          new DnDExample();
        }
      });
  }

  public DnDExample()
  {
    super("Drag'n'Drop Sample");
    setSize(800,685);

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        DnDExample.this.dispose();
        desktops.remove(DnDExample.this);
        if (desktops.size() == 0 || main) 
          System.exit(0);
      }
    });
    
    // Border layout.
    getContentPane().setLayout(new BorderLayout());
    
    // Toolbar.
    if (desktops.size() == 0) {
      main = true;
      JPanel panel = new JPanel();
      panel.setLayout(new FlowLayout(FlowLayout.LEFT));
      JButton b1 = new JButton("Open another Drag'n'Drop Desktop");
      b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          desktops.add(new DnDExample());
        }
      });
      panel.add(b1);
      JButton b2 = new JButton("Reset all Drag'n'Drop Desktops");
      panel.add(b2);
      b2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          int size = desktops.size();
          for (int i = 0; i < size; i++)
            ((DnDExample)desktops.get(i)).reset();
        }
      });
      getContentPane().add(panel, BorderLayout.NORTH);
    }
    desktops.add(this);
    
    // Creates the MDI manager.
    JDesktopPane desktop = new JDesktopPane();
    getContentPane().add(desktop, BorderLayout.CENTER);

    // Creates the <code>JInternalFrame</code> in which we will put 
    // the drag source <code>IlvManagerView</code>.
    JInternalFrame f1 =
      new JInternalFrame("Drag an IlvGraphic from this IlvManagerView", true);
    f1.setBackground(Color.white);

    // Creates the <code>JInternalFrame</code> in which we will put
    // the drop target <code>IlvMAnagerView</code>.
    JInternalFrame f2 = 
      new JInternalFrame("Drop an IlvGraphic on this IlvManagerView",
                         true);
    f2.setBackground(Color.white);

    mg1 = new IlvManager();
    mg2 = new IlvManager();

    // We create here the drag view.
    IlvManagerView view1 = new IlvManagerView(mg1);
    view1.setAntialiasing(true);

    // We create the drop view.
    IlvManagerView view2 = new IlvManagerView(mg2);
    view2.setAntialiasing(true);

    IlvJManagerViewPanel p1 = new IlvJManagerViewPanel(view1);
    IlvJManagerViewPanel p2 = new IlvJManagerViewPanel(view2);

    view1.setTransformer(new IlvTransformer(0.75, 0.75, new IlvPoint(0, 0)));
    view2.setTransformer(new IlvTransformer(0.75, 0.75, new IlvPoint(0, 0)));

    f1.setBounds(0,0, 488, 360);
    f2.setBounds(305,260,488, 360);

    f1.getContentPane().add(p1);
    f2.getContentPane().add(p2);
   
    f1.setVisible(true);
    f2.setVisible(true);

    // Loads data to transfer.
    try {
      mg1.read("data/data.ivl");
      mg2.read("data/data2.ivl");
    } catch (Exception ex) {
      System.err.println("Cannot read data file");
    }

    // We use <code>java.awt.dnd</code> API to create a 
    // <code>GestureRecognizer</code> on the first view.
    try {
      // The <code>IlvManagerView</code> is a <code>java.awt.Component</code>
      // on which we can place a <code>DragGestureRecognizer</code> to
      // initiate the Drag mechanism.
      // The <code>DragGestureListener</code> will be of 
      // <code>DragAdapter</code> type.
      DragGestureRecognizer dgr = DragSource.getDefaultDragSource().
        createDefaultDragGestureRecognizer(view1,DnDConstants.ACTION_COPY_OR_MOVE, 
                                           new DragAdapter());        
    } catch (Exception ex) {
      System.err.println("Cannot initialize DnD on IlvManagerView ");
    }
    
    // We use <code>java.awt.dnd</code> API to place a <code>DropTarget</code>
    // on the second view.
    // The <code>DropTargetListener</code> will be of type <code>DropAdapter</code>.
    DropTarget dt = 
      new DropTarget(view2, DnDConstants.ACTION_COPY_OR_MOVE, new DropAdapter());
    dt.setActive(true);
    
    desktop.add(f1);
    desktop.add(f2);
    setVisible(true);
  }

  private void reset()
  {
    mg1.deleteAll(true);
    mg2.deleteAll(true);
    // Loads data to transfer.
    try {
      mg1.read("data/data.ivl");
      mg2.read("data/data2.ivl");
    } catch (Exception ex) {
      System.err.println("Cannot read data file");
    }
  }
}