MOA 12.03
Real Time Analytics for Data Streams
PreviewPanel.java
Go to the documentation of this file.
00001 /*
00002  *    PreviewPanel.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 package moa.gui;
00021 
00022 import java.awt.BorderLayout;
00023 import java.awt.event.ActionEvent;
00024 import java.awt.event.ActionListener;
00025 
00026 import javax.swing.JButton;
00027 import javax.swing.JComboBox;
00028 import javax.swing.JLabel;
00029 import javax.swing.JPanel;
00030 
00031 import moa.core.StringUtils;
00032 import moa.tasks.ResultPreviewListener;
00033 import moa.tasks.TaskThread;
00034 
00041 public class PreviewPanel extends JPanel implements ResultPreviewListener {
00042 
00043     private static final long serialVersionUID = 1L;
00044 
00045     public static final String[] autoFreqStrings = {"never", "every second",
00046         "every 5 seconds", "every 10 seconds", "every 30 seconds",
00047         "every minute"};
00048 
00049     public static final int[] autoFreqTimeSecs = {0, 1, 5, 10, 30, 60};
00050 
00051     protected TaskThread previewedThread;
00052 
00053     protected JLabel previewLabel = new JLabel("No preview available");
00054 
00055     protected JButton refreshButton = new JButton("Refresh");
00056 
00057     protected JLabel autoRefreshLabel = new JLabel("Auto refresh: ");
00058 
00059     protected JComboBox autoRefreshComboBox = new JComboBox(autoFreqStrings);
00060 
00061     protected TaskTextViewerPanel textViewerPanel = new TaskTextViewerPanel();
00062 
00063     protected javax.swing.Timer autoRefreshTimer;
00064 
00065     public PreviewPanel() {
00066         this.autoRefreshComboBox.setSelectedIndex(1); // default to 1 sec
00067         JPanel controlPanel = new JPanel();
00068         controlPanel.add(this.previewLabel);
00069         controlPanel.add(this.refreshButton);
00070         controlPanel.add(this.autoRefreshLabel);
00071         controlPanel.add(this.autoRefreshComboBox);
00072         setLayout(new BorderLayout());
00073         add(controlPanel, BorderLayout.NORTH);
00074         add(this.textViewerPanel, BorderLayout.CENTER);
00075         this.refreshButton.addActionListener(new ActionListener() {
00076 
00077             @Override
00078             public void actionPerformed(ActionEvent arg0) {
00079                 refresh();
00080             }
00081         });
00082         this.autoRefreshTimer = new javax.swing.Timer(1000,
00083                 new ActionListener() {
00084 
00085                     @Override
00086                     public void actionPerformed(ActionEvent e) {
00087                         refresh();
00088                     }
00089                 });
00090         this.autoRefreshComboBox.addActionListener(new ActionListener() {
00091 
00092             @Override
00093             public void actionPerformed(ActionEvent arg0) {
00094                 updateAutoRefreshTimer();
00095             }
00096         });
00097         setTaskThreadToPreview(null);
00098     }
00099 
00100     public void refresh() {
00101         if (this.previewedThread != null) {
00102             if (this.previewedThread.isComplete()) {
00103                 setLatestPreview(null);
00104                 disableRefresh();
00105             } else {
00106                 this.previewedThread.getPreview(PreviewPanel.this);
00107             }
00108         }
00109     }
00110 
00111     public void setTaskThreadToPreview(TaskThread thread) {
00112         this.previewedThread = thread;
00113         setLatestPreview(thread != null ? thread.getLatestResultPreview()
00114                 : null);
00115         if (thread == null) {
00116             disableRefresh();
00117         } else if (!thread.isComplete()) {
00118             enableRefresh();
00119         }
00120     }
00121 
00122     public void setLatestPreview(Object preview) {
00123         if ((this.previewedThread != null) && this.previewedThread.isComplete()) {
00124             this.previewLabel.setText("Final result");
00125             Object finalResult = this.previewedThread.getFinalResult();
00126             this.textViewerPanel.setText(finalResult != null ? finalResult.toString() : null);
00127             disableRefresh();
00128         } else {
00129             double grabTime = this.previewedThread != null ? this.previewedThread.getLatestPreviewGrabTimeSeconds()
00130                     : 0.0;
00131             String grabString = grabTime > 0.0 ? (" ("
00132                     + StringUtils.secondsToDHMSString(grabTime) + ")") : "";
00133             this.textViewerPanel.setText(preview != null ? preview.toString()
00134                     : null);
00135             if (preview == null) {
00136                 this.previewLabel.setText("No preview available" + grabString);
00137             } else {
00138                 this.previewLabel.setText("Preview" + grabString);
00139             }
00140         }
00141     }
00142 
00143     public void updateAutoRefreshTimer() {
00144         int autoDelay = autoFreqTimeSecs[this.autoRefreshComboBox.getSelectedIndex()];
00145         if (autoDelay > 0) {
00146             if (this.autoRefreshTimer.isRunning()) {
00147                 this.autoRefreshTimer.stop();
00148             }
00149             this.autoRefreshTimer.setDelay(autoDelay * 1000);
00150             this.autoRefreshTimer.start();
00151         } else {
00152             this.autoRefreshTimer.stop();
00153         }
00154     }
00155 
00156     public void disableRefresh() {
00157         this.refreshButton.setEnabled(false);
00158         this.autoRefreshLabel.setEnabled(false);
00159         this.autoRefreshComboBox.setEnabled(false);
00160         this.autoRefreshTimer.stop();
00161     }
00162 
00163     public void enableRefresh() {
00164         this.refreshButton.setEnabled(true);
00165         this.autoRefreshLabel.setEnabled(true);
00166         this.autoRefreshComboBox.setEnabled(true);
00167         updateAutoRefreshTimer();
00168     }
00169 
00170     @Override
00171     public void latestPreviewChanged() {
00172         setTaskThreadToPreview(this.previewedThread);
00173     }
00174 }
 All Classes Namespaces Files Functions Variables Enumerations