<commit_before>package eu.ydp.empiria.player.client.module.simple.audioplayer;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.xml.client.Element;

import eu.ydp.empiria.player.client.controller.events.interaction.MediaInteractionSoundEventCallback;
import eu.ydp.empiria.player.client.controller.events.interaction.MediaInteractionSoundEventCallforward;
import eu.ydp.empiria.player.client.module.ISimpleModule;
import eu.ydp.empiria.player.client.module.ModuleSocket;
import eu.ydp.empiria.player.client.module.listener.MediaModuleInteractionListener;
import eu.ydp.empiria.player.client.module.listener.ModuleInteractionListener;
import eu.ydp.empiria.player.client.util.xml.XMLUtils;

public class AudioPlayerModule implements ISimpleModule {
	
	public AudioPlayerModule(){
		button = new PushButton();
	}
	
	protected PushButton button;
	protected String address;
	protected MediaModuleInteractionListener mediaListener;
	protected boolean playing;
	
	protected MediaInteractionSoundEventCallforward callforward;

	@Override
	public void initModule(Element element, ModuleSocket ms, ModuleInteractionListener mil) {

		mediaListener = mil;
		
		address = XMLUtils.getAttributeAsString(element, "src");
		if (address == null  ||  "".equals(address))
			address = XMLUtils.getAttributeAsString(element, "data");
		
		playing = false;
		
		button.setStyleName("qp-audioplayer-button");
		button.getElement().setId(com.google.gwt.dom.client.Document.get().createUniqueId());
		
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				onButtonClick();
			}
		});
	}

	@Override
	public Widget getView() {
		return button;
	}
	
	private void play(){
		mediaListener.onMediaSoundPlay(address, new MediaInteractionSoundEventCallback() {
			
			@Override
			public void onStop() {
				setStopped();
			}
			
			@Override
			public void onPlay() {
				setPlaying();
			}

			@Override
			public void setCallforward(MediaInteractionSoundEventCallforward cf) {
				callforward = cf;
			}
		});
	}
	
	protected void stop(){
		if (callforward != null)
			callforward.stop();
	}
	
	protected void setPlaying(){
		playing = true;
		button.setStyleName("qp-audioplayer-button-playing");
	}
	
	
	protected void setStopped(){
		playing = false;
		button.setStyleName("qp-audioplayer-button");	
	}

	
	protected void onButtonClick() {
		if (!playing)
			play();
		else
			stop();
		
	}<commit_msg>set button disabled between play/onPlay (and stop/onStop) task#57594<commit_after>package eu.ydp.empiria.player.client.module.simple.audioplayer;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.xml.client.Element;

import eu.ydp.empiria.player.client.controller.events.interaction.MediaInteractionSoundEventCallback;
import eu.ydp.empiria.player.client.controller.events.interaction.MediaInteractionSoundEventCallforward;
import eu.ydp.empiria.player.client.module.ISimpleModule;
import eu.ydp.empiria.player.client.module.ModuleSocket;
import eu.ydp.empiria.player.client.module.listener.MediaModuleInteractionListener;
import eu.ydp.empiria.player.client.module.listener.ModuleInteractionListener;
import eu.ydp.empiria.player.client.util.xml.XMLUtils;

public class AudioPlayerModule implements ISimpleModule {
	
	public AudioPlayerModule(){
		button = new PushButton();
	}
	
	protected PushButton button;
	protected String address;
	protected MediaModuleInteractionListener mediaListener;
	protected boolean playing;
	protected boolean enabled = true;
	
	protected MediaInteractionSoundEventCallforward callforward;

	@Override
	public void initModule(Element element, ModuleSocket ms, ModuleInteractionListener mil) {

		mediaListener = mil;
		
		address = XMLUtils.getAttributeAsString(element, "src");
		if (address == null  ||  "".equals(address))
			address = XMLUtils.getAttributeAsString(element, "data");
		
		playing = false;
		
		button.setStyleName("qp-audioplayer-button");
		button.getElement().setId(com.google.gwt.dom.client.Document.get().createUniqueId());
		
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				onButtonClick();
			}
		});
	}

	@Override
	public Widget getView() {
		return button;
	}
	
	private void play(){
		enabled = false;
		mediaListener.onMediaSoundPlay(address, new MediaInteractionSoundEventCallback() {
			
			@Override
			public void onStop() {
				setStopped();
			}
			
			@Override
			public void onPlay() {
				setPlaying();
			}

			@Override
			public void setCallforward(MediaInteractionSoundEventCallforward cf) {
				callforward = cf;
			}
		});
	}
	
	protected void stop(){
		enabled = false;
		if (callforward != null)
			callforward.stop();
	}
	
	protected void setPlaying(){
		enabled = true;
		playing = true;
		button.setStyleName("qp-audioplayer-button-playing");
	}
	
	
	protected void setStopped(){
		enabled = true;
		playing = false;
		button.setStyleName("qp-audioplayer-button");	
	}

	
	protected void onButtonClick() {
		if (enabled){
			if (!playing)
				play();
			else
				stop();
		}
	}
{'fraction_non_alphanumeric': 0.11139944392956441, 'fraction_numerical': 0.0009267840593141798, 'mean_word_length': 4.58592132505176, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4632532', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1639, 'n_words': 301}