/*
* VirtualNewsReaderView.java
*/
package virtualnewsreader;
import java.awt.Component;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.speech.synthesis.SynthesizerModeDesc;
import javax.speech.synthesis.Voice;
import javax.swing.ComboBoxModel;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.text.JTextComponent;
/**
* The application's main frame.
*/
public class VirtualNewsReaderView extends FrameView {
public String firstVoice = "kevin16";
public VirtualNewsReaderView(SingleFrameApplication app) {
super(app);
this.playerModel = new PlayerModelImpl();
this.playerModel.createSynthesizers();
initComponents();
this.setVoice(firstVoice);
this. addComboBoxListeners();
// status bar initialization - message timeout, idle icon and busy animation, etc
ResourceMap resourceMap = getResourceMap();
int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
messageTimer = new Timer(messageTimeout, new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusMessageLabel.setText("");
}
});
messageTimer.setRepeats(false);
int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
for (int i = 0; i < busyIcons.length; i++) {
busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
}
busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
public void actionPerformed(ActionEvent e) {
busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
}
});
idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
// connecting action tasks to status bar via TaskMonitor
TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
if ("started".equals(propertyName)) {
if (!busyIconTimer.isRunning()) {
statusAnimationLabel.setIcon(busyIcons[0]);
busyIconIndex = 0;
busyIconTimer.start();
}
progressBar.setVisible(true);
progressBar.setIndeterminate(true);
} else if ("done".equals(propertyName)) {
busyIconTimer.stop();
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
progressBar.setValue(0);
} else if ("message".equals(propertyName)) {
String text = (String)(evt.getNewValue());
statusMessageLabel.setText((text == null) ? "" : text);
messageTimer.restart();
} else if ("progress".equals(propertyName)) {
int value = (Integer)(evt.getNewValue());
progressBar.setVisible(true);
progressBar.setIndeterminate(false);
progressBar.setValue(value);
}
}
});
}
public PlayerModel getModel() {
return playerModel;
}
@Action
public void showAboutBox() {
if (aboutBox == null) {
JFrame mainFrame = VirtualNewsReaderApp.getApplication().getMainFrame();
aboutBox = new VirtualNewsReaderAboutBox(mainFrame);
aboutBox.setLocationRelativeTo(mainFrame);
}
VirtualNewsReaderApp.getApplication().show(aboutBox);
}
@Action
public void NewDocument()
{
try {
getTabManager().addDocumentTab();
} catch (IOException ex) {
Logger.getLogger(VirtualNewsReaderView.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Action
public void NewBrowser()
{
getTabManager().addBrowserTab();
}
public TabManager getTabManager()
{
return tabManager;
}
public void SetStatusText(String str)
{
statusMessageLabel.setText(str);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
mainPanel = new javax.swing.JPanel();
tabManager = new virtualnewsreader.TabManager();
menuBar = new javax.swing.JMenuBar();
javax.swing.JMenu fileMenu = new javax.swing.JMenu();
fileNewDocument = new javax.swing.JMenuItem();
fileShowBrowser = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JSeparator();
javax.swing.JMenuItem fileExit = new javax.swing.JMenuItem();
playerMenu = new javax.swing.JMenu();
playerPlay = new javax.swing.JMenuItem();
playerPause = new javax.swing.JMenuItem();
playerStop = new javax.swing.JMenuItem();
playerRecord = new javax.swing.JMenuItem();
javax.swing.JMenu helpMenu = new javax.swing.JMenu();
javax.swing.JMenuItem helpAbout = new javax.swing.JMenuItem();
statusPanel = new javax.swing.JPanel();
statusMessageLabel = new javax.swing.JLabel();
statusAnimationLabel = new javax.swing.JLabel();
progressBar = new javax.swing.JProgressBar();
myToolBar = new javax.swing.JToolBar();
defaultToolBar = new javax.swing.JToolBar();
btnNewDocument = new javax.swing.JButton();
btnNewBrowser = new javax.swing.JButton();
convertorBar = new javax.swing.JToolBar();
cbtSynthesizer = createComboBox((ComboBoxModel) playerModel.getSynthesizerList(),
"Synthesizer",
"FreeTTS Synthesizer");
cbtVoice = createComboBox
((ComboBoxModel) playerModel.getVoiceList(),
"Voice", "Voice");
btnPlay = new javax.swing.JButton();
btnPause = new javax.swing.JToggleButton();
btnStop = new javax.swing.JButton();
btnRecord = new javax.swing.JButton();
mainPanel.setBorder(null);
mainPanel.setName("mainPanel"); // NOI18N
tabManager.setName("tabManager"); // NOI18N
javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tabManager, javax.swing.GroupLayout.DEFAULT_SIZE, 647, Short.MAX_VALUE)
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tabManager, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
);
menuBar.setBorder(javax.swing.BorderFactory.createEtchedBorder());
menuBar.setName("menuBar"); // NOI18N
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(virtualnewsreader.VirtualNewsReaderApp.class).getContext().getResourceMap(VirtualNewsReaderView.class);
fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
fileMenu.setName("fileMenu"); // NOI18N
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(virtualnewsreader.VirtualNewsReaderApp.class).getContext().getActionMap(VirtualNewsReaderView.class, this);
fileNewDocument.setAction(actionMap.get("NewDocument")); // NOI18N
fileNewDocument.setMnemonic('D');
fileNewDocument.setText(resourceMap.getString("fileNewDocument.text")); // NOI18N
fileNewDocument.setName("fileNewDocument"); // NOI18N
fileMenu.add(fileNewDocument);
fileShowBrowser.setAction(actionMap.get("NewBrowser")); // NOI18N
fileShowBrowser.setMnemonic('B');
fileShowBrowser.setText(resourceMap.getString("fileShowBrowser.text")); // NOI18N
fileShowBrowser.setName("fileShowBrowser"); // NOI18N
fileMenu.add(fileShowBrowser);
jSeparator1.setName("jSeparator1"); // NOI18N
fileMenu.add(jSeparator1);
fileExit.setAction(actionMap.get("quit")); // NOI18N
fileExit.setName("fileExit"); // NOI18N
fileMenu.add(fileExit);
menuBar.add(fileMenu);
playerMenu.setText(resourceMap.getString("playerMenu.text")); // NOI18N
playerMenu.setName("playerMenu"); // NOI18N
playerPlay.setAction(actionMap.get("Play")); // NOI18N
playerPlay.setMnemonic('p');
playerPlay.setText(resourceMap.getString("playerPlay.text")); // NOI18N
playerPlay.setName("playerPlay"); // NOI18N
playerMenu.add(playerPlay);
playerPause.setAction(actionMap.get("menuPause")); // NOI18N
playerPause.setText(resourceMap.getString("playerPause.text")); // NOI18N
playerPause.setName("playerPause"); // NOI18N
playerMenu.add(playerPause);
playerStop.setAction(actionMap.get("Stop")); // NOI18N
playerStop.setMnemonic('S');
playerStop.setText(resourceMap.getString("playerStop.text")); // NOI18N
playerStop.setName("playerStop"); // NOI18N
playerMenu.add(playerStop);
playerRecord.setAction(actionMap.get("ConvertToSoundFile")); // NOI18N
playerRecord.setMnemonic('R');
playerRecord.setText(resourceMap.getString("playerRecord.text")); // NOI18N
playerRecord.setName("playerRecord"); // NOI18N
playerMenu.add(playerRecord);
menuBar.add(playerMenu);
helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
helpMenu.setName("helpMenu"); // NOI18N
helpAbout.setAction(actionMap.get("showAboutBox")); // NOI18N
helpAbout.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
helpAbout.setName("helpAbout"); // NOI18N
helpMenu.add(helpAbout);
menuBar.add(helpMenu);
statusPanel.setBorder(null);
statusPanel.setName("statusPanel"); // NOI18N
statusMessageLabel.setName("statusMessageLabel"); // NOI18N
statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N
progressBar.setName("progressBar"); // NOI18N
javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
statusPanel.setLayout(statusPanelLayout);
statusPanelLayout.setHorizontalGroup(
statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(statusPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(statusMessageLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 463, Short.MAX_VALUE)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(statusAnimationLabel)
.addContainerGap())
);
statusPanelLayout.setVerticalGroup(
statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(statusMessageLabel)
.addComponent(statusAnimationLabel)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(3, 3, 3))
);
myToolBar.setBorder(javax.swing.BorderFactory.createEtchedBorder());
myToolBar.setFloatable(false);
myToolBar.setRollover(true);
myToolBar.setName("myToolBar"); // NOI18N
defaultToolBar.setRollover(true);
defaultToolBar.setName("defaultToolBar"); // NOI18N
btnNewDocument.setAction(actionMap.get("NewDocument")); // NOI18N
btnNewDocument.setText(resourceMap.getString("btnNewDocument.text")); // NOI18N
btnNewDocument.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
btnNewDocument.setFocusable(false);
btnNewDocument.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
btnNewDocument.setName("btnNewDocument"); // NOI18N
btnNewDocument.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
defaultToolBar.add(btnNewDocument);
btnNewBrowser.setAction(actionMap.get("NewBrowser")); // NOI18N
btnNewBrowser.setText(resourceMap.getString("btnNewBrowser.text")); // NOI18N
btnNewBrowser.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
btnNewBrowser.setFocusable(false);
btnNewBrowser.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
btnNewBrowser.setName("btnNewBrowser"); // NOI18N
btnNewBrowser.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
defaultToolBar.add(btnNewBrowser);
myToolBar.add(defaultToolBar);
convertorBar.setRollover(true);
convertorBar.setName("convertorBar"); // NOI18N
cbtSynthesizer.setMaximumRowCount(100);
cbtSynthesizer.setMaximumSize(new java.awt.Dimension(300, 27));
cbtSynthesizer.setName("cbtSynthesizer"); // NOI18N
cbtSynthesizer.setPreferredSize(new java.awt.Dimension(94, 27));
convertorBar.add(cbtSynthesizer);
cbtVoice.setMaximumSize(new java.awt.Dimension(150, 27));
cbtVoice.setName("Voice"); // NOI18N
convertorBar.add(cbtVoice);
btnPlay.setAction(actionMap.get("Play")); // NOI18N
btnPlay.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
btnPlay.setFocusable(false);
btnPlay.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
btnPlay.setName("btnPlay"); // NOI18N
btnPlay.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
convertorBar.add(btnPlay);
btnPause.setAction(actionMap.get("Pause")); // NOI18N
btnPause.setText(resourceMap.getString("btnPause.text")); // NOI18N
btnPause.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
btnPause.setFocusable(false);
btnPause.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
btnPause.setName("btnPause"); // NOI18N
btnPause.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
convertorBar.add(btnPause);
btnStop.setAction(actionMap.get("Stop")); // NOI18N
btnStop.setText(resourceMap.getString("btnStop.text")); // NOI18N
btnStop.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
btnStop.setFocusable(false);
btnStop.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
btnStop.setName("btnStop"); // NOI18N
btnStop.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
convertorBar.add(btnStop);
btnRecord.setAction(actionMap.get("ConvertToSoundFile")); // NOI18N
btnRecord.setText(resourceMap.getString("btnRecord.text")); // NOI18N
btnRecord.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
btnRecord.setFocusable(false);
btnRecord.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
btnRecord.setName("btnRecord"); // NOI18N
btnRecord.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
convertorBar.add(btnRecord);
myToolBar.add(convertorBar);
setComponent(mainPanel);
setMenuBar(menuBar);
setStatusBar(statusPanel);
setToolBar(myToolBar);
}// </editor-fold>//GEN-END:initComponents
/**
* Creates a non-editable ComboBox with the given attributes.
*
* @param model the ComboBoxModel this ComboBox is based on
* @param toolTipText the tooltip text
* @param prototypeDisplayValue the String used to calculate the
* width of the ComboBox
*/
public JComboBox createComboBox(ComboBoxModel model, String toolTipText,
String prototypeDisplayValue) {
JComboBox comboBox = new JComboBox(model);
comboBox.setToolTipText(toolTipText);
comboBox.setPrototypeDisplayValue(prototypeDisplayValue);
comboBox.setEditable(false);
return comboBox;
}
/**
* Adds listeners for the synthesizer and voices JComboBoxes
*/
private void addComboBoxListeners() {
cbtSynthesizer.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
int selectedIndex = cbtSynthesizer.getSelectedIndex();
Monitor monitor = playerModel.getMonitor();
if (monitor != playerModel.getMonitor(selectedIndex)) {
if (monitor != null) {
monitor.setVisible(false);
}
if (playerModel.isMonitorVisible()) {
monitor = playerModel.getMonitor(selectedIndex);
monitor.setVisible(true);
//add(monitor, BorderLayout.SOUTH);
}
playerModel.setSynthesizer(selectedIndex);
}
}
});
cbtVoice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Cursor oldCursor = getCursor();
//setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
playerModel.setVoice(cbtVoice.getSelectedIndex());
//setCursor(oldCursor);
//updateSliders();
}
});
}
public void RemoveAllSyntVoiceFromCombo()
{
cbtSynthesizer.removeAllItems();
cbtVoice.removeAllItems();
}
public String getAppPath()
{
String path = "";
java.io.File currentDir = new java.io.File("");
path = currentDir.getAbsolutePath();
String osName = System.getProperty("os.name");
if(osName.startsWith("Windows"))
{
path = path +"\\";
}
else
{
path = path + "/";
}
return path;
}
/**
* only use by the menu command
*/
@Action
public void menuPause()
{
btnPause.doClick();
}
@Action
public void Pause() {
if (playerModel.isPaused()) {
playerModel.resume();
} else {
playerModel.pause();
}
}
@Action
public void Stop() {
if (playerModel.isPaused()) {
playerModel.stop();
btnPause.doClick();
}
else
playerModel.stop();
}
@Action
public void Play() {
//If there is a pause session...Warn then return
if (playerModel.isPaused()) {
JOptionPane.showMessageDialog(null,"The Synthesizer is currently in Pause mode\n" +
"Please Click on the Pause button to release\n " +
"Or Click on the Stop button and Try again",
"Currently Pause",
JOptionPane.ERROR_MESSAGE);
return;
}
String speakText = getTabManager().getTextOnCurrentTab();
if(speakText != null){
Playable textPlayable = Playable.createTextPlayable(speakText);
playerModel.play(textPlayable);
}
}
@Action
public void ConvertToSoundFile() {
//If there is a pause session...Warn then return
if (playerModel.isPaused()) {
JOptionPane.showMessageDialog(null,"The Synthesizer is currently in Pause mode\n" +
"Please Click on the Pause button to release\n " +
"Or Click on the Stop button and Try again",
"Currently Pause",
JOptionPane.ERROR_MESSAGE);
return;
}
String speakText = getTabManager().getTextOnCurrentTab();
if(speakText != null){
Playable textPlayable = Playable.createTextPlayable(speakText);
playerModel.ConvertToFile(textPlayable);
}
}
/**
* Sets the voice for the Player. The main purpose of this
* method is to set the Player to a nice voice on startup.
*/
public void setVoice(String voiceName) {
PlayerModel model = getModel();
ListModel descList = model.getSynthesizerList();
for (int i = 0; i < descList.getSize(); i++) {
SynthesizerModeDesc desc = (SynthesizerModeDesc)
descList.getElementAt(i);
Voice[] voices = desc.getVoices();
for (int j = 0; j < voices.length; j++) {
if (voices[j].getName().equals(voiceName)) {
model.setSynthesizer(i);
model.setVoice(j);
break;
}
}
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnNewBrowser;
private javax.swing.JButton btnNewDocument;
private javax.swing.JToggleButton btnPause;
private javax.swing.JButton btnPlay;
private javax.swing.JButton btnRecord;
private javax.swing.JButton btnStop;
private javax.swing.JComboBox cbtSynthesizer;
private javax.swing.JComboBox cbtVoice;
private javax.swing.JToolBar convertorBar;
private javax.swing.JToolBar defaultToolBar;
private javax.swing.JMenuItem fileNewDocument;
private javax.swing.JMenuItem fileShowBrowser;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
private javax.swing.JToolBar myToolBar;
private javax.swing.JMenu playerMenu;
private javax.swing.JMenuItem playerPause;
private javax.swing.JMenuItem playerPlay;
private javax.swing.JMenuItem playerRecord;
private javax.swing.JMenuItem playerStop;
private javax.swing.JProgressBar progressBar;
private javax.swing.JLabel statusAnimationLabel;
private javax.swing.JLabel statusMessageLabel;
private javax.swing.JPanel statusPanel;
private virtualnewsreader.TabManager tabManager;
// End of variables declaration//GEN-END:variables
private final Timer messageTimer;
private final Timer busyIconTimer;
private final Icon idleIcon;
private final Icon[] busyIcons = new Icon[15];
private int busyIconIndex = 0;
private JDialog aboutBox;
public JTextComponent comp;
private PlayerModel playerModel;
}