// Copyright 1996, Marimba Inc. All Rights Reserved.


// @(#)AudioWidget.java, 1.14, 11/13/96





package marimba.gui;





import java.awt.*;


import java.applet.AudioClip;





import marimba.persist.*;





/**


 * A simple audio widget. It is a subclass if ImageWidget


 * just so that it can have a visual picture associated with it.


 *


 * @author	Arthur van Hoff


 * @version 	1.14, 11/13/96


 */


public class AudioWidget extends ImageWidget {


    /**


     * Loop the audio


     */


    public boolean loop;


    


    /**


     * The URL of the sound that is played


     * when the mouse enters this widget.


     * @see #getEnterSound


     * @see #setEnterSound


     */


    public String  enterSound;





    /**


     * The URL of the sound that is played


     * when the mouse exits this widget.


     * @see #getExitSound


     * @see #setEnterSound


     */


    public String  exitSound;





    /**


     * The URL of the sound that is played


     * when the widget is started.


     * @see #getStartSound


     * @see #setStartSound


     */


    public String  startSound;





    /**


     * The URL of the sound that is played


     * when the widget is stopped.


     * @see #getStopSound


     * @see #setStopSound


     */


    public String  stopSound;





    /**


     * The URL of the sound that is played on a rest event.


     * @see #getRestSound


     * @see #setRestSound


     */


    public String  restSound;





    /**


     * The URL of the sound that is played on a wake event.


     * @see #getWakeSound


     * @see #setWakeSound


     */


    public String  wakeSound;





    /**


     * The sound that is played when the user


     * clicks on this widget.


     * @see #getClickSound


     * @see #setClickSound


     */


    public String  clickSound;





    /**


     * The most recent sound that was played.


     */


    public AudioClip recent;





    /**


     * Constructor.


     */


    public AudioWidget() {


	src = "~builder/audio.gif";


	clickSound = "~builder/beep.au";


    }





    /**


     * Get the properties of this widget.


     */


    public void getProperties(PropertyList list) {


	super.getProperties(list);


	list.setBoolean("loop", loop, false);


	list.setURL("entersound", enterSound, null);


	list.setURL("exitsound", exitSound, null);


	list.setURL("startsound", startSound, null);


	list.setURL("stopsound", stopSound, null);


	list.setURL("restsound", restSound, null);


	list.setURL("wakesound", wakeSound, null);


	list.setURL("clicksound", clickSound, null);


    }





    /**


     * Set the properties of this widget.


     */


    public void setProperties(PropertyList list) {


	super.setProperties(list);


	loop = list.getBoolean("loop", false);


	enterSound = list.getURL("entersound", null);


	exitSound = list.getURL("exitsound", null);


	startSound = list.getURL("startsound", null);


	stopSound = list.getURL("stopsound", null);


	restSound = list.getURL("restsound", null);


	wakeSound = list.getURL("wakesound", null);


	clickSound = list.getURL("clicksound", null);


    }





    /**


     * Play a sound.


     */


    public void play(String src) {


	if (loop && (recent != null)) {


	    recent.stop();


	    recent = null;


	}


	if (src != null) {


	    recent = getAudioClip(src);


	    if (recent != null) {


		if (loop) {


		    recent.loop();


		} else {


		    recent.play();


		}


	    }


	} 


    }





    /**


     * Check whether the widget loops.


     */


    public boolean isLoop() {


	return loop;


    }





    /**


     * Set loop;


     */


    public void setLoop(boolean loop) {


	if (loop != this.loop) {


	    this.loop = loop;


	    if (recent != null) {


		play(null);


	    }


	}


    }





    /**


     * Get the sound for entering this widget.


     * @see #enterSound


     */


    public String getEnterSound() {


	return enterSound;


    }





    /**


     * Set the sound for entering this widget.


     * Specify null if no sound must be played.


     * @see #enterSound


     */


    public void setEnterSound(String enterSound) {


	if (enterSound != null && enterSound.length() == 0) {


	    this.enterSound = null;


	} else {


	    this.enterSound = enterSound;


	}


    }





    /**


     * Get the sound for exiting this widget.


     * @see #exitSound


     */


    public String getExitSound() {


	return exitSound;


    }





    /**


     * Set the sound for exiting this widget.


     * Specify null if no sound must be played.


     * @see #exitSound


     */


    public void setExitSound(String exitSound) {


	if ((exitSound != null) && (exitSound.length() == 0)) {


	    this.exitSound = null;


	} else {


	    this.exitSound = exitSound;


	}


    }





    /**


     * Get the sound for starting this widget.


     * @see #startSound


     */


    public String getStartSound() {


	return startSound;


    }





    /**


     * Set the sound for starting this widget.


     * Specify null if no sound must be played.


     * @see #startSound


     */


    public void setStartSound(String startSound) {


	if ((startSound != null) && (startSound.length() == 0)) {


	    this.startSound = null;


	} else {


	    this.startSound = startSound;


	}


    }





    /**


     * Get the sound for stopping this widget.


     * @see #stopSound


     */


    public String getStopSound() {


	return stopSound;


    }





    /**


     * Set the sound for stopping this widget.


     * Specify null if no sound must be played.


     * @see #stopSound


     */


    public void setStopSound(String stopSound) {


	if ((stopSound != null) && (stopSound.length() == 0)) {


	    this.stopSound = null;


	} else {


	    this.stopSound = stopSound;


	}


    }





    /**


     * Get the sound for the rest event.


     * @see #restSound


     */


    public String getRestSound() {


	return restSound;


    }





    /**


     * Set the sound for the rest event.


     * Specify null if no sound must be played.


     * @see #restSound


     */


    public void setRestSound(String restSound) {


	if ((restSound != null) && (restSound.length() == 0)) {


	    this.restSound = null;


	} else {


	    this.restSound = restSound;


	}


    }





    /**


     * Get the sound for the wake event.


     * @see #wakeSound


     */


    public String getWakeSound() {


	return wakeSound;


    }





    /**


     * Set the sound for the wake event.


     * Specify null if no sound must be played.


     * @see #wakeSound


     */


    public void setWakeSound(String wakeSound) {


	if ((wakeSound != null) && (wakeSound.length() == 0)) {


	    this.wakeSound = null;


	} else {


	    this.wakeSound = wakeSound;


	}


    }





    /**


     * Get the sound for clicking this widget.


     * @see #clickSound


     */


    public String getClickSound() {


	return clickSound;


    }





    /**


     * Set the sound for clicking this widget.


     * Specify null if no sound must be played.


     * @see #clickSound


     */


    public void setClickSound(String clickSound) {


	if ((clickSound != null) && (clickSound.length() == 0)) {


	    this.clickSound = null;


	} else {


	    this.clickSound = clickSound;


	}


    }





    /**


     * Play the start sound (if any)


     */


    public void start() {


	super.start();


	if (startSound != null) {


	    play(startSound);


	}


    }





    /**


     * Play the stop sound (if any)


     */


    public void stop() {


	play(stopSound);


	super.stop();


    }





    /**


     * Play the enter/exit/click sound (if any)


     */


    public boolean handleEvent(Event evt) {


	switch (evt.id) {


	  case Event.MOUSE_ENTER:


	    if (enterSound != null) {


		play(enterSound);


	    }


	    break;





	  case Event.MOUSE_EXIT:


	    if (exitSound != null) {


		play(exitSound);


	    }


	    break;





	  case Event.MOUSE_DOWN:


	    if (loop && (recent != null)) {


		play(null);


	    } else if (clickSound != null) {


		play(clickSound);


	    }


	    break;





	  case REST_EVENT:


	    if (restSound != null) {


		play(restSound);


	    }


	    break;





	  case WAKE_EVENT:


	    if (wakeSound != null) {


		play(wakeSound);


	    }


	    break;


	}


	return super.handleEvent(evt);


    }





    /**


     * Debugging.


     */


    public void paramString(StringBuffer buf) {


	super.paramString(buf);


	if (loop) {


	    buf.append(",loop");


	}


	if (enterSound != null) {


	    buf.append(",enter=");


	    buf.append(enterSound);


	}


	if (exitSound != null) {


	    buf.append(",exit=");


	    buf.append(exitSound);


	}


	if (startSound != null) {


	    buf.append(",start=");


	    buf.append(startSound);


	}


	if (stopSound != null) {


	    buf.append(",stop=");


	    buf.append(stopSound);


	}


	if (restSound != null) {


	    buf.append(",rest=");


	    buf.append(restSound);


	}


	if (wakeSound != null) {


	    buf.append(",wake=");


	    buf.append(wakeSound);


	}


	if (clickSound != null) {


	    buf.append(",click=");


	    buf.append(clickSound);


	}


    }


}


