home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / statusbar.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  7.6 KB  |  278 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.     Written by the Personal Journal developers of Dow Jones & Company, Inc.
  4.  
  5.     Dow Jones makes no representations or warranties about 
  6.     the suitability of this software, either express or 
  7.     implied, including but not limited to the implied warranties 
  8.     of merchantability, fitness for a particular purpose, 
  9.     or non-infringement.  Dow Jones will not be liable for 
  10.     any damages suffered by a user as a result of using, 
  11.     modifying or distributing this software or its derivatives.
  12.  
  13.  
  14.     @(#)StatusBar.java  0.00 08-Mar-96
  15.  
  16.         Continuos time update class.
  17.  
  18.     Authors:
  19.  
  20.         jlee     James Lee
  21.  
  22.     Version Ident:
  23.  
  24.         $Header: /PjJavaClient/src/pj/awt/StatusBar.java 5     3/22/96 11:13p Jlee $
  25.  
  26.     History:
  27.  
  28.         0.00 08-Mar-96  jlee      Initial Creation
  29.         0.00 25-Mar-96  jlee      Changed StatusBar's super class from Panel to Canvas
  30.                                   for consistent use of Canvas component for drawing.
  31. ---------------------------------------------------------------------------*/
  32.  
  33. package pj.awt;
  34.  
  35. import pj.awt.PjFinals;
  36. import pj.io.DownloadProgressNotification;
  37. import pj.io.Paper;
  38. import pj.io.PaperSection;
  39.  
  40. import java.awt.Canvas;
  41. import java.awt.Color;
  42. import java.awt.Font;
  43. import java.awt.FontMetrics;
  44. import java.awt.Graphics;
  45. import java.lang.Object;
  46. import java.lang.Runnable;
  47. import java.lang.String;
  48. import java.lang.Thread;
  49. import java.util.Observable;
  50. import java.util.Observer;
  51.  
  52. /**
  53.  * Displays status and messages.
  54.  *
  55.  * @version 0.00 08-Mar-96
  56.  * @author James Lee
  57. */
  58. public class StatusBar extends Canvas implements Observer, Runnable
  59. {
  60.     // --- Class variables
  61.     public static int       TEXT = 0;
  62.     public static int       PROGRESS = 1;
  63.     public static int       SLOTDRAW = 2;
  64.  
  65.     private static String   m_strText = null;
  66.     private static int      m_nLeftOffset = 4;
  67.     private static int      m_nLineHeight = 0;
  68.     private static int      m_nFontAscent = 0;
  69.     private static int      m_nMessageType = TEXT;
  70.     private static int      m_nProgressPercent = 0;
  71.  
  72.     // --- Instance variables
  73.     private int             m_nTotalStoryCount = 75;
  74.     private int             m_nStoryCount = 0;
  75.     private int             m_nPrevSlotX = 0;
  76.     private int             m_nSlotX = 0;
  77.     private Thread          m_spThread = null;
  78.  
  79.     // --- Public constructors
  80.  
  81.     public StatusBar()
  82.         {
  83.         setFont( PjFinals.fntStatusBar );
  84.         setForeground( Color.black );
  85.  
  86.         FontMetrics fmText = getFontMetrics(getFont());
  87.         m_nLineHeight = fmText.getHeight();
  88.         m_nFontAscent = fmText.getAscent();
  89.         }
  90.  
  91.     public StatusBar( String strMessage )
  92.         {
  93.         this();
  94.         m_strText = strMessage;
  95.         m_nMessageType = TEXT;
  96.         }
  97.  
  98.     public StatusBar( int nProgressPercent )
  99.         {
  100.         this();
  101.         m_nProgressPercent = nProgressPercent;
  102.         m_nMessageType = PROGRESS;
  103.         }
  104.  
  105.     // --- Public operations
  106.     public void setMessage( String str )
  107.         {
  108.         m_strText = str;
  109.         m_nMessageType = TEXT;
  110.         repaint();
  111.         }
  112.  
  113.     public void eraseMessage()
  114.         {
  115.         m_strText = null;
  116.         m_nMessageType = TEXT;
  117.         repaint();
  118.         }
  119.  
  120.     public void setProgressPercent( int nProgressPercent )
  121.         {
  122.         m_nProgressPercent = nProgressPercent;
  123.         m_nMessageType = PROGRESS;
  124.         repaint();
  125.         }
  126.  
  127.     public void setType( int type )
  128.         {
  129.         m_nMessageType = type;
  130.         }
  131.  
  132.     public void resetProgress()
  133.         {
  134.         m_nStoryCount = 0;
  135.         m_nProgressPercent = 0;
  136.         m_nMessageType = PROGRESS;
  137.         repaint();
  138.         }
  139.  
  140.     public void update(Observable o, Object arg)
  141.         {
  142.         if ( !(arg instanceof DownloadProgressNotification) )
  143.             return;
  144.  
  145.         /*
  146.         if ( null == m_spThread )
  147.             {
  148.             m_nSlotX = 0;
  149.             m_nPrevSlotX = 0;
  150.             m_spThread = new Thread( this );
  151.             m_spThread.start();
  152.             }
  153.         */
  154.  
  155.         DownloadProgressNotification dpnProgress = (DownloadProgressNotification)arg;
  156.  
  157.  
  158.         if ( dpnProgress.newState == PaperSection.stateAdding )
  159.             setProgressPercent( 100 * ++m_nStoryCount / m_nTotalStoryCount );
  160.  
  161.  
  162.         if ( dpnProgress.strSubsect.equals( Paper.idPcolEOT ) )
  163.             resetProgress();
  164.  
  165.         /*
  166.         if ( dpnProgress.strSubsect.equals(Paper.idPcolEOT) )
  167.             {
  168.             m_spThread.stop();
  169.             m_spThread = null;
  170.             super.repaint();
  171.             }
  172.         */
  173.  
  174.         } // update
  175.  
  176.     public void update( Graphics g )
  177.         {
  178.         if ( m_nMessageType != SLOTDRAW )
  179.             super.update( g );
  180.  
  181.         paint( g );
  182.         }
  183.  
  184.     public void run()
  185.         {
  186.         m_nMessageType = SLOTDRAW;
  187.  
  188.         while(true)
  189.             {
  190.             repaint();
  191.  
  192.             try
  193.                 {
  194.                 Thread.sleep(20);
  195.                 }
  196.             catch (InterruptedException e)
  197.                 {
  198.                 }
  199.             }
  200.         } //run
  201.  
  202.     public synchronized void paint( Graphics g )
  203.         {
  204.         int nHeight = size().height - 4;
  205.         int nLength;
  206.         int nWidth;
  207.         int nDir = 4;
  208.         int nSlotSize;
  209.  
  210.         switch ( m_nMessageType )
  211.             {
  212.             case 0: //TEXT
  213.                 if ( m_strText != null )
  214.                     g.drawString( m_strText, m_nLeftOffset, (size().height - m_nLineHeight) / 2 + m_nFontAscent);
  215.                 else
  216.                     g.drawString( "", m_nLeftOffset, (size().height - m_nLineHeight) / 2 + m_nFontAscent);
  217.                 break;
  218.  
  219.             case 1: //PROGRESS
  220.                 nWidth = (size().width - m_nLeftOffset * 2)  / 100;
  221.                 int nWidthRemainder = size().width - nWidth * 100;
  222.                 int nAccumulatedWidth = 0;
  223.                 int nAccumulatedRemainder = 0;
  224.  
  225.                 g.setColor( Color.darkGray );
  226.  
  227.                 for ( int i = 0; i < m_nProgressPercent; i++ )
  228.                     {
  229.                     nAccumulatedRemainder += nWidthRemainder;
  230.  
  231.                     if ( nAccumulatedRemainder >= 100 )
  232.                         {
  233.                         nAccumulatedRemainder -= 100;
  234.                         nAccumulatedWidth++;
  235.                         }
  236.  
  237.                     if ( i % 2  == 0 )
  238.                         g.fillRect( m_nLeftOffset + nAccumulatedWidth, 2, nWidth, nHeight );
  239.  
  240.                     nAccumulatedWidth += nWidth;
  241.                     }
  242.                 break;
  243.  
  244.             case 2: //Draw One slot
  245.                 nLength = size().width;
  246.                 nWidth  = nLength  / 100;
  247.                 nSlotSize = 20 * nWidth;
  248.  
  249.                 if ( m_nSlotX < 0 )
  250.                     {
  251.                     m_nSlotX = 0;
  252.                     nDir = 4;
  253.                     }
  254.                 else
  255.                 if ( m_nPrevSlotX % (nLength - nSlotSize) > m_nSlotX % (nLength - nSlotSize) )
  256.                     {
  257.                     nDir = -4;
  258.                     m_nSlotX += nDir;
  259.                     }
  260.  
  261.                 g.setColor( Color.lightGray );
  262.                 g.fillRect( m_nPrevSlotX, 2, nSlotSize, nHeight );
  263.  
  264.                 g.setColor( Color.black );
  265.                 g.fillRect( m_nSlotX, 2, nSlotSize, nHeight );
  266.  
  267.                 m_nPrevSlotX = m_nSlotX;
  268.                 m_nSlotX += nDir;
  269.                 break;
  270.  
  271.             default:
  272.                 break;
  273.             }
  274.         }
  275.     } //StatusBar
  276.  
  277.  
  278.