Save This Page
Home » openjdk-7 » net.bereza » gui » [javadoc | source]
    1   /*
    2    *  Copyright (C) 2000  Bill Bereza
    3    *
    4    *  This program is free software; you can redistribute it and/or modify
    5    *  it under the terms of the GNU General Public License as published by
    6    *  the Free Software Foundation; either version 2 of the License, or
    7    *  (at your option) any later version.
    8    *
    9    *  This program is distributed in the hope that it will be useful,
   10    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   11    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12    *  GNU General Public License for more details.
   13    *
   14    *  You should have received a copy of the GNU General Public License
   15    *  along with this program; if not, write to the Free Software
   16    *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   17    *
   18    *  Author: Bill Bereza
   19    *  email : bereza@pobox.com
   20    *  url   : http://www.pobox.com/~bereza/
   21    */
   22   package net.bereza.gui;
   23   
   24   import java.io;
   25   import java.util;
   26   import java.text.DateFormat;
   27   
   28   import java.awt;
   29   import java.awt.event;
   30   import javax.swing;
   31   import javax.swing.event;
   32   import javax.swing.text;
   33   import javax.swing.border;
   34   
   35   /**
   36    * SETIPanel watches state.* files from the seti@home Unix software.
   37    *
   38    * @author $Author: bereza $
   39    * @version $Revision: 1.4 $
   40    * <p>
   41    * $Log: SETIPanel.java,v $
   42    * Revision 1.4  2001/07/27 15:38:21  bereza
   43    * added timesofar, completions, etc.
   44    *
   45    * Revision 1.3  2000/03/30 01:57:22  bereza
   46    * Added copyright header
   47    *
   48    * Revision 1.2  2000/03/29 02:10:27  bereza
   49    * *** empty log message ***
   50    *
   51    * Revision 1.1  2000/02/26 16:43:21  bereza
   52    * Initial revision
   53    *
   54    *
   55    * <p>
   56    * 	$Id: SETIPanel.java,v 1.4 2001/07/27 15:38:21 bereza Exp $
   57    */
   58   public class SETIPanel extends JPanel implements Runnable
   59   {
   60     public static final int MAX_PROG=1000;
   61     
   62     protected File[] states;
   63     protected JProgressBar[] stateProgs;
   64   	protected JLabel[] timeSoFar;
   65   	protected JLabel[] completions;
   66   	protected JLabel[] times;
   67   
   68     protected long granularity=60000L;
   69   
   70     private boolean keepRunning=true;
   71   
   72     private Thread checker;
   73   
   74     /**
   75      * Construct a SETIPanel which shows the progress of
   76      * the SETI state files specified.
   77      * @param files SETI state files
   78      */
   79     public SETIPanel(File []files)
   80     {
   81       super();
   82       
   83       setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
   84   
   85       states=files;
   86       stateProgs=new JProgressBar[states.length];
   87   
   88   		timeSoFar=new JLabel[states.length];
   89   		completions=new JLabel[states.length];
   90   		times=new JLabel[states.length];
   91   
   92       JPanel left=new JPanel();
   93       left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
   94       left.setOpaque(false);
   95       
   96       JPanel right=new JPanel();
   97       right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
   98       right.setOpaque(false);
   99       
  100       for(int i=0;i<states.length;i++)
  101       {
  102         stateProgs[i]=new JProgressBar(0, MAX_PROG);
  103         stateProgs[i].setOpaque(false);
  104   
  105   			timeSoFar[i]=new JLabel("");
  106   			completions[i]=new JLabel("");
  107   			times[i]=new JLabel("");
  108         
  109         JLabel stateName=new JLabel(states[i].getPath());
  110   
  111         stateName.setOpaque(false);
  112         
  113         left.add(stateName);
  114   			left.add(new JLabel("Time so far"));
  115   			left.add(new JLabel("Projected completion date"));
  116   			left.add(new JLabel("Projected time / wu"));
  117   			
  118         right.add(stateProgs[i]);
  119   			right.add(timeSoFar[i]);
  120   			right.add(completions[i]);
  121   			right.add(times[i]);
  122   
  123   			if((i + 1)<states.length)
  124   			{
  125   				left.add(new JLabel(" "));
  126   				right.add(new JLabel(" "));
  127   			}
  128   		}
  129   		
  130       add(left);
  131       add(right);
  132   
  133       checker=new Thread(this, "SETIPanel progress checker");
  134       checker.start();
  135     }
  136   
  137     protected synchronized boolean keepRunning()
  138     {
  139       return keepRunning;
  140     }
  141     
  142     public void run()
  143     {
  144       if(checker == Thread.currentThread())
  145       {
  146         while(keepRunning())
  147         {
  148           Properties pf=new Properties();
  149   
  150           for(int i=0;i<states.length;i++)
  151           {
  152             try
  153             {
  154               FileInputStream ps=new FileInputStream(states[i]);
  155   
  156               pf.load(ps);
  157   
  158               try
  159               {
  160                 double progress=new Double(pf.getProperty("prog",
  161                                                           "0")).doubleValue();
  162   
  163   							double cpu=new Double(pf.getProperty("cpu",
  164   																									 "0")).doubleValue();
  165   
  166   							double cputotal=cpu / progress;
  167   
  168   							long hours;
  169   
  170                 stateProgs[i].setValue((int)(progress * (double)MAX_PROG));
  171                 stateProgs[i].setString(((int)(progress * 100.0))+"%");
  172   
  173   							hours=(long)(cpu / 3600);
  174   							timeSoFar[i].setText(hours + " hr "+
  175   																	 (int)((cpu - (hours * 3600)) / 60) +
  176   																	 " min");
  177   							
  178   							completions[i].setText(new Date(System.currentTimeMillis()+
  179   																							(long)(cputotal - cpu)*1000L).toString());
  180   							hours=(long)(cputotal / 3600);
  181   							times[i].setText(hours + " hr "+
  182   															 (int)((cputotal - (hours * 3600)) / 60) +
  183   															 " min");
  184               }
  185               catch(NumberFormatException e)
  186               {
  187                 e.printStackTrace(System.err);
  188               }
  189             
  190               ps.close();
  191             }
  192             catch(IOException e)
  193             {
  194               e.printStackTrace(System.err);
  195             }
  196           } // for
  197   
  198           try
  199           {
  200             Thread.sleep(granularity);
  201           }
  202           catch(InterruptedException e)
  203           {
  204           }
  205         } // while      
  206       } // if checkerThread
  207     }
  208   
  209     
  210     public static void main(String argv[])
  211     {
  212       if(argv.length==0)
  213       {
  214         System.err.println("Usage: java net.bereza.gui.SETIPanel {statefile} ...");
  215         System.exit(1);
  216       }
  217       
  218       JFrame tFrame=new JFrame("SETIPanel");
  219       File states[]=new File[argv.length];
  220   
  221       for(int i=0;i<argv.length;i++)
  222       {
  223         states[i]=new File(argv[i]);
  224       }
  225       
  226       SETIPanel tp=new SETIPanel(states);
  227       
  228       //tFrame.getContentPane().setLayout(new BorderLayout());
  229   
  230       tFrame.getContentPane().add("Center",tp);
  231       
  232       tFrame.addWindowListener(new WindowAdapter()
  233                                {
  234                                  public void windowClosing(WindowEvent e)
  235                                    {
  236                                      System.exit(0);
  237                                    }
  238                                });
  239       
  240       
  241                                
  242       tFrame.pack();
  243       tFrame.show(); 
  244     } 
  245   }
  246     

Save This Page
Home » openjdk-7 » net.bereza » gui » [javadoc | source]