f@0: /* f@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool f@0: f@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) f@0: f@0: This program is free software: you can redistribute it and/or modify f@0: it under the terms of the GNU General Public License as published by f@0: the Free Software Foundation, either version 3 of the License, or f@0: (at your option) any later version. f@0: f@0: This program is distributed in the hope that it will be useful, f@0: but WITHOUT ANY WARRANTY; without even the implied warranty of f@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the f@0: GNU General Public License for more details. f@0: f@0: You should have received a copy of the GNU General Public License f@0: along with this program. If not, see . f@0: */ f@0: f@0: package uk.ac.qmul.eecs.ccmi.gui; f@0: f@0: import java.awt.Component; f@0: import java.awt.Dimension; f@0: import java.awt.Toolkit; f@0: import java.awt.event.InputEvent; f@0: import java.awt.event.KeyEvent; f@0: import java.awt.event.WindowAdapter; f@0: import java.awt.event.WindowEvent; f@0: f@0: import javax.swing.JComponent; f@0: import javax.swing.JDialog; f@0: import javax.swing.JLabel; f@0: import javax.swing.JOptionPane; f@0: import javax.swing.JScrollPane; f@0: import javax.swing.JTextArea; f@0: import javax.swing.KeyStroke; f@0: f@0: import uk.ac.qmul.eecs.ccmi.sound.SoundEvent; f@0: import uk.ac.qmul.eecs.ccmi.sound.SoundFactory; f@0: import uk.ac.qmul.eecs.ccmi.speech.NarratorFactory; f@0: import uk.ac.qmul.eecs.ccmi.speech.SpeechUtilities; f@0: f@0: /** f@0: * Abstract class with an one-line call to display a summary dialog. f@0: * The summary text as well as focused components are spoken out through text to speech f@0: * synthesis performed by the {@code Narrator} instance. f@0: * A summary dialog has non editable text field and a button for confirmation only. f@0: * f@0: * f@0: */ f@0: public abstract class SpeechSummaryPane { f@0: f@0: /** f@0: * shows the summary dialog f@0: * @param parentComponent determines the {@code Frame} in which the dialog is displayed f@0: * @param title the title of the displayed dialog f@0: * @param text the text to be displayed in this dialog. his text, together with the title f@0: * is uttered through the {@code Narrator} as soon as the dialog is shown f@0: * @param optionType an integer designating the options available on the dialog f@0: * either {@code OK_CANCEL_OPTION} or {@code OK_OPTION} f@0: * @param options an array of strings indicating the possible choices the user can make f@0: * @return an integer indicating the option selected by the user. Either {@code OK} or {@code CANCEL} f@0: */ f@0: public static int showDialog(Component parentComponent, String title, String text, int optionType, String[] options){ f@0: if(optionType == OK_CANCEL_OPTION && options.length < 2) f@0: throw new IllegalArgumentException("option type and opions number must be consistent"); f@0: final JTextArea textArea = new JTextArea(); f@0: textArea.setText(text); f@0: NarratorFactory.getInstance().speak(title+". "+ text); f@0: f@0: JScrollPane componentToDisplay = new JScrollPane(textArea); f@0: /* set the maximum size: if there is a lot of content yet it doesn't take the whole screen */ f@0: Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); f@0: f@0: int editorWidth = (int)screenSize.getWidth() * 5 / 8; f@0: int editorHeight = (int)screenSize.getHeight() * 5 / 8; f@0: f@0: Dimension currentSize = componentToDisplay.getPreferredSize(); f@0: componentToDisplay.setPreferredSize(new Dimension( f@0: Math.min(currentSize.width, editorWidth) , Math.min(currentSize.height, editorHeight))); f@0: f@0: Object[] displayObjects = { new JLabel(title), componentToDisplay }; f@0: final JOptionPane optPane = new JOptionPane(); f@0: optPane.setMessage(displayObjects); f@0: optPane.setMessageType(JOptionPane.PLAIN_MESSAGE); f@0: optPane.setOptionType(optionType); f@0: /* set the options according to the option type */ f@0: optPane.setOptions(options); f@0: /* ctrl key will hush the TTS */ f@0: optPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL,InputEvent.CTRL_DOWN_MASK),"shut_up"); f@0: optPane.getActionMap().put("shut_up", SpeechUtilities.getShutUpAction()); f@0: f@0: final JDialog dialog = optPane.createDialog(parentComponent, ""); f@0: dialog.setResizable(true); f@0: f@0: dialog.addWindowFocusListener(new WindowAdapter(){ f@0: @Override f@0: public void windowGainedFocus(WindowEvent e) { f@0: textArea.requestFocusInWindow(); f@0: } f@0: }); f@0: f@0: SpeechUtilities.changeTabListener(optPane,dialog); f@0: /* 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: textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0), "none"); f@0: textArea.addKeyListener(SpeechUtilities.getSpeechKeyListener(false)); f@0: textArea.setEditable(false); f@0: // start the editing sound f@0: SoundFactory.getInstance().startLoop(SoundEvent.EDITING); f@0: dialog.setVisible(true); f@0: dialog.dispose(); f@0: SoundFactory.getInstance().stopLoop(SoundEvent.EDITING); f@0: NarratorFactory.getInstance().shutUp(); f@0: f@0: if(optPane.getValue() == null)//window closed f@0: return CANCEL; f@0: else if(optPane.getValue().equals(options[OK]))// pressed on OK f@0: return OK; f@0: else //pressed on cancel f@0: return CANCEL; f@0: } f@0: f@0: public static final int OK = 0; f@0: public static final int CANCEL = 1; f@0: public static final int OK_CANCEL_OPTION = JOptionPane.OK_CANCEL_OPTION; f@0: public static final int OK_OPTION = JOptionPane.OK_OPTION; f@0: }