/*
 * 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 java2d;

import ilog.views.*;
import ilog.views.io.*;
import ilog.views.interactor.IlvSelectInteractor;
import ilog.views.graphic.IlvGeneralPath;
import ilog.views.graphic.IlvText;
//import ilog.views.util.IlvProductUtil;

import java.awt.*;
import java.awt.geom.*;
import java.io.IOException;

import javax.swing.*;

public class GenerateIVL
{
  private IlvManager manager;

  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 GenerateIVL();
        }
      });
  }

  public GenerateIVL()
  {
    // Creates a Manager
    manager = new IlvManager();
    
    // Creates a rectangle with a <code>CenterGradientPaint</code>.
    IlvGeneralPath path = new IlvGeneralPath(new Rectangle2D.Float(25, 25, 60, 120));
    path.setFillPaint(new CenterGradientPaint(Color.red, Color.blue)); 
    manager.addObject(path, false);
    
    // Creates an ellipse with an <code>ArrowStroke</code>.
    path = new IlvGeneralPath(new Ellipse2D.Float(60, 60, 210, 80));
    path.setFillOn(false);
    path.setStroke(new ArrowStroke(6));
    manager.addObject(path, false);
    
    // Creates the text
    String text = "These objects are regular IlvGeneralPath instances using "
      + "custom Paint and Stroke Java2D objects (CenterGradientPaint "
      + "and ArrowStroke) defined in this demo.\n"
      + "You can use them in your own IlvGeneralPath in Composer or in your "
      + "applications but don't forget to put the jviews-framework-java2d.jar "
      + "file in your CLASSPATH to allow java to find ArrowStroke and "
      + "CenterGradientPaint classes.";
    
    IlvText label = new IlvText(new IlvPoint(10, 170), text);
    label.setAntialiasing(true);
    label.setWrappingWidth(350);
    label.setWrappingMode(IlvText.WRAP_WORD);
    manager.addObject(label, false);
    
    // Show the persistency of the specialized stroke and paint objects.
    
    // Write them
    try {
      manager.write("custom.ivl");
    } catch (IOException e) {
      System.err.println("cannot write the IVL file - check permissions");
      System.exit(1);
    } 
    
    // Clear the manager
    manager.deleteAll(false);
    
    // Read them
    try {
      manager.read("custom.ivl");
    } catch (IOException e) {
      System.err.println("cannot read the IVL file - check permissions");
      System.exit(1);
    } catch (IlvReadFileException e) {
      System.err.println("cannot read the IVL file - unexpected error");
      System.exit(1);
    }
    
    // Show the result on the screen
    JFrame frame = new JFrame("Java2D Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(800, 600);
    IlvManagerView view = new IlvManagerView(manager);
    view.setBackground(Color.white);
    view.setAntialiasing(true);
    IlvSelectInteractor interactor = new IlvSelectInteractor();
    interactor.setOpaqueMove(true);
    interactor.setOpaqueResize(true);
    IlvHandlesSelection.defaultHandleShape = IlvHandlesSelection.SQUARE_SHAPE;
    view.pushInteractor(interactor);
    frame.getContentPane().add(view);
    frame.setVisible(true);
  }
}