package game.server;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
import game.EventsListener;
import game.GameEvent;
import game.impl.NetworkGameControllerServer;
public class SwingServer extends JFrame implements EventsListener{
private NetworkGameControllerServer gameControllerServer;
private JTextArea textArea;
public SwingServer() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("Start server");
menu.add(menuItem);
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
if (gameControllerServer == null) {
gameControllerServer = new NetworkGameControllerServer();
gameControllerServer.setEventsListener(SwingServer.this);
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
menuItem = new JMenuItem("Stop server");
menu.add(menuItem);
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (gameControllerServer != null) {
gameControllerServer.shutdown();
gameControllerServer = null;
}
}
});
}
});
setSize(200, 400);
textArea = new JTextArea();
getContentPane().add(new JScrollPane(textArea));
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new SwingServer().setVisible(true);
}
});
}
@Override
public void onIncomingEvent(final GameEvent gameEvent) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
textArea.append("\n" + gameEvent);
}
});
}
@Override
public void onOutgoingEvent(final GameEvent gameEvent) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
textArea.append("\n" + gameEvent);
}
});
}
}