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

📄 indicatorscannerjpanel.java

📁 JStock是一个免费股市软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            
            popup.add(menuItem);            
        }
        
        return popup;
    }
    
    private class TableColumnSelectionPopupListener extends MouseAdapter {        
        public void mousePressed(MouseEvent e) {
            maybeShowPopup(e);
        }

        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }

        private void maybeShowPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                getMyTableColumnSelectionPopupMenu(e.getX()).show(e.getComponent(), e.getX(), e.getY());
            }
        }
    }    

    private static class ColumnHeaderToolTips extends MouseMotionAdapter {

        // Current column whose tooltip is being displayed.
        // This variable is used to minimize the calls to setToolTipText().
        TableColumn curCol;
    
        // Maps TableColumn objects to tooltips
        Map<TableColumn, String> tips = new HashMap<TableColumn, String>();
    
        // If tooltip is null, removes any tooltip text.
        public void setToolTip(TableColumn col, String tooltip) {
            if (tooltip == null) {
                tips.remove(col);
            } else {
                tips.put(col, tooltip);
            }
        }
    
        public void mouseMoved(MouseEvent evt) {
            TableColumn col = null;
            JTableHeader header = (JTableHeader)evt.getSource();
            JTable table = header.getTable();
            TableColumnModel colModel = table.getColumnModel();
            int vColIndex = colModel.getColumnIndexAtX(evt.getX());
    
            // Return if not clicked on any column header
            if (vColIndex >= 0) {
                col = colModel.getColumn(vColIndex);
            }
    
            if (col != curCol) {
                header.setToolTipText((String)tips.get(col));
                curCol = col;
            }
        }
    }
    
    private void initTableHeaderToolTips() {
        JTableHeader header = jTable1.getTableHeader();
    
        ColumnHeaderToolTips tips = new ColumnHeaderToolTips();
            
        header.addMouseMotionListener(tips);        
    }
    
    @Override
    public void stateChanged(javax.swing.event.ChangeEvent evt) {
    }    
    
    public void clear()
    {
        final MainFrame m = getMainFrame();
        this.initRealTimeStockMonitor(m.getStockServerFactory());
        this.initStockHistoryMonitor(m.getStockServerFactory());

        this.operatorIndicators.clear();

        // Ask help from dirty flag, so that background thread won't have
        // chance to show indicators on the table.
        allowIndicatorShown = false;
        
        removeAllIndicatorsFromTable();

        m.setStatusBar(false, "Connected");
    }
    
    public void stop()
    {
        if (this.startScanThread != null)
        {
            try {
                this.startScanThread.join();
            } catch (InterruptedException ex) {
                log.error(null, ex);
            }

            this.startScanThread = null;
        }
        
        final MainFrame m = getMainFrame();
        this.initRealTimeStockMonitor(m.getStockServerFactory());
        this.initStockHistoryMonitor(m.getStockServerFactory());

        final ExecutorService oldSystemTrayAlertPool = systemTrayAlertPool;
        final ExecutorService oldEmailAlertPool = emailAlertPool;
        
        Utils.getZoombiePool().execute(new Runnable() {
            @Override
            public void run() {
                log.info("Prepare to shut down " + oldSystemTrayAlertPool + "...");
                oldSystemTrayAlertPool.shutdownNow();
                try {            
                    oldSystemTrayAlertPool.awaitTermination(100, TimeUnit.DAYS);
                } catch (InterruptedException exp) {
                    log.error("", exp);
                }
                log.info("Shut down " + oldSystemTrayAlertPool + " peacefully.");
                
                log.info("Prepare to shut down " + oldEmailAlertPool + "...");
                oldEmailAlertPool.shutdownNow();
                try {            
                    oldEmailAlertPool.awaitTermination(100, TimeUnit.DAYS);
                } catch (InterruptedException exp) {
                    log.error("", exp);
                }
                log.info("Shut down " + oldEmailAlertPool + " peacefully.");                
            }
        }); 
        
        emailAlertPool = Executors.newFixedThreadPool(1);
        systemTrayAlertPool = Executors.newFixedThreadPool(1);        
        
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                jButton1.setEnabled(true);
                jButton2.setEnabled(false);            
            }
            
        });

        m.setStatusBar(false, "Connected");
    }
    
    public void initWizardDialog() {
        final MainFrame m = getMainFrame();
        
        wizard = new Wizard(m);

        wizard.getDialog().setTitle("Indicator Scanning Wizard");
        wizard.getDialog().setResizable(false);
        
        WizardPanelDescriptor wizardSelectIndicatorDescriptor = new WizardSelectIndicatorDescriptor();
        wizard.registerWizardPanel(WizardSelectIndicatorDescriptor.IDENTIFIER, wizardSelectIndicatorDescriptor);
        
        // Quick hack. WizardSelectStockJPanel has no way to obtain MainFrame, during its construction
        // stage.
        WizardPanelDescriptor wizardSelectStockDescriptor = new WizardSelectStockDescriptor(m.getStockCodeAndSymbolDatabase());
        wizard.registerWizardPanel(WizardSelectStockDescriptor.IDENTIFIER, wizardSelectStockDescriptor);
        
        wizard.setCurrentPanel(WizardSelectIndicatorDescriptor.IDENTIFIER); 
 
        // Center to screen.
        wizard.getDialog().setLocationRelativeTo(null);
    }
    
    public void updateScanningSpeed(int speed) {
        this.realTimeStockMonitor.setDelay(speed);
    }

    public void initStockHistoryMonitor(java.util.List<StockServerFactory> stockServerFactories) {
        if(stockHistoryMonitor != null) {
            final StockHistoryMonitor oldStockHistoryMonitor = stockHistoryMonitor;
            Utils.getZoombiePool().execute(new Runnable() {
                public void run() {
                    log.info("Prepare to shut down " + oldStockHistoryMonitor + "...");
                    oldStockHistoryMonitor.clearStockCodes();
                    oldStockHistoryMonitor.dettachAll();
                    oldStockHistoryMonitor.stop();
                    log.info("Shut down " + oldStockHistoryMonitor + " peacefully.");
                }
            });
        }

        this.stockHistoryMonitor = new StockHistoryMonitor(NUM_OF_THREADS_HISTORY_MONITOR);

        for(StockServerFactory factory : stockServerFactories) {
            stockHistoryMonitor.addStockServerFactory(factory);
        }

        stockHistoryMonitor.attach(stockHistoryMonitorObserver);

        // No StockHistorySerializer at this moment, either read or write. This is because
        // (1) Read - If the duration of the history selected in Real-Time panel is shorter than
        // indicators's, we will be in trouble.
        // (2) Write - If the duration of the indicator's is shorter than Real-Time panel, we will
        // be in trouble again.
        //
        // Currently, we have no way but disable it.
    }

    private void update(StockHistoryMonitor monitor, StockHistoryMonitor.StockHistoryRunnable runnable)
    {
        final MainFrame m = this.getMainFrame();
        final Code code = runnable.getCode();

        List<OperatorIndicator> indicators = this.operatorIndicators.get(code);
        if (indicators == null)
        {
            return;
        }

        final StockHistoryServer stockHistoryServer = runnable.getStockHistoryServer();
        if (stockHistoryServer == null)
        {
            // Probably the network is down. Retry infinityly.
            monitor.addStockCode(code);
            return;
        }

        Symbol symbol = m.getStockCodeAndSymbolDatabase().codeToSymbol(code);

        m.setStatusBar(true, "Indicator scanner found " + symbol + " history. Perform calculation...");

        for (OperatorIndicator operatorIndicator : indicators)
        {
            if (operatorIndicator.isStockHistoryServerNeeded())
            {
                operatorIndicator.setStockHistoryServer(stockHistoryServer);
            }

            operatorIndicator.preCalculate();
        }

        // Perform real time monitoring, for the code with history information.
        realTimeStockMonitor.addStockCode(code);
    }

    private org.yccheok.jstock.engine.Observer<StockHistoryMonitor, StockHistoryMonitor.StockHistoryRunnable> getStockHistoryMonitorObserver() {
        return new org.yccheok.jstock.engine.Observer<StockHistoryMonitor, StockHistoryMonitor.StockHistoryRunnable>() {
            @Override
            public void update(StockHistoryMonitor monitor, StockHistoryMonitor.StockHistoryRunnable runnable)
            {
                IndicatorScannerJPanel.this.update(monitor, runnable);
            }
        };
    }

    public void initRealTimeStockMonitor(java.util.List<StockServerFactory> stockServerFactories) {
        if(realTimeStockMonitor != null) {
            final RealTimeStockMonitor oldRealTimeStockMonitor = realTimeStockMonitor;
            Utils.getZoombiePool().execute(new Runnable() {
                public void run() {
                    log.info("Prepare to shut down " + oldRealTimeStockMonitor + "...");
                    oldRealTimeStockMonitor.clearStockCodes();
                    oldRealTimeStockMonitor.dettachAll();
                    oldRealTimeStockMonitor.stop();
                    log.info("Shut down " + oldRealTimeStockMonitor + " peacefully.");
                }
            });
        }
        
        realTimeStockMonitor = new RealTimeStockMonitor(4, 20, MainFrame.getJStockOptions().getScanningSpeed());
        
        for(StockServerFactory factory : stockServerFactories) {
            realTimeStockMonitor.addStockServerFactory(factory);
        }
        
        realTimeStockMonitor.attach(this.realTimeStockMonitorObserver);
    }
    
    // This is the workaround to overcome Erasure by generics. We are unable to make MainFrame to
    // two observers at the same time.
    private org.yccheok.jstock.engine.Observer<RealTimeStockMonitor, java.util.List<Stock>> getRealTimeStockMonitorObserver() {
        return new org.yccheok.jstock.engine.Observer<RealTimeStockMonitor, java.util.List<Stock>>() {
            public void update(RealTimeStockMonitor monitor, java.util.List<Stock> stocks)
            {
                IndicatorScannerJPanel.this.update(monitor, stocks);
            }
        };
    }
    
    public void update(RealTimeStockMonitor monitor, final java.util.List<Stock> stocks) {
        final MainFrame m = this.getMainFrame();

        if (stocks.size() > 0)
        {
            m.setStatusBar(true, "Indicator scanner is scanning " + stocks.get(0).getSymbol() +"...");
        }

        for(Stock stock : stocks) {
            final java.util.List<OperatorIndicator> indicators = operatorIndicators.get(stock.getCode());
            
            if(indicators == null) continue;
            
            final JStockOptions jStockOptions = MainFrame.getJStockOptions();

            if(jStockOptions.isSingleIndicatorAlert()) {
                for(OperatorIndicator indicator : indicators) {
                    indicator.setStock(stock);

⌨️ 快捷键说明

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