From ced5aae13b28ae839f63bc613580369fcb3c904b Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Tue, 6 Feb 2024 22:35:43 +0100 Subject: [PATCH 01/66] Send message to server when user presses enter --- src/main/java/ChatClient.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 8d0b776..ce66879 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -2,13 +2,14 @@ import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; -import java.io.IOException; +import java.io.*; import java.net.Socket; public class ChatClient extends JFrame implements KeyListener { private String address; private int port; private Socket connectionToServer; + private PrintWriter toServerWriter; // GUI private JTextArea outputTextArea; @@ -43,8 +44,9 @@ public class ChatClient extends JFrame implements KeyListener { private void receiveMessages() { try { connectionToServer = new Socket(address, port); + toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); initGui(); - } catch (Exception e) { + } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); } } @@ -59,7 +61,8 @@ public class ChatClient extends JFrame implements KeyListener { if (e.getKeyCode() == KeyEvent.VK_ENTER) { String message = inputTextField.getText(); if (!message.isEmpty()) { - // Senden die Nachricht an Server, wenn der erstellt wird + toServerWriter.println(message); + toServerWriter.flush(); inputTextField.setText(""); } } From b3e564625f62c755d7f9889eb4e0dfb569994b84 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 11:21:46 +0100 Subject: [PATCH 02/66] Added GUI window for user login --- src/main/java/LoginGUI.java | 29 +++++++++++++++++++++++++++++ user.json | 18 ++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/main/java/LoginGUI.java diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java new file mode 100644 index 0000000..31fc2f2 --- /dev/null +++ b/src/main/java/LoginGUI.java @@ -0,0 +1,29 @@ +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.List; + +public class LoginGUI extends JFrame implements ActionListener { + + public LoginGUI() { + setTitle("Login"); + setSize(300, 150); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setLayout(null); + + + } + + @Override + public void actionPerformed(ActionEvent e) { + + } + + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + LoginGUI loginGUI = new LoginGUI(); + loginGUI.setVisible(true); + }); + } +} \ No newline at end of file diff --git a/user.json b/user.json index 0201a67..78ef422 100644 --- a/user.json +++ b/user.json @@ -10,5 +10,23 @@ "userName": "asd1", "password": "123456", "birthday": "1" + }, + { + "id": "b54391dc-c06b-496c-828a-501f9a182cf9", + "userName": "new user", + "password": "123456", + "birthday": "1" + }, + { + "id": "6db2ea00-246d-4604-bef2-0875c3cb550e", + "userName": "Another Test User", + "password": "TestPasswort123", + "birthday": "01.01.2000" + }, + { + "id": "864695a6-8037-41e5-9f0e-4b92744b2113", + "userName": "Another Test User", + "password": "TestPasswort123", + "birthday": "01.01.2000" } ] \ No newline at end of file From 061af6313f155018174364cd7f10519aed3b09b0 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 11:24:42 +0100 Subject: [PATCH 03/66] Added textfield for username input --- src/main/java/LoginGUI.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 31fc2f2..b4e90a2 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -4,6 +4,7 @@ import java.awt.event.ActionListener; import java.util.List; public class LoginGUI extends JFrame implements ActionListener { + private JTextField usernameField; public LoginGUI() { setTitle("Login"); @@ -11,6 +12,13 @@ public class LoginGUI extends JFrame implements ActionListener { setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); + JLabel usernameLabel = new JLabel("Username:"); + usernameLabel.setBounds(20, 20, 80, 25); + add(usernameLabel); + + usernameField = new JTextField(); + usernameField.setBounds(100, 20, 160, 25); + add(usernameField); } From fce141d40ef0cf9768ef311097c5840a5bd8c381 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 11:26:07 +0100 Subject: [PATCH 04/66] Added textfield for password input --- src/main/java/LoginGUI.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index b4e90a2..879cb74 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -5,6 +5,7 @@ import java.util.List; public class LoginGUI extends JFrame implements ActionListener { private JTextField usernameField; + private JPasswordField passwordField; public LoginGUI() { setTitle("Login"); @@ -20,6 +21,14 @@ public class LoginGUI extends JFrame implements ActionListener { usernameField.setBounds(100, 20, 160, 25); add(usernameField); + JLabel passwordLabel = new JLabel("Password:"); + passwordLabel.setBounds(20, 50, 80, 25); + add(passwordLabel); + + passwordField = new JPasswordField(); + passwordField.setBounds(100, 50, 160, 25); + add(passwordField); + } @Override From 2fe0eb289b1b0d5679f8002a1e0afa405d73c38f Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 11:28:15 +0100 Subject: [PATCH 05/66] Added action button for login window --- src/main/java/LoginGUI.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 879cb74..08b19a2 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -6,6 +6,7 @@ import java.util.List; public class LoginGUI extends JFrame implements ActionListener { private JTextField usernameField; private JPasswordField passwordField; + private JButton loginButton; public LoginGUI() { setTitle("Login"); @@ -29,6 +30,11 @@ public class LoginGUI extends JFrame implements ActionListener { passwordField.setBounds(100, 50, 160, 25); add(passwordField); + loginButton = new JButton("Login"); + loginButton.setBounds(100, 90, 100, 25); + loginButton.addActionListener(this); + add(loginButton); + } @Override From d0428d81ca59717d1e5404d6981dc18d36fbb7e0 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 11:31:51 +0100 Subject: [PATCH 06/66] Added basic click functionality to login button --- src/main/java/LoginGUI.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 08b19a2..4df5634 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -39,7 +39,15 @@ public class LoginGUI extends JFrame implements ActionListener { @Override public void actionPerformed(ActionEvent e) { - + if (e.getSource() == loginButton) { + String username = usernameField.getText(); + String password = new String(passwordField.getPassword()); + + //Add login functionality + JOptionPane.showMessageDialog(this, "Login"); + + + } } From 96111842f4de0dad161868cfeece38b7d65147f1 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 14:39:41 +0100 Subject: [PATCH 07/66] Added logic for checking if username / password exist --- src/main/java/LoginGUI.java | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 4df5634..d19db0e 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -43,12 +43,29 @@ public class LoginGUI extends JFrame implements ActionListener { String username = usernameField.getText(); String password = new String(passwordField.getPassword()); - //Add login functionality - JOptionPane.showMessageDialog(this, "Login"); + if (authenticateUser(username, password)) { + JOptionPane.showMessageDialog(this, "Login successful!"); + // Perform actions after successful login + } else { + JOptionPane.showMessageDialog(this, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE); + } } } + + // Function to authenticate the user by comparing the entered username and password with the saved user data + private boolean authenticateUser(String username, String password) { + List userList = CreateUser.readUserListFromJsonFile("user.json"); + if (userList != null) { + for (CreateUser user : userList) { + if (user.getUserName().equals(username) && user.getPassword().equals(password)) { + return true; //Success + } + } + } + return false; // Fail + } public static void main(String[] args) { From 13d0a13392071df08b1e90ca0ee7b9e72afd2a86 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 14:57:22 +0100 Subject: [PATCH 08/66] Made it so pressing enter on the keyboard activates the login and ok button --- src/main/java/LoginGUI.java | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index d19db0e..e18d2ea 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -2,6 +2,8 @@ import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; public class LoginGUI extends JFrame implements ActionListener { private JTextField usernameField; @@ -35,22 +37,26 @@ public class LoginGUI extends JFrame implements ActionListener { loginButton.addActionListener(this); add(loginButton); + getRootPane().setDefaultButton(loginButton); + } @Override public void actionPerformed(ActionEvent e) { - if (e.getSource() == loginButton) { - String username = usernameField.getText(); - String password = new String(passwordField.getPassword()); + if (e.getSource() == loginButton) { + login(); + } + } - if (authenticateUser(username, password)) { + private void login() { + String username = usernameField.getText(); + String password = new String(passwordField.getPassword()); + + if (authenticateUser(username, password)) { JOptionPane.showMessageDialog(this, "Login successful!"); // Perform actions after successful login - } else { + } else { JOptionPane.showMessageDialog(this, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE); - } - - } } @@ -68,6 +74,21 @@ public class LoginGUI extends JFrame implements ActionListener { } + private class EnterKeyListener implements KeyListener { + @Override + public void keyTyped(KeyEvent e) {} + + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_ENTER) { + login(); + } + } + + @Override + public void keyReleased(KeyEvent e) {} + } + public static void main(String[] args) { SwingUtilities.invokeLater(() -> { LoginGUI loginGUI = new LoginGUI(); From 36bd9110bfe88dcc40909ccffe27ae90b17353b3 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 15:03:05 +0100 Subject: [PATCH 09/66] The login window now closes automatically after a successful login --- src/main/java/LoginGUI.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index e18d2ea..5bc3739 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -55,6 +55,8 @@ public class LoginGUI extends JFrame implements ActionListener { if (authenticateUser(username, password)) { JOptionPane.showMessageDialog(this, "Login successful!"); // Perform actions after successful login + + dispose(); } else { JOptionPane.showMessageDialog(this, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE); } From 3a38f7a3235289458e75ed9ba356cec7e4532052 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 15:12:23 +0100 Subject: [PATCH 10/66] Added a checkbox to the login window to stay logged in --- src/main/java/LoginGUI.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 5bc3739..696e3a5 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -9,10 +9,11 @@ public class LoginGUI extends JFrame implements ActionListener { private JTextField usernameField; private JPasswordField passwordField; private JButton loginButton; + private JCheckBox stayLoggedInCheckbox; public LoginGUI() { setTitle("Login"); - setSize(300, 150); + setSize(300, 180); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); @@ -32,8 +33,12 @@ public class LoginGUI extends JFrame implements ActionListener { passwordField.setBounds(100, 50, 160, 25); add(passwordField); + stayLoggedInCheckbox = new JCheckBox("Stay Logged In"); + stayLoggedInCheckbox.setBounds(20, 80, 150, 25); + add(stayLoggedInCheckbox); + loginButton = new JButton("Login"); - loginButton.setBounds(100, 90, 100, 25); + loginButton.setBounds(100, 110, 100, 25); loginButton.addActionListener(this); add(loginButton); From cec86a85d870e0a978d3ecf6007ef88625885c72 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 17:25:23 +0100 Subject: [PATCH 11/66] Added a boolean value along getter and setter methods to CreateUser that later checks if the user wants to stay logged in --- src/main/java/CreateUser.java | 9 +++++++++ src/main/java/LoginGUI.java | 8 ++++++++ user.json | 32 ++++++++------------------------ 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 8e8f15f..6bccbd9 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -17,6 +17,7 @@ class CreateUser { private String userName; private String password; private String birthday; + private boolean stayLoggedIn; // Constructor @@ -59,6 +60,14 @@ class CreateUser { public void setBirthday(String birthday) { this.birthday = birthday; } + + public boolean isStayLoggedIn() { + return stayLoggedIn; + } + + public void setStayLoggedIn(boolean stayLoggedIn) { + this.stayLoggedIn = stayLoggedIn; + } // Function to create user with validation public static CreateUser createUser(String id, String userName, String password, String birthday) { diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 696e3a5..b273014 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -44,6 +44,8 @@ public class LoginGUI extends JFrame implements ActionListener { getRootPane().setDefaultButton(loginButton); + passwordField.addKeyListener(new EnterKeyListener()); + } @Override @@ -56,6 +58,7 @@ public class LoginGUI extends JFrame implements ActionListener { private void login() { String username = usernameField.getText(); String password = new String(passwordField.getPassword()); + boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); // Get checkbox state if (authenticateUser(username, password)) { JOptionPane.showMessageDialog(this, "Login successful!"); @@ -67,6 +70,11 @@ public class LoginGUI extends JFrame implements ActionListener { } } + private void updateStayLoggedIn(String username, boolean stayLoggedIn) { + // Update stayLoggedIn in the JSON file for the user + + } + // Function to authenticate the user by comparing the entered username and password with the saved user data private boolean authenticateUser(String username, String password) { List userList = CreateUser.readUserListFromJsonFile("user.json"); diff --git a/user.json b/user.json index 78ef422..1f77562 100644 --- a/user.json +++ b/user.json @@ -1,32 +1,16 @@ [ { - "id": "961ca202-ecbd-4dfc-ac0b-28f367618aa1", - "userName": "asd", - "password": "123456", - "birthday": "1" - }, - { - "id": "d563a466-753b-4a5e-8b6c-e7e4756c7397", - "userName": "asd1", - "password": "123456", - "birthday": "1" - }, - { - "id": "b54391dc-c06b-496c-828a-501f9a182cf9", - "userName": "new user", - "password": "123456", - "birthday": "1" - }, - { - "id": "6db2ea00-246d-4604-bef2-0875c3cb550e", + "id": "a2864d79-1079-4cbb-8d77-f5f84995580d", "userName": "Another Test User", "password": "TestPasswort123", - "birthday": "01.01.2000" + "birthday": "01.01.2000", + "stayLoggedIn": false }, { - "id": "864695a6-8037-41e5-9f0e-4b92744b2113", - "userName": "Another Test User", - "password": "TestPasswort123", - "birthday": "01.01.2000" + "id": "3690702d-9c7e-48fb-8a01-ef89b3b76268", + "userName": "TestUser2", + "password": "123456", + "birthday": "01.01.2000", + "stayLoggedIn": false } ] \ No newline at end of file From a6e8d868d1dfc43f6e8da582e147d82b61ebd657 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:03:03 +0100 Subject: [PATCH 12/66] Read messages from server --- src/main/java/ChatClient.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index ce66879..0179d4c 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -9,6 +9,7 @@ public class ChatClient extends JFrame implements KeyListener { private String address; private int port; private Socket connectionToServer; + private BufferedReader fromServerReader; private PrintWriter toServerWriter; // GUI @@ -44,8 +45,15 @@ public class ChatClient extends JFrame implements KeyListener { private void receiveMessages() { try { connectionToServer = new Socket(address, port); + fromServerReader = new BufferedReader(new InputStreamReader(connectionToServer.getInputStream())); toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); + initGui(); + + while (true) { + String message = fromServerReader.readLine(); + outputTextArea.append(message + "\n"); + } } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); } From fdae378c96a236745e888e60b22f685ee1aca870 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Wed, 7 Feb 2024 19:03:55 +0100 Subject: [PATCH 13/66] Mockito in pom.xml --- pom.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pom.xml b/pom.xml index 21bd5b6..26be012 100644 --- a/pom.xml +++ b/pom.xml @@ -11,6 +11,14 @@ gson 2.10.1 + + + org.mockito + mockito-core + 4.1.10 + test + + \ No newline at end of file From a0d94ed9f8e4eae8d7a13c394d1ae36b5ec875d0 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:13:56 +0100 Subject: [PATCH 14/66] Add scrollbar to window --- src/main/java/ChatClient.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 0179d4c..44a0bc6 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -15,6 +15,7 @@ public class ChatClient extends JFrame implements KeyListener { // GUI private JTextArea outputTextArea; private JTextField inputTextField; + private JScrollPane outputScrollPane; public ChatClient(int port) { super("Chat"); @@ -31,11 +32,13 @@ public class ChatClient extends JFrame implements KeyListener { outputTextArea.setEditable(false); outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); + outputScrollPane = new JScrollPane(outputTextArea); + inputTextField = new JTextField(); inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); inputTextField.addKeyListener(this); - add(outputTextArea, BorderLayout.CENTER); + add(outputScrollPane, BorderLayout.CENTER); add(inputTextField, BorderLayout.SOUTH); setVisible(true); From 6d9cb97cba88ba86b40bbfc35dd4950b65ad049d Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:15:56 +0100 Subject: [PATCH 15/66] Add auto scroll to end --- src/main/java/ChatClient.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 44a0bc6..2c043b5 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -56,6 +56,7 @@ public class ChatClient extends JFrame implements KeyListener { while (true) { String message = fromServerReader.readLine(); outputTextArea.append(message + "\n"); + outputScrollPane.getVerticalScrollBar().setValue(outputScrollPane.getVerticalScrollBar().getMaximum()); } } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); From 6bb126c186cb48ccd46aa8915a7b5700f49d9c3b Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:19:17 +0100 Subject: [PATCH 16/66] Close server at the end --- src/main/java/ChatClient.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 2c043b5..ba31d5e 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -60,6 +60,12 @@ public class ChatClient extends JFrame implements KeyListener { } } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); + } finally { + try { + connectionToServer.close(); + } catch (IOException e) { + e.printStackTrace(); + } } } From 848770d030275eedf1b4b28a9bbe40a14353646e Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:22:35 +0100 Subject: [PATCH 17/66] Close reading messages from server --- src/main/java/ChatClient.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index ba31d5e..a42c3ca 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -66,6 +66,13 @@ public class ChatClient extends JFrame implements KeyListener { } catch (IOException e) { e.printStackTrace(); } + + try { + fromServerReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } From 57b1af041325ed068d8e30ead4bc36559096d3bf Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:24:49 +0100 Subject: [PATCH 18/66] Close writing messages to server --- src/main/java/ChatClient.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index a42c3ca..93731f3 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -73,6 +73,8 @@ public class ChatClient extends JFrame implements KeyListener { e.printStackTrace(); } + toServerWriter.close(); + } } From efbf5ad6c8a8775fbe704695a13b378e97ced5d7 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:27:57 +0100 Subject: [PATCH 19/66] Check connection to server before closing --- src/main/java/ChatClient.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 93731f3..851ef84 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -61,10 +61,12 @@ public class ChatClient extends JFrame implements KeyListener { } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); } finally { - try { - connectionToServer.close(); - } catch (IOException e) { - e.printStackTrace(); + if (null != connectionToServer) { + try { + connectionToServer.close(); + } catch (IOException e) { + e.printStackTrace(); + } } try { From 64626610cab8ac85ea64a28c1abb13913e15771c Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:30:27 +0100 Subject: [PATCH 20/66] Avoid closing fromServerReader in case of null --- src/main/java/ChatClient.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 851ef84..f14ddd0 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -69,10 +69,12 @@ public class ChatClient extends JFrame implements KeyListener { } } - try { - fromServerReader.close(); - } catch (IOException e) { - e.printStackTrace(); + if (null != fromServerReader) { + try { + fromServerReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } } toServerWriter.close(); From 7022585850e6d087085dc9ba6669ba69bf1243da Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:31:54 +0100 Subject: [PATCH 21/66] Check for null value before closing toServerWriter --- src/main/java/ChatClient.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index f14ddd0..eb8fd84 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -77,8 +77,10 @@ public class ChatClient extends JFrame implements KeyListener { } } - toServerWriter.close(); - + if(null != toServerWriter) { + toServerWriter.close(); + } + } } From 50b3994180dcb9b96eae34d2f94914e086ba3eb4 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:34:28 +0100 Subject: [PATCH 22/66] Close messaging window if the connection is broken --- src/main/java/ChatClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index eb8fd84..f39bc2f 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -60,6 +60,7 @@ public class ChatClient extends JFrame implements KeyListener { } } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); + dispose(); } finally { if (null != connectionToServer) { try { @@ -80,7 +81,6 @@ public class ChatClient extends JFrame implements KeyListener { if(null != toServerWriter) { toServerWriter.close(); } - } } From 9adf4d68c15eaa4b432978b7c959189e1a49655f Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 20:00:53 +0100 Subject: [PATCH 23/66] Refactor: make window height and width constant --- src/main/java/ChatClient.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index f39bc2f..2cfdfd0 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -6,6 +6,8 @@ import java.io.*; import java.net.Socket; public class ChatClient extends JFrame implements KeyListener { + private static final int WINDOW_WIDTH = 800; + private static final int WINDOW_HEIGHT = 600; private String address; private int port; private Socket connectionToServer; @@ -42,7 +44,7 @@ public class ChatClient extends JFrame implements KeyListener { add(inputTextField, BorderLayout.SOUTH); setVisible(true); - setSize(800, 600); + setSize(WINDOW_WIDTH, WINDOW_HEIGHT); setDefaultCloseOperation(EXIT_ON_CLOSE); } private void receiveMessages() { From 48a78f41dd28eb87e30200bfd9eeb18b8cb2a805 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 20:03:26 +0100 Subject: [PATCH 24/66] Refactor: open window in the center --- src/main/java/ChatClient.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 2cfdfd0..fbc9b4e 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -46,6 +46,7 @@ public class ChatClient extends JFrame implements KeyListener { setVisible(true); setSize(WINDOW_WIDTH, WINDOW_HEIGHT); setDefaultCloseOperation(EXIT_ON_CLOSE); + setLocationRelativeTo(null); } private void receiveMessages() { try { From d1c2f9e7a4256be928c5f52ce26c942f17e84b43 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 20:23:02 +0100 Subject: [PATCH 25/66] Refactor: move window dimensions to a new Constants class --- src/main/java/ChatClient.java | 4 +--- src/main/java/Constants.java | 4 ++++ 2 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 src/main/java/Constants.java diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index fbc9b4e..0e2cf51 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -6,8 +6,6 @@ import java.io.*; import java.net.Socket; public class ChatClient extends JFrame implements KeyListener { - private static final int WINDOW_WIDTH = 800; - private static final int WINDOW_HEIGHT = 600; private String address; private int port; private Socket connectionToServer; @@ -44,7 +42,7 @@ public class ChatClient extends JFrame implements KeyListener { add(inputTextField, BorderLayout.SOUTH); setVisible(true); - setSize(WINDOW_WIDTH, WINDOW_HEIGHT); + setSize(Constants.WINDOW_WIDTH, Constants.WINDOW_HEIGHT); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); } diff --git a/src/main/java/Constants.java b/src/main/java/Constants.java new file mode 100644 index 0000000..15a8e91 --- /dev/null +++ b/src/main/java/Constants.java @@ -0,0 +1,4 @@ +public class Constants { + public static final int WINDOW_WIDTH = 800; + public static final int WINDOW_HEIGHT = 600; +} From d90dfd48ad6b34935acb83c1a277e735a027ebe4 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 20:26:13 +0100 Subject: [PATCH 26/66] Refactor: make PORT a global constant --- src/main/java/ChatClient.java | 8 +++----- src/main/java/Constants.java | 1 + 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 0e2cf51..afa6503 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -7,7 +7,6 @@ import java.net.Socket; public class ChatClient extends JFrame implements KeyListener { private String address; - private int port; private Socket connectionToServer; private BufferedReader fromServerReader; private PrintWriter toServerWriter; @@ -17,9 +16,8 @@ public class ChatClient extends JFrame implements KeyListener { private JTextField inputTextField; private JScrollPane outputScrollPane; - public ChatClient(int port) { + public ChatClient() { super("Chat"); - this.port = port; address = JOptionPane.showInputDialog("bitte IP-Adresse"); if (address != null) { @@ -48,7 +46,7 @@ public class ChatClient extends JFrame implements KeyListener { } private void receiveMessages() { try { - connectionToServer = new Socket(address, port); + connectionToServer = new Socket(address, Constants.PORT); fromServerReader = new BufferedReader(new InputStreamReader(connectionToServer.getInputStream())); toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); @@ -108,6 +106,6 @@ public class ChatClient extends JFrame implements KeyListener { } public static void main(String[] args) { - new ChatClient(3141); + new ChatClient(); } } diff --git a/src/main/java/Constants.java b/src/main/java/Constants.java index 15a8e91..5848138 100644 --- a/src/main/java/Constants.java +++ b/src/main/java/Constants.java @@ -1,4 +1,5 @@ public class Constants { public static final int WINDOW_WIDTH = 800; public static final int WINDOW_HEIGHT = 600; + public static final int PORT = 3141; } From 58c5ad97fc37884ec1c498921bf12f533e676afd Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 20:33:03 +0100 Subject: [PATCH 27/66] Refactor: use yoda notation to get error in case of mistyping --- src/main/java/ChatClient.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index afa6503..cd73618 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -20,7 +20,7 @@ public class ChatClient extends JFrame implements KeyListener { super("Chat"); address = JOptionPane.showInputDialog("bitte IP-Adresse"); - if (address != null) { + if (null != address) { receiveMessages(); } } @@ -47,8 +47,8 @@ public class ChatClient extends JFrame implements KeyListener { private void receiveMessages() { try { connectionToServer = new Socket(address, Constants.PORT); - fromServerReader = new BufferedReader(new InputStreamReader(connectionToServer.getInputStream())); - toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); + fromServerReader = new BufferedReader(new InputStreamReader(connectionToServer.getInputStream())); + toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); initGui(); From 118683356fd060847284a617f94fafae0baa1d46 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 21:05:37 +0100 Subject: [PATCH 28/66] Refactor: Create variable for connection failed message --- src/main/java/ChatClient.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index cd73618..30b486c 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -7,6 +7,7 @@ import java.net.Socket; public class ChatClient extends JFrame implements KeyListener { private String address; + private String connectionFailedMessage; private Socket connectionToServer; private BufferedReader fromServerReader; private PrintWriter toServerWriter; @@ -19,8 +20,8 @@ public class ChatClient extends JFrame implements KeyListener { public ChatClient() { super("Chat"); address = JOptionPane.showInputDialog("bitte IP-Adresse"); - if (null != address) { + connectionFailedMessage = "Verbindung zum Server " + (address.isEmpty() ? "" : ("\"" + address + "\"")) + " fehlgeschlagen."; receiveMessages(); } } @@ -58,7 +59,7 @@ public class ChatClient extends JFrame implements KeyListener { outputScrollPane.getVerticalScrollBar().setValue(outputScrollPane.getVerticalScrollBar().getMaximum()); } } catch (IOException e) { - JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); + JOptionPane.showMessageDialog(null, connectionFailedMessage); dispose(); } finally { if (null != connectionToServer) { From afb3a19d98b3e60aeab164b9dfa3a4627a9a6c68 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Thu, 8 Feb 2024 00:55:46 +0100 Subject: [PATCH 29/66] Client-Counter --- .classpath | 81 ++++++++++++++++++----------------- src/main/java/ChatServer.java | 3 ++ 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/.classpath b/.classpath index 8d95b91..acc4f10 100644 --- a/.classpath +++ b/.classpath @@ -1,40 +1,41 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 0c60774..e9cfb35 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -56,5 +56,8 @@ public class ChatServer { public void removeClient(ClientHandler client) { clients.remove(client); } + public List getClients() { + return clients; + } } \ No newline at end of file From 5f094aef5657e53ff0505f0c969598f96aebcef6 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Thu, 8 Feb 2024 01:53:32 +0100 Subject: [PATCH 30/66] Username Implementation for ClientHandler --- src/main/java/ClientHandler.java | 6 ++++-- user.json | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index fa4b924..d443891 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -1,6 +1,8 @@ import java.io.*; import java.net.Socket; +import javax.swing.JOptionPane; + public class ClientHandler implements Runnable { private ChatServer chatServer; private Socket connectionToClient; @@ -12,7 +14,7 @@ public class ClientHandler implements Runnable { public ClientHandler(ChatServer chatServer, Socket connectionToClient) { this.chatServer = chatServer; this.connectionToClient = connectionToClient; - name = connectionToClient.getInetAddress().getHostAddress(); // Use the client's IP address as their name for simplicity + name = JOptionPane.showInputDialog("welcher Benutzername?"); new Thread(this).start();} // Start a new thread for this client handler @@ -62,4 +64,4 @@ public class ClientHandler implements Runnable { toClientWriter.println(message); // Send the message to the client toClientWriter.flush(); // Flush the stream } -} +} diff --git a/user.json b/user.json index 0201a67..5cbff9f 100644 --- a/user.json +++ b/user.json @@ -10,5 +10,11 @@ "userName": "asd1", "password": "123456", "birthday": "1" + }, + { + "id": "228bf4df-c730-4cd9-9d1c-2446607066aa", + "userName": "jo", + "password": "sieben", + "birthday": "jo" } ] \ No newline at end of file From 95deb5c64073c9138ca9e13a664edf336af02158 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 10:33:44 +0100 Subject: [PATCH 31/66] Added a method that changes the stayLoggedIn variable in the JSON file --- src/main/java/CreateUser.java | 24 +++++++++++++++++++++++- user.json | 14 ++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 6bccbd9..c799654 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -110,12 +110,32 @@ class CreateUser { } return userList; } + + // Function to update stayLoggedIn variable in the JSON file + public static void updateStayLoggedIn(String filename, String username, boolean stayLoggedIn) { + List userList = readUserListFromJsonFile(filename); + + for (CreateUser user : userList) { + if (user.getUserName().equals(username)) { + user.setStayLoggedIn(stayLoggedIn); + break; + } + } + + try (FileWriter fileWriter = new FileWriter(filename)) { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + gson.toJson(userList, fileWriter); + System.out.println("StayLoggedIn updated in " + filename); + } catch (IOException e) { + System.out.println("Error occurred while updating StayLoggedIn in JSON file: " + e.getMessage()); + } + } public static void main(String[] args) { try { // Example usage UUID randomUUID = UUID.randomUUID(); - CreateUser user = createUser(randomUUID.toString(), "Another Test User", "TestPasswort123", "01.01.2000"); + CreateUser user = createUser(randomUUID.toString(), "Test User", "TestPasswort123", "01.01.2000"); // Example of accessing properties System.out.println("UserID: " + user.getId()); @@ -126,6 +146,8 @@ class CreateUser { // Save user information to a JSON file user.saveToJsonFile("user.json"); + + updateStayLoggedIn("user.json", "Test User", true); } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); } diff --git a/user.json b/user.json index 1f77562..893012b 100644 --- a/user.json +++ b/user.json @@ -12,5 +12,19 @@ "password": "123456", "birthday": "01.01.2000", "stayLoggedIn": false + }, + { + "id": "685bc3a6-e706-4214-a5e1-8443d1a5258e", + "userName": "Test User", + "password": "TestPasswort123", + "birthday": "01.01.2000", + "stayLoggedIn": false + }, + { + "id": "503f2d6a-389c-4675-8044-3ec9ca73f1b5", + "userName": "Test User", + "password": "TestPasswort123", + "birthday": "01.01.2000", + "stayLoggedIn": false } ] \ No newline at end of file From fe008a9ebede08ec0f8e7261a06a5aa99fedc84b Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 10:39:04 +0100 Subject: [PATCH 32/66] Added a method to LoginGUI that calls the updateStayLoggdIn method in CreateUser if the stay logged in checkbox is ticked --- src/main/java/LoginGUI.java | 13 +++++++++++-- user.json | 11 ++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index b273014..675ffa5 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -46,6 +46,15 @@ public class LoginGUI extends JFrame implements ActionListener { passwordField.addKeyListener(new EnterKeyListener()); + stayLoggedInCheckbox.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); + String username = usernameField.getText(); + updateStayLoggedIn(username, stayLoggedIn); + } + }); + } @Override @@ -71,8 +80,8 @@ public class LoginGUI extends JFrame implements ActionListener { } private void updateStayLoggedIn(String username, boolean stayLoggedIn) { - // Update stayLoggedIn in the JSON file for the user - + // Update stayLoggedIn in the JSON file for the user + CreateUser.updateStayLoggedIn("user.json", username, stayLoggedIn); } // Function to authenticate the user by comparing the entered username and password with the saved user data diff --git a/user.json b/user.json index 893012b..5437c96 100644 --- a/user.json +++ b/user.json @@ -16,15 +16,8 @@ { "id": "685bc3a6-e706-4214-a5e1-8443d1a5258e", "userName": "Test User", - "password": "TestPasswort123", - "birthday": "01.01.2000", - "stayLoggedIn": false - }, - { - "id": "503f2d6a-389c-4675-8044-3ec9ca73f1b5", - "userName": "Test User", - "password": "TestPasswort123", + "password": "Test", "birthday": "01.01.2000", - "stayLoggedIn": false + "stayLoggedIn": true } ] \ No newline at end of file From 757f615d207cc0be88a95abaf5841caf3c1d104a Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 10:54:40 +0100 Subject: [PATCH 33/66] Added an if statement in the action listener for the checkbox in LoginGUI so the stayLoggedIn variable gets also changed when the checkbox is unticked --- src/main/java/LoginGUI.java | 4 ++++ user.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 675ffa5..6672019 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -51,6 +51,10 @@ public class LoginGUI extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); String username = usernameField.getText(); + // Set stayLoggedIn to false if the checkbox is unchecked + if (!stayLoggedInCheckbox.isSelected()) { + stayLoggedIn = false; + } updateStayLoggedIn(username, stayLoggedIn); } }); diff --git a/user.json b/user.json index 5437c96..2dcd5ea 100644 --- a/user.json +++ b/user.json @@ -18,6 +18,6 @@ "userName": "Test User", "password": "Test", "birthday": "01.01.2000", - "stayLoggedIn": true + "stayLoggedIn": false } ] \ No newline at end of file From 1437f9f5ce043ad7e5ecc6da399bcf1710e46ac2 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 11:46:04 +0100 Subject: [PATCH 34/66] Added a signup button --- src/main/java/LoginGUI.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 6672019..08b26b5 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -10,10 +10,11 @@ public class LoginGUI extends JFrame implements ActionListener { private JPasswordField passwordField; private JButton loginButton; private JCheckBox stayLoggedInCheckbox; + private JButton signUpButton; public LoginGUI() { setTitle("Login"); - setSize(300, 180); + setSize(300, 220); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); @@ -42,6 +43,11 @@ public class LoginGUI extends JFrame implements ActionListener { loginButton.addActionListener(this); add(loginButton); + signUpButton = new JButton("Sign Up"); + signUpButton.setBounds(100, 140, 100, 25); // Adjusted position + signUpButton.addActionListener(this); + add(signUpButton); + getRootPane().setDefaultButton(loginButton); passwordField.addKeyListener(new EnterKeyListener()); From 79c72ab0fe1726df4b4f4d9d82f0e2466e5a56aa Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 11:49:14 +0100 Subject: [PATCH 35/66] Added functionality to the signup button --- src/main/java/LoginGUI.java | 4 ++++ user.json | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 08b26b5..f8dad01 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -71,6 +71,10 @@ public class LoginGUI extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == loginButton) { login(); + }else if (e.getSource() == signUpButton) { + SignUpGUI signUpGUI = new SignUpGUI(); + signUpGUI.setVisible(true); + } } diff --git a/user.json b/user.json index 2dcd5ea..2bef498 100644 --- a/user.json +++ b/user.json @@ -19,5 +19,12 @@ "password": "Test", "birthday": "01.01.2000", "stayLoggedIn": false + }, + { + "id": "57b7fb2e-50c1-4027-8871-58cbfc8405c8", + "userName": "New", + "password": "123456", + "birthday": "", + "stayLoggedIn": false } ] \ No newline at end of file From 522f04d359cf53dbe38836e738f64358cb3045cc Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 13:53:40 +0100 Subject: [PATCH 36/66] Added a firstname and surname field to SignUpGUI --- src/main/java/CreateUser.java | 12 ++++++++++++ src/main/java/SignUpGUI.java | 35 +++++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index c799654..59c55b1 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -17,6 +17,8 @@ class CreateUser { private String userName; private String password; private String birthday; + private String firstName; + private String surName; private boolean stayLoggedIn; @@ -26,6 +28,8 @@ class CreateUser { this.userName = name; this.password = password; this.birthday = birthday; + this.firstName = firstName; + this.surName = surName; } // Getters and Setters @@ -61,6 +65,14 @@ class CreateUser { this.birthday = birthday; } + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + public boolean isStayLoggedIn() { return stayLoggedIn; } diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index b19cf8e..0266fe4 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -5,12 +5,12 @@ import java.util.List; import java.util.UUID; public class SignUpGUI extends JFrame implements ActionListener { - private JTextField usernameField, passwordField, confirmPasswordField, birthdayField; + private JTextField usernameField, passwordField, confirmPasswordField, birthdayField, firstNameField, surnameField; private JButton signUpButton; - + public SignUpGUI() { setTitle("Sign Up"); - setSize(300, 250); + setSize(400, 300); // Adjusted size for accommodating more fields setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); @@ -19,7 +19,7 @@ public class SignUpGUI extends JFrame implements ActionListener { add(usernameLabel); usernameField = new JTextField(); - usernameField.setBounds(100, 20, 160, 25); + usernameField.setBounds(140, 20, 160, 25); add(usernameField); JLabel passwordLabel = new JLabel("Password:"); @@ -27,7 +27,7 @@ public class SignUpGUI extends JFrame implements ActionListener { add(passwordLabel); passwordField = new JPasswordField(); - passwordField.setBounds(100, 50, 160, 25); + passwordField.setBounds(140, 50, 160, 25); add(passwordField); JLabel confirmPasswordLabel = new JLabel("Confirm Password:"); @@ -35,22 +35,37 @@ public class SignUpGUI extends JFrame implements ActionListener { add(confirmPasswordLabel); confirmPasswordField = new JPasswordField(); - confirmPasswordField.setBounds(140, 80, 120, 25); + confirmPasswordField.setBounds(140, 80, 160, 25); add(confirmPasswordField); + JLabel firstNameLabel = new JLabel("First Name:"); // New field for first name + firstNameLabel.setBounds(20, 110, 80, 25); + add(firstNameLabel); + + firstNameField = new JTextField(); + firstNameField.setBounds(140, 110, 160, 25); + add(firstNameField); + + JLabel surnameLabel = new JLabel("Surname:"); // New field for surname + surnameLabel.setBounds(20, 140, 80, 25); + add(surnameLabel); + + surnameField = new JTextField(); + surnameField.setBounds(140, 140, 160, 25); + add(surnameField); + JLabel birthdayLabel = new JLabel("Birthday:"); - birthdayLabel.setBounds(20, 110, 80, 25); + birthdayLabel.setBounds(20, 170, 80, 25); add(birthdayLabel); birthdayField = new JTextField(); - birthdayField.setBounds(100, 110, 160, 25); + birthdayField.setBounds(140, 170, 160, 25); add(birthdayField); signUpButton = new JButton("Sign Up"); - signUpButton.setBounds(100, 150, 100, 25); + signUpButton.setBounds(140, 210, 100, 25); // Adjusted position signUpButton.addActionListener(this); add(signUpButton); - } @Override From b28a97e072f9e28f16d994d5911532761d504cfc Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Thu, 8 Feb 2024 14:01:42 +0100 Subject: [PATCH 37/66] removeClient --- src/main/java/ClientHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index fa4b924..74fa305 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -39,7 +39,7 @@ public class ClientHandler implements Runnable { throw new RuntimeException(e); // Handle exceptions by throwing a runtime exception } finally { - //chatServer.removeClient(this); + chatServer.removeClient(this); chatServer.broadcastMessage(name + " disconnected."); // Broadcast a message when the client is disconnected // Close resources in the finally block From b762e825e9f19b38b4a8daca5f58c3321f706426 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Thu, 8 Feb 2024 14:50:42 +0100 Subject: [PATCH 38/66] Benutzername abfrage modifiziert --- src/main/java/ChatServer.java | 8 ++++---- src/main/java/ClientHandler.java | 2 +- user.json | 6 ------ 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index e9cfb35..f43051b 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -39,10 +39,10 @@ public class ChatServer { } // Methode, um eine Nachricht an alle verbundenen Clients zu senden public void broadcastMessage(String message) { - System.out.println(message); - if (message != null) { - for (ClientHandler client : clients) { - client.sendMessage(message); // Nachricht an jeden Client senden + System.out.println(message); + if (message != null) { + for (ClientHandler client : clients) { + client.sendMessage(message); } } diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index d443891..edba43c 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -14,7 +14,7 @@ public class ClientHandler implements Runnable { public ClientHandler(ChatServer chatServer, Socket connectionToClient) { this.chatServer = chatServer; this.connectionToClient = connectionToClient; - name = JOptionPane.showInputDialog("welcher Benutzername?"); + name = JOptionPane.showInputDialog("Benutzername für neuen Client vergeben"); new Thread(this).start();} // Start a new thread for this client handler diff --git a/user.json b/user.json index 5cbff9f..0201a67 100644 --- a/user.json +++ b/user.json @@ -10,11 +10,5 @@ "userName": "asd1", "password": "123456", "birthday": "1" - }, - { - "id": "228bf4df-c730-4cd9-9d1c-2446607066aa", - "userName": "jo", - "password": "sieben", - "birthday": "jo" } ] \ No newline at end of file From 9ba4e0c28cf8977e53af73633b38f222a79d5921 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Thu, 8 Feb 2024 15:38:05 +0100 Subject: [PATCH 39/66] pom.xml --- pom.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 26be012..21c5d05 100644 --- a/pom.xml +++ b/pom.xml @@ -13,11 +13,11 @@ - org.mockito - mockito-core - 4.1.10 - test - + org.mockito + mockito-core + 4.1.10 + test + From 85707e32c590567b0afe53e2d4cf9114f96a7146 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 15:40:19 +0100 Subject: [PATCH 40/66] Added hashing algorithm to the user signup and login --- src/main/java/CreateUser.java | 43 +++++++++++++++++++++++++++++------ src/main/java/LoginGUI.java | 32 +++++++++++++++++++++++--- src/main/java/SignUpGUI.java | 4 +++- user.json | 32 +++++++++----------------- 4 files changed, 79 insertions(+), 32 deletions(-) diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 59c55b1..c078749 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -10,6 +10,9 @@ import java.util.List; import java.io.FileWriter; import java.io.IOException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + import java.util.UUID; class CreateUser { @@ -18,18 +21,18 @@ class CreateUser { private String password; private String birthday; private String firstName; - private String surName; + private String surname; private boolean stayLoggedIn; // Constructor - public CreateUser(String id, String name, String password, String birthday) { + public CreateUser(String id, String name, String password, String birthday, String firstName, String surname) { this.id = id; this.userName = name; - this.password = password; + this.password = hashPassword(password); this.birthday = birthday; this.firstName = firstName; - this.surName = surName; + this.surname = surname; } // Getters and Setters @@ -72,6 +75,14 @@ class CreateUser { public void setFirstName(String firstName) { this.firstName = firstName; } + + public String surname() { + return firstName; + } + + public void surname(String firstName) { + this.firstName = firstName; + } public boolean isStayLoggedIn() { return stayLoggedIn; @@ -82,7 +93,7 @@ class CreateUser { } // Function to create user with validation - public static CreateUser createUser(String id, String userName, String password, String birthday) { + public static CreateUser createUser(String id, String userName, String password, String birthday, String firstName, String surname) { if (userName == null || userName.isEmpty()) { throw new IllegalArgumentException("Username cannot be empty"); } @@ -91,7 +102,25 @@ class CreateUser { } if (password.length() < 6) { throw new IllegalArgumentException("Password must be at least 6 characters long"); - } return new CreateUser(id, userName, password, birthday); + } return new CreateUser(id, userName, password, birthday, firstName, surname); + } + + // Function to hash the password using SHA-256 algorithm + private String hashPassword(String password) { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + byte[] hash = digest.digest(password.getBytes()); + StringBuilder hexString = new StringBuilder(); + for (byte b : hash) { + String hex = Integer.toHexString(0xff & b); + if (hex.length() == 1) hexString.append('0'); + hexString.append(hex); + } + return hexString.toString(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + return null; + } } // Function to save to JSON file, replace with database call later @@ -147,7 +176,7 @@ class CreateUser { try { // Example usage UUID randomUUID = UUID.randomUUID(); - CreateUser user = createUser(randomUUID.toString(), "Test User", "TestPasswort123", "01.01.2000"); + CreateUser user = createUser(randomUUID.toString(), "Hash Test", "123456", "01.01.2000", "Hans", "Wurst"); // Example of accessing properties System.out.println("UserID: " + user.getId()); diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index f8dad01..6a442b7 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -5,6 +5,9 @@ import java.util.List; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + public class LoginGUI extends JFrame implements ActionListener { private JTextField usernameField; private JPasswordField passwordField; @@ -102,15 +105,38 @@ public class LoginGUI extends JFrame implements ActionListener { private boolean authenticateUser(String username, String password) { List userList = CreateUser.readUserListFromJsonFile("user.json"); if (userList != null) { - for (CreateUser user : userList) { - if (user.getUserName().equals(username) && user.getPassword().equals(password)) { - return true; //Success + for (CreateUser user : userList) { + if (user.getUserName().equals(username)) { + // Hash the user input password + String hashedPassword = hashPassword(password); + // Compare the hashed passwords + if (user.getPassword().equals(hashedPassword)) { + return true; // Success } } } + } return false; // Fail } + private String hashPassword(String password) { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + byte[] hash = digest.digest(password.getBytes()); + StringBuilder hexString = new StringBuilder(); + for (byte b : hash) { + String hex = Integer.toHexString(0xff & b); + if (hex.length() == 1) { + hexString.append('0'); + } + hexString.append(hex); + } + return hexString.toString(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + return null; + } + } private class EnterKeyListener implements KeyListener { @Override diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index 0266fe4..2f31890 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -75,6 +75,8 @@ public class SignUpGUI extends JFrame implements ActionListener { String password = passwordField.getText(); String confirmPassword = confirmPasswordField.getText(); String birthday = birthdayField.getText(); + String firstName = firstNameField.getText(); + String surname = surnameField.getText(); if (!password.equals(confirmPassword)) { JOptionPane.showMessageDialog(this, "Passwords do not match!", "Sign Up Error", JOptionPane.ERROR_MESSAGE); @@ -87,7 +89,7 @@ public class SignUpGUI extends JFrame implements ActionListener { } try { UUID randomUUID = UUID.randomUUID(); - CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday); + CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday, firstName, surname); user.saveToJsonFile("user.json"); JOptionPane.showMessageDialog(this, "User signed up successfully!"); dispose(); diff --git a/user.json b/user.json index 2bef498..29df4e8 100644 --- a/user.json +++ b/user.json @@ -1,30 +1,20 @@ [ { - "id": "a2864d79-1079-4cbb-8d77-f5f84995580d", - "userName": "Another Test User", - "password": "TestPasswort123", - "birthday": "01.01.2000", - "stayLoggedIn": false - }, - { - "id": "3690702d-9c7e-48fb-8a01-ef89b3b76268", - "userName": "TestUser2", - "password": "123456", - "birthday": "01.01.2000", - "stayLoggedIn": false - }, - { - "id": "685bc3a6-e706-4214-a5e1-8443d1a5258e", - "userName": "Test User", - "password": "Test", - "birthday": "01.01.2000", + "id": "d7ae19fe-4684-4d69-a73d-4cca612962a3", + "userName": "Test", + "password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92", + "birthday": "", + "firstName": "", + "surname": "", "stayLoggedIn": false }, { - "id": "57b7fb2e-50c1-4027-8871-58cbfc8405c8", - "userName": "New", - "password": "123456", + "id": "2ec2c0c5-677c-4262-8958-fef98d11cc63", + "userName": "Test2", + "password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92", "birthday": "", + "firstName": "", + "surname": "", "stayLoggedIn": false } ] \ No newline at end of file From f416a634211a633ea24a416425c1e23d0c8ad3a0 Mon Sep 17 00:00:00 2001 From: fdai5595 Date: Thu, 8 Feb 2024 15:32:42 +0000 Subject: [PATCH 41/66] Update pom.xml --- pom.xml | 53 +++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index 21c5d05..4b4d00b 100644 --- a/pom.xml +++ b/pom.xml @@ -1,24 +1,29 @@ - - 4.0.0 - org.progmethoden - java-chat - 0.0.1-SNAPSHOT - - - - - com.google.code.gson - gson - 2.10.1 - - - - org.mockito - mockito-core - 4.1.10 - test - - - - - \ No newline at end of file + + 4.0.0 + org.progmethoden + java-chat + 0.0.1-SNAPSHOT + + + + + com.google.code.gson + gson + 2.10.1 + + + junit + junit + 4.13.2 + test + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + + + From 5cf6e48004d7af71786ccae3c8225167067f11c4 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Thu, 8 Feb 2024 16:38:15 +0100 Subject: [PATCH 42/66] test_ Konstruktortest --- pom.xml | 57 ++++++++++++++++++------------- src/test/java/ChatServerTest.java | 26 ++++++++++++++ 2 files changed, 59 insertions(+), 24 deletions(-) create mode 100644 src/test/java/ChatServerTest.java diff --git a/pom.xml b/pom.xml index 21c5d05..d427c1a 100644 --- a/pom.xml +++ b/pom.xml @@ -1,24 +1,33 @@ - - 4.0.0 - org.progmethoden - java-chat - 0.0.1-SNAPSHOT - - - - - com.google.code.gson - gson - 2.10.1 - - - - org.mockito - mockito-core - 4.1.10 - test - - - - - \ No newline at end of file + + + RELEASE + + + 4.0.0 + org.progmethoden + java-chat + 0.0.1-SNAPSHOT + + + + + com.google.code.gson + gson + 2.10.1 + + + junit + junit + 4.13.2 + test + + + org.junit.jupiter + junit-jupiter + ${junit-jupiter-version} + test + + + + + diff --git a/src/test/java/ChatServerTest.java b/src/test/java/ChatServerTest.java new file mode 100644 index 0000000..80b714d --- /dev/null +++ b/src/test/java/ChatServerTest.java @@ -0,0 +1,26 @@ +import static org.junit.jupiter.api.Assertions.*; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +class ChatServerTest { + + @Test + public void testConstructor() { + // Arrange + ChatServer chatServer = null; + + // Act + try { + chatServer = new ChatServer(1234); + } catch (Exception e) { + fail("Exception occurred: " + e.getMessage()); + } + + // Assert + assertNotNull(chatServer); + } +} + + From 962bc1688aa40702fe1ac590060b54923243b402 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Thu, 8 Feb 2024 22:57:55 +0100 Subject: [PATCH 43/66] test_lastMessage Test --- src/main/java/ClientHandler.java | 5 +++++ src/test/java/ChatServerTest.java | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index e4104da..45570aa 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -9,6 +9,7 @@ public class ClientHandler implements Runnable { private String name; private BufferedReader fromClientReader; private PrintWriter toClientWriter; + private String lastMessage; // Constructor for ClientHandler public ClientHandler(ChatServer chatServer, Socket connectionToClient) { @@ -33,6 +34,7 @@ public class ClientHandler implements Runnable { while (message!=null) { // Broadcast the client's message to all connected clients chatServer.broadcastMessage(name + ": " + message); + lastMessage = message; message = fromClientReader.readLine(); } } @@ -64,4 +66,7 @@ public class ClientHandler implements Runnable { toClientWriter.println(message); // Send the message to the client toClientWriter.flush(); // Flush the stream } + public String getLastMessage() { + return lastMessage; + } } diff --git a/src/test/java/ChatServerTest.java b/src/test/java/ChatServerTest.java index 80b714d..4b7eb79 100644 --- a/src/test/java/ChatServerTest.java +++ b/src/test/java/ChatServerTest.java @@ -21,6 +21,24 @@ class ChatServerTest { // Assert assertNotNull(chatServer); } + @Test + public void testBroadcastMessage() { + // Arrange + ChatServer chatServer = new ChatServer(1234); + ClientHandler client1 = new ClientHandler(chatServer, null); + ClientHandler client2 = new ClientHandler(chatServer, null); + chatServer.getClients().add(client1); + chatServer.getClients().add(client2); + + // Act + chatServer.broadcastMessage("Test message"); + + // Assert + List clients = chatServer.getClients(); + for (ClientHandler client : clients) { + assertEquals("Test message", client.getLastMessage()); + } + } } From 62d3eb5578a363ae0a8c640bbed3c84bf674fbf2 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 01:37:30 +0100 Subject: [PATCH 44/66] neue ChatGUI(noch nicht eingebunden --- src/main/java/ChatGUI.java | 52 +++++++++++++++++++++++++++++++ src/main/java/ClientHandler.java | 1 + src/test/java/ChatServerTest.java | 44 -------------------------- 3 files changed, 53 insertions(+), 44 deletions(-) create mode 100644 src/main/java/ChatGUI.java delete mode 100644 src/test/java/ChatServerTest.java diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java new file mode 100644 index 0000000..852f623 --- /dev/null +++ b/src/main/java/ChatGUI.java @@ -0,0 +1,52 @@ +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.Toolkit; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JFrame; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; +import javax.swing.JTextField; + +public class ChatGUI implements ActionListener { + + JTextField inputTextField = new JTextField(); + JTextArea outputTextArea = new JTextArea(); + + JFrame gui; + + public ChatGUI() { + gui = new JFrame(); + + gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + gui.setTitle("java-chat"); + + gui.setLayout(new BorderLayout()); + + + JScrollPane outputScrollPane = new JScrollPane(outputTextArea); + gui.add(outputScrollPane, BorderLayout.CENTER); + + inputTextField.setPreferredSize(new Dimension(0, 30)); + gui.add(inputTextField, BorderLayout.SOUTH); + + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + int quarterWidth = screenSize.width / 2; + int quarterHeight = screenSize.height / 2; + gui.setSize(quarterWidth, quarterHeight); + gui.setVisible(true); + } + + public static void main(String[] args) { + + new ChatGUI(); + } + @Override + public void actionPerformed(ActionEvent e) { + // TODO Auto-generated method stub + + } + + +} diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 45570aa..b30e37a 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -66,6 +66,7 @@ public class ClientHandler implements Runnable { toClientWriter.println(message); // Send the message to the client toClientWriter.flush(); // Flush the stream } + //Test method public String getLastMessage() { return lastMessage; } diff --git a/src/test/java/ChatServerTest.java b/src/test/java/ChatServerTest.java deleted file mode 100644 index 4b7eb79..0000000 --- a/src/test/java/ChatServerTest.java +++ /dev/null @@ -1,44 +0,0 @@ -import static org.junit.jupiter.api.Assertions.*; - -import java.util.List; - -import org.junit.jupiter.api.Test; - -class ChatServerTest { - - @Test - public void testConstructor() { - // Arrange - ChatServer chatServer = null; - - // Act - try { - chatServer = new ChatServer(1234); - } catch (Exception e) { - fail("Exception occurred: " + e.getMessage()); - } - - // Assert - assertNotNull(chatServer); - } - @Test - public void testBroadcastMessage() { - // Arrange - ChatServer chatServer = new ChatServer(1234); - ClientHandler client1 = new ClientHandler(chatServer, null); - ClientHandler client2 = new ClientHandler(chatServer, null); - chatServer.getClients().add(client1); - chatServer.getClients().add(client2); - - // Act - chatServer.broadcastMessage("Test message"); - - // Assert - List clients = chatServer.getClients(); - for (ClientHandler client : clients) { - assertEquals("Test message", client.getLastMessage()); - } - } -} - - From 2d6074f6ae5177b3254fe20716d589170637313e Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 01:43:21 +0100 Subject: [PATCH 45/66] =?UTF-8?q?refactoring:=20Einr=C3=BCcken=20und=20Kom?= =?UTF-8?q?mentare?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatGUI.java | 59 ++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index 852f623..109f0f1 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -11,42 +11,45 @@ import javax.swing.JTextField; public class ChatGUI implements ActionListener { - JTextField inputTextField = new JTextField(); - JTextArea outputTextArea = new JTextArea(); - - JFrame gui; - - public ChatGUI() { - gui = new JFrame(); - - gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - gui.setTitle("java-chat"); - - gui.setLayout(new BorderLayout()); - - - JScrollPane outputScrollPane = new JScrollPane(outputTextArea); - gui.add(outputScrollPane, BorderLayout.CENTER); - - inputTextField.setPreferredSize(new Dimension(0, 30)); - gui.add(inputTextField, BorderLayout.SOUTH); - - Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); - int quarterWidth = screenSize.width / 2; - int quarterHeight = screenSize.height / 2; - gui.setSize(quarterWidth, quarterHeight); + JTextField inputTextField = new JTextField(); + JTextArea outputTextArea = new JTextArea(); + + JFrame gui; + + public ChatGUI() { + gui = new JFrame(); + + // Set up the main frame + gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + gui.setTitle("java-chat"); + + gui.setLayout(new BorderLayout()); + + // Set up the output text area with scrolling + JScrollPane outputScrollPane = new JScrollPane(outputTextArea); + gui.add(outputScrollPane, BorderLayout.CENTER); + + // Set up the input text field + inputTextField.setPreferredSize(new Dimension(0, 30)); + gui.add(inputTextField, BorderLayout.SOUTH); + + // Set the size of the GUI to be a quarter of the screen size + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + int quarterWidth = screenSize.width / 2; + int quarterHeight = screenSize.height / 2; + gui.setSize(quarterWidth, quarterHeight); gui.setVisible(true); - } - + } + public static void main(String[] args) { new ChatGUI(); } + @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub - + } - } From 3048bfdef80163ce08aeebc82c9cd8992c425337 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 01:46:50 +0100 Subject: [PATCH 46/66] =?UTF-8?q?menue-Bar=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatGUI.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index 109f0f1..645f2c4 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -5,6 +5,8 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; +import javax.swing.JMenu; +import javax.swing.JMenuBar; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; @@ -25,6 +27,13 @@ public class ChatGUI implements ActionListener { gui.setLayout(new BorderLayout()); + JMenuBar bar = new JMenuBar(); + gui.setJMenuBar(bar); + + JMenu menu = new JMenu("File"); + + bar.add(menu); + // Set up the output text area with scrolling JScrollPane outputScrollPane = new JScrollPane(outputTextArea); gui.add(outputScrollPane, BorderLayout.CENTER); From 5e256aee19856cc04331a64354ff392c50bfbd71 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 01:50:55 +0100 Subject: [PATCH 47/66] =?UTF-8?q?Menue=20Punkte=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatGUI.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index 645f2c4..33a6cb7 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -7,12 +7,14 @@ import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; +import javax.swing.JMenuItem; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class ChatGUI implements ActionListener { - + + JMenuItem exit = new JMenuItem("Exit"); JTextField inputTextField = new JTextField(); JTextArea outputTextArea = new JTextArea(); @@ -29,8 +31,13 @@ public class ChatGUI implements ActionListener { JMenuBar bar = new JMenuBar(); gui.setJMenuBar(bar); + + JMenu options = new JMenu("options"); JMenu menu = new JMenu("File"); + menu.add(exit); + menu.add(options); + bar.add(menu); bar.add(menu); From c5487e0bd1b47c4959992eb304d5ce43053e4add Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 01:53:43 +0100 Subject: [PATCH 48/66] =?UTF-8?q?action=20f=C3=BCr=20exitbutton=20hinzugef?= =?UTF-8?q?=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatGUI.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index 33a6cb7..124332e 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -38,6 +38,7 @@ public class ChatGUI implements ActionListener { menu.add(exit); menu.add(options); bar.add(menu); + exit.addActionListener(this); bar.add(menu); @@ -64,7 +65,9 @@ public class ChatGUI implements ActionListener { @Override public void actionPerformed(ActionEvent e) { - // TODO Auto-generated method stub + if (e.getSource() == exit) { + System.exit(0); + } } From e1ea0eaa7917e42a416afddffb4a3812d78643ab Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 01:55:42 +0100 Subject: [PATCH 49/66] =?UTF-8?q?Logik=20f=C3=BCr=20Eingabe-Ausgabe=20hinz?= =?UTF-8?q?ugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatGUI.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index 124332e..f6d09d7 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -50,6 +50,8 @@ public class ChatGUI implements ActionListener { inputTextField.setPreferredSize(new Dimension(0, 30)); gui.add(inputTextField, BorderLayout.SOUTH); + inputTextField.addActionListener(this); + // Set the size of the GUI to be a quarter of the screen size Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int quarterWidth = screenSize.width / 2; @@ -68,7 +70,12 @@ public class ChatGUI implements ActionListener { if (e.getSource() == exit) { System.exit(0); } + if (e.getSource() == inputTextField) { + String inputText = inputTextField.getText(); + outputTextArea.append(inputText + "\n"); + inputTextField.setText(""); } } +} From caa676b5a4f8e864ef3497e5d17d0a37635ef421 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 01:58:21 +0100 Subject: [PATCH 50/66] =?UTF-8?q?farben=20als=20men=C3=BCpunkte=20hinzugef?= =?UTF-8?q?=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatGUI.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index f6d09d7..0781edc 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -15,6 +15,10 @@ import javax.swing.JTextField; public class ChatGUI implements ActionListener { JMenuItem exit = new JMenuItem("Exit"); + JMenuItem black = new JMenuItem("black"); + JMenuItem red = new JMenuItem("red"); + JMenuItem blue = new JMenuItem("blue"); + JMenuItem green = new JMenuItem("green"); JTextField inputTextField = new JTextField(); JTextArea outputTextArea = new JTextArea(); @@ -33,11 +37,19 @@ public class ChatGUI implements ActionListener { gui.setJMenuBar(bar); JMenu options = new JMenu("options"); + JMenu colors = new JMenu("font-colors"); JMenu menu = new JMenu("File"); menu.add(exit); menu.add(options); bar.add(menu); + options.add(colors); + + colors.add(black); + colors.add(red); + colors.add(blue); + colors.add(green); + exit.addActionListener(this); bar.add(menu); From 9686707419612ab0f685efc4b22ae157f6789986 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 02:00:47 +0100 Subject: [PATCH 51/66] =?UTF-8?q?Schriftfarboptionen=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatGUI.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index 0781edc..3651223 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -1,4 +1,5 @@ import java.awt.BorderLayout; +import java.awt.Color; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; @@ -50,6 +51,11 @@ public class ChatGUI implements ActionListener { colors.add(blue); colors.add(green); + red.addActionListener(this); + blue.addActionListener(this); + black.addActionListener(this); + green.addActionListener(this); + exit.addActionListener(this); bar.add(menu); @@ -86,6 +92,16 @@ public class ChatGUI implements ActionListener { String inputText = inputTextField.getText(); outputTextArea.append(inputText + "\n"); inputTextField.setText(""); + } + if (e.getSource() == red) { + outputTextArea.setForeground(Color.RED); + } + if (e.getSource() == blue) { + outputTextArea.setForeground(Color.BLUE); + }else if (e.getSource() == black) { + outputTextArea.setForeground(Color.BLACK); + } else if (e.getSource() == green) { + outputTextArea.setForeground(Color.GREEN); } From 080c6393cd127f111b4173c623a17ec112826c41 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 02:02:37 +0100 Subject: [PATCH 52/66] refactoring: Verschoerung Code --- src/main/java/ChatGUI.java | 98 +++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index 3651223..b33e153 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -14,12 +14,12 @@ import javax.swing.JTextArea; import javax.swing.JTextField; public class ChatGUI implements ActionListener { - + JMenuItem exit = new JMenuItem("Exit"); - JMenuItem black = new JMenuItem("black"); - JMenuItem red = new JMenuItem("red"); - JMenuItem blue = new JMenuItem("blue"); - JMenuItem green = new JMenuItem("green"); + JMenuItem black = new JMenuItem("black"); + JMenuItem red = new JMenuItem("red"); + JMenuItem blue = new JMenuItem("blue"); + JMenuItem green = new JMenuItem("green"); JTextField inputTextField = new JTextField(); JTextArea outputTextArea = new JTextArea(); @@ -27,49 +27,49 @@ public class ChatGUI implements ActionListener { public ChatGUI() { gui = new JFrame(); - + // Set up the main frame gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setTitle("java-chat"); gui.setLayout(new BorderLayout()); - + JMenuBar bar = new JMenuBar(); - gui.setJMenuBar(bar); - - JMenu options = new JMenu("options"); - JMenu colors = new JMenu("font-colors"); - - JMenu menu = new JMenu("File"); - menu.add(exit); - menu.add(options); - bar.add(menu); - options.add(colors); - - colors.add(black); - colors.add(red); - colors.add(blue); - colors.add(green); - - red.addActionListener(this); - blue.addActionListener(this); - black.addActionListener(this); - green.addActionListener(this); - - exit.addActionListener(this); - - bar.add(menu); - + gui.setJMenuBar(bar); + + JMenu options = new JMenu("options"); + JMenu colors = new JMenu("font-colors"); + + JMenu menu = new JMenu("File"); + menu.add(exit); + menu.add(options); + bar.add(menu); + options.add(colors); + + colors.add(black); + colors.add(red); + colors.add(blue); + colors.add(green); + + red.addActionListener(this); + blue.addActionListener(this); + black.addActionListener(this); + green.addActionListener(this); + + exit.addActionListener(this); + + bar.add(menu); + // Set up the output text area with scrolling JScrollPane outputScrollPane = new JScrollPane(outputTextArea); gui.add(outputScrollPane, BorderLayout.CENTER); - + // Set up the input text field inputTextField.setPreferredSize(new Dimension(0, 30)); gui.add(inputTextField, BorderLayout.SOUTH); - + inputTextField.addActionListener(this); - + // Set the size of the GUI to be a quarter of the screen size Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int quarterWidth = screenSize.width / 2; @@ -89,21 +89,21 @@ public class ChatGUI implements ActionListener { System.exit(0); } if (e.getSource() == inputTextField) { - String inputText = inputTextField.getText(); - outputTextArea.append(inputText + "\n"); - inputTextField.setText(""); + String inputText = inputTextField.getText(); + outputTextArea.append(inputText + "\n"); + inputTextField.setText(""); + } + if (e.getSource() == red) { + outputTextArea.setForeground(Color.RED); } - if (e.getSource() == red) { - outputTextArea.setForeground(Color.RED); - } - if (e.getSource() == blue) { - outputTextArea.setForeground(Color.BLUE); - }else if (e.getSource() == black) { - outputTextArea.setForeground(Color.BLACK); - } else if (e.getSource() == green) { - outputTextArea.setForeground(Color.GREEN); + if (e.getSource() == blue) { + outputTextArea.setForeground(Color.BLUE); + } else if (e.getSource() == black) { + outputTextArea.setForeground(Color.BLACK); + } else if (e.getSource() == green) { + outputTextArea.setForeground(Color.GREEN); - } + } -} + } } From b2083f814d47495f3dd0da725f60b4235ed3b413 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 02:11:04 +0100 Subject: [PATCH 53/66] =?UTF-8?q?Optionspunkte=20Schriftgr=C3=B6=C3=9Fe=20?= =?UTF-8?q?hinzugef=C3=BCgt(noch=20keine=20Funktion)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatGUI.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index b33e153..a63cbcc 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -14,7 +14,10 @@ import javax.swing.JTextArea; import javax.swing.JTextField; public class ChatGUI implements ActionListener { - + + JMenuItem small = new JMenuItem("small-font"); + JMenuItem medium = new JMenuItem("medium-font"); + JMenuItem large = new JMenuItem("large-font"); JMenuItem exit = new JMenuItem("Exit"); JMenuItem black = new JMenuItem("black"); JMenuItem red = new JMenuItem("red"); @@ -39,17 +42,23 @@ public class ChatGUI implements ActionListener { JMenu options = new JMenu("options"); JMenu colors = new JMenu("font-colors"); + JMenu size = new JMenu("font-size"); JMenu menu = new JMenu("File"); menu.add(exit); menu.add(options); bar.add(menu); options.add(colors); + options.add(size); colors.add(black); colors.add(red); colors.add(blue); colors.add(green); + + size.add(small); + size.add(medium); + size.add(large); red.addActionListener(this); blue.addActionListener(this); @@ -95,15 +104,13 @@ public class ChatGUI implements ActionListener { } if (e.getSource() == red) { outputTextArea.setForeground(Color.RED); - } - if (e.getSource() == blue) { + + } else if (e.getSource() == blue) { outputTextArea.setForeground(Color.BLUE); } else if (e.getSource() == black) { outputTextArea.setForeground(Color.BLACK); } else if (e.getSource() == green) { outputTextArea.setForeground(Color.GREEN); - } - } } From 24c7f1d6ab1dc5eb003ab99470565e288235a9e1 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 02:24:07 +0100 Subject: [PATCH 54/66] =?UTF-8?q?Schriftgr=C3=B6=C3=9Fenwahl=20implementie?= =?UTF-8?q?rt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatGUI.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index a63cbcc..4340cd3 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -1,6 +1,7 @@ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; +import java.awt.Font; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -64,6 +65,9 @@ public class ChatGUI implements ActionListener { blue.addActionListener(this); black.addActionListener(this); green.addActionListener(this); + small.addActionListener(this); + medium.addActionListener(this); + large.addActionListener(this); exit.addActionListener(this); @@ -112,5 +116,17 @@ public class ChatGUI implements ActionListener { } else if (e.getSource() == green) { outputTextArea.setForeground(Color.GREEN); } + if(e.getSource() == small) { + outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 12)); + } + if(e.getSource() == medium) { + outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 16)); + } + if(e.getSource() == large) { + outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 20)); + } + + + } } From 4ed49f7d4bd1e2b0363d38b04f0c9c272989659f Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 02:30:54 +0100 Subject: [PATCH 55/66] =?UTF-8?q?refactoring:=20Kommentare=20und=20=C3=9Cb?= =?UTF-8?q?ersicht?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatGUI.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index 4340cd3..de20bd9 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -16,14 +16,19 @@ import javax.swing.JTextField; public class ChatGUI implements ActionListener { + // Menu items for font sizes JMenuItem small = new JMenuItem("small-font"); JMenuItem medium = new JMenuItem("medium-font"); JMenuItem large = new JMenuItem("large-font"); - JMenuItem exit = new JMenuItem("Exit"); + + // Menu items for font colors JMenuItem black = new JMenuItem("black"); JMenuItem red = new JMenuItem("red"); JMenuItem blue = new JMenuItem("blue"); JMenuItem green = new JMenuItem("green"); + + JMenuItem exit = new JMenuItem("Exit"); + JTextField inputTextField = new JTextField(); JTextArea outputTextArea = new JTextArea(); @@ -35,12 +40,13 @@ public class ChatGUI implements ActionListener { // Set up the main frame gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setTitle("java-chat"); - gui.setLayout(new BorderLayout()); - + + // Set up the menu bar JMenuBar bar = new JMenuBar(); gui.setJMenuBar(bar); - + + // Create menu items and menus JMenu options = new JMenu("options"); JMenu colors = new JMenu("font-colors"); JMenu size = new JMenu("font-size"); @@ -60,7 +66,8 @@ public class ChatGUI implements ActionListener { size.add(small); size.add(medium); size.add(large); - + + // Register action listeners for menu items red.addActionListener(this); blue.addActionListener(this); black.addActionListener(this); @@ -90,7 +97,7 @@ public class ChatGUI implements ActionListener { gui.setSize(quarterWidth, quarterHeight); gui.setVisible(true); } - + // main methode zum Testen, müsste bei Implementation entfernt werden public static void main(String[] args) { new ChatGUI(); From ea503cdf962649f9d1d6fd65dd218c8113525346 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 02:45:40 +0100 Subject: [PATCH 56/66] test_Farbtest1 + Hilfklasse --- src/test/java/ChatGUITest.java | 22 ++++++++++++++++++++++ src/test/java/TestActionEvent.java | 10 ++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/test/java/ChatGUITest.java create mode 100644 src/test/java/TestActionEvent.java diff --git a/src/test/java/ChatGUITest.java b/src/test/java/ChatGUITest.java new file mode 100644 index 0000000..ab84b47 --- /dev/null +++ b/src/test/java/ChatGUITest.java @@ -0,0 +1,22 @@ +import static org.junit.Assert.assertEquals; + +import java.awt.Color; + +import org.junit.Before; +import org.junit.Test; + +public class ChatGUITest { + + private ChatGUI chatGUI; + + @Before + public void setUp() { + chatGUI = new ChatGUI(); + } + @Test + public void testSetOutputTextColorRed() { + chatGUI.actionPerformed(new TestActionEvent(chatGUI.red)); + assertEquals(Color.RED, chatGUI.outputTextArea.getForeground()); + } +} + diff --git a/src/test/java/TestActionEvent.java b/src/test/java/TestActionEvent.java new file mode 100644 index 0000000..1a60355 --- /dev/null +++ b/src/test/java/TestActionEvent.java @@ -0,0 +1,10 @@ +import java.awt.event.ActionEvent; + +public class TestActionEvent extends ActionEvent { + + private static final long serialVersionUID = 1L; + + public TestActionEvent(Object source) { + super(source, ActionEvent.ACTION_PERFORMED, "Test command"); + } +} \ No newline at end of file From 971ec214b6d3bed0ffd47cf044046f6cb05a7de2 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 02:47:14 +0100 Subject: [PATCH 57/66] test_weiterer Test --- src/test/java/ChatGUITest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/java/ChatGUITest.java b/src/test/java/ChatGUITest.java index ab84b47..8d8eaf6 100644 --- a/src/test/java/ChatGUITest.java +++ b/src/test/java/ChatGUITest.java @@ -18,5 +18,11 @@ public class ChatGUITest { chatGUI.actionPerformed(new TestActionEvent(chatGUI.red)); assertEquals(Color.RED, chatGUI.outputTextArea.getForeground()); } + @Test + public void testSetOutputTextColorBlue() { + chatGUI.actionPerformed(new TestActionEvent(chatGUI.blue)); + assertEquals(Color.BLUE, chatGUI.outputTextArea.getForeground()); + } + } From 9b6b6e1291df0842b6e9bddba19390f26cf119f3 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 02:48:47 +0100 Subject: [PATCH 58/66] =?UTF-8?q?test=5Fweitere=20Testf=C3=A4lle=20hinzuge?= =?UTF-8?q?f=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/ChatGUITest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/test/java/ChatGUITest.java b/src/test/java/ChatGUITest.java index 8d8eaf6..3afb790 100644 --- a/src/test/java/ChatGUITest.java +++ b/src/test/java/ChatGUITest.java @@ -23,6 +23,17 @@ public class ChatGUITest { chatGUI.actionPerformed(new TestActionEvent(chatGUI.blue)); assertEquals(Color.BLUE, chatGUI.outputTextArea.getForeground()); } + @Test + public void testSetOutputTextColorBlack() { + chatGUI.actionPerformed(new TestActionEvent(chatGUI.black)); + assertEquals(Color.BLACK, chatGUI.outputTextArea.getForeground()); + } + + @Test + public void testSetOutputTextColorGreen() { + chatGUI.actionPerformed(new TestActionEvent(chatGUI.green)); + assertEquals(Color.GREEN, chatGUI.outputTextArea.getForeground()); + } } From ef68912273314f128255d42ef15a3f11daa34281 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 02:50:06 +0100 Subject: [PATCH 59/66] test_fonttest --- src/test/java/ChatGUITest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/java/ChatGUITest.java b/src/test/java/ChatGUITest.java index 3afb790..8b06bf4 100644 --- a/src/test/java/ChatGUITest.java +++ b/src/test/java/ChatGUITest.java @@ -1,6 +1,7 @@ import static org.junit.Assert.assertEquals; import java.awt.Color; +import java.awt.Font; import org.junit.Before; import org.junit.Test; @@ -34,6 +35,12 @@ public class ChatGUITest { chatGUI.actionPerformed(new TestActionEvent(chatGUI.green)); assertEquals(Color.GREEN, chatGUI.outputTextArea.getForeground()); } + @Test + public void testSetOutputTextFontSizeSmall() { + chatGUI.actionPerformed(new TestActionEvent(chatGUI.small)); + Font expectedFont = chatGUI.outputTextArea.getFont().deriveFont(Font.PLAIN, 12); + assertEquals(expectedFont, chatGUI.outputTextArea.getFont()); + } } From 29de7c748dfa1cf53dc07a4479187e130059da49 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 02:51:14 +0100 Subject: [PATCH 60/66] test_medium-font-test --- src/test/java/ChatGUITest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/java/ChatGUITest.java b/src/test/java/ChatGUITest.java index 8b06bf4..94a4fc2 100644 --- a/src/test/java/ChatGUITest.java +++ b/src/test/java/ChatGUITest.java @@ -41,6 +41,12 @@ public class ChatGUITest { Font expectedFont = chatGUI.outputTextArea.getFont().deriveFont(Font.PLAIN, 12); assertEquals(expectedFont, chatGUI.outputTextArea.getFont()); } + @Test + public void testSetOutputTextFontSizeMedium() { + chatGUI.actionPerformed(new TestActionEvent(chatGUI.medium)); + Font expectedFont = chatGUI.outputTextArea.getFont().deriveFont(Font.PLAIN, 16); + assertEquals(expectedFont, chatGUI.outputTextArea.getFont()); + } } From 0d0efe54b3734e9ba6fe5ad2e8a6a27a2b742bc8 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 02:54:36 +0100 Subject: [PATCH 61/66] =?UTF-8?q?test=5FSchriftgr=C3=B6=C3=9Fe=20large?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/ChatGUITest.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/test/java/ChatGUITest.java b/src/test/java/ChatGUITest.java index 94a4fc2..42065b8 100644 --- a/src/test/java/ChatGUITest.java +++ b/src/test/java/ChatGUITest.java @@ -47,6 +47,12 @@ public class ChatGUITest { Font expectedFont = chatGUI.outputTextArea.getFont().deriveFont(Font.PLAIN, 16); assertEquals(expectedFont, chatGUI.outputTextArea.getFont()); } - + @Test + public void testSetOutputTextFontSizeLarge() { + chatGUI.actionPerformed(new TestActionEvent(chatGUI.large)); + Font expectedFont = chatGUI.outputTextArea.getFont().deriveFont(Font.PLAIN, 20); + assertEquals(expectedFont, chatGUI.outputTextArea.getFont()); + } } + From 14c221315bb4233553831551a62ed4a69070f3e3 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 03:40:32 +0100 Subject: [PATCH 62/66] =?UTF-8?q?hinzuf=C3=BCgen=20senden-button?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatGUI.java | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index de20bd9..d5efb58 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -6,10 +6,12 @@ import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; +import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; @@ -31,6 +33,7 @@ public class ChatGUI implements ActionListener { JTextField inputTextField = new JTextField(); JTextArea outputTextArea = new JTextArea(); + JButton sendButton = new JButton("Senden"); JFrame gui; @@ -79,17 +82,19 @@ public class ChatGUI implements ActionListener { exit.addActionListener(this); bar.add(menu); - + // Set up the output text area with scrolling JScrollPane outputScrollPane = new JScrollPane(outputTextArea); gui.add(outputScrollPane, BorderLayout.CENTER); - - // Set up the input text field - inputTextField.setPreferredSize(new Dimension(0, 30)); - gui.add(inputTextField, BorderLayout.SOUTH); + + JPanel inputPanel = new JPanel(new BorderLayout()); + inputPanel.add(inputTextField, BorderLayout.CENTER); + inputPanel.add(sendButton, BorderLayout.EAST); + gui.add(inputPanel, BorderLayout.SOUTH); inputTextField.addActionListener(this); - + sendButton.addActionListener(this); + // Set the size of the GUI to be a quarter of the screen size Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int quarterWidth = screenSize.width / 2; @@ -108,10 +113,10 @@ public class ChatGUI implements ActionListener { if (e.getSource() == exit) { System.exit(0); } - if (e.getSource() == inputTextField) { - String inputText = inputTextField.getText(); - outputTextArea.append(inputText + "\n"); - inputTextField.setText(""); + if (e.getSource() == inputTextField || e.getSource() == sendButton) { + String inputText = inputTextField.getText(); + outputTextArea.append(inputText + "\n"); + inputTextField.setText(""); } if (e.getSource() == red) { outputTextArea.setForeground(Color.RED); From c089275208d580e5672221fd647ad47a6322d431 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 03:43:24 +0100 Subject: [PATCH 63/66] refactoring: Kommentare + formatieren --- src/main/java/ChatGUI.java | 54 +++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index d5efb58..743e4f4 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -17,7 +17,7 @@ import javax.swing.JTextArea; import javax.swing.JTextField; public class ChatGUI implements ActionListener { - + // Menu items for font sizes JMenuItem small = new JMenuItem("small-font"); JMenuItem medium = new JMenuItem("medium-font"); @@ -28,9 +28,9 @@ public class ChatGUI implements ActionListener { JMenuItem red = new JMenuItem("red"); JMenuItem blue = new JMenuItem("blue"); JMenuItem green = new JMenuItem("green"); - + JMenuItem exit = new JMenuItem("Exit"); - + JTextField inputTextField = new JTextField(); JTextArea outputTextArea = new JTextArea(); JButton sendButton = new JButton("Senden"); @@ -44,11 +44,11 @@ public class ChatGUI implements ActionListener { gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setTitle("java-chat"); gui.setLayout(new BorderLayout()); - + // Set up the menu bar JMenuBar bar = new JMenuBar(); gui.setJMenuBar(bar); - + // Create menu items and menus JMenu options = new JMenu("options"); JMenu colors = new JMenu("font-colors"); @@ -65,11 +65,11 @@ public class ChatGUI implements ActionListener { colors.add(red); colors.add(blue); colors.add(green); - + size.add(small); size.add(medium); size.add(large); - + // Register action listeners for menu items red.addActionListener(this); blue.addActionListener(this); @@ -82,19 +82,20 @@ public class ChatGUI implements ActionListener { exit.addActionListener(this); bar.add(menu); - + // Set up the output text area with scrolling JScrollPane outputScrollPane = new JScrollPane(outputTextArea); gui.add(outputScrollPane, BorderLayout.CENTER); + // Set up the input panel with text field and send button JPanel inputPanel = new JPanel(new BorderLayout()); - inputPanel.add(inputTextField, BorderLayout.CENTER); - inputPanel.add(sendButton, BorderLayout.EAST); - gui.add(inputPanel, BorderLayout.SOUTH); + inputPanel.add(inputTextField, BorderLayout.CENTER); + inputPanel.add(sendButton, BorderLayout.EAST); + gui.add(inputPanel, BorderLayout.SOUTH); inputTextField.addActionListener(this); sendButton.addActionListener(this); - + // Set the size of the GUI to be a quarter of the screen size Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int quarterWidth = screenSize.width / 2; @@ -102,6 +103,7 @@ public class ChatGUI implements ActionListener { gui.setSize(quarterWidth, quarterHeight); gui.setVisible(true); } + // main methode zum Testen, müsste bei Implementation entfernt werden public static void main(String[] args) { @@ -114,13 +116,13 @@ public class ChatGUI implements ActionListener { System.exit(0); } if (e.getSource() == inputTextField || e.getSource() == sendButton) { - String inputText = inputTextField.getText(); - outputTextArea.append(inputText + "\n"); - inputTextField.setText(""); + String inputText = inputTextField.getText(); + outputTextArea.append(inputText + "\n"); + inputTextField.setText(""); } if (e.getSource() == red) { outputTextArea.setForeground(Color.RED); - + } else if (e.getSource() == blue) { outputTextArea.setForeground(Color.BLUE); } else if (e.getSource() == black) { @@ -128,17 +130,15 @@ public class ChatGUI implements ActionListener { } else if (e.getSource() == green) { outputTextArea.setForeground(Color.GREEN); } - if(e.getSource() == small) { - outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 12)); - } - if(e.getSource() == medium) { - outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 16)); - } - if(e.getSource() == large) { - outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 20)); - } - - + if (e.getSource() == small) { + outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 12)); + } + if (e.getSource() == medium) { + outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 16)); + } + if (e.getSource() == large) { + outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 20)); + } } } From 0f07791c8751fb50b9f55a48784cd21b07d36879 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 03:46:31 +0100 Subject: [PATCH 64/66] test_ButtonTest --- src/test/java/ChatGUITest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/ChatGUITest.java b/src/test/java/ChatGUITest.java index 42065b8..04fa7d0 100644 --- a/src/test/java/ChatGUITest.java +++ b/src/test/java/ChatGUITest.java @@ -53,6 +53,14 @@ public class ChatGUITest { Font expectedFont = chatGUI.outputTextArea.getFont().deriveFont(Font.PLAIN, 20); assertEquals(expectedFont, chatGUI.outputTextArea.getFont()); } + @Test + public void testSendButtonActionPerformed() { + chatGUI.inputTextField.setText("Testnachricht"); + chatGUI.sendButton.doClick(); + + String expectedOutput = "Testnachricht\n"; + assertEquals(expectedOutput, chatGUI.outputTextArea.getText()); + } } From a73279496a01d9d10bef8438dd7b324e7bfef124 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 03:49:02 +0100 Subject: [PATCH 65/66] test_Inputfield-Test --- src/test/java/ChatGUITest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/test/java/ChatGUITest.java b/src/test/java/ChatGUITest.java index 04fa7d0..47cc055 100644 --- a/src/test/java/ChatGUITest.java +++ b/src/test/java/ChatGUITest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertEquals; import java.awt.Color; import java.awt.Font; +import java.awt.event.ActionEvent; import org.junit.Before; import org.junit.Test; @@ -61,6 +62,19 @@ public class ChatGUITest { String expectedOutput = "Testnachricht\n"; assertEquals(expectedOutput, chatGUI.outputTextArea.getText()); } + @Test + public void testInputTextFieldActionPerformed() { + + chatGUI.inputTextField.setText("Testnachricht"); + chatGUI.actionPerformed(new ActionEvent(chatGUI.inputTextField, ActionEvent.ACTION_PERFORMED, "")); + + + String expectedOutput = "Testnachricht\n"; + assertEquals(expectedOutput, chatGUI.outputTextArea.getText()); + + + assertEquals("", chatGUI.inputTextField.getText()); + } } From 6f67832da65d86bc3d7c26bc18dd5705dfc08b6d Mon Sep 17 00:00:00 2001 From: fdai7599 Date: Fri, 9 Feb 2024 02:58:35 +0000 Subject: [PATCH 66/66] =?UTF-8?q?merge=20conflict=20aufl=C3=B6sen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index d427c1a..4b4d00b 100644 --- a/pom.xml +++ b/pom.xml @@ -1,8 +1,4 @@ - - RELEASE - - 4.0.0 org.progmethoden java-chat @@ -24,7 +20,7 @@ org.junit.jupiter junit-jupiter - ${junit-jupiter-version} + RELEASE test