FestivalPlugin.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.plugin.festival |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* 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.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.plugin.festival;
import java.awt.GridBagLayout;
import java.io.IOException;
import java.util.List;
import java.beans.*;
import javax.swing.JTextField;
import org.apache.log4j.Logger;
import org.xnap.XNap;
import org.xnap.XNapFacade;
import org.xnap.chat.Channel;
import org.xnap.chat.ChannelEvent;
import org.xnap.chat.ChannelListener;
import org.xnap.chat.ChatManager;
import org.xnap.event.ListEvent;
import org.xnap.event.ListListener;
import org.xnap.gui.*;
import org.xnap.gui.util.*;
import org.xnap.gui.component.FilePanel;
import org.xnap.gui.event.AbstractPreferencesDialogListener;
import org.xnap.gui.util.GridBagHelper;
import org.xnap.plugin.AbstractPlugin;
import org.xnap.plugin.PluginInitializeException;
import org.xnap.util.AbstractPluginPreferences;
import org.xnap.util.StringHelper;
/**
* Provides a simple text-to-speech plugin using festival.
*
* For now it reads incoming chat messages aloud.
*/
public class FestivalPlugin extends AbstractPlugin
implements ListListener, ChannelListener, PropertyChangeListener
{
//--- Constant(s) ---
public static String ICON_FILENAME = "festival.png";
//--- Data field(s) ---
/**
* Remembers if there was an exception starting festival.
*/
private boolean failed = false;
private Process festivalProcess = null;
private FestivalRunner runner = null;
private FestivalMessageQueue queue;
private FestivalPreferences festivalPrefs;
private static Logger logger = Logger.getLogger(FestivalPlugin.class);
private PreferencesDialogListener pdl;
//--- Constructor(s) ---
public FestivalPlugin()
{
}
//--- Method(s) ---
/**
* @see xnap.plugin.Plugin#start()
*/
public void start() throws Exception
{
festivalPrefs = new FestivalPreferences();
queue = new FestivalMessageQueue();
// start festival process first in case there is an exception
try {
startFestivalProcess();
}
catch (IOException ie) {
// throw new PluginInitializeException
// (XNap.tr("Could not start Festival."));
}
// register existing channels
Channel[] channels = ChatManager.getInstance().getChannels();
for (int i = 0; i < channels.length; i++) {
channels[i].addChannelListener(this);
}
// register list listener to get notification of new channels
ChatManager.getInstance().addChannelListListener(this);
festivalPrefs.addPropertyChangeListener("command", this);
}
private void startFestivalProcess() throws IOException
{
festivalProcess = Runtime.getRuntime().exec(festivalPrefs.getCommand());
runner = new FestivalRunner(queue, festivalProcess.getOutputStream());
runner.start();
}
private void stopFestivalProcess()
{
if (runner != null) {
runner.stop();
runner = null;
}
if (festivalProcess != null) {
festivalProcess.destroy();
festivalProcess = null;
}
}
private void tts(String text)
{
text = StringHelper.replaceAll(text, "'\"", "");
text = "(SayText \"" + text + "\")\n";
queue.addMessage(text);
}
public String getName()
{
return getInfo().getName();
}
/**
* @see xnap.plugin.Plugin#startGUI()
*/
public void startGUI()
{
pdl = new PreferencesDialogListener();
XNapFacade.addPluginPreferencesDialogListener(pdl);
}
/**
* @see xnap.plugin.Plugin#stop()
*/
public void stop()
{
ChatManager.getInstance().removeChannelListListener(this);
Channel[] channels = ChatManager.getInstance().getChannels();
for (int i = 0; i < channels.length; i++) {
channels[i].removeChannelListener(this);
}
queue.clear();
queue = null;
stopFestivalProcess();
festivalPrefs.removePropertyChangeListener("command", this);
festivalPrefs = null;
}
/**
* @see xnap.plugin.Plugin#stopGUI()
*/
public void stopGUI()
{
pdl.dispose();
XNapFacade.removePluginPreferencesDialogListener(pdl);
pdl = null;
}
/**
* @see xnap.event.ListListener#itemAdded(xnap.event.ListEvent)
*/
public void itemAdded(ListEvent event)
{
((Channel)event.getItem()).addChannelListener(this);
}
/**
* @see xnap.event.ListListener#itemRemoved(xnap.event.ListEvent)
*/
public void itemRemoved(ListEvent event)
{
((Channel)event.getItem()).removeChannelListener(this);
}
/**
* @see xnap.chat.ChannelListener#channelJoined(xnap.chat.ChannelEvent)
*/
public void channelJoined(ChannelEvent event)
{
}
/**
* @see xnap.chat.ChannelListener#channelParted(xnap.chat.ChannelEvent)
*/
public void channelParted(ChannelEvent event)
{
}
/**
* @see xnap.chat.ChannelListener#messageReceived(xnap.chat.ChannelEvent)
*/
public void messageReceived(ChannelEvent event)
{
if (!((Channel)event.getSource()).isLocal(event.getPeer())) {
tts(event.getMessage());
}
}
/**
* @see xnap.chat.ChannelListener#peerAdded(xnap.chat.ChannelEvent)
*/
public void peerAdded(ChannelEvent event)
{
}
/**
* @see xnap.chat.ChannelListener#peerChanged(xnap.chat.ChannelEvent)
*/
public void peerChanged(ChannelEvent event)
{
}
/**
* @see xnap.chat.ChannelListener#peerRemoved(xnap.chat.ChannelEvent)
*/
public void peerRemoved(ChannelEvent event)
{
}
/**
* @see xnap.chat.ChannelListener#topicChanged(xnap.chat.ChannelEvent)
*/
public void topicChanged(ChannelEvent event)
{
}
public void propertyChange(PropertyChangeEvent e)
{
stopFestivalProcess();
try {
startFestivalProcess();
}
catch (IOException ie) {
// maybe show this to the user
logger.debug("Could not start festival process", ie);
}
}
// --- Inner Class(es) ---
private static class FestivalPreferences extends AbstractPluginPreferences {
public static final int VERSION = 1;
public FestivalPreferences()
{
super("plugin.festival", VERSION);
setDefault("command", "festival");
}
public String getCommand() { return get("command"); }
public void setCommand(String command) { set("command", command); }
}
private class PreferencesDialogListener
extends AbstractPreferencesDialogListener
{
public void addPanels(AbstractPreferencesDialog dialog, List panels)
{
FestivalPreferencesPanel jpp = new FestivalPreferencesPanel();
panels.add(jpp);
panels.add(dialog.addPanel(jpp, ICON_FILENAME));
}
}
private class FestivalPreferencesPanel extends AbstractSettingsPanel {
//--- Data field(s) ---
private FilePanel commandTextField;
//--- Constructor(s) ---
public FestivalPreferencesPanel()
{
setLayout(new GridBagLayout());
// command name
commandTextField = new FilePanel(festivalPrefs.getCommand(), 20);
GridBagHelper.addLabel(this, XNap.tr("Command")).setLabelFor
(commandTextField.getTextField());
GridBagHelper.add(this, commandTextField, false);
GridBagHelper.addVerticalSpacer(this);
GUIHelper.setMnemonics(this);
}
//--- Method(s) ---
public void apply()
{
festivalPrefs.setCommand(commandTextField.getFilename());
}
public String getDescription()
{
return XNap.tr("Command configuration.");
}
public String getTitle()
{
return FestivalPlugin.this.getInfo().getName();
}
}
}
The table below shows all metrics for FestivalPlugin.java.




