view java/src/uk/ac/qmul/eecs/ccmi/gui/AudioFeedback.java @ 8:ea7885bd9bff tip

fixed bug : render solid line as dotted/dashed when moving the stylus from dotted/dashed to solid
author ccmi-guest
date Thu, 03 Jul 2014 16:12:20 +0100
parents 9e67171477bc
children
line wrap: on
line source
/*  
 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
  
 Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/  
package uk.ac.qmul.eecs.ccmi.gui;

import java.text.MessageFormat;
import java.util.ResourceBundle;

import uk.ac.qmul.eecs.ccmi.diagrammodel.CollectionEvent;
import uk.ac.qmul.eecs.ccmi.diagrammodel.CollectionListener;
import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramElement;
import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramTreeNodeListener;
import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramTreeNodeEvent;
import uk.ac.qmul.eecs.ccmi.diagrammodel.ElementChangedEvent;
import uk.ac.qmul.eecs.ccmi.diagrammodel.ElementChangedEvent.PropertyChangeArgs;
import uk.ac.qmul.eecs.ccmi.sound.PlayerListener;
import uk.ac.qmul.eecs.ccmi.sound.SoundEvent;
import uk.ac.qmul.eecs.ccmi.sound.SoundFactory;
import uk.ac.qmul.eecs.ccmi.speech.NarratorFactory;

/**
 * This class is a listener providing audio (speech + sound) feedback to changes on the 
 * model (e.g. node added, node removed, node name changed etc.) operated only on the local (so not from 
 * a tree of another user sharing the same diagram)  
 * tree it is linked to. If the source of the events is different from the local tree , then no action 
 * is performed. 
 */
public class AudioFeedback implements CollectionListener, DiagramTreeNodeListener  {

	/**
	 * Construct an {@code AudioFeedback} object linked to a {@code DiagramTree}.
	 * 
	 * @param tree the tree this instance is going to be linked to
	 */
	AudioFeedback(DiagramTree tree){
		resources = ResourceBundle.getBundle(EditorFrame.class.getName());
		this.tree = tree;
	}
	
	@Override
	public void elementInserted(CollectionEvent e) {
		DiagramEventSource source = (DiagramEventSource)e.getSource();
		if(source.isLocal() && source.type == DiagramEventSource.Type.TREE){
			final DiagramElement diagramElement = e.getDiagramElement();
			boolean isNode = diagramElement instanceof Node;
			if(isNode){
				SoundFactory.getInstance().play( SoundEvent.OK ,new PlayerListener(){
					@Override
					public void playEnded() {
						NarratorFactory.getInstance().speak(MessageFormat.format(resources.getString("speech.input.node.ack"),diagramElement.spokenText()));
					}
				});
			}else{
				Edge edge = (Edge)diagramElement;
				final StringBuilder builder = new StringBuilder();
				for(int i=0; i<edge.getNodesNum();i++){
					if(i == edge.getNodesNum()-1)
						builder.append(edge.getNodeAt(i)+resources.getString("speech.input.edge.ack"));
					else
						builder.append(edge.getNodeAt(i)+ resources.getString("speech.input.edge.ack2"));
				   	}
				   	SoundFactory.getInstance().play( SoundEvent.OK, new PlayerListener(){
				   		@Override
				   		public void playEnded() {
				   			NarratorFactory.getInstance().speak(builder.toString());
				   		}
				   });
			}
		}
	}

	@Override
	public void elementTakenOut(CollectionEvent e) {
		DiagramEventSource source = (DiagramEventSource)e.getSource();
		if(source.isLocal() && source.type == DiagramEventSource.Type.TREE){
			final DiagramElement element = e.getDiagramElement();
			SoundFactory.getInstance().play(SoundEvent.OK, new PlayerListener(){
				@Override
				public void playEnded() {
					NarratorFactory.getInstance().speak(MessageFormat.format(resources.getString("speech.delete.element.ack"),element.spokenText(),tree.currentPathSpeech()));
				}
			});
		}
	}

	@Override
	public void elementChanged(ElementChangedEvent e) {
		DiagramEventSource source = (DiagramEventSource)e.getSource();
		if(!source.isLocal() || source.type != DiagramEventSource.Type.TREE)
			return;
		String change = e.getChangeType();
		if("name".equals(change)){
			playOK(tree.currentPathSpeech());
		}else if ("property.add".equals(change)){
			PropertyChangeArgs args = (PropertyChangeArgs)e.getArguments();
			String propertyValue = ((Node)e.getDiagramElement()).getProperties().getValues(args.getPropertyType()).get(args.getPropertyIndex());
			playOK(MessageFormat.format(resources.getString("speech.input.property.ack"),propertyValue));
		}else if("property.set".equals(change)){
			playOK(tree.currentPathSpeech());
		}else if("property.remove".equals(change)){
			PropertyChangeArgs args = (PropertyChangeArgs)e.getArguments();
			playOK(MessageFormat.format(resources.getString("speech.deleted.property.ack"),args.getOldValue(),tree.currentPathSpeech()));
		}else if("property.modifiers".equals(change)){
			playOK(tree.currentPathSpeech());
		}else if("arrowHead".equals(change)||"endLabel".equals(change)){
			playOK(tree.currentPathSpeech());
		}
	}

	@Override
	public void bookmarkAdded(DiagramTreeNodeEvent evt) {
		DiagramEventSource source = (DiagramEventSource)evt.getSource();
		if(source.isLocal() && source.type == DiagramEventSource.Type.TREE){
			playOK(tree.currentPathSpeech());
		}
	}

	@Override
	public void bookmarkRemoved(DiagramTreeNodeEvent evt) {
		DiagramEventSource source = (DiagramEventSource)evt.getSource();
		if(source.isLocal() && source.type == DiagramEventSource.Type.TREE){
			playOK(MessageFormat.format(
					resources.getString("speech.delete.bookmark.ack"), 
					evt.getValue(),
					tree.currentPathSpeech()));
		}
	}

	@Override
	public void notesChanged(DiagramTreeNodeEvent evt) {
		DiagramEventSource source = (DiagramEventSource)evt.getSource();
		if(source.isLocal() && source.type == DiagramEventSource.Type.TREE){
			playOK(tree.currentPathSpeech());
		}
	}
	
	private void playOK(final String speech){
		SoundFactory.getInstance().play(SoundEvent.OK, new PlayerListener(){
			   @Override
			   public void playEnded() {
				   NarratorFactory.getInstance().speak(speech);
			   }
		   });
	}

	private ResourceBundle resources;
	private DiagramTree tree;
}