/*
 * $Id: SBExample1.java,v 1.6 2003/10/10 18:28:26 dwd Exp $
 *
 * This software is provided by NOAA for full, free and open release.  It is
 * understood by the recipient/user that NOAA assumes no liability for any
 * errors contained in the code.  Although this software is released without
 * conditions or restrictions in its use, it is expected that appropriate
 * credit be given to its author and to the National Oceanic and Atmospheric
 * Administration should the software be included by the recipient as an
 * element in other product development.
 */

//package tutorial;

import java.awt.BorderLayout;
import java.awt.AWTEvent;
import java.awt.Color;

import java.awt.event.WindowEvent;

import java.io.IOException;

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

import gov.noaa.pmel.sgt.LineAttribute;

import gov.noaa.pmel.sgt.beans.Page;
import gov.noaa.pmel.sgt.beans.PanelModel;
import gov.noaa.pmel.sgt.beans.DataModel;
import gov.noaa.pmel.sgt.beans.PanelHolder;
import gov.noaa.pmel.sgt.beans.DataGroup;

import gov.noaa.pmel.sgt.dm.SGTData;

/**
 * SGT Bean Example1.  Uses the SGT Bean classes to create a simple
 * time series plot of data from a netCDF file.
 *
 * @author Donald Denbo
 * @version 1.0
 */
public class SBExample1 extends JFrame {
  /**
   * Instantiate the three primary SGT Bean objects.
   * Page -       Main SGT JavaBean used with PanelModel and DataModel to
   *              create a graphic.
   * PanelModel - A model that supports the Panel structure of a plot.
   * DataModel -  A model that supplies the relationship between SGTData objects,
   *              its Attribute and the Panel and DataGroup in which it is
   *              displayed and the Legend.
   */
  private Page page_ = new Page();
  private PanelModel panelModel_ = new PanelModel();
  private DataModel dataModel_ = new DataModel();
  /**
   * The JPanel that will be used to contain the graphic.
   */
  private JPanel graphicPanel_ = new JPanel(new BorderLayout());

  /**
   * SGT Bean Example1 constructor.
   */
  public SBExample1() {
    setTitle("SGT Beans Example 1");
    /**
     * Enable WindowEvents.  Set the layout of the content pane to a
     * BorderLayout.
     */
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    getContentPane().setLayout(new BorderLayout());
    /**
     * Create the graphic and add it to the content pane of the JFrame.
     */
    createGraphic();
    getContentPane().add(graphicPanel_, BorderLayout.CENTER);
  }

  /**
   * Main method for SGT Bean Example 1.
   * @param args Program arguments (unused)
   */
  public static void main(String[] args) {
    /**
     * Create an instance of SBExample1 and pack() the frame.
     */
    SBExample1 frame = new SBExample1();
    frame.pack();
    /**
     * Since the current version of SGT Beans doesn't directly support
     * resizing disable resizing and make the frame visible.
     */
    frame.setResizable(false);
    frame.setVisible(true);
  }

  /**
   * Create the graphic to insert into graphicPanel.
   */
  private void createGraphic() {
    NcSimpleReader1 reader = null;
    /**
     * Instantiate a NcSimpleReader1 with a time series data file.  Exit on
     * error.
     */
    try {
      reader = new NcSimpleReader1("data/OLEM_tutorial_ts.nc");
    } catch (IOException ioe) {
      ioe.printStackTrace();
      System.exit(1);
    }
    /**
     * Add the page object to graphicPanel and set dataModel.
     */
    graphicPanel_.add(page_, BorderLayout.CENTER);
    page_.setDataModel(dataModel_);
    /**
     * Create panelModel by de-serializing an existing PanelModel.  The
     * file SBExample1.xml, was created using gov.noaa.pmel.sgt.beans.PanelModelEditor.
     * Exit on error.
     */
    try {
      panelModel_ = PanelModel.loadFromXML(getClass().getResource("SBExample1.xml").openStream());
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
    /**
     * Set panelModel.
     */
    page_.setPanelModel(panelModel_);
    /**
     * To add data to dataModel, we first need to find the PanelHolder and
     * DataGroup to which we want to add data.  The SBExample.xml panelModel
     * was created with a Panel named "TimePanel" and a DataGroup named
     * "TimeSeries".  It is possible to query panelModel to determine what
     * PanelHolders and DataGroups are available.
     */
    PanelHolder time = panelModel_.findPanelHolder("TimePanel");
    DataGroup timeData = time.findDataGroup("TimeSeries");
    /**
     * Get a time series from the netCDF file.
     */
    SGTData data = reader.getTimeSeries("KE_ave");
    /**
     * Create a line attribute for a solid red line.
     */
    LineAttribute lAttr = new LineAttribute(LineAttribute.SOLID, Color.red);
    /**
     * Add data to PanelHolder, time, and DataGroup, timeData.
     */
    dataModel_.addData(data, lAttr, time, timeData, null);
  }

  /**
   * Process WindowEvents, stop program if WINDOW_CLOSING event is caught.
   * @param e WindowEvent
   */
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      /**
       * Clean up windows and exit.
       */
      setVisible(false);
      dispose();
      System.exit(0);
    }
  }
}