MOA 12.03
Real Time Analytics for Data Streams
TaskManagerPanel.java
Go to the documentation of this file.
00001 /*
00002  *    TaskManagerPanel.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
00005  *    @author Manuel Martín (msalvador@bournemouth.ac.uk)
00006  *
00007  *    This program is free software; you can redistribute it and/or modify
00008  *    it under the terms of the GNU General Public License as published by
00009  *    the Free Software Foundation; either version 3 of the License, or
00010  *    (at your option) any later version.
00011  *
00012  *    This program is distributed in the hope that it will be useful,
00013  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *    GNU General Public License for more details.
00016  *
00017  *    You should have received a copy of the GNU General Public License
00018  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00019  *    
00020  */
00021 package moa.gui;
00022 
00023 import java.awt.BorderLayout;
00024 import java.awt.Component;
00025 import java.awt.Dimension;
00026 import java.awt.event.ActionEvent;
00027 import java.awt.event.ActionListener;
00028 import java.awt.event.MouseAdapter;
00029 import java.awt.event.MouseEvent;
00030 import java.awt.Toolkit;
00031 import java.awt.datatransfer.Clipboard;
00032 import java.awt.datatransfer.StringSelection;
00033 import java.io.BufferedWriter;
00034 import java.io.File;
00035 import java.io.FileWriter;
00036 import java.io.IOException;
00037 import java.io.PrintWriter;
00038 import java.util.ArrayList;
00039 import java.util.List;
00040 
00041 import javax.swing.JButton;
00042 import javax.swing.JFileChooser;
00043 import javax.swing.JFrame;
00044 import javax.swing.JMenuItem;
00045 import javax.swing.JOptionPane;
00046 import javax.swing.JPanel;
00047 import javax.swing.JPopupMenu;
00048 import javax.swing.JProgressBar;
00049 import javax.swing.JScrollPane;
00050 import javax.swing.JTable;
00051 import javax.swing.JTextField;
00052 import javax.swing.SwingConstants;
00053 import javax.swing.UIManager;
00054 import javax.swing.event.ListSelectionEvent;
00055 import javax.swing.event.ListSelectionListener;
00056 import javax.swing.table.AbstractTableModel;
00057 import javax.swing.table.DefaultTableCellRenderer;
00058 import javax.swing.table.TableCellRenderer;
00059 
00060 import moa.core.StringUtils;
00061 import moa.options.ClassOption;
00062 import moa.options.OptionHandler;
00063 import moa.tasks.EvaluatePrequential;
00064 import moa.tasks.MainTask;
00065 import moa.tasks.Task;
00066 import moa.tasks.TaskThread;
00067 
00074 public class TaskManagerPanel extends JPanel {
00075 
00076     private static final long serialVersionUID = 1L;
00077 
00078     public static final int MILLISECS_BETWEEN_REFRESH = 600;
00079 
00080     public static String exportFileExtension = "log";
00081 
00082     public class ProgressCellRenderer extends JProgressBar implements
00083             TableCellRenderer {
00084 
00085         private static final long serialVersionUID = 1L;
00086 
00087         public ProgressCellRenderer() {
00088             super(SwingConstants.HORIZONTAL, 0, 10000);
00089             setBorderPainted(false);
00090             setStringPainted(true);
00091         }
00092 
00093         @Override
00094         public Component getTableCellRendererComponent(JTable table,
00095                 Object value, boolean isSelected, boolean hasFocus, int row,
00096                 int column) {
00097             if (value instanceof Double) {
00098                 double frac = ((Double) value).doubleValue();
00099                 if (frac >= 0.0) {
00100                     setIndeterminate(false);
00101                     setValue((int) (frac * 10000.0));
00102                     setString(StringUtils.doubleToString(frac * 100.0, 2, 2));
00103                 } else {
00104                     setValue(0);
00105                     setIndeterminate(true);
00106                     setString("?");
00107                 }
00108             }
00109             return this;
00110         }
00111 
00112         @Override
00113         public void validate() {
00114         }
00115 
00116         @Override
00117         public void revalidate() {
00118         }
00119 
00120         @Override
00121         protected void firePropertyChange(String propertyName, Object oldValue,
00122                 Object newValue) {
00123         }
00124 
00125         @Override
00126         public void firePropertyChange(String propertyName, boolean oldValue,
00127                 boolean newValue) {
00128         }
00129     }
00130 
00131     protected class TaskTableModel extends AbstractTableModel {
00132 
00133         private static final long serialVersionUID = 1L;
00134 
00135         @Override
00136         public String getColumnName(int col) {
00137             switch (col) {
00138                 case 0:
00139                     return "command";
00140                 case 1:
00141                     return "status";
00142                 case 2:
00143                     return "time elapsed";
00144                 case 3:
00145                     return "current activity";
00146                 case 4:
00147                     return "% complete";
00148             }
00149             return null;
00150         }
00151 
00152         @Override
00153         public int getColumnCount() {
00154             return 5;
00155         }
00156 
00157         @Override
00158         public int getRowCount() {
00159             return TaskManagerPanel.this.taskList.size();
00160         }
00161 
00162         @Override
00163         public Object getValueAt(int row, int col) {
00164             TaskThread thread = TaskManagerPanel.this.taskList.get(row);
00165             switch (col) {
00166                 case 0:
00167                     return ((OptionHandler) thread.getTask()).getCLICreationString(MainTask.class);
00168                 case 1:
00169                     return thread.getCurrentStatusString();
00170                 case 2:
00171                     return StringUtils.secondsToDHMSString(thread.getCPUSecondsElapsed());
00172                 case 3:
00173                     return thread.getCurrentActivityString();
00174                 case 4:
00175                     return new Double(thread.getCurrentActivityFracComplete());
00176             }
00177             return null;
00178         }
00179 
00180         @Override
00181         public boolean isCellEditable(int row, int col) {
00182             return false;
00183         }
00184     }
00185 
00186     protected MainTask currentTask = new EvaluatePrequential();//LearnModel();
00187 
00188     protected List<TaskThread> taskList = new ArrayList<TaskThread>();
00189 
00190     protected JButton configureTaskButton = new JButton("Configure");
00191 
00192     protected JTextField taskDescField = new JTextField();
00193 
00194     protected JButton runTaskButton = new JButton("Run");
00195 
00196     protected TaskTableModel taskTableModel;
00197 
00198     protected JTable taskTable;
00199 
00200     protected JButton pauseTaskButton = new JButton("Pause");
00201 
00202     protected JButton resumeTaskButton = new JButton("Resume");
00203 
00204     protected JButton cancelTaskButton = new JButton("Cancel");
00205 
00206     protected JButton deleteTaskButton = new JButton("Delete");
00207 
00208     protected PreviewPanel previewPanel;
00209 
00210     public TaskManagerPanel() {
00211         this.taskDescField.setText(this.currentTask.getCLICreationString(MainTask.class));
00212         this.taskDescField.setEditable(false);
00213         final Component comp = this.taskDescField;
00214         this.taskDescField.addMouseListener(new MouseAdapter() {
00215 
00216             @Override
00217             public void mouseClicked(MouseEvent evt) {
00218                 if (evt.getClickCount() == 1) {
00219                     if ((evt.getButton() == MouseEvent.BUTTON3)
00220                             || ((evt.getButton() == MouseEvent.BUTTON1) && evt.isAltDown() && evt.isShiftDown())) {
00221                         JPopupMenu menu = new JPopupMenu();
00222                         JMenuItem item;
00223 
00224                         item = new JMenuItem("Copy configuration to clipboard");
00225                         item.addActionListener(new ActionListener() {
00226 
00227                             @Override
00228                             public void actionPerformed(ActionEvent e) {
00229                                 copyClipBoardConfiguration();
00230                             }
00231                         });
00232                         menu.add(item);
00233 
00234 
00235 
00236                         item = new JMenuItem("Save selected tasks to file");
00237                         item.addActionListener(new ActionListener() {
00238 
00239                             @Override
00240                             public void actionPerformed(ActionEvent arg0) {
00241                                 saveLogSelectedTasks();
00242                             }
00243                         });
00244                         menu.add(item);
00245 
00246 
00247                         item = new JMenuItem("Enter configuration...");
00248                         item.addActionListener(new ActionListener() {
00249 
00250                             @Override
00251                             public void actionPerformed(ActionEvent arg0) {
00252                                 String newTaskString = JOptionPane.showInputDialog("Insert command line");
00253                                 if (newTaskString != null) {
00254                                     setTaskString(newTaskString);
00255                                 }
00256                             }
00257                         });
00258                         menu.add(item);
00259 
00260                         menu.show(comp, evt.getX(), evt.getY());
00261                     }
00262                 }
00263             }
00264         });
00265 
00266         JPanel configPanel = new JPanel();
00267         configPanel.setLayout(new BorderLayout());
00268         configPanel.add(this.configureTaskButton, BorderLayout.WEST);
00269         configPanel.add(this.taskDescField, BorderLayout.CENTER);
00270         configPanel.add(this.runTaskButton, BorderLayout.EAST);
00271         this.taskTableModel = new TaskTableModel();
00272         this.taskTable = new JTable(this.taskTableModel);
00273         DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
00274         centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
00275         this.taskTable.getColumnModel().getColumn(1).setCellRenderer(
00276                 centerRenderer);
00277         this.taskTable.getColumnModel().getColumn(2).setCellRenderer(
00278                 centerRenderer);
00279         this.taskTable.getColumnModel().getColumn(4).setCellRenderer(
00280                 new ProgressCellRenderer());
00281         JPanel controlPanel = new JPanel();
00282         controlPanel.add(this.pauseTaskButton);
00283         controlPanel.add(this.resumeTaskButton);
00284         controlPanel.add(this.cancelTaskButton);
00285         controlPanel.add(this.deleteTaskButton);
00286         setLayout(new BorderLayout());
00287         add(configPanel, BorderLayout.NORTH);
00288         add(new JScrollPane(this.taskTable), BorderLayout.CENTER);
00289         add(controlPanel, BorderLayout.SOUTH);
00290         this.taskTable.getSelectionModel().addListSelectionListener(
00291                 new ListSelectionListener() {
00292 
00293                     @Override
00294                     public void valueChanged(ListSelectionEvent arg0) {
00295                         taskSelectionChanged();
00296                     }
00297                 });
00298         this.configureTaskButton.addActionListener(new ActionListener() {
00299 
00300             @Override
00301             public void actionPerformed(ActionEvent arg0) {
00302                 String newTaskString = ClassOptionSelectionPanel.showSelectClassDialog(TaskManagerPanel.this,
00303                         "Configure task", MainTask.class,
00304                         TaskManagerPanel.this.currentTask.getCLICreationString(MainTask.class),
00305                         null);
00306                 setTaskString(newTaskString);
00307             }
00308         });
00309         this.runTaskButton.addActionListener(new ActionListener() {
00310 
00311             @Override
00312             public void actionPerformed(ActionEvent arg0) {
00313                 runTask((Task) TaskManagerPanel.this.currentTask.copy());
00314             }
00315         });
00316         this.pauseTaskButton.addActionListener(new ActionListener() {
00317 
00318             @Override
00319             public void actionPerformed(ActionEvent arg0) {
00320                 pauseSelectedTasks();
00321             }
00322         });
00323         this.resumeTaskButton.addActionListener(new ActionListener() {
00324 
00325             @Override
00326             public void actionPerformed(ActionEvent arg0) {
00327                 resumeSelectedTasks();
00328             }
00329         });
00330         this.cancelTaskButton.addActionListener(new ActionListener() {
00331 
00332             @Override
00333             public void actionPerformed(ActionEvent arg0) {
00334                 cancelSelectedTasks();
00335             }
00336         });
00337         this.deleteTaskButton.addActionListener(new ActionListener() {
00338 
00339             @Override
00340             public void actionPerformed(ActionEvent arg0) {
00341                 deleteSelectedTasks();
00342             }
00343         });
00344 
00345         javax.swing.Timer updateListTimer = new javax.swing.Timer(
00346                 MILLISECS_BETWEEN_REFRESH, new ActionListener() {
00347 
00348             @Override
00349             public void actionPerformed(ActionEvent e) {
00350                 TaskManagerPanel.this.taskTable.repaint();
00351             }
00352         });
00353         updateListTimer.start();
00354         setPreferredSize(new Dimension(0, 200));
00355     }
00356 
00357     public void setPreviewPanel(PreviewPanel previewPanel) {
00358         this.previewPanel = previewPanel;
00359     }
00360 
00361     public void setTaskString(String cliString) {
00362         try {
00363             this.currentTask = (MainTask) ClassOption.cliStringToObject(
00364                     cliString, MainTask.class, null);
00365             this.taskDescField.setText(this.currentTask.getCLICreationString(MainTask.class));
00366         } catch (Exception ex) {
00367             GUIUtils.showExceptionDialog(this, "Problem with task", ex);
00368         }
00369     }
00370 
00371     public void runTask(Task task) {
00372         TaskThread thread = new TaskThread(task);
00373         this.taskList.add(0, thread);
00374         this.taskTableModel.fireTableDataChanged();
00375         this.taskTable.setRowSelectionInterval(0, 0);
00376         thread.start();
00377     }
00378 
00379     public void taskSelectionChanged() {
00380         TaskThread[] selectedTasks = getSelectedTasks();
00381         if (selectedTasks.length == 1) {
00382             setTaskString(((OptionHandler) selectedTasks[0].getTask()).getCLICreationString(MainTask.class));
00383             if (this.previewPanel != null) {
00384                 this.previewPanel.setTaskThreadToPreview(selectedTasks[0]);
00385             }
00386         } else {
00387             this.previewPanel.setTaskThreadToPreview(null);
00388         }
00389     }
00390 
00391     public TaskThread[] getSelectedTasks() {
00392         int[] selectedRows = this.taskTable.getSelectedRows();
00393         TaskThread[] selectedTasks = new TaskThread[selectedRows.length];
00394         for (int i = 0; i < selectedRows.length; i++) {
00395             selectedTasks[i] = this.taskList.get(selectedRows[i]);
00396         }
00397         return selectedTasks;
00398     }
00399 
00400     public void pauseSelectedTasks() {
00401         TaskThread[] selectedTasks = getSelectedTasks();
00402         for (TaskThread thread : selectedTasks) {
00403             thread.pauseTask();
00404         }
00405     }
00406 
00407     public void resumeSelectedTasks() {
00408         TaskThread[] selectedTasks = getSelectedTasks();
00409         for (TaskThread thread : selectedTasks) {
00410             thread.resumeTask();
00411         }
00412     }
00413 
00414     public void cancelSelectedTasks() {
00415         TaskThread[] selectedTasks = getSelectedTasks();
00416         for (TaskThread thread : selectedTasks) {
00417             thread.cancelTask();
00418         }
00419     }
00420 
00421     public void deleteSelectedTasks() {
00422         TaskThread[] selectedTasks = getSelectedTasks();
00423         for (TaskThread thread : selectedTasks) {
00424             thread.cancelTask();
00425             this.taskList.remove(thread);
00426         }
00427         this.taskTableModel.fireTableDataChanged();
00428     }
00429 
00430     public void copyClipBoardConfiguration() {
00431 
00432         StringSelection selection = new StringSelection(this.taskDescField.getText().trim());
00433         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
00434         clipboard.setContents(selection, selection);
00435 
00436     }
00437 
00438     public void saveLogSelectedTasks() {
00439         String tasksLog = "";
00440         TaskThread[] selectedTasks = getSelectedTasks();
00441         for (TaskThread thread : selectedTasks) {
00442             tasksLog += ((OptionHandler) thread.getTask()).getCLICreationString(MainTask.class) + "\n";
00443         }
00444 
00445         JFileChooser fileChooser = new JFileChooser();
00446         fileChooser.setAcceptAllFileFilterUsed(true);
00447         fileChooser.addChoosableFileFilter(new FileExtensionFilter(
00448                 exportFileExtension));
00449         if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
00450             File chosenFile = fileChooser.getSelectedFile();
00451             String fileName = chosenFile.getPath();
00452             if (!chosenFile.exists()
00453                     && !fileName.endsWith(exportFileExtension)) {
00454                 fileName = fileName + "." + exportFileExtension;
00455             }
00456             try {
00457                 PrintWriter out = new PrintWriter(new BufferedWriter(
00458                         new FileWriter(fileName)));
00459                 out.write(tasksLog);
00460                 out.close();
00461             } catch (IOException ioe) {
00462                 GUIUtils.showExceptionDialog(
00463                         this,
00464                         "Problem saving file " + fileName, ioe);
00465             }
00466         }
00467     }
00468 
00469     private static void createAndShowGUI() {
00470 
00471         // Create and set up the window.
00472         JFrame frame = new JFrame("Test");
00473         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00474 
00475         // Create and set up the content pane.
00476         JPanel panel = new TaskManagerPanel();
00477         panel.setOpaque(true); // content panes must be opaque
00478         frame.setContentPane(panel);
00479 
00480         // Display the window.
00481         frame.pack();
00482         // frame.setSize(400, 400);
00483         frame.setVisible(true);
00484     }
00485 
00486     public static void main(String[] args) {
00487         try {
00488             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
00489             javax.swing.SwingUtilities.invokeLater(new Runnable() {
00490 
00491                 @Override
00492                 public void run() {
00493                     createAndShowGUI();
00494                 }
00495             });
00496         } catch (Exception e) {
00497             e.printStackTrace();
00498         }
00499     }
00500 }
 All Classes Namespaces Files Functions Variables Enumerations