Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

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               serverMap.put(service,new Integer(i));
00234               }
00235 
00236 
00237            serverInfoTable.reTable(tableList);
00238            updateLabel();
00239 
00240         }catch(Exception ioE){}
00241    }
00242 
00243 private void updateTable(int row, String name, String service) {
00244         Vector theLine = getDataLine(name,service);
00245         if(theLine==null) return;
00246         serverInfoTable.setValueAt(theLine,row);
00247         updateLabel();
00248         }
00249 
00250    
00251 private Vector getDataLine(String objectName, String service) {
00252          try {
00253               ServiceInformation.ServiceStatus status = new ServiceInformation.ServiceStatus();
00254               Sct.IS.SctNames.getISRepository().getValue(objectName, status);
00255               Vector thisLine = new Vector();
00256               thisLine.addElement(service);
00257               thisLine.addElement(new Integer(status.done));
00258               thisLine.addElement(new Integer(status.queue));
00259               thisLine.addElement(new Float(status.averageTimeEach));
00260               thisLine.addElement(new Float(status.timeLeft));
00261               return thisLine;
00262         }catch(Exception e){System.err.println("Exception reading SctService object "+objectName+" : "+e.toString());}
00263         return null;
00264         }
00265 
00266 private void updateLabel() {
00267         try {
00268           java.util.Date today = new java.util.Date();
00269           java.text.DateFormat formatter = java.text.DateFormat.getTimeInstance(java.text.DateFormat.DEFAULT,new java.util.Locale("en","GB"));
00270           if(subscribed) {
00271              jLabel2.setForeground(java.awt.Color.black);
00272              jLabel2.setText("Service status last changed at "+formatter.format(today));
00273              }
00274           else {
00275               jLabel2.setForeground(java.awt.Color.red);
00276               jLabel2.setText("IS server is not currently available - updates suspended.");
00277               }
00278           }catch(Exception e){}
00279         }
00280 
00281 
00282 
00283 public class myTableAdapter extends javax.swing.table.AbstractTableModel {
00284     Vector              rows;
00285     public int recordCount;
00286     String[]            columnNames = {"Service","# Done","# Queued","Average Time(s)","Time Left(s)"};
00287     public myTableAdapter() {
00288 
00289          rows = new Vector();
00290          recordCount=0;
00291          }
00292 
00293 
00294 // update the table using externally queried and processed data
00295     public void reTable(Vector theData) {
00296          rows = theData;
00297          recordCount= (theData.size()>=0) ? theData.size() : 0;
00298          fireTableChanged(null); // Tell the listeners a new table has arrived.
00299          return ;
00300     }
00301 
00302 
00303     public String getColumnName(int column) {
00304         if (columnNames[column] != null) {
00305             return columnNames[column];
00306         } else {
00307             return "";
00308         }
00309     }
00310 
00311     public Class getColumnClass(int column) {
00312 //            return String;
00313             return getValueAt(0, column).getClass();
00314 
00315    }
00316 
00317     public boolean isCellEditable(int row, int column) {
00318             return false;
00319 
00320     }
00321 
00322     public int getColumnCount() {
00323         return columnNames.length;
00324     }
00325 
00326     // Data methods
00327 
00328     public int getRowCount() {
00329         return rows.size();
00330     }
00331 
00332     public Object getValueAt(int aRow, int aColumn) {
00333         Vector row = (Vector)rows.elementAt(aRow);
00334         return row.elementAt(aColumn);
00335     }
00336     
00337     public void setValueAt(Object value, int row) {
00338         rows.setElementAt(value,row);
00339         fireTableRowsUpdated(row,row);
00340         }
00341 
00342 
00343     public void setValueAt(Object value, int row, int column) {
00344         Vector dataRow = (Vector)rows.elementAt(row);
00345         dataRow.setElementAt(value, column);
00346 
00347     }
00348  }
00349 
00350 
00351 
00352 }

Generated on Thu Feb 3 17:42:22 2005 for SCT DAQ/DCS Software - Java by doxygen 1.3.5