annotate java/src/uk/ac/qmul/eecs/ccmi/gui/SpeechSummaryPane.java @ 1:e3935c01cde2 tip

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