From e11739bf0faecd2a50bf79d676028c4b1b752404 Mon Sep 17 00:00:00 2001
From: Hai HoDac <hhodac@student.unimelb.edu.au>
Date: Fri, 25 Oct 2019 14:59:33 +1100
Subject: [PATCH] Merged with master (Eldar code)

---
 src/GUI/ChatScreen.form            |  8 ++++--
 src/GUI/ChatScreen.java            | 28 +++++++++++++++++----
 src/GUI/DrawingArea.java           | 40 +++++++++++++++++++++++++++---
 src/client/DrawingUpdate.java      | 23 +++++++++++++++--
 src/remote/IDrawingController.java |  1 +
 src/remote/IDrawingUpdate.java     |  1 +
 src/server/DrawingController.java  | 10 ++++++++
 7 files changed, 99 insertions(+), 12 deletions(-)

diff --git a/src/GUI/ChatScreen.form b/src/GUI/ChatScreen.form
index 8d821a8..140273c 100644
--- a/src/GUI/ChatScreen.form
+++ b/src/GUI/ChatScreen.form
@@ -129,13 +129,17 @@
                 <properties/>
                 <border type="none" title="Drawing Users"/>
                 <children>
-                  <component id="9aa9f" class="javax.swing.JList" binding="list1" default-binding="true">
+                  <component id="2214f" class="javax.swing.JList" binding="allUsersList">
                     <constraints>
                       <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
                         <preferred-size width="150" height="50"/>
                       </grid>
                     </constraints>
-                    <properties/>
+                    <properties>
+                      <model>
+                        <item value="drawingUserList"/>
+                      </model>
+                    </properties>
                   </component>
                 </children>
               </grid>
diff --git a/src/GUI/ChatScreen.java b/src/GUI/ChatScreen.java
index e98608c..1ffd49f 100644
--- a/src/GUI/ChatScreen.java
+++ b/src/GUI/ChatScreen.java
@@ -32,13 +32,13 @@ public class ChatScreen {
     private JPanel managersPanel;
     private JPanel chatPanel;
     private JButton exitThisRoomButton;
-    private JList list1;
+    private DefaultListModel defaultListModel;
 
-    public JTextArea getActiveDrawingUserBox() {
-        return activeDrawingUserBox;
+    public DefaultListModel getAllUserModel() {
+        return defaultListModel;
     }
 
-    private JTextArea activeDrawingUserBox;
+    private JList allUsersList;
     private JFrame frame;
 
     public Client getClient() {
@@ -50,12 +50,29 @@ public class ChatScreen {
     public ChatScreen(Client client)
     {
         this.client = client;
-        activeDrawingUserBox = new JTextArea();
         yourNameDisplay.setText(client.getUserName());
         exitThisRoomButton.addActionListener(actionListener);
         sendButton.addActionListener(actionListener);
         kickOutButton.addActionListener(actionListener);
         promoteToManagerButton.addActionListener(actionListener);
+        defaultListModel = new DefaultListModel();
+        allUsersList.setModel(defaultListModel);
+
+        allUsersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+        allUsersList.setSelectedIndex(0);
+        allUsersList.setVisibleRowCount(5);
+
+
+
+
+    }
+
+    public void addUserToList(){
+
+    }
+
+    public void removeUserFromList(){
+
     }
 
     public void setUserName(String userName)
@@ -180,4 +197,5 @@ public class ChatScreen {
         }
 
     };
+
 }
diff --git a/src/GUI/DrawingArea.java b/src/GUI/DrawingArea.java
index a670fea..6282dfd 100644
--- a/src/GUI/DrawingArea.java
+++ b/src/GUI/DrawingArea.java
@@ -10,7 +10,6 @@ import java.awt.event.*;
 import java.awt.geom.Ellipse2D;
 import java.awt.geom.Line2D;
 import java.awt.geom.Rectangle2D;
-import java.awt.geom.RectangularShape;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
@@ -290,7 +289,13 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
 
         try {
             drawingController.broadcastDrawing(client.getUserName(), drawing, currentMode.toString(), shapeColor, strokeSize);
-            drawingController.broadcastDrawingUser(client.getUserName());
+            if (currentMode == Mode.TEXT){
+                notifyUsingTimer();
+            }
+            else {
+                drawingController.broadcastDrawingUserStopped(client.getUserName());
+            }
+
         } catch (RemoteException ex) {
             ex.printStackTrace();
         }
@@ -298,6 +303,31 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
         drawing = null;
     }
 
+    private void notifyUsingTimer() throws RemoteException {
+
+        Timer timer = new Timer(4000, new ActionListener()
+        {
+
+            @Override
+            public void actionPerformed(ActionEvent e)
+            {
+                try {
+                    client.getDrawingController().broadcastDrawingUserStopped(client.getDefaultUserName());
+                } catch (RemoteException ex) {
+                    ex.printStackTrace();
+                }
+
+            }
+
+        });
+        try {
+            client.getDrawingController().broadcastDrawingUser(client.getDefaultUserName());
+        } catch (RemoteException ex) {
+            ex.printStackTrace();
+        }
+        timer.start();
+    }
+
     @Override
     public void mouseEntered(MouseEvent e) {
 
@@ -371,7 +401,11 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
         g2.setStroke(lineStroke);
         repaint();
         try {
-            drawingController.broadcastDraggingDrawing(client.getUserName(), drawing, currentMode.toString(), shapeColor, strokeSize);
+            if (currentMode != Mode.TEXT){
+                drawingController.broadcastDraggingDrawing(client.getUserName(), drawing, currentMode.toString(), shapeColor, strokeSize);
+                drawingController.broadcastDrawingUser(client.getUserName());
+            }
+
         } catch (RemoteException ex) {
             ex.printStackTrace();
         }
diff --git a/src/client/DrawingUpdate.java b/src/client/DrawingUpdate.java
index 0b9d086..60ea668 100644
--- a/src/client/DrawingUpdate.java
+++ b/src/client/DrawingUpdate.java
@@ -1,8 +1,8 @@
 package client;
 
-import GUI.DrawingArea;
 import remote.IDrawingUpdate;
 
+import javax.swing.*;
 import java.awt.*;
 import java.awt.image.BufferedImage;
 import java.io.ByteArrayInputStream;
@@ -21,10 +21,28 @@ public class DrawingUpdate extends UnicastRemoteObject implements IDrawingUpdate
     }
 
     public boolean notifyUserIsDrawing(String fromClient) throws RemoteException {
-        client.getApplicationMain().getChatScreen().getActiveDrawingUserBox().append(fromClient);
+        System.out.println("Adding name of user to the list of drawing users");
+        DefaultListModel temp = client.getApplicationMain().getChatScreen().getAllUserModel();
+        if(temp.contains(fromClient)){
+            System.out.println("Already in the list");
+        }
+        else {
+            temp.addElement(fromClient);
+        }
+
         return true;
     }
 
+    public void notifyUserStoppedDrawing(String fromClient) throws RemoteException{
+        DefaultListModel temp = client.getApplicationMain().getChatScreen().getAllUserModel();
+        int elementIndex = -1;
+        if(temp.contains(fromClient)){
+            elementIndex = temp.indexOf(fromClient);
+            temp.remove(elementIndex);
+        }
+
+    }
+
     @Override
     public boolean notifyTextDrawing(String fromClient, String text, Font font, Color color, Point startPoint) throws RemoteException {
         client.getApplicationMain().getPaintGUI().getDrawingArea().getG2().setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
@@ -55,6 +73,7 @@ public class DrawingUpdate extends UnicastRemoteObject implements IDrawingUpdate
         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
         g2.setColor(color);
         client.getApplicationMain().getPaintGUI().getDrawingArea().repaint();
+
         return true;
     }
 
diff --git a/src/remote/IDrawingController.java b/src/remote/IDrawingController.java
index 8b2e030..571cd2f 100644
--- a/src/remote/IDrawingController.java
+++ b/src/remote/IDrawingController.java
@@ -23,4 +23,5 @@ public interface IDrawingController extends Remote {
     boolean updateImage(byte[] rawImage) throws RemoteException;
     void broadcastDrawingUser (String fromClient) throws RemoteException;
     void getImage(String fromClient) throws RemoteException;
+    void broadcastDrawingUserStopped (String fromClient) throws RemoteException;
 }
diff --git a/src/remote/IDrawingUpdate.java b/src/remote/IDrawingUpdate.java
index cd06602..3509141 100644
--- a/src/remote/IDrawingUpdate.java
+++ b/src/remote/IDrawingUpdate.java
@@ -12,4 +12,5 @@ public interface IDrawingUpdate extends Remote, Serializable {
     boolean notifyCanvasClearance(String fromClient) throws RemoteException;
     boolean receiveImage(byte[] rawImage) throws RemoteException;
     boolean notifyUserIsDrawing(String fromClient) throws RemoteException;
+    void notifyUserStoppedDrawing(String fromClient) throws RemoteException;
 }
diff --git a/src/server/DrawingController.java b/src/server/DrawingController.java
index efae67e..38a850d 100644
--- a/src/server/DrawingController.java
+++ b/src/server/DrawingController.java
@@ -175,6 +175,16 @@ public class DrawingController extends UnicastRemoteObject implements IDrawingCo
 
     }
 
+    public void broadcastDrawingUserStopped (String fromClient) throws RemoteException{
+        System.out.println("Current user stopped drawing" + fromClient);
+        IDrawingUpdate client;
+        for (User u: server.users){
+            client = u.getIDrawingUpdate();
+            client.notifyUserStoppedDrawing(fromClient);
+        }
+
+    }
+
 
 
     public boolean broadcastUpdateImage(String fromClient) throws RemoteException {
-- 
GitLab