/*
* P2P-Radio - Peer to peer streaming system
* Project homepage: http://p2p-radio.sourceforge.net/
* Copyright (C) 2003-2004 Michael Kaufmann <hallo@michael-kaufmann.ch>
*
* ---------------------------------------------------------------------------
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* ---------------------------------------------------------------------------
*/
package p2pradio.gui;
import p2pradio.*;
import p2pradio.logging.*;
import p2pradio.players.*;
import p2pradio.sources.YellowPages;
import p2pradio.tools.BrowserLauncher;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Filter;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Level;
import javax.swing.*;
/**
* The main frame of the GUI.
*
* @author Michael Kaufmann
*/
public class MainFrame extends JFrame implements Filter
{
private Peer peer;
private Metadata metadata;
// Men�leiste
private JMenuBar MenuBar = new JMenuBar();
private JMenu MenuRadio = new JMenu(Messages.getString("MainFrame.MENU_RADIO")); //$NON-NLS-1$
private JMenuItem MenuSenderAusw�hlen = new JMenuItem(Messages.getString("MainFrame.CHOOSE_NEW_STATION")); //$NON-NLS-1$
private JMenuItem MenuInformationenAnzeigen = new JMenuItem(Messages.getString("MainFrame.DISPLAY_VERBOSE_INFORMATION")); //$NON-NLS-1$
private JMenuItem MenuMediaPlayerStarten = new JMenuItem(Messages.getString("MainFrame.MENU_LAUNCH_MEDIA_PLAYER")); //$NON-NLS-1$
private JMenuItem MenuLogLoeschen = new JMenuItem(Messages.getString("MainFrame.MENU_CLEAR_LOG")); //$NON-NLS-1 //$NON-NLS-1$
private JMenuItem MenuBeenden = new JMenuItem(Messages.getString("MainFrame.MENU_EXIT")); //$NON-NLS-1$
private JMenu MenuEinstellungen = new JMenu(Messages.getString("MainFrame.MENU_SETTINGS")); //$NON-NLS-1$
private JMenuItem MenuBandbreiteSetzen = new JMenuItem(Messages.getString("MainFrame.MENU_SET_BANDWIDTH")); //$NON-NLS-1$
private JCheckBoxMenuItem MenuDebugNachrichtenAnzeigen = new JCheckBoxMenuItem(Messages.getString("MainFrame.MENU_SHOW_DEBUG_MESSAGES"), Radio.displayDebugMessages); //$NON-NLS-1$
private JMenu MenuHilfe = new JMenu(Messages.getString("MainFrame.MENU_HELP")); //$NON-NLS-1$
private JMenuItem MenuHomepageBesuchen = new JMenuItem(Messages.getString("MainFrame.MENU_VISIT_HOMEPAGE", Radio.Name)); //$NON-NLS-1$
private JMenuItem MenuInfo = new JMenuItem(Messages.getString("MainFrame.MENU_INFO")); //$NON-NLS-1$
// Anzeigeelemente
private JPanel LabelsPanel;
private JPanel TopPanel;
private JLabel LabelStation = new JLabel(Messages.getString("MainFrame.LABEL_STATION")); //$NON-NLS-1$
private JLabel LabelStream = new JLabel(Messages.getString("MainFrame.LABEL_STREAM")); //$NON-NLS-1$
private JLabel LabelSong = new JLabel(Messages.getString("MainFrame.LABEL_SONG")); //$NON-NLS-1$
private JLabel LabelStationContent = new JLabel(Messages.getString("MainFrame.EMPTY_LABEL")); //$NON-NLS-1$
private JLabel LabelStreamContent = new JLabel(Messages.getString("MainFrame.EMPTY_LABEL")); //$NON-NLS-1$
private JLabel LabelSongContent = new JLabel(Messages.getString("MainFrame.EMPTY_LABEL")); //$NON-NLS-1$
private JTextArea messages = new JTextArea();
private JScrollPane messagesPane = new JScrollPane(messages);
private JPanel MainPanel = new JPanel(new BorderLayout());
public MainFrame()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
Init();
}
catch (Exception e)
{
Logger.severe("MainFrame", "INTERNAL_ERROR", e); //$NON-NLS-1$ //$NON-NLS-2$
}
Handler logHandler = new GUILogHandler(messages);
logHandler.setFilter(this);
logHandler.setFormatter(new LogFormatter());
Logger.getLogger().addHandler(logHandler);
}
private void Init() throws Exception
{
setTitle(Radio.Name);
// Icon
ImageIcon icon = Icon.getImageIcon();
if (icon != null)
{
setIconImage(icon.getImage());
}
// Men�s
MenuInformationenAnzeigen.setEnabled(false);
MenuBandbreiteSetzen.setEnabled(false);
MenuBar.add(MenuRadio);
MenuRadio.add(MenuSenderAusw�hlen);
MenuRadio.add(MenuInformationenAnzeigen);
MenuRadio.add(MenuMediaPlayerStarten);
MenuRadio.addSeparator();
MenuRadio.add(MenuLogLoeschen);
MenuRadio.addSeparator();
MenuRadio.add(MenuBeenden);
MenuBar.add(MenuEinstellungen);
MenuEinstellungen.add(MenuBandbreiteSetzen);
MenuEinstellungen.add(MenuDebugNachrichtenAnzeigen);
MenuBar.add(MenuHilfe);
MenuHilfe.add(MenuHomepageBesuchen);
MenuHilfe.addSeparator();
MenuHilfe.add(MenuInfo);
setJMenuBar(MenuBar);
// Anzeigeelemente
GridBagLayout layout = new GridBagLayout();
LabelsPanel = new JPanel(layout);
GridBagConstraints left = new GridBagConstraints();
//left.weightx = 1.0;
left.gridwidth = GridBagConstraints.RELATIVE;
left.anchor = GridBagConstraints.EAST;
left.insets = new Insets(0, 0, 0, 5);
GridBagConstraints right = new GridBagConstraints();
right.fill = GridBagConstraints.HORIZONTAL;
right.weightx = 1.0;
right.gridwidth = GridBagConstraints.REMAINDER;
messages.setFont(LabelStationContent.getFont());
messages.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
Font bold = LabelStation.getFont().deriveFont(Font.BOLD, LabelStation.getFont().getSize2D() + 2);
LabelStation.setFont(bold);
LabelSong.setFont(bold);
LabelStream.setFont(bold);
Font plain = LabelStationContent.getFont().deriveFont(LabelStation.getFont().getSize2D() + 2);
LabelStationContent.setFont(plain);
LabelSongContent.setFont(plain);
LabelStreamContent.setFont(plain);
LabelsPanel.add(LabelStation);
layout.setConstraints(LabelStation, left);
LabelsPanel.add(LabelStationContent);
layout.setConstraints(LabelStationContent, right);
LabelsPanel.add(LabelSong);
layout.setConstraints(LabelSong, left);
LabelsPanel.add(LabelSongContent);
layout.setConstraints(LabelSongContent, right);
LabelsPanel.add(LabelStream);
layout.setConstraints(LabelStream, left);
LabelsPanel.add(LabelStreamContent);
layout.setConstraints(LabelStreamContent, right);
int borderSize = 7;
LabelsPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(borderSize, borderSize, borderSize, borderSize), BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(borderSize, borderSize, borderSize, borderSize))));
TopPanel = new JPanel(new BorderLayout());
TopPanel.add(LabelsPanel, BorderLayout.CENTER);
messages.setEditable(false);
messagesPane.setPreferredSize(new Dimension(400, 200));
MainPanel.add(TopPanel, BorderLayout.NORTH);
MainPanel.add(messagesPane, BorderLayout.CENTER);
getContentPane().add(MainPanel, BorderLayout.CENTER);
// ActionListener f�r die Men�s
MenuSenderAusw�hlen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
BrowserLauncher.startBrowserExtended("http://" + YellowPages.YP_HOST + YellowPages.YP_DIR, MainFrame.this); //$NON-NLS-1$
}
});
MenuMediaPlayerStarten.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (Radio.httpPlayer != null)
{
new MediaPlayerLauncher(peer.getListenBuffer(), Radio.httpPlayer).start();
}
}
});
MenuInformationenAnzeigen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String webAddress = "http://localhost:" + (peer.getSocketAddress().getPort() + Peer.WEBINTERFACE_PORT_OFFSET) + "/"; //$NON-NLS-1$ //$NON-NLS-2$
BrowserLauncher.startBrowserExtended(webAddress, MainFrame.this);
}
});
MenuLogLoeschen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
messages.setText(null);
}
});
MenuBeenden.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
exit();
}
});
MenuDebugNachrichtenAnzeigen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Radio.displayDebugMessages = MenuDebugNachrichtenAnzeigen.isSelected();
}
});
MenuBandbreiteSetzen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Object maxUploadBandwidthString = JOptionPane.showInputDialog(MainFrame.this, Messages.getString("RadioGUI.ENTER_BANDWIDTH_LIMIT", new Object[]{Radio.Name, new Integer(Peer.MAX_NUMBER_OF_CHILDREN)}), Messages.getString("RadioGUI.ENTER_BANDWIDTH_LIMIT_TITLE"), JOptionPane.QUESTION_MESSAGE, null, null, Integer.toString(peer.getMaxUploadBandwidth())); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
if (maxUploadBandwidthString == null)
{
// Benutzer hat das Fenster geschlossen
return;
}
int maxUploadBandwidth = 0;
try
{
maxUploadBandwidth = Integer.parseInt((String)maxUploadBandwidthString);
peer.setMaxUploadBandwidth(maxUploadBandwidth);
}
catch (NumberFormatException exception)
{
JOptionPane.showMessageDialog(MainFrame.this, Messages.getString("RadioGUI.INVALID_BANDWIDTH_LIMIT"), Messages.getString("GENERAL_ERROR"), JOptionPane.ERROR_MESSAGE, null); //$NON-NLS-1$ //$NON-NLS-2$
}
}
});
MenuHomepageBesuchen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
BrowserLauncher.startBrowserExtended(Radio.HOMEPAGE, MainFrame.this);
}
});
MenuInfo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(MainFrame.this, Messages.getString("MainFrame.ABOUT_MESSAGE", new String[]{Radio.Name, Radio.Version}), Messages.getString("MainFrame.ABOUT_TITLE", Radio.Name), JOptionPane.INFORMATION_MESSAGE, Icon.getImageIcon()); //$NON-NLS-1$ //$NON-NLS-2$
}
});
pack();
center();
}
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
exit();
}
}
protected void exit()
{
if (peer != null)
{
peer.leave();
}
System.exit(0);
}
/**
* Sets the peer that is going to be managed by the GUI
*/
public void setPeer(Peer peer)
{
this.peer = peer;
if (peer.isServer())
{
setTitle(Messages.getString("MainFrame.WINDOW_TITLE_SERVER", Radio.Name)); //$NON-NLS-1$
}
else
{
setTitle(Messages.getString("MainFrame.WINDOW_TITLE_CLIENT", Radio.Name)); //$NON-NLS-1$
}
MenuInformationenAnzeigen.setEnabled(true);
MenuBandbreiteSetzen.setEnabled(true);
new GUIPlayer(peer.getListenBuffer(), this).start();
}
public boolean isLoggable(LogRecord record)
{
if (record.getLevel().intValue() < Level.INFO.intValue())
{
return Radio.displayDebugMessages;
}
else
{
return true;
}
}
public void updateMetadata(Metadata metadata)
{
this.metadata = metadata;
EventQueue.invokeLater(new Runnable()
{
public void run()
{
updateDisplay();
}
});
}
public void updateDisplay()
{
LabelStationContent.setText(metadata.getStationName());
LabelSongContent.setText(metadata.getSongTitle());
String streamType;
String contentType = metadata.getContentType().toLowerCase();
if (contentType.equals("audio/mpeg")) //$NON-NLS-1$
{
streamType = Messages.getString("MainFrame.STREAM_MP3"); //$NON-NLS-1$
}
else if (contentType.equals("video/nsv")) //$NON-NLS-1$
{
streamType = Messages.getString("MainFrame.STREAM_NSV"); //$NON-NLS-1$
}
else if (contentType.equals("application/ogg")) //$NON-NLS-1$
{
streamType = Messages.getString("MainFrame.STREAM_OGG_VORBIS"); //$NON-NLS-1$
}
else
{
streamType = contentType;
}
Object args[] = new Object[2];
args[0] = streamType;
args[1] = new Float(metadata.getAverageByterate() / 1024f);
LabelStreamContent.setText(Messages.getString("MainFrame.LABEL_STREAM_CONTENT", args)); //$NON-NLS-1$
LabelsPanel.validate();
}
public void center()
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}
}