|
@@ -39,107 +39,108 @@ import common.MessagePacket.MessageType;
|
|
public class ChatWindow extends JFrame implements ActionListener, Runnable, KeyListener {
|
|
public class ChatWindow extends JFrame implements ActionListener, Runnable, KeyListener {
|
|
Thread getMessages;
|
|
Thread getMessages;
|
|
static File confFile = new File("client.properties");
|
|
static File confFile = new File("client.properties");
|
|
-
|
|
|
|
|
|
+
|
|
String adress, username;
|
|
String adress, username;
|
|
ArrayList<String> lastMess = new ArrayList<String>();
|
|
ArrayList<String> lastMess = new ArrayList<String>();
|
|
int port, messIndex = 0;
|
|
int port, messIndex = 0;
|
|
-
|
|
|
|
- Socket so = new Socket();
|
|
|
|
|
|
+
|
|
|
|
+ Socket so;
|
|
ObjectInputStream objIn;
|
|
ObjectInputStream objIn;
|
|
PrintWriter out;
|
|
PrintWriter out;
|
|
-
|
|
|
|
|
|
+
|
|
GridBagLayout g = new GridBagLayout();
|
|
GridBagLayout g = new GridBagLayout();
|
|
GridBagConstraints con = new GridBagConstraints();
|
|
GridBagConstraints con = new GridBagConstraints();
|
|
-
|
|
|
|
|
|
+
|
|
JPanel right = new JPanel();
|
|
JPanel right = new JPanel();
|
|
JLabel infoLabel = new JLabel("Users online:");
|
|
JLabel infoLabel = new JLabel("Users online:");
|
|
DefaultListModel<String> model = new DefaultListModel<String>();
|
|
DefaultListModel<String> model = new DefaultListModel<String>();
|
|
JList<String> userList = new JList<String>(model);
|
|
JList<String> userList = new JList<String>(model);
|
|
JButton reconnect = new JButton("Reconnect");
|
|
JButton reconnect = new JButton("Reconnect");
|
|
-
|
|
|
|
|
|
+
|
|
Console chat = new Console();
|
|
Console chat = new Console();
|
|
JScrollPane scroll = new JScrollPane(chat);
|
|
JScrollPane scroll = new JScrollPane(chat);
|
|
JTextField write = new JTextField();
|
|
JTextField write = new JTextField();
|
|
-
|
|
|
|
|
|
+
|
|
public ChatWindow(String adress, int port, String username) {
|
|
public ChatWindow(String adress, int port, String username) {
|
|
this.adress = adress;
|
|
this.adress = adress;
|
|
this.port = port;
|
|
this.port = port;
|
|
this.username = username;
|
|
this.username = username;
|
|
-
|
|
|
|
- //List config
|
|
|
|
|
|
+
|
|
|
|
+ // List config
|
|
userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
|
userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
|
userList.setLayoutOrientation(JList.VERTICAL);
|
|
userList.setLayoutOrientation(JList.VERTICAL);
|
|
- //Label config
|
|
|
|
|
|
+ // Label config
|
|
infoLabel.setHorizontalAlignment(JLabel.CENTER);
|
|
infoLabel.setHorizontalAlignment(JLabel.CENTER);
|
|
infoLabel.setBorder(new EmptyBorder(5, 5, 5, 5));
|
|
infoLabel.setBorder(new EmptyBorder(5, 5, 5, 5));
|
|
- //Layout config
|
|
|
|
|
|
+ // Layout config
|
|
right.setLayout(g);
|
|
right.setLayout(g);
|
|
con.fill = GridBagConstraints.HORIZONTAL;
|
|
con.fill = GridBagConstraints.HORIZONTAL;
|
|
con.weightx = 1;
|
|
con.weightx = 1;
|
|
con.gridx = 0;
|
|
con.gridx = 0;
|
|
-
|
|
|
|
|
|
+
|
|
right.add(infoLabel, con);
|
|
right.add(infoLabel, con);
|
|
-
|
|
|
|
|
|
+
|
|
con.weighty = 1;
|
|
con.weighty = 1;
|
|
con.fill = GridBagConstraints.BOTH;
|
|
con.fill = GridBagConstraints.BOTH;
|
|
right.add(userList, con);
|
|
right.add(userList, con);
|
|
-
|
|
|
|
|
|
+
|
|
con.weighty = 0;
|
|
con.weighty = 0;
|
|
con.fill = GridBagConstraints.HORIZONTAL;
|
|
con.fill = GridBagConstraints.HORIZONTAL;
|
|
right.add(reconnect, con);
|
|
right.add(reconnect, con);
|
|
-
|
|
|
|
|
|
+
|
|
setLayout(new BorderLayout());
|
|
setLayout(new BorderLayout());
|
|
add(chat, BorderLayout.NORTH);
|
|
add(chat, BorderLayout.NORTH);
|
|
add(write, BorderLayout.SOUTH);
|
|
add(write, BorderLayout.SOUTH);
|
|
add(right, BorderLayout.EAST);
|
|
add(right, BorderLayout.EAST);
|
|
-
|
|
|
|
- //Scrollbar config
|
|
|
|
|
|
+
|
|
|
|
+ // Scrollbar config
|
|
add(scroll);
|
|
add(scroll);
|
|
scroll.setMinimumSize(new Dimension(100, 100));
|
|
scroll.setMinimumSize(new Dimension(100, 100));
|
|
scroll.setViewportView(chat);
|
|
scroll.setViewportView(chat);
|
|
scroll.setSize(500, 130);
|
|
scroll.setSize(500, 130);
|
|
-
|
|
|
|
- //Listener config
|
|
|
|
|
|
+
|
|
|
|
+ // Listener config
|
|
reconnect.addActionListener(this);
|
|
reconnect.addActionListener(this);
|
|
write.addKeyListener(this);
|
|
write.addKeyListener(this);
|
|
-
|
|
|
|
- //Window config
|
|
|
|
|
|
+
|
|
|
|
+ // Window config
|
|
this.setLocation(new Point(100, 100));
|
|
this.setLocation(new Point(100, 100));
|
|
setSize(600, 600);
|
|
setSize(600, 600);
|
|
setVisible(true);
|
|
setVisible(true);
|
|
setTitle("Chat on " + adress + " | Username: " + username);
|
|
setTitle("Chat on " + adress + " | Username: " + username);
|
|
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
|
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
|
-
|
|
|
|
|
|
+
|
|
connect(adress, port, username);
|
|
connect(adress, port, username);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
public void send(String text) {
|
|
public void send(String text) {
|
|
- if (so.isConnected() && !so.isClosed())
|
|
|
|
|
|
+ if (so.isConnected() && !so.isClosed()) {
|
|
out.println(text);
|
|
out.println(text);
|
|
- else {
|
|
|
|
|
|
+ out.flush();
|
|
|
|
+ } else {
|
|
chat.log(new MessagePacket("Not connected to server!", MessageType.WARNING));
|
|
chat.log(new MessagePacket("Not connected to server!", MessageType.WARNING));
|
|
write.setEnabled(false);
|
|
write.setEnabled(false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
void connect(String address, int port, String username) {
|
|
void connect(String address, int port, String username) {
|
|
chat.log(new MessagePacket("Connecting to " + address + " on port " + port + ".", MessageType.INFO));
|
|
chat.log(new MessagePacket("Connecting to " + address + " on port " + port + ".", MessageType.INFO));
|
|
if (getMessages != null)
|
|
if (getMessages != null)
|
|
getMessages.interrupt();
|
|
getMessages.interrupt();
|
|
-
|
|
|
|
|
|
+
|
|
try {
|
|
try {
|
|
so.close();
|
|
so.close();
|
|
objIn.close();
|
|
objIn.close();
|
|
out.close();
|
|
out.close();
|
|
} catch (NullPointerException ex) {
|
|
} catch (NullPointerException ex) {
|
|
- //Nothing
|
|
|
|
|
|
+ // Nothing
|
|
} catch (IOException ex) {
|
|
} catch (IOException ex) {
|
|
chat.log(new MessagePacket(ex.toString(), MessageType.ERROR));
|
|
chat.log(new MessagePacket(ex.toString(), MessageType.ERROR));
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
try {
|
|
try {
|
|
so = new Socket();
|
|
so = new Socket();
|
|
- so.connect(new InetSocketAddress(address, port), 1000);
|
|
|
|
|
|
+ so.connect(new InetSocketAddress(address, port));
|
|
objIn = new ObjectInputStream(so.getInputStream());
|
|
objIn = new ObjectInputStream(so.getInputStream());
|
|
out = new PrintWriter(so.getOutputStream(), true);
|
|
out = new PrintWriter(so.getOutputStream(), true);
|
|
} catch (SocketTimeoutException ex) {
|
|
} catch (SocketTimeoutException ex) {
|
|
@@ -149,21 +150,21 @@ public class ChatWindow extends JFrame implements ActionListener, Runnable, KeyL
|
|
chat.log(new MessagePacket(e.toString(), MessageType.ERROR));
|
|
chat.log(new MessagePacket(e.toString(), MessageType.ERROR));
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
-
|
|
|
|
- send(username); //First packet sent to server sets username
|
|
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ send(username); // First packet sent to server sets username
|
|
|
|
+
|
|
getMessages = new Thread(this);
|
|
getMessages = new Thread(this);
|
|
getMessages.start();
|
|
getMessages.start();
|
|
-
|
|
|
|
|
|
+
|
|
write.setEnabled(true);
|
|
write.setEnabled(true);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
public void actionPerformed(ActionEvent e) {
|
|
if (e.getSource().equals(reconnect))
|
|
if (e.getSource().equals(reconnect))
|
|
connect(adress, port, username);
|
|
connect(adress, port, username);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void run() {
|
|
public void run() {
|
|
try {
|
|
try {
|
|
@@ -171,12 +172,14 @@ public class ChatWindow extends JFrame implements ActionListener, Runnable, KeyL
|
|
} catch (EOFException eof) {
|
|
} catch (EOFException eof) {
|
|
chat.log(new MessagePacket(eof.toString() + " Disconnected from host.", MessageType.ERROR));
|
|
chat.log(new MessagePacket(eof.toString() + " Disconnected from host.", MessageType.ERROR));
|
|
} catch (ClassNotFoundException cnf) {
|
|
} catch (ClassNotFoundException cnf) {
|
|
- chat.log(new MessagePacket("The message recieved from the server could not be understood. Are you using the right version?", MessageType.ERROR));
|
|
|
|
|
|
+ chat.log(new MessagePacket(
|
|
|
|
+ "The message recieved from the server could not be understood. Are you using the right version?",
|
|
|
|
+ MessageType.ERROR));
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
chat.log(new MessagePacket(e.toString(), MessageType.ERROR));
|
|
chat.log(new MessagePacket(e.toString(), MessageType.ERROR));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
public void getMessages() throws IOException, ClassNotFoundException {
|
|
public void getMessages() throws IOException, ClassNotFoundException {
|
|
while (!getMessages.isInterrupted()) {
|
|
while (!getMessages.isInterrupted()) {
|
|
Object fromServer = objIn.readObject();
|
|
Object fromServer = objIn.readObject();
|
|
@@ -185,22 +188,21 @@ public class ChatWindow extends JFrame implements ActionListener, Runnable, KeyL
|
|
chat.log(mess);
|
|
chat.log(mess);
|
|
} else if (fromServer instanceof InfoPacket) {
|
|
} else if (fromServer instanceof InfoPacket) {
|
|
InfoPacket info = (InfoPacket) fromServer;
|
|
InfoPacket info = (InfoPacket) fromServer;
|
|
-
|
|
|
|
|
|
+
|
|
infoLabel.setText("<html>" + info.toString().replace("\n", "<br>"));
|
|
infoLabel.setText("<html>" + info.toString().replace("\n", "<br>"));
|
|
-
|
|
|
|
|
|
+
|
|
model = new DefaultListModel<String>();
|
|
model = new DefaultListModel<String>();
|
|
- for (String user: info.usersOnline)
|
|
|
|
|
|
+ for (String user : info.usersOnline)
|
|
model.addElement(user);
|
|
model.addElement(user);
|
|
-
|
|
|
|
|
|
+
|
|
userList.setModel(model);
|
|
userList.setModel(model);
|
|
- }
|
|
|
|
- else if (fromServer instanceof String) {
|
|
|
|
|
|
+ } else if (fromServer instanceof String) {
|
|
chat.log(new MessagePacket((String) fromServer, MessageType.NORMAL));
|
|
chat.log(new MessagePacket((String) fromServer, MessageType.NORMAL));
|
|
} else
|
|
} else
|
|
throw new ClassNotFoundException();
|
|
throw new ClassNotFoundException();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void keyPressed(KeyEvent eKey) {
|
|
public void keyPressed(KeyEvent eKey) {
|
|
int keyCode = eKey.getKeyCode();
|
|
int keyCode = eKey.getKeyCode();
|
|
@@ -231,10 +233,12 @@ public class ChatWindow extends JFrame implements ActionListener, Runnable, KeyL
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
- public void keyReleased(KeyEvent arg0) {}
|
|
|
|
-
|
|
|
|
|
|
+ public void keyReleased(KeyEvent arg0) {
|
|
|
|
+ }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
- public void keyTyped(KeyEvent arg0) {}
|
|
|
|
|
|
+ public void keyTyped(KeyEvent arg0) {
|
|
|
|
+ }
|
|
}
|
|
}
|