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

📄 indicatorscannerjpanel.java

📁 JStock是一个免费股市软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * IndicatorScannerJPanel.java
 *
 * Created on June 15, 2007, 9:58 PM
 *
 * 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) 2007 Cheok YanCheng <yccheok@yahoo.com>
 */

package org.yccheok.jstock.gui;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.swing.event.*;
import com.nexes.wizard.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
import org.yccheok.jstock.engine.*;
import org.yccheok.jstock.analysis.*;
import java.util.concurrent.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author  yccheok
 */
public class IndicatorScannerJPanel extends javax.swing.JPanel implements ChangeListener {
    
    /** Creates new form IndicatorScannerJPanel */
    public IndicatorScannerJPanel() {
        initComponents();
        
        initTableHeaderToolTips();
    }
    
    /** 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();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setLayout(new java.awt.BorderLayout(5, 5));

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

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

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

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Indicator Scan Result"));
        jPanel2.setLayout(new java.awt.BorderLayout());

        jTable1.setAutoCreateRowSorter(true);
        jTable1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jTable1.setModel(new IndicatorTableModel());
        jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        this.jTable1.setDefaultRenderer(Number.class, new StockTableCellRenderer());
        this.jTable1.setDefaultRenderer(Double.class, new StockTableCellRenderer());
        this.jTable1.setDefaultRenderer(Object.class, new StockTableCellRenderer());

        this.jTable1.getTableHeader().addMouseListener(new TableColumnSelectionPopupListener());
        this.jTable1.addMouseListener(new TableRowPopupListener());
        jScrollPane1.setViewportView(jTable1);

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

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

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
        stop();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
        if (this.startScanThread != null)
        {
            try {
                this.startScanThread.join();
            } catch (InterruptedException ex) {
                log.error(null, ex);
            }

            this.startScanThread = null;
        }

        final MainFrame m = getMainFrame();
        
        if(m.getStockCodeAndSymbolDatabase() == null) {
            javax.swing.JOptionPane.showMessageDialog(this, "We haven't connected to stock server.", "Not Connected", javax.swing.JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        // Reset dirty flag, to allow background thread to show indicator on
        // the table.
        allowIndicatorShown = true;
        
        if(operatorIndicators.size() > 0) {
            final int result = JOptionPane.showConfirmDialog(this, "You have previous built indicators, do you want to re-use them?", "Re-use Indicators", JOptionPane.YES_NO_OPTION);
            
            if(result == JOptionPane.YES_OPTION)
            {
                this.initRealTimeStockMonitor(m.getStockServerFactory());
                this.initStockHistoryMonitor(m.getStockServerFactory());

                removeAllIndicatorsFromTable();
                alertRecords.clear();

                submitOperatorIndicatorToMonitor();

                jButton1.setEnabled(false);
                jButton2.setEnabled(true);
                
                return;
            }
            /*
            else if(result == JOptionPane.CLOSED_OPTION) {
                return;
            }
            */
        }
        
        initWizardDialog();
        
        int ret = wizard.showModalDialog();

        if(ret != Wizard.FINISH_RETURN_CODE)
            return;

        final WizardModel wizardModel = wizard.getModel();

        this.startScanThread = getStartScanThread(wizardModel, m);
        this.startScanThread.start();

        jButton1.setEnabled(false);
        jButton2.setEnabled(true);

        m.setStatusBar(true, "Indicator scanner is scanning...");
    }//GEN-LAST:event_jButton1ActionPerformed

    // Time consuming method. It involves file I/O reading (getOperatorIndicator).
    private void initOperatorIndicators(WizardModel wizardModel)
    {
        this.operatorIndicators.clear();

        WizardPanelDescriptor wizardPanelDescriptor0 = wizardModel.getPanelDescriptor(WizardSelectStockDescriptor.IDENTIFIER);
        WizardSelectStockJPanel wizardSelectStockJPanel = (WizardSelectStockJPanel)wizardPanelDescriptor0.getPanelComponent();
        WizardPanelDescriptor wizardPanelDescriptor1 = wizardModel.getPanelDescriptor(WizardSelectIndicatorDescriptor.IDENTIFIER);
        WizardSelectIndicatorJPanel wizardSelectIndicatorJPanel = (WizardSelectIndicatorJPanel)wizardPanelDescriptor1.getPanelComponent();

        final MainFrame m = getMainFrame();
        final StockCodeAndSymbolDatabase stockCodeAndSymbolDatabase = m.getStockCodeAndSymbolDatabase();
        final IndicatorProjectManager indicatorProjectManager = m.getIndicatorProjectManager();
        java.util.List<String> projects = wizardSelectIndicatorJPanel.getSelectedProjects();
        java.util.List<Code> codes = wizardSelectStockJPanel.getSelectedCodes();

        for(final Code code : codes) {
            final java.util.List<OperatorIndicator> result = new java.util.ArrayList<OperatorIndicator>();

            operatorIndicators.put(code, result);

            for(String project : projects) {
                final OperatorIndicator operatorIndicator = indicatorProjectManager.getOperatorIndicator(project);

                if(operatorIndicator != null) {
                    final Stock stock = Utils.getEmptyStock(code, stockCodeAndSymbolDatabase.codeToSymbol(code));

                    operatorIndicator.setStock(stock);

                    result.add(operatorIndicator);
                }
            }   /* for(String project : projects) */
        }   /* for(String code : codes) */
    }

    private void submitOperatorIndicatorToMonitor()
    {
        final Set<Code> codes = operatorIndicators.keySet();
        Duration historyDuration = Duration.getTodayDurationByDays(0);

        for(Code code : codes) {
            final List<OperatorIndicator> operatorIndicatos = operatorIndicators.get(code);

            for (OperatorIndicator operatorIndicator : operatorIndicatos)
            {
                historyDuration = historyDuration.getUnionDuration(operatorIndicator.getNeededStockHistoryDuration());
            }

            // Dirty way to speed up calculation.
            // Currently, all codes are having same set of indicators,
            // and duration is independent of code type.
            break;
        }

        // Duration must be initialized, before codes being added.
        this.stockHistoryMonitor.setDuration(historyDuration);

        for(Code code : codes) {
            final List<OperatorIndicator> operatorIndicatos = operatorIndicators.get(code);

            boolean done = true;
            for (OperatorIndicator operatorIndicator : operatorIndicatos)
            {
                if (operatorIndicator.isStockHistoryCalculationDone() == false)
                {
                    done = false;
                    
                    // Early break. We will let history monitor to perform pre-calculation.
                    break;
                }
                else
                {
                    operatorIndicator.preCalculate();
                }
            }

            if (done)
            {
                // Perform real time monitoring, for the code with history information.
                realTimeStockMonitor.addStockCode(code);
            }
            else
            {
                this.stockHistoryMonitor.addStockCode(code);
            }
        }
    }

    private JPopupMenu getMyTableColumnSelectionPopupMenu(final int mouseXLocation) {
        JPopupMenu popup = new JPopupMenu();
        TableModel tableModel = jTable1.getModel();
        final int col = tableModel.getColumnCount();
        
        for(int i=2; i<col; i++) {
            String name = tableModel.getColumnName(i);            
            
            boolean isVisible = true;
            
            try {
                TableColumn tableColumn = jTable1.getColumn(name);
            }
            catch(java.lang.IllegalArgumentException exp) {
                isVisible = false;
            }
            
            javax.swing.JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(name, isVisible);
                        
            menuItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    String name = evt.getActionCommand();
                    JCheckBoxMenuItem menuItem = (JCheckBoxMenuItem)evt.getSource();
                    if(menuItem.isSelected() == false) {
                        JTableUtilities.removeTableColumn(jTable1, name);
                    }
                    else {
                        TableColumnModel colModel = jTable1.getColumnModel();
                        int vColIndex = colModel.getColumnIndexAtX(mouseXLocation);
                        JTableUtilities.insertTableColumnFromModel(jTable1, name, vColIndex);
                    }
                }
            });

⌨️ 快捷键说明

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