MRJTalkTest.java

/**
 *  File:       MRJTalkTest.java
 *
 *  Contains:   Standalone application example that interfaces to MRJTalker native class.
 *
 *  Version:    1.05
 *
 *  Technology: System 7.6.1 or later
 *  
 *  Package:    Mac OS Runtime For Javaª SDK 2.0.1
 *
 *  Copyright:  © 1984-1998 by Apple Computer, Inc.
 *              All rights reserved.
 *
 *  Bugs?:      If you find a problem with this file, report them to mrj_bugs.
 *              Include the file and version information (from above)
 *              in the problem description and send to:
 *                  Internet:   mrj_bugs@apple.com
 */
 
import java.awt.*;
import java.util.Vector;
 
public
class MRJTalkTest extends Frame implements Runnable {
    /**
     * main for the test
     */
    public static void main() {
        MRJTalkTest sample = new MRJTalkTest();
 
        // add the strings      
        sample.addString("One Ring to rule Them all,");
        sample.addString("One Ring to find Them.");
        sample.addString("One Ring to bring them all,");
        sample.addString("and in the Darkness bind Them,");
        sample.addString("In the Land of Mordor, where the Shadows lie.");
        
        // show the window
        sample.show();
    }
    
    /**
     * Instance Data
     */
    Label itsLabel;
    Vector itsStrings = new Vector();
    Thread itsThread;
    MRJTalker itsTalker;
    
    /**
     * our constructor for this sample
     */
    
    public MRJTalkTest() {
        setTitle("Standalone Application");
        setBackground(Color.lightGray);
        setLayout(new BorderLayout());
        add("Center", itsLabel = new Label("Initial Label", Label.CENTER));
        itsLabel.setFont(new Font("Geneva", Font.PLAIN, 12));
        reshape(100, 100, 300, 60);
        
        // try to construct a talker - if one isn't availiable, getTalker will return null
        itsTalker = MRJTalker.getTalker();
    }
    
    /**
     * When our window is shown, we start a timer thread
     */
     
    public void show() {
        super.show();
        itsThread = new Thread(this);
        itsThread.setPriority(2);
        itsThread.start();
    }
    
    /**
     * When our window is hidden, we stop our timer
     */
 
    public void hide() {
        itsThread.stop();
        itsThread = null;
        super.hide();
    }
    
    /**
     * Add a string to the strings we display
     */
     
    void addString(String s) {
        itsStrings.addElement(s);
    }
 
    /**
     * Handle an event - we only do window destroys here
     */
     
    public boolean handleEvent(Event evt) {
        if (evt.id == Event.WINDOW_DESTROY) {
            hide();
            System.exit(0);
        }
        return false;
    }
    
    /**
     * Our periodic update thread
     */
 
    private boolean snooze(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            return false;
        }
        return true;
    }
    
    public void run() {
        int ixString = 0;
        while (itsThread != null) {
            if (! snooze(500))  
                break;
            
            synchronized (itsStrings) {
                int ctStrings = itsStrings.size();
                if (ctStrings > 0) {
                    if (ixString >= ctStrings) {
                        ixString = 0;
                        snooze(1000);
                    }
                    String s = (String) itsStrings.elementAt(ixString++);
                    if (s != null) {
                        itsLabel.setText(s);
                        if (itsTalker != null)
                            itsTalker.speakString(s);
                    }
                }
            }
        }
        hide();
    }
};