Skip to content
Snippets Groups Projects
Commit 8e3eea01 authored by Hai HoDac's avatar Hai HoDac
Browse files

All works so far

parent c79f0bbf
No related branches found
No related tags found
No related merge requests found
......@@ -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 );
......
......@@ -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>
......
......@@ -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 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,8 +82,18 @@ public class ChatScreen {
try
{
System.out.println("Send button pressed");
String toUser = sendMessageToComboBox.getSelectedItem().toString();
if( toUser.equals("All") )
{
chatController.broadcastMessage(client.getUserName(), message);
}
else
{
chatController.sendPrivateMessage(client.getUserName(), toUser, message);
}
}
catch (RemoteException ex)
{
ex.printStackTrace();
......@@ -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();
// }
}
......@@ -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;
}
......
......@@ -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");
......
......@@ -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;
}
......
......@@ -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;
}
......@@ -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;
}
......@@ -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;
......
......@@ -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;
}
......@@ -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;
}
}
......@@ -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);
}
}
}
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment