diff java/src/uk/ac/qmul/eecs/ccmi/gui/SpeechSummaryPane.java @ 0:9418ab7b7f3f

Initial import
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Fri, 16 Dec 2011 17:35:51 +0000
parents
children d66dd5880081
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/src/uk/ac/qmul/eecs/ccmi/gui/SpeechSummaryPane.java	Fri Dec 16 17:35:51 2011 +0000
@@ -0,0 +1,116 @@
+/*  
+ 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.awt.Component;
+import java.awt.Dimension;
+import java.awt.Toolkit;
+import java.awt.event.InputEvent;
+import java.awt.event.KeyEvent;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+
+import javax.swing.JComponent;
+import javax.swing.JDialog;
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
+import javax.swing.KeyStroke;
+
+import uk.ac.qmul.eecs.ccmi.sound.SoundEvent;
+import uk.ac.qmul.eecs.ccmi.sound.SoundFactory;
+import uk.ac.qmul.eecs.ccmi.speech.NarratorFactory;
+import uk.ac.qmul.eecs.ccmi.speech.SpeechUtilities;
+
+/**
+ * Abstract class with an one-line call to display a summary dialog.
+ * The summary text as well as focused components are spoken out through text to speech 
+ * synthesis performed by the {@link Narrator} instance.  
+ * A summary dialog has non editable text field and a button
+ * for confirmation only.  
+ * 
+ *
+ */
+public abstract class SpeechSummaryPane {
+	
+	public static int showDialog(Component parentComponent, String title, String text, int optionType, String[] options){
+		if(optionType == OK_CANCEL_OPTION && options.length < 2)
+			throw new IllegalArgumentException("option type and opions number must be consistent");
+		final JTextArea textArea =  new JTextArea();
+		textArea.setText(text);
+		NarratorFactory.getInstance().speak(title+". "+ text);
+		
+		JScrollPane componentToDisplay = new JScrollPane(textArea);
+		/* set the maximum size: if there is a lot of content yet it doesn't take the whole screen */
+		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
+		
+		int editorWidth = (int)screenSize.getWidth() * 5 / 8;
+		int editorHeight = (int)screenSize.getHeight() * 5 / 8;
+		
+		Dimension currentSize = componentToDisplay.getPreferredSize();
+		componentToDisplay.setPreferredSize(new Dimension(
+				Math.min(currentSize.width, editorWidth) , Math.min(currentSize.height, editorHeight)));
+		
+		Object[] displayObjects = { new JLabel(title), componentToDisplay };
+		final JOptionPane optPane = new JOptionPane();
+		optPane.setMessage(displayObjects);
+		optPane.setMessageType(JOptionPane.PLAIN_MESSAGE);	
+		optPane.setOptionType(optionType);
+		/* set the options according to the option type */
+		optPane.setOptions(options);
+		/* ctrl key will hush the TTS */
+		optPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL,InputEvent.CTRL_DOWN_MASK),"shut_up");
+		optPane.getActionMap().put("shut_up", SpeechUtilities.getShutUpAction());
+		
+		final JDialog dialog = optPane.createDialog(parentComponent,  "");
+		dialog.setResizable(true);
+		
+		dialog.addWindowFocusListener(new WindowAdapter(){
+			@Override
+			public void windowGainedFocus(WindowEvent e) {
+				textArea.requestFocusInWindow();
+		    }
+		});
+		
+		SpeechUtilities.changeTabListener(optPane,dialog);
+		/* the textArea is not editable, so tab key event must not be consumed so that it can be picked up by the focus manager */
+		textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0), "none");	
+		textArea.addKeyListener(SpeechUtilities.getSpeechKeyListener(false));
+		textArea.setEditable(false);
+		// start the editing sound
+		SoundFactory.getInstance().startLoop(SoundEvent.EDITING);
+		dialog.setVisible(true);
+		SoundFactory.getInstance().stopLoop(SoundEvent.EDITING);
+		NarratorFactory.getInstance().shutUp();
+		
+		if(optPane.getValue() == null)//window closed
+			return CANCEL;
+		else if(optPane.getValue().equals(options[OK]))// pressed on OK
+			return OK;
+		else //pressed on cancel
+			return CANCEL;
+	}	
+
+	public static final int OK = 0;
+	public static final int CANCEL = 1;
+	public static final int OK_CANCEL_OPTION = JOptionPane.OK_CANCEL_OPTION;
+	public static final int OK_OPTION = JOptionPane.OK_OPTION;
+}