/*
 * -------------------------------------------------------------------------
 *      $Id: PlotFFT.java,v 1.2 2004/05/26 18:21:24 estewart Exp $
 * -------------------------------------------------------------------------
 *      Copyright (c) 1999 Visual Numerics Inc. All Rights Reserved.
 *
 *      This software is confidential information which is proprietary to
 *      and a trade secret of Visual Numerics, Inc.  Use, duplication or
 *      disclosure is subject to the terms of an appropriate license
 *      agreement.
 *
 *      VISUAL NUMERICS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 *      SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 *      BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 *      FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. VISUAL
 *      NUMERICS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 *      AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
 *      ITS DERIVATIVES.
 *--------------------------------------------------------------------------
 */

package com.imsl.demo.Harmonic;
import com.imsl.chart.*;

/**
 *  Launches a new window showing the power spectrum
 *  of a segment of a .wav file.
 *
 * @author     R.B.E.Taylor
 * @created    October 23, 2002
 */
public class PlotFFT extends javax.swing.JDialog {

    private JPanelChart jPanelChart;
    private Chart chart;
    private AxisXY axis;
    private Data data;
    private javax.swing.JTextField displayField;
    private double[] power, f, t_lim;
    private String plotColor, plotLabel;


    public PlotFFT(java.awt.Frame parent) {
        super(parent, false);
        initComponents();
        java.awt.Dimension parentSize = parent.getSize();
        java.awt.Point parentLoc = parent.getLocationOnScreen();
        parentLoc.x += parentSize.width/2.5;
        parentLoc.y += parentSize.height/2.5;
        setLocation(parentLoc.x, parentLoc.y);

        jPanelChart = new JPanelChart();
        int h = (int)((parentSize.height/2)*1.25);
        int w = (int)(h/0.8);
        jPanelChart.setPreferredSize(new java.awt.Dimension(w,h));
        getContentPane().add(jPanelChart);
        pack();
    }

    void draw(double[] f, double[] power, double[] t_lim, String plotLabel, String plotColor) {
        chart = jPanelChart.getChart();
        chart.getLegend().setPaint(true);
        chart.getLegend().setViewport(0.65, 0.85, 0.05, 0.15);
        axis = new AxisXY(chart);
        axis.getAxisX().getAxisLabel().setTextFormat(new java.text.DecimalFormat("###"));
        axis.getAxisX().getAxisTitle().setTitle("Frequency, Hz");
        axis.getAxisY().getAxisLabel().setTextFormat(new java.text.DecimalFormat("###"));
        axis.getAxisY().getAxisTitle().setTitle("Relative amplitude");

        java.awt.Container cp = getContentPane();

        // Create the display panel
        javax.swing.JPanel displayPanel = new javax.swing.JPanel();

        // Round off the time limits for displaying
        double t_initial = Math.floor(t_lim[0]*1000+0.5)/1000;
        double t_final = Math.floor(t_lim[1]*1000+0.5)/1000;

        String describe_sp = "Fourier power spectrum, " +
            " t = [" + t_initial + ", " + t_final +
            "]; " + f.length*2 + " sample points.";
        displayField = new javax.swing.JTextField(describe_sp);
        displayField.setBorder(null);
        displayField.setEditable(false);
        displayPanel.add(displayField);
        cp.add(displayPanel, java.awt.BorderLayout.SOUTH);

        axis.getAxisX().setAutoscaleInput(AxisXY.AUTOSCALE_OFF);
        axis.getAxisX().setWindow(0.0, 1024.0);
        data = new Data(axis, f, power);
        data.setTitle(plotLabel+" FFT");

        data.setDataType(Data.DATA_TYPE_FILL);
        data.setLineColor(java.awt.Color.black);
        data.setFillColor(plotColor);
    }

    private void initComponents() {
        setTitle("Fast Fourier Transform");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });
        pack();
    }

    private void closeDialog(java.awt.event.WindowEvent evt) {
        setVisible(false);
        dispose();
    }
}