FileDocCategorySizeDatePackage
TalkingJDialog.javaAPI DocExample2253Wed Jul 17 00:42:38 BST 2002com.wiverson.macosbook.speech

TalkingJDialog.java

package com.wiverson.macosbook.speech;

/* This single class does the vast bulk of the
   heavy lifting of actually making Mac OS X talk.
 
   Don't blink or you'll miss it.
 */
import com.apple.speech.synthesis.Synthesizer;

/* This class describes a very generic version of
   JDialog with a few methods added for speech recognition
   and related user interface. It's extraordinarily
   straightforward.
 */

public class TalkingJDialog extends javax.swing.JDialog
implements java.awt.event.MouseListener
{
    
    public TalkingJDialog()
    {
        this.setResizable(false);
        this.addMouseListener(this);
    }
    
    /* This method is used to allow the user to click
     anywhere and immediately cancel out of the
     speech playback - even if the dialog isn't
     dismissed
     */
    public void mousePressed(java.awt.event.MouseEvent mouseEvent)
    {
        if(mySynthesizer != null)
            mySynthesizer.stopSpeech();
    }
    
    // Needed to complete the MouseListener interface
    public void mouseReleased(java.awt.event.MouseEvent mouseEvent)
    {}
    public void mouseExited(java.awt.event.MouseEvent mouseEvent)
    {}
    public void mouseEntered(java.awt.event.MouseEvent mouseEvent)
    { }
    public void mouseClicked(java.awt.event.MouseEvent mouseEvent)
    {}
    
    public void dispose()
    {
        super.dispose();
    }
    
    public void hide()
    {
        super.hide();
        // If the dialog goes away, be sure to stop talking.
        mySynthesizer.stopSpeech();
    }
    
    private Synthesizer mySynthesizer = null;
    
    public void show()
    {
        super.show();
        // Get a synthesizer for this dialog
        // if one isn't already available
        if(mySynthesizer == null)
            mySynthesizer = new Synthesizer();
        // Start talking!
        mySynthesizer.speakText(getNotificationText());
    }
    
    // Storage & accessors for the text to be spoken
    private String spokenText;
    public void setNotificationText(String inString)
    {
        spokenText = inString;
    }
    public String getNotificationText()
    {
        return spokenText;
    }
}