home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / Source.bin / StatusScroller.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  17.1 KB  |  631 lines

  1. package symantec.itools.awt.util;
  2.  
  3. import java.applet.*;
  4. import java.beans.PropertyVetoException;
  5. import java.beans.PropertyChangeListener;
  6. import java.beans.VetoableChangeListener;
  7.  
  8.  
  9. //  Created and implemented by Levi Brown, Symantec Macintosh Internet Tools.
  10. //     04/11/97    LAB    Checked it in
  11. //     04/17/97    LAB    Changed a few names to comply with Java standards.
  12. //                Added automatic initialization of the applet context in the constructor.
  13. //                Changed the function comment style to comply with JavaDoc standards.
  14. //                Took out some unnecessary code in setAppletContext(Applet a).
  15. //                Reorganized the code, to group public and private methods separately.
  16. //                Removed some unnecessary import statements.
  17. //                Fixed a bug in setAppletContext(Applet a) that could cause a NullPointerException.
  18. //                Added set/getAutoStart(boolean f) to allow the scrolling to occur without a direct call to start().
  19. //                Changed the default state of execute to true, so scrolling would automatically occur.
  20. //     06/02/97    LAB    Updated to Java 1.1
  21.  
  22. /**
  23.  * Displays a scrolling message in the status window of a browser or
  24.  * applet viewer.
  25.  *
  26.  * @version 1.1, June 2, 1997
  27.  * @author Symantec
  28.  */
  29. public class StatusScroller implements Runnable
  30. {
  31.     /**
  32.      * Constructs a StatusScroller.
  33.      * By default, the message (set by the setString() method) will scroll
  34.      * right-to-left, repeat, and will completely scroll off before scrolling
  35.      * on again.
  36.      * @see #setString
  37.      */
  38.     public StatusScroller()
  39.     {
  40.         isRightToLeft    = true;
  41.         isRepeat        = true;
  42.         isScrollClean    = true;
  43.         execute            = true;
  44.         loopFlag        = false;
  45.         statusString    = null;
  46.         delay            = 100;
  47.         thread            = new Thread(this);
  48.  
  49.         //Initalize the context variable
  50.         try
  51.         {
  52.             setAppletContext(symantec.itools.lang.Context.getApplet());
  53.         }
  54.         catch(PropertyVetoException e){}
  55.  
  56.         thread.start();
  57.     }
  58.  
  59.  
  60.     //***** Public Methods *****//
  61.  
  62.     /**
  63.      * Sets the string to be scrolled in the browser or applet viewer.
  64.      *
  65.      * @param s the message to scroll
  66.      * @see #getString
  67.      *
  68.      * @exception PropertyVetoException
  69.      * if the specified property value is unacceptable
  70.      */
  71.     public void setString(String s) throws PropertyVetoException
  72.     {
  73.         if(!symantec.itools.util.GeneralUtils.objectsEqual(statusString, s))
  74.         {
  75.             String oldValue = statusString;
  76.  
  77.             vetos.fireVetoableChange("String", oldValue, s);
  78.  
  79.             statusString = s;
  80.             index = 0;
  81.  
  82.             if(statusString == null || statusString.equals(""))
  83.             {
  84.                 statusString =    null;
  85.                 workingBuffer = null;
  86.                 wblength = 0;
  87.                 sslength = 0;
  88.                 if(execute)
  89.                     clear();
  90.             }
  91.             else
  92.             {
  93.                 sslength = statusString.length();
  94.                 updateWorkingBuffer();
  95.             }
  96.  
  97.             changes.firePropertyChange("String", oldValue, statusString);
  98.         }
  99.     }
  100.  
  101.     /**
  102.      * Gets the string being scrolled.
  103.      * @return the current scroll string
  104.      * @see #setString
  105.      */
  106.     public String getString()
  107.     {
  108.         return statusString;
  109.     }
  110.  
  111.     /**
  112.      * Controls the direction the text will scroll.
  113.      * @param b the direction to scroll the text;
  114.      * true for right-to-left, false for left-to-right
  115.      * @see #getRightToLeft
  116.      *
  117.      * @exception PropertyVetoException
  118.      * if the specified property value is unacceptable
  119.      */
  120.     public void setRightToLeft(boolean b) throws PropertyVetoException
  121.     {
  122.         Boolean oldValue = new Boolean(isRightToLeft);
  123.         Boolean newValue = new Boolean(b);
  124.  
  125.         vetos.fireVetoableChange("RightToLeft", oldValue, newValue);
  126.  
  127.         isRightToLeft = b;
  128.  
  129.         changes.firePropertyChange("RightToLeft", oldValue, newValue);
  130.     }
  131.  
  132.     /**
  133.      * Gets the direction the text will scroll.
  134.      * @return true if the text will scroll right-to-left,
  135.      * false if the text will scroll left-to-right
  136.      * @see #setRightToLeft
  137.      */
  138.     public boolean isRightToLeft()
  139.     {
  140.         return isRightToLeft;
  141.     }
  142.  
  143.     /**
  144.      * @deprecated
  145.      * @see #isRightToLeft
  146.      */
  147.     public boolean getRightToLeft()
  148.     {
  149.         return isRightToLeft();
  150.     }
  151.  
  152.     /**
  153.      * Controls whether text will scroll completely off before scrolling on
  154.      * again.
  155.      * @param b if true the text will scroll completely off before scrolling
  156.      * on again, if false the text will scroll without any gap
  157.      * @see #isScrollClean
  158.      *
  159.      * @exception PropertyVetoException
  160.      * if the specified property value is unacceptable
  161.      */
  162.     public void setScrollClean(boolean b) throws PropertyVetoException
  163.     {
  164.         if(isScrollClean != b)
  165.         {
  166.             Boolean oldValue = new Boolean(isScrollClean);
  167.             Boolean newValue = new Boolean(b);
  168.  
  169.             vetos.fireVetoableChange("ScrollClean", oldValue, newValue);
  170.  
  171.             isScrollClean = b;
  172.             updateWorkingBuffer();
  173.  
  174.             changes.firePropertyChange("ScrollClean", oldValue, newValue);
  175.         }
  176.     }
  177.  
  178.     /**
  179.      * Gets whether text will scroll completely off before scrolling on
  180.      * again.
  181.      * @return true if the text will scroll completely off before scrolling
  182.      * on again, false if the text will scroll without any gap
  183.      * @see #setScrollClean
  184.      */
  185.     public boolean isScrollClean()
  186.     {
  187.         return isScrollClean;
  188.     }
  189.  
  190.     /**
  191.      * @deprecated
  192.      * @see #isScrollClean
  193.      */
  194.     public boolean getScrollClean()
  195.     {
  196.         return isScrollClean();
  197.     }
  198.  
  199.     /**
  200.      * Controls whether scrolling will automatically start when the applet is loaded.
  201.      * @param f if true the text will start scrolling as soon as the applet
  202.      * is loaded, if false the text will not scroll until start() is called
  203.      * @see #isAutoStart
  204.      * @see #start
  205.      * @see #stop
  206.      *
  207.      * @exception PropertyVetoException
  208.      * if the specified property value is unacceptable
  209.      */
  210.     public void setAutoStart(boolean f) throws PropertyVetoException
  211.     {
  212.         Boolean oldValue = new Boolean(isAutoStart());
  213.         Boolean newValue = new Boolean(f);
  214.  
  215.         vetos.fireVetoableChange("AutoStart", oldValue, newValue);
  216.  
  217.         if(f)
  218.             start();
  219.         else
  220.             stop();
  221.  
  222.         changes.firePropertyChange("AutoStart", oldValue, newValue);
  223.     }
  224.  
  225.     /**
  226.      * Gets whether scrolling will automatically start when the applet is loaded.
  227.      * @return true if the text will start scrolling as soon as the applet is loaded,
  228.      * false if the text will not scroll until start() is called
  229.      * @see #setAutoStart
  230.      * @see #start
  231.      * @see #stop
  232.      */
  233.     public boolean isAutoStart()
  234.     {
  235.         return execute;
  236.     }
  237.  
  238.     /**
  239.      * @depicated
  240.      * @see #isAutoStart
  241.      */
  242.     public boolean getAutoStart()
  243.     {
  244.         return isAutoStart();
  245.     }
  246.  
  247.     /**
  248.      * Controls whether text will repeatedly scroll or just scroll once.
  249.      * @param f if true the text will scroll over and over,
  250.      * if false the text will scroll off and back on, then stop
  251.      * @see #isRepeat
  252.      *
  253.      * @exception PropertyVetoException
  254.      * if the specified property value is unacceptable
  255.      */
  256.     public void setRepeat(boolean f) throws PropertyVetoException
  257.     {
  258.         Boolean oldValue = new Boolean(isRepeat);
  259.         Boolean newValue = new Boolean(f);
  260.  
  261.         vetos.fireVetoableChange("Repeat", oldValue, newValue);
  262.  
  263.         isRepeat = f;
  264.  
  265.         changes.firePropertyChange("Repeat", oldValue, newValue);
  266.     }
  267.  
  268.     /**
  269.      * Gets whether text will repeatedly scroll or just scroll once.
  270.      * @return true if the text will scroll over and over,
  271.      * false if the text will scroll off and back on, then stop
  272.      * @see #setRepeat
  273.      */
  274.     public boolean isRepeat()
  275.     {
  276.         return isRepeat;
  277.     }
  278.  
  279.     /**
  280.      * @deprecated
  281.      * @see #isRepeat
  282.      */
  283.     public boolean getRepeat()
  284.     {
  285.         return isRepeat();
  286.     }
  287.  
  288.     /**
  289.      * Sets the time between the display of each character in milliseconds.
  290.      * The minimum delay is 30 milliseconds.
  291.      * @param d the delay, in milliseconds
  292.      * @see #getDelay
  293.      *
  294.      * @exception PropertyVetoException
  295.      * if the specified property value is unacceptable
  296.      */
  297.     public void setDelay(int d) throws PropertyVetoException
  298.     {
  299.         int temp = 0;
  300.  
  301.         if(d < 30)
  302.             temp = 30;
  303.         else
  304.             temp = d;
  305.  
  306.         Integer oldValue = new Integer(delay);
  307.         Integer newValue = new Integer(temp);
  308.  
  309.         vetos.fireVetoableChange("Delay", oldValue, newValue);
  310.  
  311.         delay = temp;
  312.  
  313.         changes.firePropertyChange("Delay", oldValue, newValue);
  314.     }
  315.  
  316.     /**
  317.      * Gets the time between the display of each character in milliseconds.
  318.      * @return the delay, in milliseconds
  319.      * @see #setDelay
  320.      */
  321.     public int getDelay()
  322.     {
  323.         return delay;
  324.     }
  325.  
  326.     /**
  327.      * Sets the AppletContext that has the status area for scrolling text.
  328.      * Note: this overrides the automatically set AppletContext.
  329.      *
  330.      * @param c the new AppletContext
  331.      * @see #setAppletContext(java.applet.Applet)
  332.      *
  333.      * @exception PropertyVetoException
  334.      * if the specified property value is unacceptable
  335.      */
  336.     public void setAppletContext(AppletContext c) throws PropertyVetoException
  337.     {
  338.         AppletContext oldValue = context;
  339.  
  340.         vetos.fireVetoableChange("AppletContext", oldValue, c);
  341.  
  342.         context = c;
  343.  
  344.         changes.firePropertyChange("AppletContext", oldValue, c);
  345.     }
  346.  
  347.     /**
  348.      * Sets the AppletContext that has the status area for scrolling text.
  349.      * This version takes an Applet object and gets the needed AppletContext from that.
  350.      * Note: this overrides the automatically set AppletContext.
  351.      *
  352.      * @param a the Applet with the AppletContext to use
  353.      * @see #setAppletContext(java.applet.AppletContext)
  354.      *
  355.      * @exception PropertyVetoException
  356.      * if the specified property value is unacceptable
  357.      */
  358.     public void setAppletContext(Applet a) throws PropertyVetoException
  359.     {
  360.         if(a == null)
  361.             setAppletContext((AppletContext)null);
  362.         else
  363.             setAppletContext(a.getAppletContext());
  364.     }
  365.  
  366.     /**
  367.      * Starts the status text scrolling.
  368.      * @see #stop
  369.      * @see #run
  370.      */
  371.     public void start()
  372.     {
  373.         execute = true;
  374.         thread.resume();
  375.     }
  376.  
  377.     /**
  378.      * Stops the status text scrolling.
  379.      * @see #start
  380.      */
  381.     public void stop()
  382.     {
  383.         execute   = false;
  384.     }
  385.  
  386.     /**
  387.      * Clears the status area.
  388.      * Note: this does not clear the string to scroll,
  389.      * it just clears the status area in the current AppletContext.
  390.      */
  391.     public void clear()
  392.     {
  393.         if(context != null)
  394.         {
  395.             //Display an empty string
  396.             context.showStatus("");
  397.         }
  398.     }
  399.  
  400.     /**
  401.      * The body of the StatusScroller Thread.
  402.      * This method is called by the Java Virtual Machine in response to a
  403.      * call to the start method of this object.
  404.      */
  405.     public void run()
  406.     {
  407.         String workingString = null;
  408.         int loopIndicator;
  409.  
  410.         if(!execute) thread.suspend();
  411.         try
  412.         {
  413.             while(true)
  414.             {
  415.                 do
  416.                 {
  417.                        //loopIndicator = index;
  418.  
  419.                     thread.sleep(delay);
  420.  
  421.                     if (execute)
  422.                     {
  423.                         //Get the string to display
  424.                         workingString = scrollString();
  425.                         if (context != null && workingString != null)
  426.                         {
  427.                             //Display the string
  428.                             context.showStatus(workingString);
  429.                         }
  430.                     }
  431.                 }
  432.                 //Keep scrolling until an exit condition is met
  433.                 while (index != 0 || isRepeat);
  434.  
  435.                 thread.suspend();
  436.             }
  437.         }
  438.         catch (InterruptedException e)
  439.         {
  440.         }
  441.     }
  442.  
  443.     /**
  444.      * Adds a listener for all property change events.
  445.      * @param listener the listener to add
  446.      * @see #removePropertyChangeListener
  447.      */
  448.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
  449.     {
  450.         changes.addPropertyChangeListener(listener);
  451.     }
  452.  
  453.     /**
  454.      * Removes a listener for all property change events.
  455.      * @param listener the listener to remove
  456.      * @see #addPropertyChangeListener
  457.      */
  458.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
  459.     {
  460.         changes.removePropertyChangeListener(listener);
  461.     }
  462.  
  463.     /**
  464.      * Adds a listener for all vetoable property change events.
  465.      * @param listener the listener to add
  466.      * @see #removeVetoableChangeListener
  467.      */
  468.     public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
  469.     {
  470.         vetos.addVetoableChangeListener(listener);
  471.     }
  472.  
  473.     /**
  474.      * Removes a listener for all vetoable property change events.
  475.      * @param listener the listener to remove
  476.      * @see #addVetoableChangeListener
  477.      */
  478.     public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
  479.     {
  480.         vetos.removeVetoableChangeListener(listener);
  481.     }
  482.  
  483.        //***** Private Methods *****//
  484.  
  485.     /**
  486.      * Handles the manipulation of the string buffer to give the
  487.      * desired scrolling effect.
  488.      * @return the string to be displayed
  489.      * @see #run
  490.      * @see #setRightToLeft
  491.      * @see #getRightToLeft
  492.      */
  493.     protected String scrollString()
  494.     {
  495.         //If the string is null
  496.         if(workingBuffer == null)
  497.             return null;
  498.         //If there is only one character
  499.         if(wblength < 2)
  500.             return(workingBuffer.toString());
  501.  
  502.         char ch, temp[];
  503.         //Allocate space for the rest of the characters
  504.         temp = new char[wblength - 1];
  505.  
  506.         if(isRightToLeft)
  507.         {
  508.             //Store the first character
  509.             ch = workingBuffer.charAt(0);
  510.             //Store the rest of the characters
  511.             workingBuffer.getChars(1, wblength, temp, 0);
  512.             //Shift the rest of the characters to the left one index
  513.             workingBuffer = new StringBuffer(new String(temp));
  514.             //Append the first character
  515.             workingBuffer = workingBuffer.append(ch);
  516.             //Keep track of where the original first character is
  517.             index++;
  518.             if(index > wblength -1)
  519.                 index = 0;
  520.         }
  521.         else
  522.         {
  523.             //Store the last character
  524.             ch = workingBuffer.charAt(wblength - 1);
  525.             //Store the rest of the characters
  526.             workingBuffer.getChars(0, wblength - 1, temp, 0);
  527.             //Place the last character at the beginning
  528.             workingBuffer = new StringBuffer("" + ch);
  529.             //Append the rest of the characters
  530.             workingBuffer = workingBuffer.append(temp);
  531.             //Keep track of where the original first character is
  532.             index--;
  533.             if(index < 0)
  534.                 index = wblength -1;
  535.         }
  536.  
  537.         //Return a window onto the modified string buffer to be displayed
  538.         return((workingBuffer.toString()).substring(0, sslength));
  539.     }
  540.  
  541.     /**
  542.      * Returns a String with howBig number of spaces as the content
  543.      * @param howBig the requested number of spaces
  544.      * @returns a String with the requested number of spaces
  545.      */
  546.     protected String makePadding(int howBig)
  547.     {
  548.         StringBuffer str = new StringBuffer(0);
  549.  
  550.         for(int i = 0; i < howBig; ++i)
  551.             str.append(" ");
  552.         return(str.toString());
  553.     }
  554.  
  555.     /**
  556.      * Updates the string buffer depending on the current settings.
  557.      * @see #setScrollClean
  558.      */
  559.     protected void updateWorkingBuffer()
  560.     {
  561.         if(isScrollClean)
  562.         {
  563.             String temp = makePadding(sslength);
  564.             workingBuffer = new StringBuffer(statusString + temp);
  565.         }
  566.         else
  567.             workingBuffer = new StringBuffer(statusString);
  568.  
  569.         wblength = workingBuffer.length();
  570.     }
  571.  
  572.     /**
  573.      * If true, the text will scroll from the right to the left.
  574.      * @see #setRightToLeft
  575.      * @see #isRightToLeft
  576.      */
  577.     protected boolean isRightToLeft;
  578.     /**
  579.      * If true, the text will continue scrolling over and over.
  580.      * @see #setRepeat
  581.      * @see #isRepeat
  582.      */
  583.     protected boolean isRepeat;
  584.     /**
  585.      * If true, the text will scroll completely off before scrolling on again.
  586.      * @see #setScrollClean
  587.      * @see #isScrollClean
  588.      */
  589.     protected boolean isScrollClean;
  590.     /**
  591.      * The applet context that shows the status text.
  592.      */
  593.     protected AppletContext context;
  594.     /**
  595.      * The thread that handles scrolling the text.
  596.      * @see #start
  597.      * @see #stop
  598.      */
  599.     protected Thread thread;
  600.     /**
  601.      * The time between the display of each character in milliseconds.
  602.      * @see #setDelay
  603.      * @see #getDelay
  604.      */
  605.     protected int delay;
  606.     /**
  607.      * Working buffer length, in characters.
  608.      */
  609.     protected int wblength;
  610.     /**
  611.      * Status string length, in characters.
  612.      */
  613.     protected int sslength;
  614.     /**
  615.      * The zero-relative index of the original first character.
  616.      */
  617.     protected int index;
  618.     /**
  619.      * The string to scroll.
  620.      */
  621.     protected    String    statusString;
  622.     /**
  623.      * The string that gets scrolled.
  624.      */
  625.     protected StringBuffer    workingBuffer;
  626.  
  627.     private boolean execute, loopFlag;
  628.     private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  629.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  630. }
  631.