package oracle.forms.fd;

import java.applet.AudioClip;
import java.util.StringTokenizer;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.GridBagLayout;
import oracle.forms.ui.VBean;
import oracle.forms.handler.IHandler;
import oracle.forms.properties.ID;

  /**
   * A javabean that play sound files
   * like : .WAV  .AU  .MID  .AIFF
   *
   * @author Francois Degrelle
   * @version 1.0
   */

public class Sound extends VBean {
    public final static ID setFile   = ID.registerProperty("SET_FILE");  
    public final static ID play      = ID.registerProperty("PLAY");  
    SoundList soundList;
    String soundFile;
        
    AudioClip Clip ;
    URL codeBase;

    public Sound() 
    {             
    }

  public boolean setProperty(ID property, Object value)
  {
    /***********************
     * load the sound file *
     **********************/
    if (property == setFile) 
    {
      String sValue="", sDir="", sFile="" ;
      sValue = value.toString() ;
      StringTokenizer st ;
      st = new StringTokenizer(sValue,",");      
      if (st.hasMoreTokens()) sDir = st.nextToken() ; 
      if (st.hasMoreTokens()) sFile = st.nextToken() ;       
      System.out.println("dir="+sDir+"  file="+sFile);
       try {
           codeBase = new URL("file:" + sDir + "/");
       } catch (MalformedURLException e) {
           System.err.println(e.getMessage());
       }
       soundList = new SoundList(codeBase);
       soundFile = sFile ;
       soundList.startLoading(soundFile);
       
      return true ;
    }
    /******************
     * play the sound *
     *****************/
    else if (property == play)
    {
      Clip = soundList.getClip(soundFile);
       if( Clip != null )
       {
         Clip.play(); 
         System.out.println("Playing sound " + soundFile);
       }
       else
       {
         System.out.println("Sound " + soundFile+ " not loaded");
       }
      return true ;
    }
    else
    {
     return super.setProperty(property, value);
    }
  }
  

}
