view java/src/uk/ac/qmul/eecs/ccmi/gui/SpeechSummaryPane.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 d66dd5880081
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.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 {@code Narrator} instance.  
 * A summary dialog has non editable text field and a button for confirmation only.  
 * 
 *
 */
public abstract class SpeechSummaryPane {
	
	/**
	 * shows the summary dialog 
	 * @param parentComponent determines the {@code Frame} in which the dialog is displayed
	 * @param title the title of the displayed dialog
	 * @param text the text to be displayed in this dialog. his text, together with the title 
	 *   is uttered through the {@code Narrator} as soon as the dialog is shown  
	 * @param optionType  an integer designating the options available on the dialog 
	 *  either {@code OK_CANCEL_OPTION} or {@code OK_OPTION} 
	 * @param options an array of strings indicating the possible choices the user can make 
	 * @return an integer indicating the option selected by the user. Either {@code OK} or {@code CANCEL}
	 */
	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);
		dialog.dispose();
		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;
}