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