2013年9月15日日曜日

Handle IContext.openChart

Handle IContext.openChart
IContext.openChartのハンドル

Consider a program which opens a chart whenever the strategy calls IContext.openChart and closes a chart whenever the strategy call IContext.closeChart:
ストラテジがIContext.openChartをコールするごとにチャートを開くか、ストラテジがIContext.closeChartをコールするごとにチャートを閉じるか、ということをプログラムとして検討して欲しい。

client.addClientGUIListener(new IClientGUIListener() {  
    @Override
    public void onOpenChart(final IClientGUI clientGUI) {
        LOGGER.info("Chart opened from a startegy " + clientGUI.getChart().getFeedDescriptor());
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ChartFrame frame = new ChartFrame(clientGUI, client.getSubscribedInstruments());
                chartFrameMap.put(clientGUI.getChart(), frame);
                //Handle manual close - we need to call IClient.closeChart for strategy to know that the chart is no more there
                frame.addWindowListener(new WindowAdapter(){
                    public void windowClosing(WindowEvent e) {
                        LOGGER.info("Chart manually closed, removing the chart from the strategy context");
                        client.closeChart(clientGUI.getChart());
                        updateOnClose(clientGUI.getChart());
                    }
                });
            }
        });
    }
 
    @Override
    public void onCloseChart(IChart chart) {
        LOGGER.info("Chart closed from a startegy " + chart.getFeedDescriptor());
        //we need to take care of closing the frame ourselves in gui
        ChartFrame frame = chartFrameMap.get(chart);
        frame.dispose();
        updateOnClose(chart);
    }
 
    private void updateOnClose(IChart chart){
        chartFrameMap.remove(chart);
        if(chartFrameMap.isEmpty()){
            LOGGER.info("All charts closed, stopping the program");
            System.exit(0);
        }
    }
});

0 件のコメント:

コメントを投稿