package de.uniluebeck.mi.projmi6.controller; import de.uniluebeck.mi.projmi6.model.HL7Message; import de.uniluebeck.mi.projmi6.view.MessageIcon; import javafx.beans.property.SimpleListProperty; import javafx.collections.FXCollections; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.stage.Modality; import javafx.stage.Stage; import java.io.IOException; /** * This controller controlls the small message icon at the bottom of the application's main window. * @author Johannes */ public class MessageController { /** * The superior controller */ final MainController mainController; /** * The list of unread messages */ private final SimpleListProperty messages = new SimpleListProperty<>(FXCollections.observableArrayList()); /** * The view that is mantained by this controller */ @FXML private MessageIcon messageIcon; /** * Constructor * @param mainController The controller that creates this instance */ public MessageController(MainController mainController) { this.mainController = mainController; } /** * FXMLLoaders initialize()-method */ @FXML private void initialize() { messageIcon.messageCountProperty().bind(messages.sizeProperty()); } /** * EventHandler for the {@link #messageIcon}. */ @FXML private void onMessageIconClicked() { showMessageList(); } /** * Opens a window that shows all the unread messages. * @see de.uniluebeck.mi.projmi6.controller.MessageListController */ private void showMessageList() { //Load the window content from FXML FXMLLoader fxmlLoader = new FXMLLoader(); fxmlLoader.setLocation(getClass().getClassLoader().getResource("message_list.fxml")); //Non-empty constructor of the controller class MessageListController messageListController = new MessageListController(messages, mainController); fxmlLoader.setControllerFactory(clazz -> messageListController); Parent root = null; try { root = fxmlLoader.load(); } catch (IOException e) { e.printStackTrace(); return; } //Create stage and open window. Stage stage = new Stage(); stage.setTitle("Neue HL7-Nachrichten"); stage.setScene(new Scene(root, 600, 400)); stage.getIcons().add(new Image("icon.png")); stage.initModality(Modality.WINDOW_MODAL); stage.initOwner(messageIcon.getScene().getWindow()); stage.show(); } /** * Adds a message to the list of unread messages that is controlled by this controller * @param message The message that will be added. */ public void addMessage(HL7Message message) { messages.add(message); mainController.getLogController().refreshLogFromDb(); } }