Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,36 @@
</dependencies>
<build>
<plugins>

<!-- Compiler plugin: tells Maven to use Java 25 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>25</source>
<target>25</target>
<release>25</release>
</configuration>
</plugin>

<!-- JavaFX plugin -->
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>com.example.HelloFX</mainClass>
<options>
<option>--enable-native-access=javafx.graphics</option>
<option>--enable-native-access=javafx.graphics</option>
</options>
<launcher>javafx</launcher>
<stripDebug>true</stripDebug>
<noHeaderFiles>true</noHeaderFiles>
<noManPages>true</noManPages>
</configuration>
</plugin>

</plugins>
</build>
</project>
26 changes: 26 additions & 0 deletions src/main/java/com/example/ChatController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example;

import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;

public class ChatController {

@FXML
private ListView<String> messagesList;

@FXML
private TextField inputField;

private final ChatModel model = new ChatModel();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Prefer dependency injection over direct instantiation for better testability.

The direct instantiation of ChatModel creates tight coupling and makes the controller difficult to unit test.

Consider using constructor injection:

-private final ChatModel model = new ChatModel();
+private final ChatModel model;
+
+public ChatController() {
+    this(new ChatModel());
+}
+
+// Package-private constructor for testing
+ChatController(ChatModel model) {
+    this.model = model;
+}

This allows you to inject a mock ChatModel during testing.

🤖 Prompt for AI Agents
In src/main/java/com/example/ChatController.java around line 15, the controller
directly instantiates ChatModel which creates tight coupling and hinders unit
testing; change to constructor injection by removing the new ChatModel()
instantiation, add a constructor that accepts a ChatModel parameter and assigns
it to the final field, update any framework annotations if needed (e.g.,
@Autowired or leave as plain constructor for manual wiring), and update calling
code/tests to provide a mock or real ChatModel via the constructor.


@FXML
private void onSend() {
String message = inputField.getText().trim();
if (!message.isEmpty()) {
messagesList.getItems().add("Me: " + message);
model.sendMessage(message);
inputField.clear();
}
}
Comment on lines +20 to +27
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add error handling for model.sendMessage().

The onSend() method should handle potential exceptions from model.sendMessage() to prevent the UI from becoming unresponsive.

Apply this diff to add error handling:

 @FXML
 private void onSend() {
     String message = inputField.getText().trim();
     if (!message.isEmpty()) {
-        messagesList.getItems().add("Me: " + message);
-        model.sendMessage(message);
-        inputField.clear();
+        try {
+            messagesList.getItems().add("Me: " + message);
+            model.sendMessage(message);
+            inputField.clear();
+        } catch (Exception e) {
+            System.err.println("Failed to send message: " + e.getMessage());
+            // TODO: Show error dialog to user
+        }
     }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private void onSend() {
String message = inputField.getText().trim();
if (!message.isEmpty()) {
messagesList.getItems().add("Me: " + message);
model.sendMessage(message);
inputField.clear();
}
}
private void onSend() {
String message = inputField.getText().trim();
if (!message.isEmpty()) {
try {
messagesList.getItems().add("Me: " + message);
model.sendMessage(message);
inputField.clear();
} catch (Exception e) {
System.err.println("Failed to send message: " + e.getMessage());
// TODO: Show error dialog to user
}
}
}
🤖 Prompt for AI Agents
In src/main/java/com/example/ChatController.java around lines 18-25, wrap the
call to model.sendMessage(...) with proper error handling and avoid blocking the
JavaFX thread: dispatch model.sendMessage(...) to a background thread (e.g.,
CompletableFuture.runAsync or a Task), catch any thrown exceptions, log them,
and then use Platform.runLater to update the UI (show an error Alert or mark the
message as failed) so the UI remains responsive; ensure inputField clearing and
messagesList updates occur on the JavaFX thread and adjust their order as needed
(e.g., show the sent message immediately, run sendMessage async, and on
exception notify the user).

}
9 changes: 9 additions & 0 deletions src/main/java/com/example/ChatModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example;

public class ChatModel {

public void sendMessage(String message) {
// TODO: send POST JSON to ntfy
System.out.println("Sending message: " + message);
}
Comment on lines +34 to +55
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Add input validation to prevent null or empty messages.

The method should validate the input parameter to avoid processing invalid messages.

Apply this diff to add validation:

 public void sendMessage(String message) {
+    if (message == null || message.isBlank()) {
+        throw new IllegalArgumentException("Message cannot be null or empty");
+    }
     // TODO: send POST JSON to ntfy
     System.out.println("Sending message: " + message);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public void sendMessage(String message) {
// TODO: send POST JSON to ntfy
System.out.println("Sending message: " + message);
}
public void sendMessage(String message) {
if (message == null || message.isBlank()) {
throw new IllegalArgumentException("Message cannot be null or empty");
}
// TODO: send POST JSON to ntfy
System.out.println("Sending message: " + message);
}
🤖 Prompt for AI Agents
In src/main/java/com/example/ChatModel.java around lines 5 to 8, the sendMessage
method currently does not validate its input; add a defensive check that the
message is not null and not blank (trim and verify length > 0) and if invalid
throw an IllegalArgumentException with a clear message (e.g., "message must not
be null or empty"); keep existing behavior for valid messages. Ensure the check
is the first statement in the method so invalid inputs are rejected before any
processing or I/O.

}
2 changes: 1 addition & 1 deletion src/main/java/com/example/HelloFX.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class HelloFX extends Application {

@Override
public void start(Stage stage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader(HelloFX.class.getResource("hello-view.fxml"));
FXMLLoader fxmlLoader = new FXMLLoader(HelloFX.class.getResource("chat-view.fxml"));
Parent root = fxmlLoader.load();
Scene scene = new Scene(root, 640, 480);
stage.setTitle("Hello MVC");
Expand Down
24 changes: 24 additions & 0 deletions src/main/resources/com/example/chat-view.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>

<BorderPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="com.example.ChatController">

<center>
<ListView fx:id="messagesList"/>
</center>

<bottom>
<HBox spacing="8" style="-fx-padding: 10;">
<TextField fx:id="inputField" HBox.hgrow="ALWAYS"/>
<Button text="Send" onAction="#onSend"/>
</HBox>
</bottom>

</BorderPane>
Loading