/*

Persistent Systems Private Limited, Pune, India (website - http://www.pspl.co.in)

Permission is hereby granted, without written agreement and without
license or royalty fees, to use, copy, modify, and distribute this
software and its documentation for any purpose, provided that this
copyright notice and the following paragraph appears in all
copies of this software.

DISCLAIMER OF WARRANTY.
-----------------------
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
IN NO EVENT SHALL THE RIGHTHOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

*/


/**
 * <p>Title: VOPlot Launcher class</p>
 * <p>Description: Simple class to demonstrate the usage of the external interface to VOPlot</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: PSPL </p>
 * @author K. D. Balaji
 * @version 1.0
 */

package voi.ui;
 
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.*;
import java.util.Vector;
import com.jvt.applets.ColumnInfo;
import com.jvt.applets.PlotVOApplet;
import com.jvt.applets.VOPlotExternalApps;



public class VOPlotLauncher
{
  static String _VOTable =
      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n" +
      "<VOTABLE xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + "\n" +
      "xsi:noNamespaceSchemaLocation=\"http://vizier.u-strasbg.fr/xml/VOTable.xsd\">" + "\n" +
      "<DESCRIPTION>" + "\n" +
      "VizieR Astronomical Server: urania.iucaa.ernet.in	2002-10-04T05:20:16" + "\n" +
      "Explanations and Statistics of UCDs:			See LINK below" + "\n" +
      "In case of problem, please report to:	question@simbad.u-strasbg.fr" + "\n" +
      "</DESCRIPTION>" + "\n" +
      "<!-- VOTable description at  http://vizier.u-strasbg.fr/doc/VOTable/ -->" + "\n" +
      "<DEFINITIONS>" + "\n" +
      "<COOSYS ID=\"J2000\" system=\"eq_FK5\" equinox=\"J2000\"/>" + "\n" +
      "</DEFINITIONS>" + "\n" +


  "<!-- Parameters used for the Query:" + "\n" +
      "-from=-3" + "\n" +
      "-to=4" + "\n" +
      "-this=-3" + "\n" +
      "-meta=2" + "\n" +
      "-file=." + "\n" +
      "-bgcat=DarkSeaGreen1" + "\n" +
      "-bgtit=gray86" + "\n" +
      "-bgdat=PapayaWhip" + "\n" +
      "-out.max=1000" + "\n" +
      "-out.form=XML-VOTable(1.0)" + "\n" +
      "-order=I" + "\n" +
      "-c=" + "\n" +
      "-c.eq=J2000" + "\n" +
      "-oc.form=sexa" + "\n" +
      "-c.r= 10" + "\n" +
      "-c.u=arcmin" + "\n" +
      "-c.geom=r" + "\n" +
      "-sort=_r" + "\n" +
      "-source=V/105/sky2v3r1" + "\n" +
      "-out=SKY2000" + "\n" +
      "SKY2000=" + "\n" +
      "-out=HD" + "\n" +
      "HD=" + "\n" +
      "-out=RAJ2000" + "\n" +
      "RAJ2000=" + "\n" +
      "-out=DEJ2000" + "\n" +
      "DEJ2000=" + "\n" +
      "-out=Vmag" + "\n" +
      "Vmag=" + "\n" +
      "-out=B-V" + "\n" +
      "B-V=" + "\n" +
      "-out=U-B" + "\n" +
      "U-B=" + "\n" +
      "-out=Sp" + "\n" +
      "Sp=" + "\n" +
      "-->" + "\n" +
      "<INFO ID=\"Ref\" name=\"-ref\" value=\"VOTx11451\" />" + "\n" +
      "<RESOURCE ID=\"yCat_5105\" name=\"V/105\">" + "\n" +
      "<DESCRIPTION>SKY2000 Catalog, Version 3 (Myers+ 2000)</DESCRIPTION>" + "\n" +
      "<TABLE ID=\"V_105_sky2v3r1\" name=\"V/105/sky2v3r1\">" + "\n" +
      "<DESCRIPTION>The Sky2000 Version 2 Catalogue</DESCRIPTION>" + "\n" +
      "<!-- MatchedRows: 1000 (upper limit) -->" + "\n" +
      "<!-- Now comes the definition of each field -->" + "\n" +
      "<FIELD name=\"B_V\" ucd=\"ID_ALTERNATIVE\" datatype=\"int\" width=\"6\">" + "\n" +
      "<DESCRIPTION>?Henry Draper &lt;III/135&gt; number</DESCRIPTION>" + "\n" +
      "</FIELD>" + "\n" +
      "<FIELD name=\"V\" ucd=\"POS_EQ_RA_MAIN\" ref=\"J2000\" datatype=\"char\" arraysize=\"13\" unit=\"&quot;h:m:s&quot;\">" + "\n" +
      "<DESCRIPTION>Right ascension (J2000) hours</DESCRIPTION>" + "\n" +
      "</FIELD>" + "\n" +
      "<DATA><TABLEDATA>" + "\n" +
      "<TR><TD>224701</TD><TD>00 00 00.0806</TD></TR>" + "\n" +
      "<TR><TD>224700</TD><TD>00 00 00.2158</TD></TR>" + "\n" +
      "</TABLEDATA>" + "\n" +
      "</DATA>" + "\n" +
      "<!-- Result Truncated to 1000 -->" + "\n" +
      "</TABLE>" + "\n" +
      "</RESOURCE>" + "\n" +
      "</VOTABLE>" + "\n";

  public VOPlotLauncher()
  {
    JPanel panel = null;
    VOPlotExternalApps voExtApp = null;
    //**************** Load a VOTable from a File *****************************
    // Create an instance of a class which is a subclass of the java.io.InputStream Object
    // for the reading the VOTable.
    InputStream votableStream = new java.io.ByteArrayInputStream(_VOTable.getBytes());
    panel = new JPanel();
    panel.setLayout(new BorderLayout());
    // Call the static method addVOPlot(Panel) of PlotVOApplet class to create a new VOPlot object and
    // add it the panel passed as argument to the function.
    voExtApp = PlotVOApplet.addVOPlot(panel);
    final JFrame frame = new JFrame("VOPlot frame");
    frame.getContentPane().add(panel, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation (JFrame.HIDE_ON_CLOSE);
    frame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        frame.dispose ();
      }
    });
    
    // Load the VOTable from the InputStream object.
    voExtApp.loadVOTable(votableStream, false);
    //=========================================================================

    
    //****************** Select some points in the Plot **********************
    voExtApp.selectPoints(new int[]{0});
    //========================================================================

    //****************** Highlight some points in the plot *******************
    voExtApp.highlightPoints(new int[]{0, 1});
    //========================================================================

    
    //*************** Add  new Columns ***************************************
    voExtApp.addColumn("$1 + $2", "deg", "$1 + $2");
    voExtApp.addColumn("$1 - $2", "deg", "$1 - $3");
    //========================================================================

    //***************** Overlay  plots on top of the current one *************
    voExtApp.plotColumns(1, false, 2, false, true);
    voExtApp.plotColumns(1, false, 3, false, true);
	voExtApp.showFullRange();
    //========================================================================

    
    //***************** Add a Filter for the data ****************************
    voExtApp.addFilter("($1 > 0.0) && ($2 < 0.0)", "($1 > 0.0) && ($2 < 0.0)");
    //========================================================================

    //***************** Set this filter as current one ***********************
    voExtApp.setFilter("($1 > 0.0) && ($2 < 0.0)");
    //========================================================================

    
    //********************* Load Data into VOPlot which is not in VOTable format **************
    // Create a vector for MetaData
    Vector metaData = new Vector();
    // Create com.jvt.applets.ColumnInfo Objects for storing information about columns.
    ColumnInfo column1Info = new ColumnInfo("RA", ColumnInfo.DATATYPE_DOUBLE, "deg", " ");
    ColumnInfo column2Info = new ColumnInfo("DEC", ColumnInfo.DATATYPE_DOUBLE, "deg", " ");
    // Add ColumnInfo objects to MetaData.
    metaData.add(column1Info);
    metaData.add(column2Info);
    // Create a Vector of Vectors for the Data.
    Vector data = new Vector();
    Vector column1 = new Vector();
    Vector column2 = new Vector();
    column1.add("1.0");
    column1.add("2.0");
    column1.add("3.0");
    data.add(column1);
    column2.add("7.9");
    column2.add("8.9");
    column2.add("18.9");
    data.add(column2);
    // Load the Data so created without erasing previous plot.
    voExtApp.loadData(metaData, data, true);
    // Show the full data range.
    voExtApp.showFullRange();
    frame.show();
    //===========================================================================
  }
}
