From 9adf4d68c15eaa4b432978b7c959189e1a49655f Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 20:00:53 +0100 Subject: [PATCH 1/6] 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 2/6] 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 3/6] 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 4/6] 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 5/6] 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 6/6] 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) {