view java/src/uk/ac/qmul/eecs/ccmi/gui/SpeechLogDialog.java @ 0:9418ab7b7f3f

Initial import
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Fri, 16 Dec 2011 17:35:51 +0000
parents
children
line wrap: on
line source
/*  
 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
  
 Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package uk.ac.qmul.eecs.ccmi.gui;

import java.awt.Frame;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

import uk.ac.qmul.eecs.ccmi.speech.NarratorFactory;

/**
 * A log handler that displays log records on a JTextArea in a dedicated Frame and speaks them out
 * through text to speech synthesis performed by the {@link Narrator} instance. 
 *
 */
public class SpeechLogDialog extends Handler {
	public static SpeechLogDialog getSpeechLogDialog(Frame parent){
		/* the static reference prevent the handler from being garbage collected * 
		 * as the logger has a static management   							     */
		if(speechLogDialog == null)
			speechLogDialog = new SpeechLogDialog(parent);
		return speechLogDialog;
	}
	
	private SpeechLogDialog(Frame parent){
		setLevel(Level.CONFIG);
		
		area = new JTextArea(ROWS,COLS);
		area.setEditable(false);
		area.setLineWrap(true);
		area.addFocusListener(new FocusAdapter(){
			@Override
			public void	focusGained(FocusEvent e) {
				NarratorFactory.getInstance().speak("Server window focused");
			}
		});

		frame = new JFrame("Server");
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.getContentPane().add(new JScrollPane(area,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER));
		frame.pack();
        frame.setLocationRelativeTo(parent);
        frame.setVisible(true);
	}

	@Override
	public void close() throws SecurityException {
		frame.dispose();
		speechLogDialog = null;
	}

	@Override
	public void flush() {}

	@Override
	public void publish(LogRecord log) {
		String prefix = "";
		if(log.getLevel().equals(Level.WARNING)){
			prefix = WARNING_PREFIX;
		}
		if(log.getLevel().equals(Level.SEVERE))
			prefix = ERROR_PREFIX;
		String message = prefix+log.getMessage()+'\n';
		NarratorFactory.getInstance().speakWholeText(message);
		area.append(message);
	}
	
	JFrame frame;
	JTextArea area;
	private static final int ROWS = 10;
	private static final int COLS = 50;
	private static final String WARNING_PREFIX = "WARNING: ";
	private static final String ERROR_PREFIX = "ERROR: ";
	private static SpeechLogDialog speechLogDialog;
}