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

📄 portfoliomanagementjpanel.java

📁 JStock是一个免费股市软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    }
    
    public void showNewBuyTransactionJDialog(Symbol stockSymbol, double lastPrice, boolean JComboBoxEnabled) {

        final MainFrame mainFrame = MainFrame.getMe();

        final StockCodeAndSymbolDatabase stockCodeAndSymbolDatabase = mainFrame.getStockCodeAndSymbolDatabase();
        
        if(stockCodeAndSymbolDatabase == null) {
            javax.swing.JOptionPane.showMessageDialog(this, "We haven't connected to stock server.", "Not Connected", javax.swing.JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        
        NewBuyTransactionJDialog newTransactionJDialog = new NewBuyTransactionJDialog(mainFrame, true);
        newTransactionJDialog.setLocationRelativeTo(this);
        newTransactionJDialog.setStockSymbol(stockSymbol);
        newTransactionJDialog.setPrice(lastPrice);
        newTransactionJDialog.setJComboBoxEnabled(JComboBoxEnabled);
        newTransactionJDialog.setStockCodeAndSymbolDatabase(stockCodeAndSymbolDatabase);
        newTransactionJDialog.initjComboBox1EditorComponentKeyListerner();                
        newTransactionJDialog.setVisible(true);
        
        final Transaction transaction = newTransactionJDialog.getTransaction();
        if(transaction != null) {
            this.addBuyTransaction(transaction);
            updateWealthHeader();
        }
    }
    
    public void clearTableSelection() {
        buyTreeTable.getSelectionModel().clearSelection();
        sellTreeTable.getSelectionModel().clearSelection();
    }
    
    private void deleteSelectedTreeTableRow(org.jdesktop.swingx.JXTreeTable treeTable) {
        final AbstractPortfolioTreeTableModel portfolioTreeTableModel = (AbstractPortfolioTreeTableModel)treeTable.getTreeTableModel();
        final TreePath[] treePaths = treeTable.getTreeSelectionModel().getSelectionPaths();
        
        if(treePaths == null) {
            return;
        }
        
        for(TreePath treePath : treePaths) {
            final Object o = treePath.getLastPathComponent();

            if(portfolioTreeTableModel.getRoot() == o) continue;
            
            final MutableTreeTableNode mutableTreeTableNode = (MutableTreeTableNode)o;

            if(isValidTreeTableNode(portfolioTreeTableModel, mutableTreeTableNode) == false) {
                //???
                portfolioTreeTableModel.fireTreeTableNodeChanged(mutableTreeTableNode);
                continue;
            }
                        
            if(o instanceof Transaction) {                
                portfolioTreeTableModel.removeTransaction((Transaction)o);
                
            }
            else if(o instanceof TransactionSummary) {
                portfolioTreeTableModel.removeTransactionSummary((TransactionSummary)o);
            }
        }        
    }
    
    private void deteleSelectedTreeTableRow() {
        deleteSelectedTreeTableRow(this.buyTreeTable);
        deleteSelectedTreeTableRow(this.sellTreeTable);
        
        updateWealthHeader();
    }
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        deteleSelectedTreeTableRow();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void buyTreeTableValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_buyTreeTableValueChanged
        // TODO add your handling code here:
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    if(buyTreeTable.getSelectedRowCount() > 0) {
                        sellTreeTable.clearSelection();
                    }
                }        
        });
    }//GEN-LAST:event_buyTreeTableValueChanged

    private void sellTreeTableValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_sellTreeTableValueChanged
        // TODO add your handling code here:
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    if(sellTreeTable.getSelectedRowCount() > 0) {
                        buyTreeTable.clearSelection();
                    }
                }        
        });
    }//GEN-LAST:event_sellTreeTableValueChanged

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        // TODO add your handling code here:
        this.clearTableSelection();
    }//GEN-LAST:event_formMouseClicked

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        final Transaction transaction = getSelectedTransaction(buyTreeTable);
        
        if(transaction == null) {
            JOptionPane.showMessageDialog(this, "You need to select 1 buy transaction, in order to sell it", "No buy transaction selected", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        
        this.showNewSellTransactionJDialog(transaction);
    }//GEN-LAST:event_jButton3ActionPerformed
    
    // Will return null, if more than one buyTransaction being selected, or no
    // buyTransaction being selected.
    private Transaction getSelectedTransaction(JXTreeTable treeTable) {
        if(treeTable.getSelectedRowCount() != 1) return null;
        
        final TreePath[] treePaths = treeTable.getTreeSelectionModel().getSelectionPaths();
        
        final Object o = treePaths[0].getLastPathComponent();

        if (o instanceof Transaction) {
            return (Transaction)o;
        }
        
        return null;        
    }
    
    private boolean isOnlyTreeTableRootBeingSelected(JXTreeTable treeTable) {
        if(treeTable.getSelectedRowCount() != 1) return false;
        
        final TreePath[] treePaths = treeTable.getTreeSelectionModel().getSelectionPaths();
        
        final Object o = treePaths[0].getLastPathComponent();

        final AbstractPortfolioTreeTableModel portfolioTreeTableModel = (AbstractPortfolioTreeTableModel)treeTable.getTreeTableModel();
        
        return (portfolioTreeTableModel.getRoot() == o);
    }
        
    private class BuyTableRowPopupListener extends MouseAdapter {
        
        @Override
        public void mouseClicked(MouseEvent evt) {
            if(evt.getClickCount() == 2) {
                final Transaction transaction = getSelectedTransaction(buyTreeTable);
                if(transaction != null) {
                    PortfolioManagementJPanel.this.showEditTransactionJDialog(transaction);
                }
            }
        }
        
        @Override
        public void mousePressed(MouseEvent e) {
            maybeShowPopup(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }
        
        private void maybeShowPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                getBuyTreeTablePopupMenu().show(e.getComponent(), e.getX(), e.getY());
            }
        }
    }

    private class SellTableRowPopupListener extends MouseAdapter {
        
        public void mouseClicked(MouseEvent evt) {
            if(evt.getClickCount() == 2) {
                final Transaction transaction = getSelectedTransaction(sellTreeTable);
                if(transaction != null) {
                    PortfolioManagementJPanel.this.showEditTransactionJDialog(transaction);
                }
            }
        }
        
        @Override
        public void mousePressed(MouseEvent e) {
            maybeShowPopup(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }
        
        private void maybeShowPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                final JPopupMenu popupMenu = getSellTreeTablePopupMenu();
                if(popupMenu != null)
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    }
    
    private ImageIcon getImageIcon(String imageIcon) {
        return new javax.swing.ImageIcon(getClass().getResource(imageIcon));
    }
    
    private void showBuyPortfolioChartJDialog() {
        final MainFrame m = MainFrame.getMe();
        final BuyPortfolioTreeTableModel buyPortfolioTreeTableModel = (BuyPortfolioTreeTableModel)buyTreeTable.getTreeTableModel();
        BuyPortfolioChartJDialog buyPortfolioChartJDialog = new BuyPortfolioChartJDialog(m, false, buyPortfolioTreeTableModel);
        buyPortfolioChartJDialog.setVisible(true);                                    
    }
    
    private void showSellPortfolioChartJDialog() {
        final MainFrame m = MainFrame.getMe();
        final SellPortfolioTreeTableModel sellPortfolioTreeTableModel = (SellPortfolioTreeTableModel)sellTreeTable.getTreeTableModel();
        SellPortfolioChartJDialog sellPortfolioChartJDialog = new SellPortfolioChartJDialog(m, false, sellPortfolioTreeTableModel);
        sellPortfolioChartJDialog.setVisible(true);                                    
    }
    
    private JPopupMenu getSellTreeTablePopupMenu() {                
        final Transaction transaction = getSelectedTransaction(this.sellTreeTable);

        JPopupMenu popup = new JPopupMenu();

        JMenuItem menuItem = null;
        
        if(transaction != null) {
            menuItem = new JMenuItem("Edit...", this.getImageIcon("/images/16x16/edit.png"));

            menuItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    PortfolioManagementJPanel.this.showEditTransactionJDialog(transaction);
                }
            });            

            popup.add(menuItem);

            popup.addSeparator();
        }
        
       menuItem = new JMenuItem("Summary...", this.getImageIcon("/images/16x16/chart.png"));
        
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                PortfolioManagementJPanel.this.showSellPortfolioChartJDialog();
            }
        });

        popup.add(menuItem);        
        
        if(isOnlyTreeTableRootBeingSelected(sellTreeTable) == false && (sellTreeTable.getSelectedRow() > 0)) {
            final MainFrame m = MainFrame.getMe();
                                
            menuItem = new JMenuItem("History...", this.getImageIcon("/images/16x16/strokedocker.png"));

            menuItem.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        List<Stock> stocks = getSelectedStock(sellTreeTable);

                        for(Stock stock : stocks) {
                            m.displayHistoryChart(stock);
                        }
                    }
            });
                        
            popup.add(menuItem);
            popup.addSeparator();
            
            menuItem = new JMenuItem("Delete", this.getImageIcon("/images/16x16/editdelete.png"));

            menuItem.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        PortfolioManagementJPanel.this.deteleSelectedTreeTableRow();
                    }
            });

            popup.add(menuItem);
        }
        
        return popup;
    }
    
    private JPopupMenu getBuyTreeTablePopupMenu() {                
        JPopupMenu popup = new JPopupMenu();

        JMenuItem menuItem = new JMenuItem("Buy...", this.getImageIcon("/images/16x16/inbox.png"));

        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                PortfolioManagementJPanel.this.showNewBuyTransactionJDialog(getSelectedStockSymbolForNewTransactionJDialog(), getSelectedStockLastPriceForNewTransactionJDialog(), true);
            }
        });

        popup.add(menuItem);

        final Transaction transaction = getSelectedTransaction(this.buyTreeTable);

        if(transaction != null) {
            menuItem = new JMenuItem("Sell...", this.getImageIcon("/images/16x16/outbox.png"));
            
            menuItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    PortfolioManagementJPanel.this.showNewSellTransactionJDialog(transaction);
                }
            });            
            
            popup.add(menuItem);  
        }       
        
        popup.addSeparator();
                
        if(transaction != null) {
            menuItem = new JMenuItem("Edit...", this.getImageIcon("/images/16x16/edit.png"));
            
            menuItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    PortfolioManagementJPanel.this.showEditTransactionJDialog(transaction);
                }
            });            
            
            popup.add(menuItem);
            
            popup.addSeparator();    
        }       
        
        menuItem = new JMenuItem("Summary...", this.getImageIcon("/images/16x16/chart.png"));
        
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                PortfolioManagementJPanel.this.showBuyPortfolioChartJDialog();
            }
        });

        popup.add(menuItem);                
        
        if(isOnlyTreeTableRootBeingSelected(buyTreeTable) == false && (buyTreeTable.getSelectedRow() > 0)) {
            //popup.addSeparator();
            
            final MainFrame m = MainFrame.getMe();
                                
            menuItem = new JMenuItem("History...", this.getImageIcon("/images/16x16/strokedocker.png"));

            menuItem.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        List<Stock> stocks = getSelectedStock(buyTreeTable);

⌨️ 快捷键说明

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