// ServerGUI.Java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/*
* The server as a GUI
*/
public class ServerGUI extends JFrame implements
ActionListener, WindowListener {
private static
final long serialVersionUID = 1L;
// the stop and
start buttons
private JButton
stopStart;
// JTextArea for
the chat room and the events
private JTextArea
chat, event;
// The port number
private JTextField
tPortNumber;
// my server
private Server
server;
// server
constructor that receive the port to listen to for connection as parameter
ServerGUI(int
port) {
super("Chat Server");
server = null;
// in the
NorthPanel the PortNumber the Start and Stop buttons
JPanel north =
new JPanel();
north.add(new
JLabel("Port number: "));
tPortNumber =
new JTextField(" " + port);
north.add(tPortNumber);
// to stop or
start the server, we start with "Start"
stopStart =
new JButton("Start");
stopStart.addActionListener(this);
north.add(stopStart);
add(north, BorderLayout.NORTH);
// the event
and chat room
JPanel center
= new JPanel(new GridLayout(2,1));
chat = new
JTextArea(80,80);
chat.setEditable(false);
appendRoom("Chat room.\n");
center.add(new
JScrollPane(chat));
event = new
JTextArea(80,80);
event.setEditable(false);
appendEvent("Events log.\n");
center.add(new
JScrollPane(event));
add(center);
// need to be
informed when the user click the close button on the frame
addWindowListener(this);
setSize(400,
600);
setVisible(true);
}
// append message
to the two JTextArea
// position at the
end
void
appendRoom(String str) {
chat.append(str);
chat.setCaretPosition(chat.getText().length() - 1);
}
void
appendEvent(String str) {
event.append(str);
event.setCaretPosition(chat.getText().length()
- 1);
}
// start or stop
where clicked
public void
actionPerformed(ActionEvent e) {
// if running
we have to stop
if(server !=
null) {
server.stop();
server =
null;
tPortNumber.setEditable(true);
stopStart.setText("Start");
return;
}
// OK start
the server
int port;
try {
port =
Integer.parseInt(tPortNumber.getText().trim());
}
catch(Exception er) {
appendEvent("Invalid port number");
return;
}
// ceate a new
Server
server = new
Server(port, this);
// and start
it as a thread
new
ServerRunning().start();
stopStart.setText("Stop");
tPortNumber.setEditable(false);
}
// entry point to
start the Server
public static void
main(String[] arg) {
// start
server default port 1500
new
ServerGUI(1500);
}
/*
* If the user
click the X button to close the application
* I need to close
the connection with the server to free the port
*/
public void
windowClosing(WindowEvent e) {
// if my
Server exist
if(server !=
null) {
try {
server.stop(); // ask the
server to close the conection
}
catch(Exception eClose) {
}
server =
null;
}
// dispose the
frame
dispose();
System.exit(0);
}
// I can ignore
the other WindowListener method
public void
windowClosed(WindowEvent e) {}
public void
windowOpened(WindowEvent e) {}
public void
windowIconified(WindowEvent e) {}
public void
windowDeiconified(WindowEvent e) {}
public void
windowActivated(WindowEvent e) {}
public void
windowDeactivated(WindowEvent e) {}
/*
* A thread to run
the Server
*/
class
ServerRunning extends Thread {
public void
run() {
server.start(); // should
execute until if fails
// the
server failed
stopStart.setText("Start");
tPortNumber.setEditable(true);
appendEvent("Server crashed\n");
server =
null;
}
}
}
// ClientGUI.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/*
* The Client with its GUI
*/
public class ClientGUI extends JFrame implements
ActionListener {
private static
final long serialVersionUID = 1L;
// will first hold
"Username:", later on "Enter message"
private JLabel
label;
// to hold the
Username and later on the messages
private JTextField
tf;
// to hold the
server address an the port number
private JTextField
tfServer, tfPort;
// to Logout and
get the list of the users
private JButton
login, logout, whoIsIn;
// for the chat
room
private JTextArea
ta;
// if it is for
connection
private boolean
connected;
// the Client
object
private Client
client;
// the default
port number
private int
defaultPort;
private String
defaultHost;
// Constructor
connection receiving a socket number
ClientGUI(String
host, int port) {
super("Chat Client");
defaultPort =
port;
defaultHost =
host;
// The
NorthPanel with:
JPanel
northPanel = new JPanel(new GridLayout(3,1));
// the server
name anmd the port number
JPanel
serverAndPort = new JPanel(new GridLayout(1,5, 1, 3));
// the two
JTextField with default value for server address and port number
tfServer = new
JTextField(host);
tfPort = new
JTextField("" + port);
tfPort.setHorizontalAlignment(SwingConstants.RIGHT);
serverAndPort.add(new JLabel("Server Address: "));
serverAndPort.add(tfServer);
serverAndPort.add(new
JLabel("Port Number: "));
serverAndPort.add(tfPort);
serverAndPort.add(new JLabel(""));
// adds the
Server an port field to the GUI
northPanel.add(serverAndPort);
// the Label
and the TextField
label = new
JLabel("Enter your username below", SwingConstants.CENTER);
northPanel.add(label);
tf = new
JTextField("Anonymous");
tf.setBackground(Color.WHITE);
northPanel.add(tf);
add(northPanel, BorderLayout.NORTH);
// The
CenterPanel which is the chat room
ta = new
JTextArea("Welcome to the Chat room\n", 80, 80);
JPanel
centerPanel = new JPanel(new GridLayout(1,1));
centerPanel.add(new
JScrollPane(ta));
ta.setEditable(false);
add(centerPanel, BorderLayout.CENTER);
// the 3
buttons
login = new
JButton("Login");
login.addActionListener(this);
logout = new
JButton("Logout");
logout.addActionListener(this);
logout.setEnabled(false); //
you have to login before being able to logout
whoIsIn = new
JButton("Who is in");
whoIsIn.addActionListener(this);
whoIsIn.setEnabled(false); //
you have to login before being able to Who is in
JPanel
southPanel = new JPanel();
southPanel.add(login);
southPanel.add(logout);
southPanel.add(whoIsIn);
add(southPanel, BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600,
600);
setVisible(true);
tf.requestFocus();
}
// called by the
Client to append text in the TextArea
void append(String
str) {
ta.append(str);
ta.setCaretPosition(ta.getText().length() - 1);
}
// called by the
GUI is the connection failed
// we reset our
buttons, label, textfield
void
connectionFailed() {
login.setEnabled(true);
logout.setEnabled(false);
whoIsIn.setEnabled(false);
label.setText("Enter your username below");
tf.setText("Anonymous");
// reset port
number and host name as a construction time
tfPort.setText("" + defaultPort);
tfServer.setText(defaultHost);
// let the
user change them
tfServer.setEditable(false);
tfPort.setEditable(false);
// don't react
to a <CR> after the username
tf.removeActionListener(this);
connected =
false;
}
/*
* Button or
JTextField clicked
*/
public void actionPerformed(ActionEvent
e) {
Object o =
e.getSource();
// if it is
the Logout button
if(o ==
logout) {
client.sendMessage(new ChatMessage(ChatMessage.LOGOUT, ""));
return;
}
// if it the
who is in button
if(o ==
whoIsIn) {
client.sendMessage(new ChatMessage(ChatMessage.WHOISIN,
""));
return;
}
// ok it is
coming from the JTextField
if(connected)
{
// just
have to send the message
client.sendMessage(new ChatMessage(ChatMessage.MESSAGE,
tf.getText()));
tf.setText("");
return;
}
if(o == login)
{
// ok it
is a connection request
String
username = tf.getText().trim();
// empty
username ignore it
if(username.length() == 0)
return;
// empty
serverAddress ignore it
String
server = tfServer.getText().trim();
if(server.length() == 0)
return;
// empty
or invalid port numer, ignore it
String portNumber =
tfPort.getText().trim();
if(portNumber.length() == 0)
return;
int port =
0;
try {
port =
Integer.parseInt(portNumber);
}
catch(Exception en) {
return; // nothing I can do if
port number is not valid
}
// try
creating a new Client with GUI
client =
new Client(server, port, username, this);
// test if
we can start the Client
if(!client.start())
return;
tf.setText("");
label.setText("Enter your message below");
connected
= true;
// disable
login button
login.setEnabled(false);
// enable
the 2 buttons
logout.setEnabled(true);
whoIsIn.setEnabled(true);
// disable
the Server and Port JTextField
tfServer.setEditable(false);
tfPort.setEditable(false);
// Action
listener for when the user enter a message
tf.addActionListener(this);
}
}
// to start the
whole thing the server
public static void
main(String[] args) {
new
ClientGUI("localhost", 1500);
}
}
No comments:
Post a Comment