generated from kappsegla/javafx-classrom
-
Notifications
You must be signed in to change notification settings - Fork 66
Lab3 #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Lab3 #10
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
388ee6f
Initial commit
simonforsberg 12283ee
Edited View
simonforsberg d205b0e
Edited View, Controller, Model
simonforsberg 81917ad
Created tests, change topic feature
simonforsberg efa87b1
Edited NtfyConnectionImpl: add cancellation, thread safety, error han…
simonforsberg e245be3
Edited typos
simonforsberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| target/ | ||
| /.idea/ | ||
| .env |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,52 @@ | ||
| package com.example; | ||
|
|
||
| import javafx.event.ActionEvent; | ||
| import javafx.fxml.FXML; | ||
| import javafx.scene.control.Label; | ||
| import javafx.scene.control.ListView; | ||
| import javafx.scene.control.TextField; | ||
|
|
||
| /** | ||
| * Controller layer: mediates between the view (FXML) and the model. | ||
| */ | ||
| public class HelloController { | ||
|
|
||
| private final HelloModel model = new HelloModel(); | ||
| private final HelloModel model = new HelloModel(new NtfyConnectionImpl()); | ||
|
|
||
| public ListView<NtfyMessageDto> messageView; | ||
|
|
||
| @FXML | ||
| private Label messageLabel; | ||
|
|
||
| @FXML | ||
| private TextField inputField; | ||
|
|
||
| @FXML | ||
| private TextField topicField; | ||
|
|
||
| @FXML | ||
| private void initialize() { | ||
| if (messageLabel != null) { | ||
| messageLabel.setText(model.getGreeting()); | ||
| } | ||
|
|
||
| inputField.textProperty().bindBidirectional(model.messageToSendProperty()); | ||
|
|
||
| topicField.textProperty().bindBidirectional(model.topicProperty()); | ||
|
|
||
| messageView.setItems(model.getMessages()); | ||
|
|
||
| model.connectToTopic(); | ||
| } | ||
|
|
||
| public void sendMessage(ActionEvent actionEvent) { | ||
| if (!inputField.getText().trim().isEmpty()) { | ||
| model.sendMessage(); | ||
| inputField.clear(); | ||
| } | ||
| } | ||
|
|
||
| public void connectToTopic(ActionEvent actionEvent) { | ||
| model.connectToTopic(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.example; | ||
|
|
||
| import java.util.function.Consumer; | ||
|
|
||
| public interface NtfyConnection { | ||
|
|
||
| public boolean send(String topic, String message); | ||
|
|
||
| public void receive(String topic, Consumer<NtfyMessageDto> messageHandler); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package com.example; | ||
|
|
||
| import io.github.cdimascio.dotenv.Dotenv; | ||
| import tools.jackson.databind.ObjectMapper; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.net.http.HttpClient; | ||
| import java.net.http.HttpRequest; | ||
| import java.net.http.HttpResponse; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.function.Consumer; | ||
|
|
||
| public class NtfyConnectionImpl implements NtfyConnection { | ||
|
|
||
| private final HttpClient http = HttpClient.newHttpClient(); | ||
| private final String hostName; | ||
| private final ObjectMapper mapper = new ObjectMapper(); | ||
| private CompletableFuture<Void> receiveTask; | ||
|
|
||
| public NtfyConnectionImpl() { | ||
| Dotenv dotenv = Dotenv.load(); | ||
| hostName = Objects.requireNonNull(dotenv.get("HOST_NAME")); | ||
| } | ||
|
|
||
| public NtfyConnectionImpl(String hostName) { | ||
| this.hostName = hostName; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean send(String topic, String message) { | ||
| HttpRequest httpRequest = HttpRequest.newBuilder() | ||
| .POST(HttpRequest.BodyPublishers.ofString(message)) | ||
| .header("Cache", "no") | ||
| .uri(URI.create(hostName + "/" + topic)) | ||
| .build(); | ||
| try { | ||
| var response = http.send(httpRequest, HttpResponse.BodyHandlers.discarding()); | ||
| return true; | ||
| } catch (IOException e) { | ||
| System.out.println("Error sending message"); | ||
| } catch (InterruptedException e) { | ||
| System.out.println("Interrupted sending message"); | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void receive(String topic, Consumer<NtfyMessageDto> messageHandler) { | ||
| if (receiveTask != null) { | ||
| receiveTask.cancel(true); | ||
| } | ||
| HttpRequest httpRequest = HttpRequest.newBuilder() | ||
| .GET() | ||
| .uri(URI.create(hostName + "/" + topic + "/json")) | ||
| .build(); | ||
|
|
||
| receiveTask = http.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofLines()) | ||
| .thenAccept(response -> response.body() | ||
| .map(s -> { | ||
| try { | ||
| return mapper.readValue(s, NtfyMessageDto.class); | ||
| } catch (Exception e) { | ||
| System.err.println("Failed to parse message: " + e.getMessage()); | ||
| return null; | ||
| } | ||
| }) | ||
| .filter(Objects::nonNull) | ||
| .filter(message -> "message".equals(message.event())) | ||
| .peek(System.out::println) | ||
| .forEach(messageHandler)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.example; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
|
||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| public record NtfyMessageDto(String id, long time, String event, String topic, String message) { | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return message; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,41 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <?import javafx.scene.layout.StackPane?> | ||
| <?import javafx.scene.control.Label?> | ||
|
|
||
| <StackPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.HelloController"> | ||
| <children> | ||
| <Label fx:id="messageLabel" text="Hello, JavaFX!" /> | ||
| </children> | ||
| </StackPane> | ||
|
|
||
| <?import javafx.geometry.*?> | ||
| <?import javafx.scene.control.*?> | ||
| <?import javafx.scene.layout.*?> | ||
|
|
||
| <VBox alignment="CENTER" spacing="15" | ||
| xmlns="http://javafx.com/javafx/17.0.12" | ||
| xmlns:fx="http://javafx.com/fxml/1" | ||
| fx:controller="com.example.HelloController"> | ||
|
|
||
| <padding> | ||
| <Insets bottom="20" left="20" right="20" top="20" /> | ||
| </padding> | ||
|
|
||
| <Label fx:id="messageLabel" | ||
| style="-fx-font-size: 20px;" | ||
| text="JavaFX Chat App 💬" /> | ||
|
|
||
| <HBox alignment="CENTER_LEFT" | ||
| spacing="10"> | ||
| <Label text="Topic:" /> | ||
| <TextField fx:id="topicField" | ||
| text="mytopic" | ||
| HBox.hgrow="ALWAYS" /> | ||
| <Button text="Connect" | ||
| onAction="#connectToTopic" /> | ||
| </HBox> | ||
|
|
||
| <HBox spacing="10"> | ||
| <TextField fx:id="inputField" | ||
| promptText="Type a message!" | ||
| HBox.hgrow="ALWAYS" /> | ||
| <Button text="Send" | ||
| onAction="#sendMessage" /> | ||
| </HBox> | ||
|
|
||
| <ListView fx:id="messageView" | ||
| VBox.vgrow="ALWAYS" /> | ||
|
|
||
| </VBox> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix invalid Jackson import for ObjectMapper.
tools.jackson.databind.ObjectMapperdoes not exist, so this class will not compile. Switch to the correct Jackson package.📝 Committable suggestion
🤖 Prompt for AI Agents