diff java/src/uk/ac/qmul/eecs/ccmi/speech/TreeSonifier.java @ 0:78b7fc5391a2

first import, outcome of NIME 2014 hackaton
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 16:28:59 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/src/uk/ac/qmul/eecs/ccmi/speech/TreeSonifier.java	Tue Jul 08 16:28:59 2014 +0100
@@ -0,0 +1,163 @@
+/*  
+ 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.speech;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.KeyEvent;
+
+import javax.swing.AbstractAction;
+import javax.swing.JTree;
+import javax.swing.KeyStroke;
+import javax.swing.tree.DefaultMutableTreeNode;
+import javax.swing.tree.TreePath;
+
+import uk.ac.qmul.eecs.ccmi.sound.PlayerListener;
+import uk.ac.qmul.eecs.ccmi.sound.SoundEvent;
+import uk.ac.qmul.eecs.ccmi.sound.SoundFactory;
+
+/**
+ *
+ */
+@SuppressWarnings("serial")
+public class TreeSonifier {
+
+	public void sonify(final JTree tree){
+		/* select the root node as selected */ 
+		tree.setSelectionRow(0);
+		/* Overwrite keystrokes up,down,left,right arrows and space, shift, ctrl */
+		   tree.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0),"down");
+		   tree.getActionMap().put("down", new AbstractAction(){
+				@Override
+				public void actionPerformed(ActionEvent evt) {
+					DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
+					/* look if we've got a sibling node after (we are not at the bottom) */
+					DefaultMutableTreeNode nextTreeNode = treeNode.getNextSibling(); 
+					SoundEvent loop = null;
+					if(nextTreeNode == null){
+						DefaultMutableTreeNode parent = (DefaultMutableTreeNode)treeNode.getParent();
+						if(parent == null) /* root node, just stay there */
+							nextTreeNode = treeNode;
+						else /* loop = go to first child of own parent */
+							nextTreeNode = (DefaultMutableTreeNode)parent.getFirstChild();
+						loop = SoundEvent.LIST_BOTTOM_REACHED;
+					}
+					tree.setSelectionPath(new TreePath(nextTreeNode.getPath()));
+					final String currentPathSpeech = currentPathSpeech(tree);
+					SoundFactory.getInstance().play(loop, new PlayerListener(){
+						public void playEnded() {
+							NarratorFactory.getInstance().speak(currentPathSpeech);
+						}
+					});
+				}});
+		   
+		   tree.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0),"up");
+		   tree.getActionMap().put("up", new AbstractAction(){
+				@Override
+				public void actionPerformed(ActionEvent evt) {
+					DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
+					DefaultMutableTreeNode previousTreeNode = treeNode.getPreviousSibling();
+					SoundEvent loop = null;
+					if(previousTreeNode == null){
+						DefaultMutableTreeNode parent = (DefaultMutableTreeNode)treeNode.getParent();
+						if(parent == null) /* root node */
+							previousTreeNode = treeNode;
+						else 
+							previousTreeNode = (DefaultMutableTreeNode)parent.getLastChild();
+						loop = SoundEvent.LIST_TOP_REACHED;
+					}
+					tree.setSelectionPath(new TreePath(previousTreeNode.getPath()));
+					final String currentPathSpeech = currentPathSpeech(tree);
+					SoundFactory.getInstance().play(loop, new PlayerListener(){
+						public void playEnded() {
+							NarratorFactory.getInstance().speak(currentPathSpeech);
+						}
+					});
+				}});
+		   
+		   tree.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0),"right");
+		   tree.getActionMap().put("right", new AbstractAction(){
+			   @Override
+			   public void actionPerformed(ActionEvent evt) {
+				   TreePath path = tree.getSelectionPath();
+				   DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)path.getLastPathComponent();
+				   if(treeNode.isLeaf()){
+					   SoundFactory.getInstance().play(SoundEvent.ERROR);
+				   }
+				   else{
+					   tree.expandPath(path);
+					   tree.setSelectionPath(new TreePath(((DefaultMutableTreeNode)treeNode.getFirstChild()).getPath()));
+					   final String currentPathSpeech = currentPathSpeech(tree);
+					   SoundFactory.getInstance().play(SoundEvent.TREE_NODE_EXPAND,new PlayerListener(){
+						@Override
+						public void playEnded() {
+							NarratorFactory.getInstance().speak(currentPathSpeech);
+						}
+					   });
+				   }
+			   }
+		   });
+		   
+		   tree.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0),"left");
+		   tree.getActionMap().put("left", new AbstractAction(){
+			   @Override
+			   public void actionPerformed(ActionEvent evt) {
+				   TreePath path = tree.getSelectionPath();
+				   DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)path.getLastPathComponent();
+				   DefaultMutableTreeNode parent = (DefaultMutableTreeNode)treeNode.getParent();
+				   if(parent == null){/* root node */
+					   SoundFactory.getInstance().play(SoundEvent.ERROR);
+				   }
+				   else{
+					   TreePath newPath = new TreePath(((DefaultMutableTreeNode)parent).getPath());
+					   tree.setSelectionPath(newPath);
+					   tree.collapsePath(newPath);
+					   final String currentPathSpeech = currentPathSpeech(tree); 
+					   SoundFactory.getInstance().play(SoundEvent.TREE_NODE_COLLAPSE,new PlayerListener(){
+							@Override
+							public void playEnded() {
+								NarratorFactory.getInstance().speak(currentPathSpeech);
+							}
+					   });
+				   }
+			   }
+		   });
+		   
+		   tree.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,0),"space");
+		   tree.getActionMap().put("space",new AbstractAction(){
+			   @Override
+			   public void actionPerformed(ActionEvent arg0) {
+				   space(tree);
+			   }
+		   });
+		   /* make the tree ignore the page up and page down keys */
+		   tree.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP,0),"none");
+		   tree.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN,0),"none");
+	}
+
+	protected String currentPathSpeech(JTree tree) {
+		TreePath path =	tree.getSelectionPath();
+		DefaultMutableTreeNode selectedPathTreeNode = (DefaultMutableTreeNode)path.getLastPathComponent();
+		return selectedPathTreeNode.toString();
+	}
+	
+	protected void space(JTree tree){
+		NarratorFactory.getInstance().speak(currentPathSpeech(tree));
+	}
+	
+}