org.yccheok.jstock.gui.portfolio.SplitJDialog.java Source code

Java tutorial

Introduction

Here is the source code for org.yccheok.jstock.gui.portfolio.SplitJDialog.java

Source

/*
 * JStock - Free Stock Market Software
 * Copyright (C) 2011 Yan Cheng CHEOK <yccheok@yahoo.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package org.yccheok.jstock.gui.portfolio;

import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.ComboBoxModel;
import javax.swing.JFormattedTextField;
import javax.swing.text.NumberFormatter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.yccheok.jstock.engine.StockInfo;
import org.yccheok.jstock.internationalization.GUIBundle;

/**
 *
 * @author yccheok
 */
public class SplitJDialog extends javax.swing.JDialog {

    /** 
     * Constructs new SplitJDialog.
     *
     * @param parent the parent
     * @param modal true if this should be a model dialog
     * @param stockInfo stock info of the stock to be split
     */
    public SplitJDialog(java.awt.Frame parent, boolean modal, StockInfo stockInfo) {
        super(parent, modal);
        initComponents();
        this.stockInfo = stockInfo;
        updateGUI();
    }

    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jFormattedTextField1 = getUnitJFormattedTextField();
        jLabel1 = new javax.swing.JLabel();
        jFormattedTextField2 = getUnitJFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jComboBox1.setModel(getComboBoxModel());
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jFormattedTextField1.setPreferredSize(new java.awt.Dimension(40, 20));

        jFormattedTextField2.setPreferredSize(new java.awt.Dimension(40, 20));

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/gui"); // NOI18N
        jLabel2.setText(bundle.getString("SplitJDialog_Unit")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFormattedTextField2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel2)
                        .addContainerGap(17, Short.MAX_VALUE)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1)
                        .addComponent(jFormattedTextField2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

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

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/16x16/apply.png"))); // NOI18N
        jButton1.setText(bundle.getString("SplitJDialog_OK")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton1);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/16x16/button_cancel.png"))); // NOI18N
        jButton2.setText(bundle.getString("SplitJDialog_Cancel")); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton2);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        updateGUI();
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            jFormattedTextField1.commitEdit();
            jFormattedTextField2.commitEdit();
        } catch (ParseException ex) {
            log.error(null, ex);
        }
        Double v0 = (Double) jFormattedTextField1.getValue();
        Double v1 = (Double) jFormattedTextField2.getValue();
        if (v0 == null || v1 == null) {
            // Null value do not make sense.
            return;
        }
        double _v0 = v0;
        double _v1 = v1;
        if (_v0 <= 0.0 || _v1 <= 0.0) {
            // Negative and zero value do not make sense.
            return;
        }

        this.ratio = _v1 / _v0;
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.ratio = null;
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private ComboBoxModel getComboBoxModel() {
        return new javax.swing.DefaultComboBoxModel(new String[] { GUIBundle.getString("SplitJDialog_Split"),
                GUIBundle.getString("SplitJDialog_Merge"), });
    }

    private void updateGUI() {
        if (jComboBox1.getSelectedItem().equals(GUIBundle.getString("SplitJDialog_Split"))) {
            String title = MessageFormat.format(GUIBundle.getString("SplitJDialog_Split_template"),
                    stockInfo.symbol);
            this.setTitle(title);
            String message = MessageFormat.format(GUIBundle.getString("SplitJDialog_UnitTo_template"),
                    stockInfo.symbol);
            jLabel1.setText(message);
            jFormattedTextField1.setEditable(false);
            jFormattedTextField2.setEditable(true);
            jFormattedTextField1.setValue(new Double(1.0));
            jFormattedTextField2.setValue(new Double(1.0));
        } else {
            assert (jComboBox1.getSelectedItem().equals(GUIBundle.getString("SplitJDialog_Merge")));
            String title = MessageFormat.format(GUIBundle.getString("SplitJDialog_Merge_template"),
                    stockInfo.symbol);
            this.setTitle(title);
            String message = MessageFormat.format(GUIBundle.getString("SplitJDialog_UnitTo_template"),
                    stockInfo.symbol);
            jLabel1.setText(message);
            jFormattedTextField1.setEditable(true);
            jFormattedTextField2.setEditable(false);
            jFormattedTextField1.setValue(new Double(1.0));
            jFormattedTextField2.setValue(new Double(1.0));
        }
    }

    private JFormattedTextField getUnitJFormattedTextField() {
        NumberFormat format = new DecimalFormat("#,##0.###");
        NumberFormatter formatter = new NumberFormatter(format);
        formatter.setMinimum(1.0);
        formatter.setValueClass(Double.class);
        JFormattedTextField field = new JFormattedTextField(formatter);
        return field;
    }

    /**
     * Returns the ratio of splitting. If this is a reverse split (aka merging),
     * ratio will be less than 1, but greater than 0. Returns null if user
     * doesn't press OK button.
     *
     * @return the ratio of splitting
     */
    public Double getRatio() {
        return ratio;
    }

    private final StockInfo stockInfo;
    private Double ratio = null;
    private static final Log log = LogFactory.getLog(SplitJDialog.class);

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JFormattedTextField jFormattedTextField2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables

}