/*
 * -------------------------------------------------------------------------
 *      $Id: WallStreet.java,v 1.8 2004/05/26 19:20:43 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.WallStreet;
import com.imsl.demo.gallery.Describe;
import java.awt.Dimension;

/**
 *
 * @author  brophy
 * @created January 24, 2002
 */
public class WallStreet {

    /** Creates new WallStreet */
    public WallStreet(boolean exitOnClose) throws Exception {
        // read file sp500hst.txt and save to sp500hst.db
        //Database database = new Database("com/imsl/demo/WallStreet/sp500hst.txt");
        //database.save("sp500hst.db");

        // load the db created above
        //Database database = Database.load("sp500hst.db");

        // read the db compressed into a Jar
        java.io.InputStream is = getClass().getClassLoader().getResourceAsStream("sp500hst.db");
        Database database = Database.load(is);
        is.close();

        String tickers[] = database.getTickers();
        Model model = new Model(database);
        ChartStock chartStock = new ChartStock(tickers, model);
        if (!exitOnClose) {
            Object l[] = chartStock.getListeners(java.awt.event.WindowListener.class);
            for (int k = 0;  k < l.length;  k++) {
                chartStock.removeWindowListener((java.awt.event.WindowListener)l[k]);
            }
        }

        SelectTicker selectTicker = new SelectTicker(chartStock, tickers, model);
        Options options = new Options(chartStock, model);

        // position windows
        Describe des = new Describe(chartStock, "/com/imsl/demo/WallStreet/WallStreet.html");
        des.show();
        Dimension ds = des.getSize();
        Dimension ss = chartStock.getToolkit().getScreenSize();
        int w = ss.width - 250;
        int h = (int)Math.min(0.6*w,ss.height-ds.height-64);
        w = (int)(h/0.6);   // preserve aspect ratio
        chartStock.setSize(w, h);
        chartStock.setLocation(ss.width-w, ds.height);
        Dimension st = selectTicker.getSize();
        Dimension o = options.getSize();
        selectTicker.setSize(o.width, h-o.height);
        selectTicker.setLocation(ss.width-w-o.width, ds.height);
        options.setLocation(ss.width-w-o.width, ds.height+h-o.height);

        model.setActive();
        selectTicker.setVisible(true);
        options.setVisible(true);
        chartStock.setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws Exception {
        if (args.length == 2  && args[0].equals("-createDB")) {
            // read file  and save to sp500hst.db
            new Database(args[1]).save("sp500hst.db");
        } else {
            boolean exitOnClose = true;
            if (args.length > 0  && args[0].equals("-noexit"))  exitOnClose = false;
            new WallStreet(exitOnClose);
        }
    }
}