generated from kappsegla/javafx-classrom
-
Notifications
You must be signed in to change notification settings - Fork 66
labb3 #17
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
labb3 #17
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b83bd2c
Add chat functionality, UI enhancements, and first update controller …
annikaholmqvist94 67cc5f2
Update .gitignore
annikaholmqvist94 1b9b0b8
Implement message sending/receiving logic with Ntfy integration and u…
annikaholmqvist94 d3a2149
Add file attachment button and stub method in controller
annikaholmqvist94 6584d5b
Implement file upload functionality with Ntfy integration
annikaholmqvist94 17546f9
Add unit tests for message receiving and file sending
annikaholmqvist94 e80bd27
Refactor `HelloModel` for JavaFX thread update tests for enhanced mes…
annikaholmqvist94 a69df76
Add validation for empty or whitespace messages in `HelloModel.sendMe…
annikaholmqvist94 0877a20
Add topic-based messaging and file sharing with dynamic UI updates an…
annikaholmqvist94 37fe2ce
Add support for dynamic topic selection update tests and model logic…
annikaholmqvist94 32adbde
Add fixed-topic chat functionality, update file sending message/file …
annikaholmqvist94 fe3d0d0
update tests for enhanced verification and UI consistency.
annikaholmqvist94 fc0902a
update tests for enhanced verification and UI consistency.
annikaholmqvist94 0afcc08
update tests for enhanced verification and UI consistency.
annikaholmqvist94 065761a
Add Javadoc comments for improved code clarity and maintainability.
annikaholmqvist94 e04e60b
Refactor `HelloModel` to enhance async message/file sending logic and…
annikaholmqvist94 10acd5d
Add `HOST_NAME` validation in `HelloController` and improve error han…
annikaholmqvist94 5b0a5bf
Improve file attachment handling in `HelloController`, enhance error …
annikaholmqvist94 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,213 @@ | ||
| package com.example; | ||
|
|
||
| import javafx.beans.binding.Bindings; | ||
|
|
||
| import javafx.fxml.FXML; | ||
| import javafx.scene.control.Label; | ||
|
|
||
| import javafx.scene.control.*; | ||
|
|
||
| import javafx.stage.FileChooser; | ||
| import javafx.stage.Stage; | ||
|
|
||
| import java.io.File; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Controller layer: mediates between the view (FXML) and the model. | ||
| * Controller layer: Manages the user interface and mediates communication between the View (FXML) and the Model (HelloModel). | ||
| * Handles user interactions such as sending messages, attaching files, and displaying chat content. | ||
| */ | ||
| public class HelloController { | ||
|
|
||
| private final HelloModel model = new HelloModel(); | ||
|
|
||
| private static final String HOST_NAME = System.getenv("HOST_NAME"); | ||
|
|
||
| static { | ||
| if (HOST_NAME == null || HOST_NAME.isBlank()) { | ||
| throw new IllegalStateException( | ||
| "Environment variable HOST_NAME must be set to the server URL." | ||
| ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| /** | ||
| * The model instance that holds application data and business logic. | ||
| * Initializes connection to the specified Ntfy server. | ||
| */ | ||
| private final HelloModel model = new HelloModel(new NtfyConnectionImpl(HOST_NAME)); | ||
|
|
||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * The ListView component displaying the list of chat messages. | ||
| */ | ||
| @FXML | ||
| private Label messageLabel; | ||
| public ListView<NtfyMessageDto> chatListView; | ||
|
|
||
| /** | ||
| * The label displaying the current fixed topic being used. | ||
| */ | ||
| @FXML | ||
| public Label topicLabel; | ||
|
|
||
| /** | ||
| * The label indicating the name of the file currently attached for sending. | ||
| */ | ||
| @FXML | ||
| public Label attachedFileLabel; | ||
|
|
||
|
|
||
| /** | ||
| * The button used to send the message or the attached file. | ||
| */ | ||
| @FXML | ||
| private Button sendButton; | ||
|
|
||
| @FXML | ||
| private TextField messageInput; | ||
|
|
||
| @FXML | ||
| private Button attachFile; | ||
|
|
||
|
|
||
| /** | ||
| * Initializes the controller. This method is called automatically by the FXML loader. | ||
| * It sets up bindings between the view components and the model and configures the chat list view. | ||
| */ | ||
| @FXML | ||
| private void initialize() { | ||
| if (messageLabel != null) { | ||
| messageLabel.setText(model.getGreeting()); | ||
| if (sendButton != null) { | ||
| sendButton.setText(model.getGreeting()); | ||
|
|
||
| // Bindning: Knappen är inaktiverad ENDAST om BÅDE meddelandet är tomt OCH fil inte är bifogad | ||
| sendButton.disableProperty().bind( | ||
| Bindings.createBooleanBinding(() -> { | ||
| boolean isMessageEmpty = model.messageToSendProperty().get() == null || | ||
| model.messageToSendProperty().get().trim().isEmpty(); | ||
| boolean isFileNotAttached = model.fileToSendProperty().get() == null; | ||
|
|
||
| return isMessageEmpty && isFileNotAttached; | ||
| }, | ||
| model.messageToSendProperty(), | ||
| model.fileToSendProperty()) | ||
| ); | ||
| } | ||
|
|
||
| if (topicLabel != null) { | ||
| // Visar den fasta topicen | ||
| topicLabel.textProperty().bind( | ||
| Bindings.concat("Fixed Topic: ", model.currentTopicProperty()) | ||
| ); | ||
| } | ||
|
|
||
| // Hanterar visning av bifogad fil | ||
| if (attachedFileLabel != null) { | ||
| model.fileToSendProperty().addListener((obs, oldFile, newFile) -> { | ||
| if (newFile != null) { | ||
| attachedFileLabel.setText("Attached file: " + newFile.getName()); | ||
| attachedFileLabel.setStyle("-fx-font-style: italic;" + | ||
| " -fx-font-size: 12px;" + | ||
| " -fx-text-fill: #008000;"); | ||
| } else { | ||
| attachedFileLabel.setText("No file attached"); | ||
| attachedFileLabel.setStyle("-fx-font-style: italic; " + | ||
| "-fx-font-size: 12px; " + | ||
| "-fx-text-fill: #333;"); | ||
| } | ||
| }); | ||
| attachedFileLabel.setText("No file attached"); | ||
| } | ||
|
|
||
| if (messageInput!=null){ | ||
| messageInput.textProperty().bindBidirectional(model.messageToSendProperty()); | ||
| } | ||
|
|
||
| if(chatListView!=null){ | ||
| chatListView.setItems(model.getMessages()); | ||
| // Använd den enkla CellFactoryn | ||
| chatListView.setCellFactory(param -> new SimpleMessageCell()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A simple custom ListCell implementation for the chatListView. | ||
| * It displays the message text or a placeholder for file uploads, and indicates if the message was sent locally. | ||
| */ | ||
| private static class SimpleMessageCell extends ListCell<NtfyMessageDto> { | ||
|
|
||
| @Override | ||
| protected void updateItem(NtfyMessageDto item, boolean empty) { | ||
| super.updateItem(item, empty); | ||
|
|
||
| if (empty || item == null) { | ||
| setText(null); | ||
| setGraphic(null); | ||
| setStyle(null); | ||
| } else { | ||
| // Hämta meddelandet eller visa filstatus om meddelandet är tomt | ||
| String displayMessage = item.message() != null && !item.message().trim().isEmpty() | ||
| ? item.message() | ||
| : ("file".equals(item.event())) ? item.topic() + " Uploaded" : ""; | ||
|
|
||
| // Lägg till prefix för att visa om det är skickat lokalt | ||
| String prefix = item.isLocal() ? "(Sent) " : ""; | ||
|
|
||
| setText(prefix + displayMessage); | ||
| setGraphic(null); | ||
|
|
||
| // Mycket enkel stil utan bubblor/färger. Använd standard utseende. | ||
| setStyle(null); | ||
| } | ||
| } | ||
| } | ||
annikaholmqvist94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| /** | ||
| * Handles the action of the send button. | ||
| * If a file is attached, it calls the model to send the file; otherwise, it calls the model to send the text message. | ||
| */ | ||
| @FXML | ||
| protected void sendMessage() { | ||
| if (model.fileToSendProperty().get() != null) { | ||
| // Om en fil är bifogad, skicka filen och rensa bilagan i modellen | ||
| model.sendFile(); | ||
| } else { | ||
| // Annars, skicka textmeddelandet | ||
| model.sendMessage(); | ||
| } | ||
|
|
||
| if (messageInput!=null){ | ||
| messageInput.requestFocus(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Handles the action of the attach file button. | ||
| * Opens a FileChooser dialog and sets the selected file in the model. | ||
| */ | ||
| @FXML | ||
| protected void attachFile() { | ||
| // Hämta scenen från en av kontrollerna | ||
|
|
||
| if (attachFile == null || attachFile.getScene() == null || attachFile.getScene().getWindow() == null) { | ||
| System.err.println("Cannot open file chooser: UI not ready."); | ||
| return; | ||
| } | ||
| Stage stage = (Stage) attachFile.getScene().getWindow(); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| FileChooser fileChooser = new FileChooser(); | ||
| fileChooser.setTitle("Choose file to attach"); | ||
|
|
||
| File selectedFile = fileChooser.showOpenDialog(stage); | ||
|
|
||
| if (selectedFile != null) { | ||
| model.setFileToSend(selectedFile); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.