/*
 * -------------------------------------------------------------------------
 *      $Id: Strip.java,v 1.3 2004/05/26 19:15:34 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.Strip;
import com.imsl.chart.*;
import com.imsl.demo.gallery.Describe;
import java.awt.Font;
import javax.swing.*;

/**
 *  Strip Chart
 */
public class Strip extends JFrameChart implements Runnable {
    static private String title[] = {"Red", "Dark Green", "Magenta"};
    static private String color[] = {"Red", "DarkGreen", "Magenta"};

    private double  y[][];
    private Data    label;

    /**
     *  Creates a strip chart.
     */
    public Strip(boolean exitOnClose) {
        setTitle("Strip Chart");
        if (!exitOnClose) {
            // remove the WindowListener,  installed by JFrameChart, that
            // exits the application when the window is closed.
            Object l[] = getListeners(java.awt.event.WindowListener.class);
            for (int k = 0;  k < l.length;  k++) {
                removeWindowListener((java.awt.event.WindowListener)l[k]);
            }
        }

        Describe des = new Describe(this, "/com/imsl/demo/Strip/Strip.html");
        des.show();
        java.awt.Dimension ds = des.getSize();

        java.awt.Dimension ss = getToolkit().getScreenSize();
        int h = Math.min(ss.width/2, ss.height-ds.height-32);
        int w = (int)(h/0.8);
        setSize(w, h);
        setLocation(ss.width-ds.width, ds.height);

        /*
         *  Create the chart
         */
        Chart chart = getChart();
        AxisXY axis = new AxisXY(chart);

        // axis titles
        axis.getAxisX().getAxisTitle().setTitle("Time Offset");
        axis.getAxisX().getAxisTitle().setFontStyle(Font.BOLD | Font.ITALIC);
        axis.getAxisX().getAxisTitle().setTextColor("mediumblue");

        axis.getAxisY().getAxisTitle().setTitle("Amplitude");
        axis.getAxisY().getAxisTitle().setFontStyle(Font.BOLD | Font.ITALIC);
        axis.getAxisY().getAxisTitle().setTextColor("mediumblue");

        // turn off autoscaling so that the window does not jump during updates
        // fix y-axis range to [-1, +1]
        axis.getAxisY().setAutoscaleInput(AxisXY.AUTOSCALE_OFF);
        axis.getAxisY().setWindow(-1,1);

        // initial raw data
        double x[] = new double[100];
        y = new double[3][100];
        for (int k = 1;  k < x.length;  k++) {
            x[k] = 0.01*k;
            for (int i = 0;  i < y.length;  i++) {
                y[i][k] = y[i][k-1] + 0.10*Math.random() - 0.05;
            }
        }

        // data lines
        axis.setDataType(AxisXY.DATA_TYPE_LINE);
        final int count = 3;
        final Data data[] = new Data[count];
        for (int i = 0;  i < count;  i++) {
            data[i] = new Data(axis, x, y[i]);
            data[i].setLineColor(color[i%color.length]);
            data[i].setLineWidth(2.0);
            data[i].setTitle(title[i]);
            new ToolTip(data[i]);
        }

        // current time label
        label = new Data(axis, new double[]{x[20]},new double[]{0.8});
        label.setDataType(0);
        label.setLabelType(Data.LABEL_TYPE_TITLE);
        label.setTextColor("blue");
        label.setFontName("serif");
        label.setFontStyle(java.awt.Font.BOLD);

        /*
         *  Add "Lines" menu to main menubar
         */
        JMenuBar jMenuBar = getJMenuBar();
        JMenu jMenuLines = new JMenu("Lines");
        jMenuLines.setMnemonic('L');
        jMenuBar.add(jMenuLines);
        for (int k = 0;  k < 3;  k++) {
            final int index = k;
            final JCheckBoxMenuItem jMenuItem = new JCheckBoxMenuItem(title[index]);
            jMenuLines.add(jMenuItem);
            jMenuItem.setState(true);
            jMenuItem.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    data[index].setPaint(jMenuItem.getState());
                }
            });
        }
    }


    /**
     *  Updates the strip chart.
     *  This method is designed to run in a different thread than the
     *  Java Swing thread.
     */
    public void run() {
        int n = y[0].length;
        float t = 0;
        while (true) {
            synchronized(this) {
                t += 1.0f;
                label.setTitle("Current Time "+t);
                for (int i = 0;  i < y.length;  i++) {
                    for (int k = n-1;  k > 0;  k--) {
                        y[i][k] = y[i][k-1];
                    }
                    y[i][0] += 0.10*Math.random() - 0.05;
                    if (Math.abs(y[i][0]) > 1)  y[i][0] = 0.0;
                }
            }
            repaint();
            try {
                Thread.sleep(50);
            } catch (Exception e) {
            }
        }
    }

    /**
     *  Synchronize the paint method so it waits for the
     *  updates in run() to be ready.
     */
    synchronized public void paint(java.awt.Graphics g) {
        super.paint(g);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        boolean exitOnClose = true;
        if (args.length > 0  && args[0].equals("-noexit"))  exitOnClose = false;
        Strip strip = new Strip(exitOnClose);
        strip.setVisible(true);

        // Create thread to continually update chart
        Thread thread = new Thread(strip);
        thread.setDaemon(true);
        thread.start();
    }
}