⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 portfoliomanagementjpanel.java

📁 JStock是一个免费股市软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * PortfoliioJPanel.java
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Copyright (C) 2008 Cheok YanCheng <yccheok@yahoo.com>
 */

package org.yccheok.jstock.gui;

import com.thoughtworks.xstream.XStream;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

import javax.swing.tree.TreePath;
import org.apache.commons.logging.*;
import org.jdesktop.swingx.JXTreeTable;
import org.yccheok.jstock.portfolio.*;
import org.yccheok.jstock.engine.*;
import org.jdesktop.swingx.treetable.*;

/**
 *
 * @author  Owner
 */
public class PortfolioManagementJPanel extends javax.swing.JPanel {
    
    /** Creates new form PortfoliioJPanel */
    public PortfolioManagementJPanel() {
        initComponents();        
        
        this.initPortfolio();
    }
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        buyTreeTable = new org.jdesktop.swingx.JXTreeTable(new BuyPortfolioTreeTableModel());
        jScrollPane2 = new javax.swing.JScrollPane();
        sellTreeTable = new org.jdesktop.swingx.JXTreeTable(new SellPortfolioTreeTableModel());
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });
        setLayout(new java.awt.BorderLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Portfolio Management"));
        jPanel1.setLayout(new java.awt.BorderLayout(0, 5));

        jPanel4.setLayout(new java.awt.BorderLayout());

        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel1.setText("Share ($): ");
        jPanel3.add(jLabel1);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jPanel3.add(jLabel2);

        jLabel3.setText("Cash ($): ");
        jPanel3.add(jLabel3);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jPanel3.add(jLabel4);

        jPanel4.add(jPanel3, java.awt.BorderLayout.WEST);

        jLabel5.setText("Paper Profit ($): ");
        jPanel5.add(jLabel5);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11));
        jPanel5.add(jLabel6);

        jLabel7.setText("Realized Profit ($): ");
        jPanel5.add(jLabel7);

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11));
        jPanel5.add(jLabel8);

        jPanel4.add(jPanel5, java.awt.BorderLayout.EAST);

        jPanel1.add(jPanel4, java.awt.BorderLayout.NORTH);

        jSplitPane1.setDividerLocation(250);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Buy"));

        buyTreeTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        buyTreeTable.setRootVisible(true);
        buyTreeTable.getTableHeader().addMouseListener(new TableColumnSelectionPopupListener(1));
        buyTreeTable.addMouseListener(new BuyTableRowPopupListener());
        buyTreeTable.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                buyTreeTableValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(buyTreeTable);

        jSplitPane1.setLeftComponent(jScrollPane1);

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Sell"));

        sellTreeTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        sellTreeTable.setRootVisible(true);
        sellTreeTable.getTableHeader().addMouseListener(new TableColumnSelectionPopupListener(1));
        sellTreeTable.addMouseListener(new SellTableRowPopupListener());
        sellTreeTable.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                sellTreeTableValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(sellTreeTable);

        jSplitPane1.setRightComponent(jScrollPane2);

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

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

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

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/16x16/outbox.png"))); // NOI18N
        jButton3.setText("Sell...");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton3);

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

        add(jPanel2, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.showNewBuyTransactionJDialog(getSelectedStockSymbolForNewTransactionJDialog(), getSelectedStockLastPriceForNewTransactionJDialog(), true);
    }//GEN-LAST:event_jButton1ActionPerformed
    
    private boolean isValidTreeTableNode(TreeTableModel treeTableModel, Object node) {
        boolean result = false;
        
        final Object root = treeTableModel.getRoot();
        
        if (node instanceof TreeTableNode) {
            TreeTableNode ttn = (TreeTableNode) node;

            while (!result && ttn != null) {
                result = ttn == root;

                ttn = ttn.getParent();
            }
        }

        return result;
    }
    
    private Symbol getSelectedStockSymbolForNewTransactionJDialog() {
        final TreePath[] treePaths = buyTreeTable.getTreeSelectionModel().getSelectionPaths();

        if(treePaths == null) return Symbol.newInstance("");
        
        if(treePaths.length == 1) {
            if(treePaths[0].getLastPathComponent() instanceof TransactionSummary) {
                final TransactionSummary transactionSummary = (TransactionSummary)treePaths[0].getLastPathComponent();
                assert(transactionSummary.getChildCount() > 0);
                final Transaction transaction = (Transaction)transactionSummary.getChildAt(0);
                return transaction.getContract().getStock().getSymbol();
                
            }
            else if(treePaths[0].getLastPathComponent() instanceof Transaction) {
                final Transaction transaction = (Transaction)treePaths[0].getLastPathComponent();
                return transaction.getContract().getStock().getSymbol();
            }
        }        
        
        return Symbol.newInstance("");
    }
    
    private double getSelectedStockLastPriceForNewTransactionJDialog() {
        final TreePath[] treePaths = buyTreeTable.getTreeSelectionModel().getSelectionPaths();
        final BuyPortfolioTreeTableModel portfolioTreeTableModel = (BuyPortfolioTreeTableModel)buyTreeTable.getTreeTableModel();
        
        if(treePaths == null) return 0.0;
        
        if(treePaths.length == 1) {
            if(treePaths[0].getLastPathComponent() instanceof TransactionSummary) {
                final TransactionSummary transactionSummary = (TransactionSummary)treePaths[0].getLastPathComponent();
                assert(transactionSummary.getChildCount() > 0);
                final Transaction transaction = (Transaction)transactionSummary.getChildAt(0);
                final Code code = transaction.getContract().getStock().getCode();
                return portfolioTreeTableModel.getLastPrice(code);
            }
            else if(treePaths[0].getLastPathComponent() instanceof Transaction) {
                final Transaction transaction = (Transaction)treePaths[0].getLastPathComponent();
                final Code code = transaction.getContract().getStock().getCode();
                return portfolioTreeTableModel.getLastPrice(code);
            }
        }        
        
        return 0.0;
    }
    
    private void showNewSellTransactionJDialog(Transaction buyTransaction) {
        assert(buyTransaction.getContract().getType() == Contract.Type.Buy);
        
        final Symbol stockSymbol = buyTransaction.getContract().getStock().getSymbol();
        final int maxSellQuantity = buyTransaction.getQuantity();
        double buyCost = 0.0;
        if(buyTransaction.getQuantity() > 0) {
            buyCost = buyTransaction.getNetTotal() / buyTransaction.getQuantity();
        }
        final MainFrame mainFrame = MainFrame.getMe();
        
        NewSellTransactionJDialog newSellTransactionJDialog = new NewSellTransactionJDialog(mainFrame, true);
        newSellTransactionJDialog.setLocationRelativeTo(this);
        newSellTransactionJDialog.setStockSymbol(stockSymbol);
        newSellTransactionJDialog.setMaxSellQuantity(maxSellQuantity);
        newSellTransactionJDialog.setSellQuantity(maxSellQuantity);
        
        newSellTransactionJDialog.setBuyCost(buyCost);
        newSellTransactionJDialog.setPrice(newSellTransactionJDialog.suggestBestSellingPrice());
        newSellTransactionJDialog.setVisible(true);
        
        final Transaction newSellTransaction = newSellTransactionJDialog.getTransaction();
        if(newSellTransaction != null) {                        
            final int remain = buyTransaction.getQuantity() - newSellTransaction.getQuantity();
            
            assert(remain >= 0);
            
            addSellTransaction(newSellTransaction);
            
            final BuyPortfolioTreeTableModel portfolioTreeTableModel = (BuyPortfolioTreeTableModel)buyTreeTable.getTreeTableModel();
            
            if(remain <= 0) {
                portfolioTreeTableModel.removeTransaction(buyTransaction);
            }
            else {
                this.editBuyTransaction(buyTransaction.setQuantity(remain), buyTransaction);
            }
            
            updateWealthHeader();
        }
    }
    
    private void showEditTransactionJDialog(Transaction transaction) {
        final MainFrame mainFrame = MainFrame.getMe();

        if(transaction.getContract().getType() == Contract.Type.Buy) {
            NewBuyTransactionJDialog newTransactionJDialog = new NewBuyTransactionJDialog(mainFrame, true);
            newTransactionJDialog.setStockSelectionEnabled(false);
            newTransactionJDialog.setTransaction(transaction);
            newTransactionJDialog.setTitle("Edit " + transaction.getContract().getStock().getSymbol() + " Buy");
            newTransactionJDialog.setLocationRelativeTo(this);
            newTransactionJDialog.setVisible(true);

            final Transaction newTransaction = newTransactionJDialog.getTransaction();
            if(newTransaction != null) {
                this.editBuyTransaction(newTransaction, transaction);
                updateWealthHeader();
            }        
        }
        else {
            assert(transaction.getContract().getType() == Contract.Type.Sell);
            
            NewSellTransactionJDialog newTransactionJDialog = new NewSellTransactionJDialog(mainFrame, true);
            newTransactionJDialog.setTransaction(transaction);
            newTransactionJDialog.setTitle("Edit " + transaction.getContract().getStock().getSymbol() + " Sell");
            newTransactionJDialog.setLocationRelativeTo(this);
            newTransactionJDialog.setVisible(true);

            final Transaction newTransaction = newTransactionJDialog.getTransaction();
            if(newTransaction != null) {
                this.editSellTransaction(newTransaction, transaction);
                updateWealthHeader();
            }                    
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -