mainframe.java
来自「java语言开发的P2P流媒体系统」· Java 代码 · 共 383 行
JAVA
383 行
/*
* 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.monitor;
import stream2stream.network.*;
import p2pradio.*;
import p2pradio.logging.*;
import p2pradio.packets.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.util.logging.*;
class MainFrame extends JFrame implements ActionListener, Filter
{
private MonitorApp monitorApp;
// Men黮eiste
private JMenuBar MenuBar = new JMenuBar();
private JMenu MenuRadio = new JMenu(Messages.getString("MainFrame.MENU_MONITOR")); //$NON-NLS-1$
private JMenuItem MenuEreignisseAnzeigen = new JMenu(Messages.getString("MainFrame.MENU_SHOW_EVENTS")); //$NON-NLS-1$
private JCheckBoxMenuItem MenuLogDateiErstellen = new JCheckBoxMenuItem(Messages.getString("MainFrame.MENU_CREATE_LOGFILE")); //$NON-NLS-1$
private JMenuItem MenuUnverbundenePeersLoeschen = new JMenuItem(Messages.getString("MainFrame.MENU_CLEAR_UNCONNECTED_PEERS")); //$NON-NLS-1$
private JMenuItem MenuBaumLoeschen = new JMenuItem(Messages.getString("MainFrame.MENU_CLEAR_TREE")); //$NON-NLS-1$
private JMenuItem MenuLogLoeschen = new JMenuItem(Messages.getString("MainFrame.MENU_CLEAR_LOG")); //$NON-NLS-1$
private JMenuItem MenuBeenden = new JMenuItem(Messages.getString("MainFrame.MENU_EXIT")); //$NON-NLS-1$
private JTree peersTree;
private JScrollPane peersTreePane;
private JSplitPane splitPane;
private JTextArea eventsLog;
private JScrollPane eventsLogPane;
private JPopupMenu commandsMenu = new JPopupMenu(Messages.getString("MainFrame.MENU_SEND_FOLLOWING_COMMAND")); //$NON-NLS-1$
private JMenuItem[] commandMenuEntries = new JMenuItem[Commands.MESSAGES.length];
private RemotePeer selectedPeer;
private DatagramSocket udpSocket;
private String logEvents[] = {Messages.getString("MainFrame.MENU_LOGTYPE_ERRORS"), Messages.getString("MainFrame.MENU_LOGTYPE_WARNINGS"), Messages.getString("MainFrame.MENU_LOGTYPE_INFORMATION_MESSAGES"), Messages.getString("MainFrame.MENU_LOGTYPE_IMPORTANT_DEBUG_MESSAGES"), Messages.getString("MainFrame.MENU_LOGTYPE_UNIMPORTANT_DEBUG_MESSAGES")}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
private Level logLevels[] = {Level.SEVERE,Level.WARNING, Level.INFO, Level.FINE, Level.FINER};
private boolean logShow[] = {true, true, false, true, false};
private JCheckBoxMenuItem logMenus[];
private FileLogHandler fileLogHandler;
public final static String p2pradioIcon = "Icon.png";
public final static String s2sIcon = "s2s.png";
public MainFrame(MonitorApp monitorApp)
{
this.monitorApp = monitorApp;
try
{
udpSocket = new DatagramSocket();
}
catch (Exception e)
{
e.printStackTrace();
}
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
Init();
}
catch (Exception e)
{
e.printStackTrace();
}
//pack();
}
private void Init() throws Exception
{
setTitle(MonitorApp.Name);
getContentPane().setLayout(new BorderLayout());
setSize(new Dimension(700, 500));
// Icon
ImageIcon icon = p2pradio.gui.Icon.getImageIcon(p2pradioIcon);
if (icon != null)
{
setIconImage(icon.getImage());
}
// Men黶
MenuBar.add(MenuRadio);
MenuRadio.add(MenuEreignisseAnzeigen);
// Nachrichtentypen
logMenus = new JCheckBoxMenuItem[logEvents.length];
for (int i=0; i < logMenus.length; i++)
{
logMenus[i] = new JCheckBoxMenuItem(logEvents[i]);
logMenus[i].setSelected(logShow[i]);
MenuEreignisseAnzeigen.add(logMenus[i]);
}
MenuRadio.add(MenuLogDateiErstellen);
MenuRadio.addSeparator();
MenuRadio.add(MenuUnverbundenePeersLoeschen);
MenuRadio.add(MenuBaumLoeschen);
MenuRadio.add(MenuLogLoeschen);
MenuRadio.addSeparator();
MenuRadio.add(MenuBeenden);
setJMenuBar(MenuBar);
// ActionListener f黵 die Men黶
for (int i=0; i < logMenus.length; i++)
{
logMenus[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for (int i=0; i < logMenus.length; i++)
{
if (e.getSource() == logMenus[i])
{
logShow[i] = !logShow[i];
break;
}
}
}
});
}
MenuLogDateiErstellen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (MenuLogDateiErstellen.isSelected())
{
try
{
fileLogHandler = new FileLogHandler(MonitorApp.Name);
fileLogHandler.setFormatter(new MonitorLogFormatter());
monitorApp.getLogger().addHandler(fileLogHandler);
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(MainFrame.this, Messages.getString("MonitorApp.COULD_NOT_CREATE_LOGFILE"), MonitorApp.Name, JOptionPane.ERROR_MESSAGE, null); //$NON-NLS-1$
}
}
else
{
if (fileLogHandler != null)
{
fileLogHandler.close();
monitorApp.getLogger().removeHandler(fileLogHandler);
fileLogHandler = null;
}
}
}
});
MenuUnverbundenePeersLoeschen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
monitorApp.getTreeDataManager().removeUnconnectedPeers();
}
});
MenuBaumLoeschen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
monitorApp.getTreeDataManager().clear();
}
});
MenuLogLoeschen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
eventsLog.setText(null);
}
});
MenuBeenden.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
exit();
}
});
eventsLog = new JTextArea();
eventsLog.setEditable(false);
eventsLogPane = new JScrollPane(eventsLog);
peersTree = new JTree(monitorApp.getTreeDataManager().getTreeModel());
peersTree.setRootVisible(false);
for (int i=0; i < Commands.MESSAGES.length; i++)
{
commandMenuEntries[i] = new JMenuItem(Messages.getString(Commands.MESSAGES[i]));
commandMenuEntries[i].addActionListener(this);
commandsMenu.add(commandMenuEntries[i]);
if (Commands.separatorAfterMenuEntry[i])
{
commandsMenu.addSeparator();
}
}
MouseListener mouseListener = new MouseAdapter()
{
public void mouseReleased(MouseEvent e)
{
treeElementClicked(e);
}
};
peersTree.addMouseListener(mouseListener);
peersTreePane = new JScrollPane(peersTree);
// Funktioniert nur mit dem Windows-Look&Feel
// peersTree.setVisibleRowCount(20);
monitorApp.getTreeDataManager().setTree(peersTree);
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, peersTreePane, eventsLogPane);
peersTreePane.setBackground(peersTree.getBackground());
eventsLogPane.setBackground(eventsLog.getBackground());
peersTreePane.setBorder(BorderFactory.createTitledBorder(Messages.getString("MainFrame.NETWORK_TREE_STRUCTURE"))); //$NON-NLS-1$
eventsLogPane.setBorder(BorderFactory.createTitledBorder(Messages.getString("MainFrame.EVENTS"))); //$NON-NLS-1$
getContentPane().add(splitPane, BorderLayout.CENTER);
}
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
exit();
}
}
protected void exit()
{
System.exit(0);
}
protected void addEvent(String text)
{
EventQueue.invokeLater(new p2pradio.gui.EventAdder(text, eventsLog));
}
protected void treeElementClicked(MouseEvent e)
{
if (e.getID() == MouseEvent.MOUSE_RELEASED)
{
if (e.getButton() == MouseEvent.BUTTON3)
{
TreePath selectionPath = peersTree.getPathForLocation(e.getX(), e.getY());
if (selectionPath != null)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)selectionPath.getLastPathComponent();
if (node.getUserObject() instanceof RemotePeer)
{
selectedPeer = (RemotePeer)node.getUserObject();
peersTree.setSelectionPath(selectionPath);
commandsMenu.show(peersTree, e.getX(), e.getY());
}
}
}
}
}
public void actionPerformed(ActionEvent e)
{
JMenuItem source = (JMenuItem)e.getSource();
int index = -1;
for (int i=0; i < commandMenuEntries.length; i++)
{
if (commandMenuEntries[i] == source)
{
index = i;
break;
}
}
DatagramPacket outPacket = null;
try
{
outPacket = PacketFactory.createUDPPacket(new MonitorCommandPacket((byte)index), selectedPeer);
}
catch (SocketException ee)
{
System.err.println(Messages.getString("COULD_NOT_CREATE_UDP_PACKET")); //$NON-NLS-1$
ee.printStackTrace();
return;
}
try
{
udpSocket.send(outPacket);
}
catch(Exception ee)
{
System.err.println(Messages.getString("MainFrame.COULD_NOT_SEND_COMMAND", ee.getMessage())); //$NON-NLS-1$
}
}
public void setDividerLocation()
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
splitPane.setDividerLocation(0.65);
}
});
}
public boolean isLoggable(LogRecord record)
{
Level level = record.getLevel();
for (int i=0; i < logLevels.length; i++)
{
if (level.equals(logLevels[i]))
{
return logShow[i];
}
}
// Unbekannte Log-Nachrichten anzeigen
return true;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?