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