package org.csstudio.opibuilder.widgetActions;

import java.io.IOException;
import java.io.InputStream;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;

import org.csstudio.opibuilder.properties.FilePathProperty;
import org.csstudio.opibuilder.properties.WidgetPropertyCategory;
import org.csstudio.opibuilder.util.ConsoleService;
import org.csstudio.opibuilder.util.ResourceUtil;
import org.csstudio.opibuilder.widgetActions.WidgetActionFactory.ActionType;
import org.csstudio.platform.logging.CentralLogger;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.progress.UIJob;

/**An action which plays a .wav file. 
 * @author Xihui Chen
 *
 */
public class PlayWavFileAction extends AbstractWidgetAction {

	public static final String PROP_PATH = "path";//$NON-NLS-1$
	
	@Override
	protected void configureProperties() {
		addProperty(new FilePathProperty(
				PROP_PATH, "WAV File Path", WidgetPropertyCategory.Basic, new Path(""), 
				new String[]{"wav"}));
	
	}

	@Override
	public ActionType getActionType() {		
		return ActionType.PLAY_SOUND;
	}

	@Override
	public void run() {
		UIJob job = new UIJob(getDescription()){
			@Override
			public IStatus runInUIThread(IProgressMonitor monitor) {
				IPath absolutePath = getPath();
		        try {		              	
		           if(!getPath().isAbsolute()){
		                absolutePath = 
		                	ResourceUtil.buildAbsolutePath(getWidgetModel(), getPath());
		           }
		           
			       final InputStream in = ResourceUtil.pathToInputStream(absolutePath); 
			       
		           final AudioInputStream stream = AudioSystem.getAudioInputStream(in);
		           Clip clip = AudioSystem.getClip();
		           clip.open(stream);         
		           
		           clip.addLineListener(new LineListener() {		        
						public void update(LineEvent event) {
							if(event.getType() == LineEvent.Type.STOP){
								try {
									stream.close();
									in.close();
								} catch (IOException e) {
									CentralLogger.getInstance().error(this, e);
								}
							}						
						}
			      });		           
		          clip.start();
		          
		        } catch (Exception e) {
		        	String message = "Failed to play file " + getPath() + "\n" +  e.getMessage(); //$NON-NLS-2$         		
		        	MessageDialog.openError(null, "Failed to play file", message);
		        	CentralLogger.getInstance().error(this, message, e);
		        	ConsoleService.getInstance().writeError(message);		        	
		        }
		        return Status.OK_STATUS;
		    }
		};
		job.schedule();		
	}
	
	private IPath getPath(){
		return (IPath)getPropertyValue(PROP_PATH);
	}
	
	
	
	@Override
	public String getDefaultDescription() {
		return super.getDefaultDescription() + " " + getPath(); //$NON-NLS-1$
	}

}

@@ -41,16 +41,52 @@
 tions;%0A%0A
+import java.io.BufferedInputStream;%0A
 import j
@@ -105,16 +105,16 @@
 eption;%0A
-
 import j
@@ -901,56 +901,8 @@
 us;%0A
-import org.eclipse.jface.dialogs.MessageDialog;%0A
 impo
@@ -1901,24 +1901,210 @@
  %0A%09%09%09       
+final BufferedInputStream bis;%0A%09%09%09       if(!(in instanceof BufferedInputStream))%0A%09%09%09    %09   bis = new BufferedInputStream(in);%0A%09%09%09       else%0A%09%09%09    %09   bis = (BufferedInputStream) in;%0A
 %0A%09%09         
@@ -2165,26 +2165,27 @@
 InputStream(
-in
+bis
 );%0A%09%09       
@@ -2469,32 +2469,78 @@
 stream.close();%0A
+%09%09%09%09%09%09%09%09%09bis.close();%0A%09%09%09%09%09%09%09%09%09if(in != bis)%0A%09
 %09%09%09%09%09%09%09%09%09in.clos
@@ -2536,32 +2536,32 @@
 %09%09%09%09in.close();%0A
-
 %09%09%09%09%09%09%09%09%7D catch 
@@ -2763,16 +2763,16 @@
 on e) %7B%0A
+
 %09%09      
@@ -2881,82 +2881,8 @@
  %09%09%0A
-%09%09        %09MessageDialog.openError(null, %22Failed to play file%22, message);%0A
 %09%09  

fix a bug that wav file cannot be played on linux_x64