package server.gui;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import server.Server;
import client.gfx.wrapper.FaviconEngine;
/**
* The Class ServerFrame. Creates a new frame for the server with
* start and stop buttons.
*
* @author Priit Laht
*/
public class ServerFrame extends JFrame implements ActionListener {
private final static long serialVersionUID = 00000001L;
private final static String FAVICON_PATH = "gfx/admfavicon.png";
private JButton btnStart = new JButton();
private JButton btnStop = new JButton();
private JLabel txt_lblStatus = new JLabel("Status: ");
private JLabel lblStatus = new JLabel();
private Server server = new Server();
/**
* Instantiates a new server frame.
*
* @param width the width for the frame
* @param height the height for the frame
*/
public ServerFrame(int width, int height) {
super("Yahtzee Server Admin");
setDefaultCloseOperation(EXIT_ON_CLOSE);
FaviconEngine fav = new FaviconEngine(FAVICON_PATH);
setIconImage(fav.getFaviconImg());
setResizable(false);
setSize(width, height);
setLayout(new FlowLayout());
setLocationRelativeTo(null);
createStartButton("Start");
createStopButton("Stop");
createStatusLabel("Offline", 100, 30);
addWindowAdapter();
add(txt_lblStatus);
add(lblStatus);
add(btnStart);
add(btnStop);
setVisible(true);
}
/**
* Creates the start button.
*
* @param text the text for the button
*/
private void createStartButton(String text) {
btnStart.setText(text);
btnStart.addActionListener(this);
}
/**
* Creates the stop button.
*
* @param text the text for the button
*/
private void createStopButton(String text) {
btnStop.setText(text);
btnStop.setEnabled(false);
btnStop.addActionListener(this);
}
/**
* Creates the status label.
*
* @param text the text for the label
* @param width the width for the the label
* @param height the height for the label
*/
private void createStatusLabel(String text, int width, int height) {
lblStatus.setSize(width, height);
lblStatus.setPreferredSize(lblStatus.getSize());
lblStatus.setText(text);
lblStatus.setForeground(Color.RED);
}
/**
* Adds the window adapter.
*/
private void addWindowAdapter() {
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (!server.isClosed()) server.stop();
}
});
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnStart) {
server.start();
lblStatus.setText("Online");
lblStatus.setForeground(Color.GREEN);
btnStart.setEnabled(false);
btnStop.setEnabled(true);
} else {
server.stop();
lblStatus.setText("Offline");
lblStatus.setForeground(Color.RED);
btnStop.setEnabled(false);
btnStart.setEnabled(true);
}
}
}