/*
 * -------------------------------------------------------------------------
 *      $Id: DialogSelect.java,v 1.4 2003/04/04 14:32:29 trudisch 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.BondChart;
import javax.swing.JCheckBox;
import java.awt.*;
import java.awt.event.*;
import java.util.GregorianCalendar;
import java.text.*;

/**
 *
 * @author  brophy
 * @created September 21, 2001
 */
public class DialogSelect extends javax.swing.JDialog implements ActionListener {
    static private final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
    static private final NumberFormat nf = new DecimalFormat("0.00");

    private ChartPanel      chartPanel;
    private GridBagConstraints gb;

    /** Creates new form DialogSelect */
    public DialogSelect(javax.swing.JFrame parent) {
        super(parent, false);
        setTitle("Select Bonds");
        initComponents();
        gb = new GridBagConstraints();
        gb.gridwidth = GridBagConstraints.REMAINDER;
        gb.fill = GridBagConstraints.HORIZONTAL;
    }

    public void setChartPanel(ChartPanel chartPanel) {
        this.chartPanel = chartPanel;
    }

    void addBond(final int iBond, GregorianCalendar maturity, double coupon) {
        StringBuffer sb = new StringBuffer(100);
        sb.append(dateFormat.format(maturity.getTime()));
        sb.append("  ");
        sb.append(nf.format(coupon));
        sb.append("%");
        JCheckBox cb = new JCheckBox(sb.toString());
        cb.setSelected(false);
        cb.setActionCommand(""+iBond);
        cb.addActionListener(this);
        jPanel.add(cb, gb);
    }

    public void actionPerformed(ActionEvent actionEvent) {
        boolean selected = ((JCheckBox)actionEvent.getSource()).isSelected();
        int iBond = Integer.parseInt(actionEvent.getActionCommand());
        chartPanel.showCurve(iBond, selected);
    }


    /** 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();
        jPanel = new javax.swing.JPanel();

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

        jScrollPane1.setPreferredSize(new java.awt.Dimension(150, 450));
        jPanel.setLayout(new java.awt.GridBagLayout());

        jScrollPane1.setViewportView(jPanel);

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

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

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel jPanel;
    // End of variables declaration//GEN-END:variables
}