ServiceStatusGUI.java

00001 /*
00002  * ExitPrompt.java
00003  *
00004  */
00005 
00006 package GuiComponents.Panels;
00007 import java.util.Vector;
00008 import java.util.Map;
00009 import java.util.HashMap;
00010 import java.util.regex.*;
00011 import javax.swing.Timer;
00012 import GuiComponents.System.*;
00013 import is.*;
00014 import ServiceInformation.*;
00015 
00020 public class ServiceStatusGUI extends javax.swing.JPanel implements InfoListener {
00021     private myTableAdapter serverInfoTable;
00022     private javax.swing.JTable table;
00023     private javax.swing.JButton cancelButton;
00024     private javax.swing.JLabel jLabel1,jLabel2;
00025     private javax.swing.JScrollPane jScrollPane1;
00026     
00027     private final String serverName = "ServiceStatus";
00028     
00029     public final static int TIME_INTERVAL = 2000;
00030     private Timer timer;
00031     private boolean subscribed=false;
00032 
00033     private Pattern oPattern;
00034 
00035     Map serverMap;
00036 
00037     public static void main(String args[]) {
00038 
00039         javax.swing.JFrame frame = new javax.swing.JFrame("Service Status");
00040         ServiceStatusGUI gui = new ServiceStatusGUI();
00041         frame.getContentPane().add(gui);
00042 
00043         frame.addWindowListener(new java.awt.event.WindowAdapter() {
00044              public void windowClosing(java.awt.event.WindowEvent e) {
00045 //               gui.close();
00046                  System.exit(0);
00047              }
00048          });
00049 
00050         frame.pack();
00051         frame.setVisible(true);
00052     }
00053     
00055     public ServiceStatusGUI() {
00056 
00057         initComponents();
00058         oPattern = Pattern.compile(serverName+"\\.(.*)");
00059         fillTable();
00060         subscribe();
00061         startTimer();
00062     }
00063     
00064     public void startTimer() {
00065        timer = new Timer(TIME_INTERVAL, new java.awt.event.ActionListener() {
00066             public void actionPerformed(java.awt.event.ActionEvent evt) {
00067                try {
00068                  boolean exists = Sct.IS.SctNames.getISRepository().contains(serverName+".anything");
00069                }catch(Exception e) {
00070                  // server does not exist!
00071                  if(subscribed) {
00072                     subscribed=false;
00073                     updateLabel();
00074                     return;
00075                     }
00076                  }
00077                // server does exist!
00078                if(!subscribed) {
00079                  fillTable();
00080                  subscribe();
00081                  }
00082             }
00083         });
00084      timer.setRepeats(true);
00085      timer.start();
00086      }
00087     
00088     public void subscribe() {
00089             subscribed = GuiComponents.System.ISInterface.getInstance().subscribe(serverName, ".*", true, this).matches(".*successful.*");
00090             updateLabel();
00091         }
00092 
00093     public void unsubscribe() {
00094             if(!subscribed) return;
00095             GuiComponents.System.ISInterface.getInstance().unsubscribe(serverName, ".*");
00096         }
00097         
00098         
00099     abstract class StringRunnable implements Runnable {
00100         protected String name;
00101         StringRunnable(String name) {
00102             this.name = name;
00103         }
00104     }
00105 
00106     public void infoCreated(InfoEvent e) {
00107 //      System.out.println("created "+e.getName());
00108        javax.swing.SwingUtilities.invokeLater(new StringRunnable(e.getName()) {
00109           public void run() {
00110              fillTable();
00111              }
00112         });
00113     }
00114 
00115 
00116     public void infoDeleted(InfoEvent e) {
00117        javax.swing.SwingUtilities.invokeLater(new StringRunnable(e.getName()) {
00118           public void run() {
00119             fillTable();
00120           }
00121         });
00122     }
00123 
00124     public void infoUpdated(InfoEvent e) {
00125 //        System.out.println("updated "+e.getName());
00126         javax.swing.SwingUtilities.invokeLater(new StringRunnable(e.getName()) {
00127           public void run() {
00128             Matcher matcher = oPattern.matcher(name);
00129             if(!matcher.matches()) return;
00130             String service = name.substring(matcher.start(1),matcher.end(1));
00131             if(!serverMap.containsKey(service)) {  // must be a new object! (somwehow...)
00132                fillTable();
00133                return;
00134                }
00135             int row = ((Integer)serverMap.get(service)).intValue();
00136             updateTable(row,name,service);
00137             }
00138         });
00139     }
00140     
00141 
00142     private void initComponents() {
00143 
00144         java.awt.Font displayScaleFont = new java.awt.Font("Arial",java.awt.Font.PLAIN,10);
00145 
00146         setBackground(new java.awt.Color(255, 255, 255));
00147 
00148         serverInfoTable = new myTableAdapter();
00149         table = new javax.swing.JTable(serverInfoTable);
00150         jScrollPane1 = new javax.swing.JScrollPane(table);
00151         table.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
00152 
00153         java.awt.GridBagConstraints gridBagConstraints;
00154 
00155 //        buttonGroup1 = new javax.swing.ButtonGroup();
00156         jLabel1 = new javax.swing.JLabel();
00157         jLabel2 = new javax.swing.JLabel();
00158         jScrollPane1 = new javax.swing.JScrollPane(table);
00159         cancelButton = new javax.swing.JButton();
00160 
00161         setLayout(new java.awt.GridBagLayout());
00162 
00163 
00164         jLabel1.setText("Service Status");
00165         jLabel1.setFont( new java.awt.Font("Arial",java.awt.Font.BOLD,16));
00166         jLabel1.setForeground(new java.awt.Color(51, 0, 153));
00167         gridBagConstraints = new java.awt.GridBagConstraints();
00168         gridBagConstraints.gridx = 0;
00169         gridBagConstraints.gridy = 0;
00170         gridBagConstraints.gridwidth = 2;
00171         gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
00172         gridBagConstraints.weighty = 0.1;
00173         add(jLabel1, gridBagConstraints);
00174 
00175         jLabel2.setText("Last update: Not yet updated.");
00176         jLabel2.setFont(displayScaleFont);
00177         gridBagConstraints = new java.awt.GridBagConstraints();
00178         gridBagConstraints.gridx = 0;
00179         gridBagConstraints.gridy = 1;
00180         gridBagConstraints.gridwidth = 2;
00181         gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
00182         gridBagConstraints.weighty = 0.1;
00183         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
00184         add(jLabel2, gridBagConstraints);
00185 
00186         jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
00187         table.setBackground(new java.awt.Color(255, 255, 255));
00188         table.setFont(displayScaleFont);
00189         jScrollPane1.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.LOWERED));
00190         jScrollPane1.setMaximumSize(new java.awt.Dimension(800, 300));
00191         jScrollPane1.setMinimumSize(new java.awt.Dimension(300, 100));
00192         jScrollPane1.setPreferredSize(new java.awt.Dimension(400, 100));
00193         gridBagConstraints = new java.awt.GridBagConstraints();
00194         gridBagConstraints.gridx = 0;
00195         gridBagConstraints.gridy = 2;
00196         gridBagConstraints.gridwidth = 2;
00197         gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
00198         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
00199         gridBagConstraints.weighty = 0.8;
00200         add(jScrollPane1, gridBagConstraints);
00201 
00202 
00203 
00204 
00205     }
00206 
00207 
00208     private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
00209         unsubscribe();
00210     }   
00211 
00212 
00213   public void fillTable() {
00214 
00215        serverMap = new HashMap();
00216        try {
00217            Vector tableList = new Vector();
00218            Vector theLine = new Vector();
00219 
00220            String[] objects = GuiComponents.System.ISInterface.getInstance().getISObjects(serverName);
00221            for(int i=0;i<objects.length;i++) {
00222              Matcher matcher = oPattern.matcher(objects[i]);
00223              String service = matcher.matches() ? objects[i].substring(matcher.start(1),matcher.end(1)) : null;
00224              if(service==null) {
00225                 System.err.println("SctGUI - failed to match object name "+objects[i]);
00226                 continue;
00227                 }
00228                 
00229               Vector thisLine = getDataLine(objects[i],service);
00230               if(thisLine==null) continue;
00231               
00232               tableList.addElement(thisLine);
00233               
00234               serverMap.put(service,new Integer(i));
00235            }
00236            serverInfoTable.reTable(tableList);
00237            updateLabel();
00238 
00239         }catch(Exception ioE){}
00240    }
00241 
00242 private void updateTable(int row, String name, String service) {
00243         Vector theLine = getDataLine(name,service);
00244         if(theLine==null) return;
00245         serverInfoTable.setValueAt(theLine,row);
00246         updateLabel();
00247         }
00248 
00249    
00250 private Vector getDataLine(String objectName, String service) {
00251          try {
00252               ServiceInformation.ServiceStatus status = new ServiceInformation.ServiceStatus();
00253               Sct.IS.SctNames.getISRepository().getValue(objectName, status);
00254               Vector thisLine = new Vector();
00255               thisLine.addElement(service);
00256               thisLine.addElement(new Integer(status.done));
00257               thisLine.addElement(new Integer(status.queue));
00258               thisLine.addElement(new Float(status.averageTimeEach));
00259               Float left=new Float(status.timeLeft);
00260               Integer hours = new Integer(left.intValue()/3600);
00261               Integer mins = new Integer(left.intValue()%3600/60);
00262               Integer secs = new Integer(left.intValue()%60);
00263               thisLine.addElement(new String(" "+hours+"h "+mins+"m "+secs+"s"));
00264               return thisLine;
00265         }catch(Exception e){System.err.println("Exception reading SctService object "+objectName+" : "+e.toString());}
00266         return null;
00267         }
00268 
00269 private void updateLabel() {
00270         try {
00271           java.util.Date today = new java.util.Date();
00272           java.text.DateFormat formatter = java.text.DateFormat.getTimeInstance(java.text.DateFormat.DEFAULT,new java.util.Locale("en","GB"));
00273           if(subscribed) {
00274              jLabel2.setForeground(java.awt.Color.black);
00275              jLabel2.setText("Service status last changed at "+formatter.format(today));
00276              }
00277           else {
00278               jLabel2.setForeground(java.awt.Color.red);
00279               jLabel2.setText("IS server is not currently available - updates suspended.");
00280               }
00281           }catch(Exception e){}
00282         }
00283 
00284 
00285 
00286 public class myTableAdapter extends javax.swing.table.AbstractTableModel {
00287     Vector              rows;
00288     public int recordCount;
00289     String[]            columnNames = {"Service","# Done","# Queued","Av.Time(s)","Time Left"};
00290     public myTableAdapter() {
00291 
00292          rows = new Vector();
00293          recordCount=0;
00294          }
00295 
00296 
00297 // update the table using externally queried and processed data
00298     public void reTable(Vector theData) {
00299          rows = theData;
00300          recordCount= (theData.size()>=0) ? theData.size() : 0;
00301          fireTableChanged(null); // Tell the listeners a new table has arrived.
00302          return ;
00303     }
00304 
00305 
00306     public String getColumnName(int column) {
00307         if (columnNames[column] != null) {
00308             return columnNames[column];
00309         } else {
00310             return "";
00311         }
00312     }
00313 
00314     public Class getColumnClass(int column) {
00315 //            return String;
00316             return getValueAt(0, column).getClass();
00317 
00318    }
00319 
00320     public boolean isCellEditable(int row, int column) {
00321             return false;
00322 
00323     }
00324 
00325     public int getColumnCount() {
00326         return columnNames.length;
00327     }
00328 
00329     // Data methods
00330 
00331     public int getRowCount() {
00332         return rows.size();
00333     }
00334 
00335     public Object getValueAt(int aRow, int aColumn) {
00336         Vector row = (Vector)rows.elementAt(aRow);
00337         return row.elementAt(aColumn);
00338     }
00339     
00340     public void setValueAt(Object value, int row) {
00341         rows.setElementAt(value,row);
00342         fireTableRowsUpdated(row,row);
00343         }
00344 
00345 
00346     public void setValueAt(Object value, int row, int column) {
00347         Vector dataRow = (Vector)rows.elementAt(row);
00348         dataRow.setElementAt(value, column);
00349 
00350     }
00351  }
00352 
00353 
00354 
00355 }

Generated on Mon Feb 6 14:12:19 2006 for SCT DAQ/DCS Software - Java by  doxygen 1.4.6