/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2016 
 * © 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 demo.readingsvg;

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import ilog.views.IlvManager;
import ilog.views.IlvManagerView;
import ilog.views.accelerator.IlvFitToSizeAccelerator;
import ilog.views.accelerator.IlvIdentityAccelerator;
import ilog.views.accelerator.IlvScrollDownAccelerator;
import ilog.views.accelerator.IlvScrollLeftAccelerator;
import ilog.views.accelerator.IlvScrollRightAccelerator;
import ilog.views.accelerator.IlvScrollUpAccelerator;
import ilog.views.accelerator.IlvZoomInAccelerator;
import ilog.views.accelerator.IlvZoomOutAccelerator;
import ilog.views.interactor.IlvMagnifyInteractor;
import ilog.views.svg.SVGStreamFactory;
import ilog.views.swing.IlvJManagerViewPanel;

/**
 * Demo showing how to use the IlvMagnifyInteractor 
 * class to magnify an area of a Rogue Wave JViews IlvManager
 * instance that has been filled with contents imported from a file in
 * Scalable Vector Graphic (SVG)
 * format.
 */
public class ReadingSVG extends JPanel
{
  /**
   * Subclass of the Rogue Wave JViews magnifier interactor that allows you
   * to change dynamically the size of the magnification area,
   * the magnification factor, and the mode used by the interactor by 
   * using keyboard shortcuts.
   */
  public static class Magnifier extends IlvMagnifyInteractor
  {
    /**
     * Builds a <code>Magnifier</code> instance.
     */
    public Magnifier()
    {
      super();
      enableEvents(AWTEvent.KEY_EVENT_MASK);
    }

    /**
     * Processes the mouse events.
     */
    protected void processMouseEvent(MouseEvent e)
    {
      super.processMouseEvent(e);
      if (e.getID() == MouseEvent.MOUSE_PRESSED)
        getManagerView().setOptimizedTranslation(false); // avoid traces on the screen
      else
        if (e.getID() == MouseEvent.MOUSE_RELEASED)
          getManagerView().setOptimizedTranslation(true); // come back to fastest mode
    }

    /**
     * Processes key events.
     */
    protected void processKeyEvent(KeyEvent e)
    {
      if (e.getID() == KeyEvent.KEY_PRESSED) {
        switch (e.getKeyChar()) {
        case '+':
          // Increase the size of the magnification area.
          setSize(getSize()+4);
          break;
        case '-':
          // Decrease the size of the magnification area.
          if (getSize() > 4)
            setSize(getSize()-4);
          break;
        case '<':
          // Increase the magnification factor.
          if (getFactor() > 1.2)
            setFactor(getFactor() - 0.2f);
          break;
        case '>':
          // Decrease the magnification factor.
          if (getFactor() < 3.8)
            setFactor(getFactor() + 0.2f);
          break;
        }
        // Change to LENS mode if Alt key is down.
        if (e.isAltDown()) {
          setMode(LENS);
          e.consume(); // Prevent browser from using that event.
        }
      } else if (e.getID() == KeyEvent.KEY_RELEASED &&
                 !e.isAltDown()) {
        // Come back to CIRCULAR mode.
        setMode(CIRCULAR);
      }
      // Send accelerators to the manager.
      getManager().shortCut(e, getManagerView());
    }
  }

  // The manager we will deal with.
  private IlvManager manager = null;
  // The view we will deal with.
  private IlvManagerView view = null;

  /**
   * Constructor.
   */
  public ReadingSVG() {}

  /**
   * Initialization of the Demo.
   */
  public void init()
  {
    // 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);

    // Build the manager and configure it for SVG.
    manager = new IlvManager(1);
    SVGStreamFactory factory = new SVGStreamFactory();
    manager.setStreamFactory(factory);
    initAccelerators();
    // Build the view and configure it.
    view = new IlvManagerView(manager);
    view.setAntialiasing(true);
    view.setKeepingAspectRatio(true);
    // Build the magnifier interactor and configure it.
    IlvMagnifyInteractor inter = new Magnifier();
    inter.setMode(IlvMagnifyInteractor.CIRCULAR);
    inter.setPermanent(true);
    inter.setFactor(1.8f);
    // Use the Magnifier interactor on the view.
    view.pushInteractor(inter);
    IlvJManagerViewPanel panel = new IlvJManagerViewPanel(view);
    setLayout(new BorderLayout());
    add(panel, BorderLayout.CENTER);
    // Load the SVG file
    try {
      manager.read(new URL("file:data/lou.svg"));
      view.fitTransformerToContent(true);
    } catch (java.io.IOException io) {
      io.printStackTrace();
    } catch (ilog.views.io.IlvReadFileException rf) {
      rf.printStackTrace();
    }
  }

  /**
   * Installs some accelerators on the manager.
   */
  private void initAccelerators()
  {
    manager.addAccelerator(new IlvIdentityAccelerator(KeyEvent.KEY_PRESSED,
                                                      KeyEvent.VK_I, 0));
    manager.addAccelerator(new IlvZoomOutAccelerator(KeyEvent.KEY_PRESSED,
                                                     KeyEvent.VK_U,
                                                     KeyEvent.CTRL_MASK));
    manager.addAccelerator(new IlvZoomInAccelerator(KeyEvent.KEY_PRESSED,
                                                    KeyEvent.VK_Z,
                                                    KeyEvent.CTRL_MASK));
    manager.addAccelerator(new IlvFitToSizeAccelerator(KeyEvent.KEY_PRESSED,
                                                       KeyEvent.VK_F, 0));
    manager.addAccelerator(new IlvScrollUpAccelerator(KeyEvent.KEY_PRESSED,
                                                      KeyEvent.VK_UP, 0));
    manager.addAccelerator(new IlvScrollDownAccelerator(KeyEvent.KEY_PRESSED,
                                                        KeyEvent.VK_DOWN, 0));
    manager.addAccelerator(new IlvScrollRightAccelerator(KeyEvent.KEY_PRESSED,
                                                         KeyEvent.VK_RIGHT, 0));
    manager.addAccelerator(new IlvScrollLeftAccelerator(KeyEvent.KEY_PRESSED,
                                                        KeyEvent.VK_LEFT, 0));
  }

  /**
   * Allows you to run the demo as a standalone application.
   */
  public static void main(String[] arg)
  {
    // Sun recommends that to put the entire GUI initialization into the
    // AWT thread
    SwingUtilities.invokeLater(
      new Runnable() {
        public void run() {
          ReadingSVG panel = new ReadingSVG();
          panel.init();

          JFrame frame = new JFrame("SVG");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(500, 500);
          frame.getContentPane().add(panel);
          frame.setVisible(true);
        }
      });
  }

}