annotate 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
rev   line source
fiore@0 1 /*
fiore@0 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
fiore@0 3
fiore@0 4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
fiore@0 5
fiore@0 6 This program is free software: you can redistribute it and/or modify
fiore@0 7 it under the terms of the GNU General Public License as published by
fiore@0 8 the Free Software Foundation, either version 3 of the License, or
fiore@0 9 (at your option) any later version.
fiore@0 10
fiore@0 11 This program is distributed in the hope that it will be useful,
fiore@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
fiore@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fiore@0 14 GNU General Public License for more details.
fiore@0 15
fiore@0 16 You should have received a copy of the GNU General Public License
fiore@0 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
fiore@0 18 */
fiore@0 19
fiore@0 20 package uk.ac.qmul.eecs.ccmi.gui;
fiore@0 21
fiore@0 22 import java.awt.Component;
fiore@0 23 import java.awt.Dimension;
fiore@0 24 import java.awt.Toolkit;
fiore@0 25 import java.awt.event.InputEvent;
fiore@0 26 import java.awt.event.KeyEvent;
fiore@0 27 import java.awt.event.WindowAdapter;
fiore@0 28 import java.awt.event.WindowEvent;
fiore@0 29
fiore@0 30 import javax.swing.JComponent;
fiore@0 31 import javax.swing.JDialog;
fiore@0 32 import javax.swing.JLabel;
fiore@0 33 import javax.swing.JOptionPane;
fiore@0 34 import javax.swing.JScrollPane;
fiore@0 35 import javax.swing.JTextArea;
fiore@0 36 import javax.swing.KeyStroke;
fiore@0 37
fiore@0 38 import uk.ac.qmul.eecs.ccmi.sound.SoundEvent;
fiore@0 39 import uk.ac.qmul.eecs.ccmi.sound.SoundFactory;
fiore@0 40 import uk.ac.qmul.eecs.ccmi.speech.NarratorFactory;
fiore@0 41 import uk.ac.qmul.eecs.ccmi.speech.SpeechUtilities;
fiore@0 42
fiore@0 43 /**
fiore@0 44 * Abstract class with an one-line call to display a summary dialog.
fiore@0 45 * The summary text as well as focused components are spoken out through text to speech
fiore@0 46 * synthesis performed by the {@link Narrator} instance.
fiore@0 47 * A summary dialog has non editable text field and a button
fiore@0 48 * for confirmation only.
fiore@0 49 *
fiore@0 50 *
fiore@0 51 */
fiore@0 52 public abstract class SpeechSummaryPane {
fiore@0 53
fiore@0 54 public static int showDialog(Component parentComponent, String title, String text, int optionType, String[] options){
fiore@0 55 if(optionType == OK_CANCEL_OPTION && options.length < 2)
fiore@0 56 throw new IllegalArgumentException("option type and opions number must be consistent");
fiore@0 57 final JTextArea textArea = new JTextArea();
fiore@0 58 textArea.setText(text);
fiore@0 59 NarratorFactory.getInstance().speak(title+". "+ text);
fiore@0 60
fiore@0 61 JScrollPane componentToDisplay = new JScrollPane(textArea);
fiore@0 62 /* set the maximum size: if there is a lot of content yet it doesn't take the whole screen */
fiore@0 63 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
fiore@0 64
fiore@0 65 int editorWidth = (int)screenSize.getWidth() * 5 / 8;
fiore@0 66 int editorHeight = (int)screenSize.getHeight() * 5 / 8;
fiore@0 67
fiore@0 68 Dimension currentSize = componentToDisplay.getPreferredSize();
fiore@0 69 componentToDisplay.setPreferredSize(new Dimension(
fiore@0 70 Math.min(currentSize.width, editorWidth) , Math.min(currentSize.height, editorHeight)));
fiore@0 71
fiore@0 72 Object[] displayObjects = { new JLabel(title), componentToDisplay };
fiore@0 73 final JOptionPane optPane = new JOptionPane();
fiore@0 74 optPane.setMessage(displayObjects);
fiore@0 75 optPane.setMessageType(JOptionPane.PLAIN_MESSAGE);
fiore@0 76 optPane.setOptionType(optionType);
fiore@0 77 /* set the options according to the option type */
fiore@0 78 optPane.setOptions(options);
fiore@0 79 /* ctrl key will hush the TTS */
fiore@0 80 optPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL,InputEvent.CTRL_DOWN_MASK),"shut_up");
fiore@0 81 optPane.getActionMap().put("shut_up", SpeechUtilities.getShutUpAction());
fiore@0 82
fiore@0 83 final JDialog dialog = optPane.createDialog(parentComponent, "");
fiore@0 84 dialog.setResizable(true);
fiore@0 85
fiore@0 86 dialog.addWindowFocusListener(new WindowAdapter(){
fiore@0 87 @Override
fiore@0 88 public void windowGainedFocus(WindowEvent e) {
fiore@0 89 textArea.requestFocusInWindow();
fiore@0 90 }
fiore@0 91 });
fiore@0 92
fiore@0 93 SpeechUtilities.changeTabListener(optPane,dialog);
fiore@0 94 /* the textArea is not editable, so tab key event must not be consumed so that it can be picked up by the focus manager */
fiore@0 95 textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0), "none");
fiore@0 96 textArea.addKeyListener(SpeechUtilities.getSpeechKeyListener(false));
fiore@0 97 textArea.setEditable(false);
fiore@0 98 // start the editing sound
fiore@0 99 SoundFactory.getInstance().startLoop(SoundEvent.EDITING);
fiore@0 100 dialog.setVisible(true);
fiore@0 101 SoundFactory.getInstance().stopLoop(SoundEvent.EDITING);
fiore@0 102 NarratorFactory.getInstance().shutUp();
fiore@0 103
fiore@0 104 if(optPane.getValue() == null)//window closed
fiore@0 105 return CANCEL;
fiore@0 106 else if(optPane.getValue().equals(options[OK]))// pressed on OK
fiore@0 107 return OK;
fiore@0 108 else //pressed on cancel
fiore@0 109 return CANCEL;
fiore@0 110 }
fiore@0 111
fiore@0 112 public static final int OK = 0;
fiore@0 113 public static final int CANCEL = 1;
fiore@0 114 public static final int OK_CANCEL_OPTION = JOptionPane.OK_CANCEL_OPTION;
fiore@0 115 public static final int OK_OPTION = JOptionPane.OK_OPTION;
fiore@0 116 }