/*
 * -------------------------------------------------------------------------
 *      $Id: SelectTicker.java,v 1.5 2004/05/26 19:20:42 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;

/**
 *
 * @author  brophy
 * @created January 24, 2002
 */
class SelectTicker extends javax.swing.JDialog {
    private int     nTickers;
    private Model   model;
    private String  lastTicker = "";

    /** Creates new form SelectTicker */
    public SelectTicker(java.awt.Frame parent, String tickers[], Model model) {
        super(parent);
        this.model = model;
        initComponents();
        jList.setBackground(com.imsl.chart.ChartNode.parseColor("lightYellow"));
        nTickers = tickers.length;
        jList.setListData(tickers);
        jList.setSelectedValue(model.getTicker(), true);
        pack();
        jList.ensureIndexIsVisible(jList.getSelectedIndex());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jScrollPane1 = new javax.swing.JScrollPane();
        jList = new javax.swing.JList();
        jPanelControl = new javax.swing.JPanel();
        jButtonBack = new javax.swing.JButton();
        jButtonNext = new javax.swing.JButton();

        setTitle("Ticker");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jScrollPane1.setPreferredSize(new java.awt.Dimension(100, 100));
        jList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListValueChanged(evt);
            }
        });

        jScrollPane1.setViewportView(jList);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jButtonBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imsl/demo/WallStreet/Back24.gif")));
        jButtonBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBackActionPerformed(evt);
            }
        });

        jPanelControl.add(jButtonBack);

        jButtonNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imsl/demo/WallStreet/Forward24.gif")));
        jButtonNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNextActionPerformed(evt);
            }
        });

        jPanelControl.add(jButtonNext);

        getContentPane().add(jPanelControl, java.awt.BorderLayout.NORTH);

        pack();
    }//GEN-END:initComponents

    private void jButtonBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBackActionPerformed
        int index = jList.getSelectedIndex() - 1;
        if (index < 0)  index = nTickers - 1;
        jList.setSelectedIndex(index);
        jList.ensureIndexIsVisible(index);
    }//GEN-LAST:event_jButtonBackActionPerformed

    private void jButtonNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNextActionPerformed
        selectNext();
    }//GEN-LAST:event_jButtonNextActionPerformed

    private void jListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListValueChanged
        String ticker = (String)jList.getSelectedValue();
        if (ticker.equals(lastTicker))  return;
        lastTicker = ticker;
        model.setTicker(ticker);
    }//GEN-LAST:event_jListValueChanged

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog


    void selectNext() {
        int index = jList.getSelectedIndex() + 1;
        if (index >= nTickers)  index = 0;
        jList.setSelectedIndex(index);
        jList.ensureIndexIsVisible(index);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel jPanelControl;
    private javax.swing.JButton jButtonNext;
    private javax.swing.JButton jButtonBack;
    private javax.swing.JList jList;
    // End of variables declaration//GEN-END:variables
}