/*
 * 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.
 */

import ilog.views.*;
import ilog.views.graphic.IlvIcon;
import ilog.views.accelerator.*;
import ilog.views.swing.*;
import ilog.views.util.*;
import ilog.views.util.swing.IlvSwingUtil;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.net.*;
import javax.swing.*;

// You can uncomment the following import statements
// if you want to test IlvChart inside an IlvManager
/**
import ilog.views.chart.IlvChart;
import ilog.views.chart.IlvDataInterval;
import ilog.views.chart.IlvChartRenderer;
import ilog.views.chart.IlvStyle;
import ilog.views.chart.data.IlvDataSet;
import ilog.views.chart.data.IlvDefaultDataSet;
import ilog.views.chart.data.IlvDefaultDataSource;
import ilog.views.chart.java2d.IlvValueGradientPaint;
**/

/**
 * The main applet class.
 */
public class GraphicComponent extends JApplet
{
  static {
    // This applet is designed to run only with default resource bundle
    // and various selected other resource bundles.
    // Setting the available resource suffixes avoids that the applet
    // tries to load resource bundles for other locales over the net,
    // even if the current locale of the browser is different.
    if (IlvResourceUtil.isInApplet())
      IlvResourceUtil.setAvailableResourceSuffixes("", "_ja");
  }

  {
    // 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);
  }

  /** The grapher. */
  private IlvGrapher grapher = new IlvGrapher();
  /** The view of the grapher.*/
  private IlvManagerView mgrview;

  /**
   * Initializes the applet.
   */
  public void init()
  {
    super.init();

    getContentPane().setLayout(new BorderLayout(0,0));

    // Creates the view for the grapher.
    mgrview = new IlvManagerView(grapher, null);
    mgrview.setKeepingAspectRatio(true);
    mgrview.setAntialiasing(true);

    // Adds the objects.
    addObjects();

    // Installs some accelerators.
    grapher.addAccelerator(new IlvIdentityAccelerator(KeyEvent.KEY_PRESSED,
                    KeyEvent.VK_I, 0));
    grapher.addAccelerator(new IlvZoomOutAccelerator(KeyEvent.KEY_PRESSED,
                    KeyEvent.VK_U, KeyEvent.CTRL_MASK));
    grapher.addAccelerator(new IlvZoomInAccelerator(KeyEvent.KEY_PRESSED,
                                                    KeyEvent.VK_Z, KeyEvent.CTRL_MASK));
    grapher.addAccelerator(new IlvFitToSizeAccelerator(KeyEvent.KEY_PRESSED,
                                                       KeyEvent.VK_F, 0));
    grapher.addAccelerator(new IlvRotateAccelerator(KeyEvent.KEY_PRESSED,
                                                    KeyEvent.VK_R, 0));
    grapher.addAccelerator(new IlvScrollUpAccelerator(KeyEvent.KEY_PRESSED,
                                                      KeyEvent.VK_UP, 0));
    grapher.addAccelerator(new IlvScrollDownAccelerator(KeyEvent.KEY_PRESSED,
                                                        KeyEvent.VK_DOWN, 0));
    grapher.addAccelerator(new IlvScrollRightAccelerator(KeyEvent.KEY_PRESSED,
                                                         KeyEvent.VK_RIGHT, 0));
    grapher.addAccelerator(new IlvScrollLeftAccelerator(KeyEvent.KEY_PRESSED,
                                                        KeyEvent.VK_LEFT, 0));
    IlvJScrollManagerView sc  = new IlvJScrollManagerView(mgrview);
    mgrview.setWheelZoomingEnabled(true);
    sc.setWheelScrollingEnabled(true);
    getContentPane().add(sc, BorderLayout.CENTER);
  }

  /**
   * Called when this applet is being reclaimed in order to destroy
   * any resources that it has allocated.
   */
  public void destroy()
  {
    super.destroy();

    // This method is intended to workaround memory management issues
    // in the Sun JRE. Please refer to the method documentation for more
    // details and a description of the known issues.
    IlvSwingUtil.cleanupApplet();
  }

  /**
   * Adds the graphic objects to the grapher.
   */
  private void addObjects()
  {
    JEditorPane mainPane = new JEditorPane();
    mainPane.setEditorKit(new javax.swing.text.html.HTMLEditorKit());
    mainPane.setEditable(false);
    mainPane.setText("<html><body><p>This demo shows how you can embed <code>JComponent</code> instances in a Rogue Wave JViews <code>IlvGrapher</code></p><p>In that order from left to right, top to bottom, you will find: a <code>JTextArea</code>, a <code>JTree</code>, the same <code>JEditorPane</code> shown at different zoom levels, and a rotated version of the <code>JTextArea</code>.</p><p>You can zoom in or out on the view using Ctrl-Z and Ctrl-U accelerators, or rotate using R accelerator.</p><p>You can navigate the manager using arrow keys.</p></body></html>");
    JScrollPane sPane = new JScrollPane(mainPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    IlvJComponentGraphic wrapper = new IlvJComponentGraphic(new IlvRect(40, 20, 320, 120),
                                                            sPane);
    grapher.addObject(wrapper, false);

    // You can uncomment the following if you want
    // to test IlvChart inside an IlvManager
    /*
      IlvChart chart = new IlvChart();
      chart.getYScale(0).setMajorTickVisible(false);
      IlvDataSet[] sets = new IlvDefaultDataSet[] {
      new IlvDefaultDataSet("DataSet 1",
      ilog.views.chart.util.IlvArrays.randomValues(31, 0., 20.)),
      new IlvDefaultDataSet("DataSet 2",
      ilog.views.chart.util.IlvArrays.randomValues(31, 0., 20.))
      };
      chart.setDataSource(new IlvDefaultDataSource(sets));
      IlvDataInterval range = chart.getYAxis(0).getDataRange();
      double[] values = {range.getMin(), range.getMiddle(), range.getMax()};
      Color[]  colors = {Color.green, Color.orange, Color.red};
      IlvValueGradientPaint gp = new IlvValueGradientPaint(chart, 0, values, colors);
      
      IlvStyle[] styles = new IlvStyle[] {
      new IlvStyle(new BasicStroke(2), gp, Color.red),
      new IlvStyle(new BasicStroke(2), gp, Color.red)
      };
      chart.getRenderer(0).setStyles(styles);
      wrapper = new IlvJComponentGraphic(new IlvRect(40, 160, 150, 70), chart);
      grapher.addObject(wrapper, false);
    */
    try {
      grapher.addObject(new IlvIcon(IlvSwingUtil.getRelativeURL(this, "data/chart.png"),
                                    new IlvRect(40, 160, 150, 65)), false);
    } catch (MalformedURLException e) {
    }

    JTextArea text = 
      new JTextArea("you can type a message here and\n look at " +
                    "the rotated text area\n below");
    sPane =  new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_NEVER,
                             JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    wrapper = new IlvJComponentGraphic(new IlvRect(210, 160, 200, 100), sPane);
    wrapper.resize(150, 70);
    grapher.addObject(wrapper, false);

    wrapper = new IlvJComponentGraphic(new IlvRect(210, 160, 150, 70), sPane);
    wrapper.rotate(new IlvPoint(285, 195), 90);
    wrapper.moveResize(new IlvRect(308, 240, 50, 90));
    grapher.addObject(wrapper, false);

    JTree tree = new JTree();
    sPane =  new JScrollPane(tree);
    wrapper = new IlvJComponentGraphic(new IlvRect(40, 240, 200, 200), sPane);
    wrapper.resize(90, 90);
    grapher.addObject(wrapper, false);

    JEditorPane pane1 = new JEditorPane();
    pane1.setEditorKit(new javax.swing.text.html.HTMLEditorKit());
    pane1.setEditable(false);
    try {
      pane1.setPage(IlvSwingUtil.getRelativeURL(this, "data/sample.html"));
    } catch (java.io.IOException e) {
      e.printStackTrace();
    }
    sPane = new JScrollPane(pane1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    wrapper = new IlvJComponentGraphic(new IlvRect(140, 240, 240, 320),
                                       sPane);
    wrapper.resize(67.5f, 90);
    grapher.addObject(wrapper, false);

    wrapper = new IlvJComponentGraphic(new IlvRect(240, 240, 33, 45), sPane);
    grapher.addObject(wrapper, false);

    wrapper = new IlvJComponentGraphic(new IlvRect(230, 300, 16, 22), sPane);
    grapher.addObject(wrapper, false);

    wrapper = new IlvJComponentGraphic(new IlvRect(266, 300, 16, 22), sPane);
    grapher.addObject(wrapper, false);

    Object[] data1 = { "France", "Germany", "Great Britain", "USA", "Spain" };
    JComboBox comboBox = new JComboBox(data1);
    wrapper = new IlvJComponentGraphic(new IlvRect(40, 340, 100, 25), comboBox);
    wrapper.getComboBoxOptions().setMaximumPopupZoomFactor(3.0);
    grapher.addObject(wrapper, false);

    Object[] data2 = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn",
              "Uranus", "Neptune", "Pluto" };
    comboBox = new JComboBox(data2);
    wrapper = new IlvJComponentGraphic(new IlvRect(230, 340, 100, 25), comboBox);
    wrapper.resize(50, 12.5f);
    wrapper.getComboBoxOptions().setMaximumPopupZoomFactor(3.0);
    wrapper.getComboBoxOptions().setKeepingAspectRatio(false);
    grapher.addObject(wrapper, false);

    Object[] data3 = { "Spring", "Summer", "Fall", "Winter" };
    comboBox = new JComboBox(data3);
    wrapper = new IlvJComponentGraphic(new IlvRect(310, 340, 100, 25), comboBox);
    wrapper.resize(50, 12.5f);
    wrapper.getComboBoxOptions().setAutoPopupWidth(false);
    // wrapper.getComboBoxOptions().setMaximumPopupWidth(100);
    // wrapper.getComboBoxOptions().setMaximumPopupHeight(100);
    wrapper.getComboBoxOptions().setMinimumPopupZoomFactor(1.0);
    wrapper.getComboBoxOptions().setMaximumPopupZoomFactor(1.0);
    // wrapper.getComboBoxOptions().setKeepingAspectRatio(false);
    grapher.addObject(wrapper, false);
  }

  /**
   * 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() {
          GraphicComponent applet = new GraphicComponent();
          applet.init();

          JFrame frame = new JFrame("Component Graphic Demo");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(600, 600);
          frame.getContentPane().add(applet);
          frame.setVisible(true);
        }
      });
  }
}