From 8e3eea0119a72749b230ccbd0c4cfbf264036e00 Mon Sep 17 00:00:00 2001
From: Hai HoDac <hhodac@student.unimelb.edu.au>
Date: Mon, 21 Oct 2019 22:02:33 +1100
Subject: [PATCH] All works so far
---
src/GUI/ApplicationMain.java | 28 ++++++++++++++++++
src/GUI/ChatScreen.form | 4 +--
src/GUI/ChatScreen.java | 49 ++++++++++++++++---------------
src/client/ChatUpdate.java | 17 +++++++++--
src/client/Client.java | 2 +-
src/client/ClientUpdate.java | 26 +++++++++++++++-
src/remote/IChatController.java | 1 +
src/remote/IChatUpdate.java | 2 +-
src/remote/IClientController.java | 2 +-
src/remote/IClientUpdate.java | 2 ++
src/server/ChatController.java | 20 ++++++++++++-
src/server/ClientController.java | 29 ++++++++++++++++--
src/server/User.java | 2 +-
13 files changed, 146 insertions(+), 38 deletions(-)
diff --git a/src/GUI/ApplicationMain.java b/src/GUI/ApplicationMain.java
index 35fa142..6c35fec 100644
--- a/src/GUI/ApplicationMain.java
+++ b/src/GUI/ApplicationMain.java
@@ -4,6 +4,9 @@ import client.Client;
import javax.swing.*;
import java.awt.*;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.rmi.RemoteException;
public class ApplicationMain extends JPanel {
Client client;
@@ -32,6 +35,31 @@ public class ApplicationMain extends JPanel {
content.setLayout(new BorderLayout());
content.add(paintGUI.getGlobal(), BorderLayout.WEST);
content.add(chatScreen.panel2, BorderLayout.EAST);
+ SwingUtilities.getRootPane(chatScreen.getSendButton()).setDefaultButton(chatScreen.getSendButton());
+ frame.addWindowListener(new WindowAdapter()
+ {
+ @Override
+ public void windowClosing(WindowEvent arg0)
+ {
+ int answer = JOptionPane.showConfirmDialog(null,
+ "Are you sure you want to quit the session?",
+ "Shut down session", JOptionPane.YES_NO_OPTION);
+ System.out.println(answer);
+ if (answer == 0)
+ {
+ System.out.println("Quitting session");
+ try
+ {
+ client.getClientController().quit(client.getUserName());
+ }
+ catch (RemoteException e)
+ {
+ e.printStackTrace();
+ }
+ System.exit(0);
+ }
+ }
+ });
frame.setSize(1200, 700);
frame.setLocationRelativeTo( null );
diff --git a/src/GUI/ChatScreen.form b/src/GUI/ChatScreen.form
index dcf8652..baeed11 100644
--- a/src/GUI/ChatScreen.form
+++ b/src/GUI/ChatScreen.form
@@ -8,7 +8,7 @@
<properties>
<maximumSize width="-1" height="-1"/>
<minimumSize width="-1" height="-1"/>
- <preferredSize width="700" height="700"/>
+ <preferredSize width="700" height="600"/>
</properties>
<border type="none"/>
<children>
@@ -185,7 +185,7 @@
<text value="Send Message To:"/>
</properties>
</component>
- <component id="cc35d" class="javax.swing.JComboBox" binding="sentMessageToComboBox">
+ <component id="cc35d" class="javax.swing.JComboBox" binding="sendMessageToComboBox">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
diff --git a/src/GUI/ChatScreen.java b/src/GUI/ChatScreen.java
index 5e34602..6cfea2e 100644
--- a/src/GUI/ChatScreen.java
+++ b/src/GUI/ChatScreen.java
@@ -5,17 +5,18 @@ import remote.IChatController;
import remote.IClientController;
import javax.swing.*;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
+import java.awt.event.*;
import java.rmi.RemoteException;
public class ChatScreen {
public JPanel panel2;
+
private JButton sendButton;
-// private JPanel drawingPanel;
+
+ // private JPanel drawingPanel;
private JPanel othersPanel;
- private JComboBox sentMessageToComboBox;
+ private JComboBox sendMessageToComboBox;
private JTextArea chatDisplayBox;
private JComboBox userSelectComboBox;
private JButton kickOutButton;
@@ -32,7 +33,6 @@ public class ChatScreen {
private JButton exitThisRoomButton;
private JButton quitButton;
private JFrame frame;
-
public Client getClient() {
return client;
}
@@ -47,22 +47,29 @@ public class ChatScreen {
sendButton.addActionListener(actionListener);
yourNameDisplay.setText(client.getUserName());
// drawingPanel = new PaintGUI(client);
+// SwingUtilities.getRootPane(sendButton).setDefaultButton(sendButton);
}
+ public JButton getSendButton() {
+ return sendButton;
+ }
+
public JTextArea getChatDisplayBox() {
return chatDisplayBox;
}
- public JComboBox getSentMessageToComboBox() {
- return sentMessageToComboBox;
+ public JComboBox getSendMessageToComboBox()
+ {
+ return sendMessageToComboBox;
}
// public JPanel getDrawingPanel() {
// return drawingPanel;
// }
+
ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
@@ -75,7 +82,17 @@ public class ChatScreen {
try
{
System.out.println("Send button pressed");
- chatController.broadcastMessage(client.getUserName(), message);
+
+ String toUser = sendMessageToComboBox.getSelectedItem().toString();
+
+ if( toUser.equals("All") )
+ {
+ chatController.broadcastMessage(client.getUserName(), message);
+ }
+ else
+ {
+ chatController.sendPrivateMessage(client.getUserName(), toUser, message);
+ }
}
catch (RemoteException ex)
{
@@ -99,20 +116,4 @@ public class ChatScreen {
}
};
-
- public void showGUI() {
- frame = new JFrame("Application");
- JFrame.setDefaultLookAndFeelDecorated(true);
- frame.setContentPane(panel2);
-// createUIComponents();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.pack();
- frame.setResizable(false);
- frame.setVisible(true);
- }
-
-// private void createUIComponents() {
-// PaintGUI paintGUI = new PaintGUI(client);
-// drawingPanel = paintGUI.getGlobal();
-// }
}
diff --git a/src/client/ChatUpdate.java b/src/client/ChatUpdate.java
index 08f89e0..2edc3cc 100644
--- a/src/client/ChatUpdate.java
+++ b/src/client/ChatUpdate.java
@@ -17,12 +17,23 @@ public class ChatUpdate extends UnicastRemoteObject implements IChatUpdate, Seri
}
@Override
- public boolean notifyChat(String fromClient, String message) throws RemoteException
+ public boolean notifyChat(String fromClient, String message, boolean isPrivate) throws RemoteException
{
- client.getApplicationMain().getChatScreen().getChatDisplayBox().append(fromClient + ": " + message + "\n");
+ String outputString;
+
+ if( isPrivate )
+ {
+ outputString = "PRIVATE (from " + fromClient + "): " + message + "\n";
+ }
+ else
+ {
+ outputString = fromClient + ": " + message + "\n";
+ }
+
+ client.getApplicationMain().getChatScreen().getChatDisplayBox().append(outputString);
//client.setReceivedMessage(message);
- System.out.println(fromClient + ": " + message);
+ //System.out.println(fromClient + ": " + message);
return true;
}
diff --git a/src/client/Client.java b/src/client/Client.java
index 01b3194..3edda91 100644
--- a/src/client/Client.java
+++ b/src/client/Client.java
@@ -105,7 +105,7 @@ public class Client
clientController = (IClientController) registryServer.lookup("ClientController");
drawingController = (IDrawingController) registryServer.lookup("DrawingController");
- if (clientController.join(userName, this.chatUpdate, this.drawingUpdate, this.clientUpdate))
+ if (clientController.join(userName, this.chatUpdate, this.clientUpdate, this.drawingUpdate))
{
System.out.println("Connected to server");
diff --git a/src/client/ClientUpdate.java b/src/client/ClientUpdate.java
index 4faa272..aefdaf5 100644
--- a/src/client/ClientUpdate.java
+++ b/src/client/ClientUpdate.java
@@ -2,7 +2,9 @@ package client;
import remote.IClientUpdate;
+import javax.swing.*;
import java.io.Serializable;
+import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
@@ -19,7 +21,29 @@ public class ClientUpdate extends UnicastRemoteObject implements IClientUpdate,
@Override
public boolean notifyClient(String fromClient, String newUsername) throws RemoteException
{
- client.getChatScreen().getSentMessageToComboBox().addItem(newUsername);
+ client.getApplicationMain().getChatScreen().getSendMessageToComboBox().addItem(newUsername);
+
+ return true;
+ }
+
+ @Override
+ public boolean updateUserList(String[] users) throws RemoteException
+ {
+ //client.setConnectedUsers(users);
+
+ JComboBox userBox = client.getApplicationMain().getChatScreen().getSendMessageToComboBox();
+
+ userBox.removeAllItems();
+
+ userBox.addItem("All");
+
+ for( String s : users )
+ {
+ if( !s.equals(client.getUserName()) )
+ {
+ userBox.addItem(s);
+ }
+ }
return true;
}
diff --git a/src/remote/IChatController.java b/src/remote/IChatController.java
index 64e6811..8230587 100644
--- a/src/remote/IChatController.java
+++ b/src/remote/IChatController.java
@@ -8,4 +8,5 @@ public interface IChatController extends Remote
boolean broadcastMessage(String fromClient, String message) throws RemoteException;
boolean broadcastMessageUserLogin(String fromClient) throws RemoteException;
boolean broadcastMessageUserLogout(String fromClient) throws RemoteException;
+ boolean sendPrivateMessage(String fromClient, String toClient, String message) throws RemoteException;
}
diff --git a/src/remote/IChatUpdate.java b/src/remote/IChatUpdate.java
index 55b2e62..0c9b54d 100644
--- a/src/remote/IChatUpdate.java
+++ b/src/remote/IChatUpdate.java
@@ -6,7 +6,7 @@ import java.rmi.RemoteException;
public interface IChatUpdate extends Remote, Serializable
{
- boolean notifyChat(String fromClient, String message) throws RemoteException;
+ boolean notifyChat(String fromClient, String message, boolean isPrivate) throws RemoteException;
boolean notifyUserLogin(String fromClient) throws RemoteException;
boolean notifyUserLogout(String fromClient) throws RemoteException;
}
diff --git a/src/remote/IClientController.java b/src/remote/IClientController.java
index 35539cd..bde0a0f 100644
--- a/src/remote/IClientController.java
+++ b/src/remote/IClientController.java
@@ -5,7 +5,7 @@ import java.rmi.RemoteException;
public interface IClientController extends Remote
{
- boolean join(String username, IChatUpdate clientChat, IDrawingUpdate clientDrawing, IClientUpdate clientUpdate) throws RemoteException;
+ boolean join(String username, IChatUpdate clientChat, IClientUpdate clientUpdate, IDrawingUpdate clientDrawing) throws RemoteException;
void quit(String username) throws RemoteException;
diff --git a/src/remote/IClientUpdate.java b/src/remote/IClientUpdate.java
index 9eb5bc9..c1337f9 100644
--- a/src/remote/IClientUpdate.java
+++ b/src/remote/IClientUpdate.java
@@ -7,4 +7,6 @@ import java.rmi.RemoteException;
public interface IClientUpdate extends Remote, Serializable {
boolean notifyClient(String fromClient, String newUsername) throws RemoteException;
+
+ boolean updateUserList(String[] users) throws RemoteException;
}
diff --git a/src/server/ChatController.java b/src/server/ChatController.java
index 79c344a..8fbff1e 100644
--- a/src/server/ChatController.java
+++ b/src/server/ChatController.java
@@ -26,7 +26,7 @@ public class ChatController extends UnicastRemoteObject implements IChatControll
for( User u : server.users )
{
client = u.getIChatUpdate();
- client.notifyChat(fromClient, message);
+ client.notifyChat(fromClient, message, false);
}
System.out.print("...DONE\n");
@@ -69,4 +69,22 @@ public class ChatController extends UnicastRemoteObject implements IChatControll
return true;
}
+
+ @Override
+ public boolean sendPrivateMessage(String fromClient, String toClient, String message) throws RemoteException
+ {
+ int toClientIndex = server.clientController.getUserIndex(toClient);
+ int fromClientIndex = server.clientController.getUserIndex(fromClient);
+
+ if( toClientIndex >= 0 && fromClientIndex >= 0 )
+ {
+ server.users.get(toClientIndex).getIChatUpdate().notifyChat(fromClient, message, true);
+
+ server.users.get(fromClientIndex).getIChatUpdate().notifyChat(fromClient, message, true);
+
+ return true;
+ }
+
+ return false;
+ }
}
diff --git a/src/server/ClientController.java b/src/server/ClientController.java
index 267c0cb..5e7638f 100644
--- a/src/server/ClientController.java
+++ b/src/server/ClientController.java
@@ -6,6 +6,7 @@ import remote.IDrawingUpdate;
import remote.IClientController;
import java.io.Serializable;
+import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
@@ -19,11 +20,11 @@ public class ClientController extends UnicastRemoteObject implements IClientCont
}
@Override
- public boolean join(String username, IChatUpdate clientChat, IDrawingUpdate clientDrawing, IClientUpdate clientUpdate) throws RemoteException
+ public boolean join(String username, IChatUpdate clientChat, IClientUpdate clientUpdate, IDrawingUpdate clientDrawing) throws RemoteException
{
server.chatController.broadcastMessageUserLogin(username);
- User newUser = new User(username, clientChat, clientDrawing, clientUpdate);
+ User newUser = new User(username, clientChat, clientUpdate, clientDrawing);
server.users.add(newUser);
@@ -34,6 +35,8 @@ public class ClientController extends UnicastRemoteObject implements IClientCont
System.out.println(username + " registered successfully");
+ broadcastUserList();
+
return true;
}
@@ -47,6 +50,8 @@ public class ClientController extends UnicastRemoteObject implements IClientCont
server.users.remove(userIndex);
server.chatController.broadcastMessageUserLogout(username);
+
+ broadcastUserList();
}
}
@@ -75,13 +80,16 @@ public class ClientController extends UnicastRemoteObject implements IClientCont
if ( adminIndex > 0 && userIndex > 0 && server.users.get(adminIndex).isAdmin() )
{
server.users.remove(userIndex);
+
+ broadcastUserList();
+
return true;
}
return false;
}
- private int getUserIndex(String username)
+ public int getUserIndex(String username)
{
int index = -1;
@@ -96,4 +104,19 @@ public class ClientController extends UnicastRemoteObject implements IClientCont
return index;
}
+
+ private void broadcastUserList() throws RemoteException
+ {
+ String[] connectedUsers = new String[server.users.size()];
+
+ for( int i = 0; i<server.users.size(); i++ )
+ {
+ connectedUsers[i] = server.users.get(i).getUserName();
+ }
+
+ for( User u : server.users )
+ {
+ u.getIClientUpdate().updateUserList(connectedUsers);
+ }
+ }
}
diff --git a/src/server/User.java b/src/server/User.java
index 7211122..543f26e 100644
--- a/src/server/User.java
+++ b/src/server/User.java
@@ -12,7 +12,7 @@ public class User
private IClientUpdate IClientUpdate;
private boolean isAdmin;
- public User(String username, IChatUpdate IChatUpdate, IDrawingUpdate IDrawingUpdate, IClientUpdate IClientUpdate)
+ public User(String username, IChatUpdate IChatUpdate, IClientUpdate IClientUpdate, IDrawingUpdate IDrawingUpdate)
{
this.username = username;
this.IChatUpdate = IChatUpdate;
--
GitLab