home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 10.9 KB | 433 lines |
- package symantec.itools.awt.util;
-
- import java.applet.*;
-
- /**
- * @version 1.0, April 11, 1997
- * @author Symantec
- */
-
- // Created and implemented by Levi Brown, Symantec Macintosh Internet Tools.
- // 04/11/97 LAB Checked it in
- // 04/17/97 LAB Changed a few names to comply with Java standards.
- // Added automatic initialization of the applet context in the constructor.
- // Changed the function comment style to comply with JavaDoc standards.
- // Took out some unnecessary code in setAppletContext(Applet a).
- // Reorganized the code, to group public and private methods separately.
- // Removed some unnecessary import statements.
- // Fixed a bug in setAppletContext(Applet a) that could cause a NullPointerException.
- // Added set/getAutoStart(boolean f) to allow the scrolling to occur without a direct call to start().
- // Changed the default state of execute to true, so scrolling would automatically occur.
-
- public class StatusScroller implements Runnable
- {
- protected boolean isRightToLeft, isRepeat, isScrollClean;
- protected AppletContext context;
- protected Thread thread;
- protected int delay, wblength, sslength, index;
- protected String statusString;
- protected StringBuffer workingBuffer;
-
- private boolean execute, loopFlag;
-
- /**
- * Default Constructor
- */
- public StatusScroller()
- {
- isRightToLeft = true;
- isRepeat = true;
- isScrollClean = true;
- execute = true;
- loopFlag = false;
- statusString = null;
- delay = 100;
- thread = new Thread(this);
-
- //Initalize the context variable
- setAppletContext(symantec.itools.lang.Context.getApplet());
-
- thread.start();
- }
-
-
- //***** Public Methods *****//
-
- //start
- /**
- * Start the status text scrolling
- * @see #run
- */
- public void start()
- {
- execute = true;
- thread.resume();
- }
-
- //stop
- /**
- * Stops the status text from scrolling
- * @see #start
- */
- public void stop()
- {
- execute = false;
- }
-
- //clear
- /**
- * Clears the status area
- */
- public void clear()
- {
- if(context != null)
- {
- //Display an empty string
- context.showStatus("");
- }
- }
-
- //setString
- /**
- * Sets the string to use for scrolling
- * @see #getString
- * @param String s
- */
- public void setString(String s)
- {
- statusString = s;
- index = 0;
-
- if(statusString == null || statusString.equals(""))
- {
- statusString = null;
- workingBuffer = null;
- wblength = 0;
- sslength = 0;
- if(execute)
- clear();
- }
- else
- {
- sslength = statusString.length();
- updateWorkingBuffer();
- }
- }
-
- //getString
- /**
- * The string currently being used for scrolling
- * @see #setString
- * @return String
- */
- public String getString()
- {
- return statusString;
- }
-
- //setRightToLeft
- /**
- * Controlls the direction the text will scroll.
- * If true, the text will scroll from the Right to the Left;
- * if false, the text will scroll from the Left to the Right.
- * @see #getRightToLeft
- * @param boolean b
- */
- public void setRightToLeft(boolean b)
- {
- isRightToLeft = b;
- }
-
- //getRightToLeft
- /**
- * The direction the text will scroll.
- * If true, the text will scroll from the Right to the Left;
- * if false, the text will scroll from the Left to the Right.
- * @see #setRightToLeft
- * @return boolean
- */
- public boolean getRightToLeft()
- {
- return isRightToLeft;
- }
-
- //setScrollClear
- /**
- * Controlls one behavior of the scrolling.
- * If true, the text will scroll completely off before scrolling on again;
- * if false, the text will scroll from head to tail or tail to head
- * (depending on the direction) directly.
- * @see #getScrollClean
- * @see #getRightToLeft
- * @param boolean b
- */
- public void setScrollClean(boolean b)
- {
- isScrollClean = b;
-
- updateWorkingBuffer();
- }
-
- //getScrollClear
- /**
- * One behavior of the scrolling.
- * If true, the text will scroll completely off before scrolling on again;
- * if false, the text will scroll from head to tail or tail to head
- * (depending on the direction) directly.
- * @see #setScrollClean
- * @see #getRightToLeft
- * @return boolean
- */
- public boolean getScrollClean()
- {
- return isScrollClean;
- }
-
- //setAutoStart
- /**
- * Determines if the scrolling will automatically start when the applet is loaded.
- * If true the text will start scrolling as soon as the applet is loaded;
- * if false, the text will not scroll until start() is called.
- * @see #getAutoStart
- * @see #start
- * @see #stop
- * @param boolean f
- */
- public void setAutoStart(boolean f)
- {
- if(f)
- start();
- else
- stop();
- }
-
- //getAutoStart
- /**
- * Determines if the scrolling will automatically start when the applet is loaded.
- * If true the text will start scrolling as soon as the applet is loaded;
- * if false, the text will not scroll until start() is called.
- * @see #setAutoStart
- * @see #start
- * @see #stop
- * @return boolean Wether or not the text will scroll as soon as the applet is loaded.
- */
- public boolean getAutoStart()
- {
- return execute;
- }
-
- //setRepeat
- /**
- * Controlls one behavior of the scrolling
- * If true the text will continue scrolling over and over;
- * if false, the text will scroll off and back on, then stop.
- * @see #getRepeat
- * @param boolean f
- */
- public void setRepeat(boolean f)
- {
- isRepeat = f;
- }
-
- //getRepeat
- /**
- * One behavior of the scrolling
- * If true the text will continue scrolling over and over;
- * if false, the text will scroll off and back on, then stop.
- * @see #setRepeat
- * @return boolean
- */
- public boolean getRepeat()
- {
- return isRepeat;
- }
-
- //setDelay
- /**
- * Set the time between the display of each character in milliseconds.
- * Minimum delay of 30 miliseconds.
- * @see #getDelay
- * @param int d
- */
- public void setDelay(int d)
- {
- if(d < 30)
- delay = 30;
- else
- delay = d;
- }
-
- //getDelay
- /**
- * The time between the display of each character in milliseconds
- * @see #setDelay
- * @return int
- */
- public int getDelay()
- {
- return delay;
- }
-
- //setAppletContext
- /**
- * StatusScroller needs an AppletContext in order to set the status area
- * This version takes an AppletContext object directly.
- * @see #setAppletContext(Applet)
- * @param AppletContext c
- */
- public void setAppletContext(AppletContext c)
- {
- context = c;
- }
-
- //setAppletContext
- /**
- * StatusScroller needs an AppletContext in order to set the status area
- * This version takes an Applet object and gets the needed AppletContext from it.
- * @see #setAppletContext(AppletContext)
- * @param Applet a
- */
- public void setAppletContext(Applet a)
- {
- if(a == null)
- context = null;
- else
- context = a.getAppletContext();
- }
-
- //run
- /**
- * The main logic loop
- */
- public void run()
- {
- String workingString = null;
- int loopIndicator;
-
- if(!execute) thread.suspend();
- try
- {
- while(true)
- {
- do
- {
- //loopIndicator = index;
-
- thread.sleep(delay);
-
- if (execute)
- {
- //Get the string to display
- workingString = scrollString();
- if (context != null && workingString != null)
- {
- //Display the string
- context.showStatus(workingString);
- }
- }
- }
- //Keep scrolling until an exit condition is met
- while (index != 0 || isRepeat);
-
- thread.suspend();
- }
- }
- catch (InterruptedException e)
- {
- }
- }
-
- //***** Private Methods *****//
-
- //scrollString
- /**
- * Handles the manipulation of the string buffer to give the
- * desired scrolling effect.
- * @see #run
- * @see #setRightToLeft
- * @see #getRightToLeft
- * @return String
- */
- protected String scrollString()
- {
- //If the string is null
- if(workingBuffer == null)
- return null;
- //If there is only one character
- if(wblength < 2)
- return(workingBuffer.toString());
-
- char ch, temp[];
- //Allocate space for the rest of the characters
- temp = new char[wblength - 1];
-
- if(isRightToLeft)
- {
- //Store the first character
- ch = workingBuffer.charAt(0);
- //Store the rest of the characters
- workingBuffer.getChars(1, wblength, temp, 0);
- //Shift the rest of the characters to the left one index
- workingBuffer = new StringBuffer(new String(temp));
- //Append the first character
- workingBuffer = workingBuffer.append(ch);
- //Keep track of where the original first character is
- index++;
- if(index > wblength -1)
- index = 0;
- }
- else
- {
- //Store the last character
- ch = workingBuffer.charAt(wblength - 1);
- //Store the rest of the characters
- workingBuffer.getChars(0, wblength - 1, temp, 0);
- //Place the last character at the beginning
- workingBuffer = new StringBuffer("" + ch);
- //Append the rest of the characters
- workingBuffer = workingBuffer.append(temp);
- //Keep track of where the original first character is
- index--;
- if(index < 0)
- index = wblength -1;
- }
-
- //Return a window onto the modified string buffer to be displayed
- return((workingBuffer.toString()).substring(0, sslength));
- }
-
- //makePadding
- /**
- * Returns a String with howBig number of spaces as the content
- * @see #updateWorkingBuffer
- * @param int howBig
- * @returns String
- */
- protected String makePadding(int howBig)
- {
- StringBuffer str = new StringBuffer(0);
-
- for(int i = 0; i < howBig; ++i)
- str.append(" ");
- return(str.toString());
- }
-
- //updateWorkingBuffer
- /**
- * Updates the string buffer depending on the settings
- * @see #setScrollClean
- */
- protected void updateWorkingBuffer()
- {
- if(isScrollClean)
- {
- String temp = makePadding(sslength);
- workingBuffer = new StringBuffer(statusString + temp);
- }
- else
- workingBuffer = new StringBuffer(statusString);
-
- wblength = workingBuffer.length();
- }
-
- }