diff --git a/src/main/java/ch/hepia/events/ChatMessage.java b/src/main/java/ch/hepia/events/ChatMessage.java index 9c448c7e89dd751a0d4713f9dd60499fcecb86ad..f765e35996885750060f99d553427eb0a3e08521 100644 --- a/src/main/java/ch/hepia/events/ChatMessage.java +++ b/src/main/java/ch/hepia/events/ChatMessage.java @@ -1,9 +1,10 @@ package ch.hepia.events; import ch.hepia.models.User; + import java.io.Serializable; -public class ChatMessage implements Serializable { +public class ChatMessage implements Event, Serializable { private static final long serialVersionUID = 0xAEF34565631L; private User user; private String chatMessage; diff --git a/src/main/java/ch/hepia/events/Event.java b/src/main/java/ch/hepia/events/Event.java new file mode 100644 index 0000000000000000000000000000000000000000..b99571598b38a51b10854c194ef2704aae4baa55 --- /dev/null +++ b/src/main/java/ch/hepia/events/Event.java @@ -0,0 +1,10 @@ +package ch.hepia.events; + +import ch.hepia.models.User; + +/** + * Represents an event send into the MQ. + */ +public interface Event { + public User getUser(); +} diff --git a/src/main/java/ch/hepia/events/JoinedJourney.java b/src/main/java/ch/hepia/events/JoinedJourney.java index 7cf7edebdb95a971bd8940252583239b21bdc97f..72da3b4823cb83c24858df7a47cbae2b053f7ee7 100644 --- a/src/main/java/ch/hepia/events/JoinedJourney.java +++ b/src/main/java/ch/hepia/events/JoinedJourney.java @@ -2,6 +2,7 @@ package ch.hepia.events; import ch.hepia.api.transport.Connection; import ch.hepia.models.User; + import java.io.Serializable; import java.util.Objects; @@ -10,20 +11,22 @@ import java.util.Objects; * journey is composed of several sections (Genève-Vauderens : Genève-Morges is * the first section, Morges-Vauderens is the second.) */ -public class JoinedJourney implements Serializable { +public class JoinedJourney implements Event, Serializable { private static final long serialVersionUID = 0xAEF34565673L; private User user; private Connection connection; + private String weatherToDestination; /** * Main constructor * * @param user The user triggering the event - * @param sections The sections of the journey + * @param connection The sections of the journey */ - public JoinedJourney(User user, Connection connection) { + public JoinedJourney(User user, Connection connection, String weatherToDestination) { this.user = user; this.connection = connection; + this.weatherToDestination = weatherToDestination; } public User getUser() { @@ -34,6 +37,10 @@ public class JoinedJourney implements Serializable { return this.connection; } + public String getWeatherToDestination(){ + return weatherToDestination; + } + @Override public boolean equals(Object obj) { if (this == obj) { diff --git a/src/main/java/ch/hepia/events/LeftJourney.java b/src/main/java/ch/hepia/events/LeftJourney.java index 443bb5a4db117d202ac9323714fd36189202b94b..0ec8de362dd3b8134b0eb97aae75f0637abc6125 100644 --- a/src/main/java/ch/hepia/events/LeftJourney.java +++ b/src/main/java/ch/hepia/events/LeftJourney.java @@ -10,7 +10,7 @@ import java.io.Serializable; * A journey is composed of several sections * (Genève-Vauderens : Genève-Morges is the first section, Morges-Vauderens is the second.) */ -public class LeftJourney implements Serializable { +public class LeftJourney implements Event, Serializable { private static final long serialVersionUID = 0xAEF34565674L; private User user; private Connection connection; @@ -18,7 +18,7 @@ public class LeftJourney implements Serializable { /** * Main constructor * @param user The user triggering the event - * @param sections The sections of the journey + * @param connection The sections of the journey */ public LeftJourney(User user, Connection connection){ this.user = user; diff --git a/src/main/java/ch/hepia/ui/MainWindowController.java b/src/main/java/ch/hepia/ui/MainWindowController.java index 819fddacdaadf247a6ca1e245778c9add036a928..a8a56ecfac241e085e8ea8810899818e722129b0 100644 --- a/src/main/java/ch/hepia/ui/MainWindowController.java +++ b/src/main/java/ch/hepia/ui/MainWindowController.java @@ -1,14 +1,12 @@ package ch.hepia.ui; import ch.hepia.Main; -import ch.hepia.api.transport.Connection; -import ch.hepia.api.transport.Journey; -import ch.hepia.api.transport.LinkAPI; -import ch.hepia.api.transport.Location; -import ch.hepia.api.transport.Section; +import ch.hepia.api.transport.*; +import ch.hepia.api.weather.WeatherAPI; import ch.hepia.config.AppConfig; import ch.hepia.config.AppContext; import ch.hepia.events.ChatMessage; +import ch.hepia.events.Event; import ch.hepia.events.JoinedJourney; import ch.hepia.events.LeftJourney; import ch.hepia.models.User; @@ -36,7 +34,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.ResourceBundle; -import javafx.scene.layout.Pane; +import java.util.function.Predicate; public class MainWindowController implements Initializable { @@ -62,7 +60,7 @@ public class MainWindowController implements Initializable { private Button searchDestinationButton; @FXML - private Button launchItinaryButton; + private Button launchItineraryButton; @FXML private Button sendMessageButton; @@ -192,12 +190,12 @@ public class MainWindowController implements Initializable { p.setBackground( new Background( new BackgroundFill( - new LinearGradient(0, 0, 0, 1, true, + new LinearGradient(0, 0, 0, 1, true, CycleMethod.NO_CYCLE, - new Stop(1, color), + new Stop(1, color), new Stop(0, Color.WHITE) ), - new CornerRadii(5), + new CornerRadii(5), Insets.EMPTY))); p.setBorder(new Border(new BorderStroke( Color.color(0.6, 0.6, 0.6), BorderStrokeStyle.SOLID, new CornerRadii(5), @@ -221,8 +219,8 @@ public class MainWindowController implements Initializable { ), new CornerRadii(5), Insets.EMPTY))); - p.setBorder(new Border(new BorderStroke(Color.color(0.6, 0.6, 0.6), BorderStrokeStyle.SOLID, new CornerRadii(5), - BorderWidths.DEFAULT))); + p.setBorder(new Border(new BorderStroke(Color.color(0.6, 0.6, 0.6), BorderStrokeStyle.SOLID, + new CornerRadii(5), BorderWidths.DEFAULT))); p.setPrefWidth(AppConfig.APP_MAIN_VIEW_WIDTH + 10); p.setPrefHeight(90); } @@ -232,8 +230,7 @@ public class MainWindowController implements Initializable { * @param message Message to print */ private void drawHelpMessage(String message) { - drawMessage(message, AppConfig.HELP_MESSAGE_ICON, - Color.color(1, 0.9, 0.5, 0.3)); + drawMessage(message, AppConfig.HELP_MESSAGE_ICON, Color.color(1, 0.9, 0.5, 0.3)); } /** @@ -241,8 +238,9 @@ public class MainWindowController implements Initializable { */ private void drawCommands(){ StringBuilder buffer = new StringBuilder("/"+AppConfig.CHAT_COMMANDS.get(0)); - for(int i = 1; i < AppConfig.CHAT_COMMANDS.size(); i++) + for(int i = 1; i < AppConfig.CHAT_COMMANDS.size(); i++){ buffer.append(", /").append(AppConfig.CHAT_COMMANDS.get(i)); + } drawHelpMessage(buffer.toString()); } @@ -268,8 +266,8 @@ public class MainWindowController implements Initializable { currentJourneyLabel.setText(AppConfig.DEFAULT_JOURNEY_TEXT); try { currentJourney = new Connection.EmptyConnection(); - showSadMessage("Quelque chose s'est mal passé en voulant quitter votre trajet."); } catch (ParseException e){ + showSadMessage("Quelque chose s'est mal passé en voulant quitter votre trajet."); e.printStackTrace(); } currentJourneyLabel.setUnderline(false); @@ -281,17 +279,21 @@ public class MainWindowController implements Initializable { * @param app App context * @param pnl Panel containing the journey information */ - private void createJourney(AppContext app, Pane pnl) { + private void createJourney(AppContext app, Pane pnl) throws IOException { Integer pos = Integer.parseInt(pnl.getId()); - if (currentJourney instanceof Connection.EmptyConnection){ + WeatherAPI api = new WeatherAPI(); + if (!(currentJourney instanceof Connection.EmptyConnection)){ leaveJourney(app); } - app.getMessageManager().sendJoinedJourney(new JoinedJourney(app.getUser().get(), displayedConnections.get(pos))); + Connection connection = displayedConnections.get(pos); + String weather = api.getWeatherFrom(connection.getFrom().toString()).getConditions(); + JoinedJourney joinedJourney = new JoinedJourney(app.getUser().get(), connection, weather); + app.getMessageManager().sendJoinedJourney(joinedJourney); + currentJourney = displayedConnections.get(pos); currentJourneyLabel.setText("Vous voyagez de " + currentJourney.getFrom().getLocation().getName() + " vers " + currentJourney.getTo().getLocation().getName() + "."); currentJourneyLabel.setUnderline(true); - currentJourneyLabel.setOnMouseClicked(event -> { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); alert.setTitle("Quitter le trajet"); @@ -323,8 +325,7 @@ public class MainWindowController implements Initializable { case "ignore": if (command.length > 1) { Main.getContext().getUser().get().addIgnoredUser(new User(command[1])); - drawMessage(command[1] + " bloqué.", - AppConfig.HELP_MESSAGE_ICON, c); + drawMessage(command[1] + " bloqué.", AppConfig.HELP_MESSAGE_ICON, c); } break; case "ignoredlist": @@ -354,15 +355,15 @@ public class MainWindowController implements Initializable { UiUtils.buttonWhenEnter(destinationComboBox, searchDestinationButton); UiUtils.buttonWhenEnter(messageTextBox, sendMessageButton); - searchOriginButton. - setOnAction(event -> { originComboBox.setValue(originComboBox.getEditor().getText()); - searchStops(originComboBox.getValue(), transportApi, originComboBox); - }); + searchOriginButton.setOnAction(event -> { + originComboBox.setValue(originComboBox.getEditor().getText()); + searchStops(originComboBox.getValue(), transportApi, originComboBox); + }); - searchDestinationButton. - setOnAction(event -> { destinationComboBox.setValue(destinationComboBox.getEditor().getText()); - searchStops(destinationComboBox.getValue(), transportApi, destinationComboBox); - }); + searchDestinationButton.setOnAction(event -> { + destinationComboBox.setValue(destinationComboBox.getEditor().getText()); + searchStops(destinationComboBox.getValue(), transportApi, destinationComboBox); + }); sendMessageButton.setOnAction(event -> { if (app.getUser().isPresent() && !messageTextBox.getText().isEmpty()){ @@ -376,6 +377,47 @@ public class MainWindowController implements Initializable { }); } + /** + * Handle event subscriptions. + * @param app The app context + */ + private void subscribeToEvents(AppContext app){ + Predicate<Event> userFilter = event -> !(app.getUser().get().getIgnoredUserList().contains(event.getUser())); + + // Subscribe to chat message + app.getMessageManager().conditionalSubscribeChatMessage(chatMessage -> Platform.runLater(() -> { + User sender = chatMessage.getUser(); + String message = sender.getName() + ": " + chatMessage.getMessage(); + if (sender.equals(app.getUser().get())) { + drawMessage(message, AppConfig.CHAT_MESSAGE_ICON_SELF, AppConfig.COLOR_BLUE_10_OPACITY); + } else { + drawMessage(message, AppConfig.CHAT_MESSAGE_ICON, AppConfig.COLOR_BLUE_10_OPACITY); + } + }), userFilter::test); + + // Subscribe to joined journey + app.getMessageManager().conditionalSubscribeJoinedJourney(joinedJourney -> Platform.runLater(() -> { + User sender = joinedJourney.getUser(); + String message = sender.getName() + " voyage !"; + if (sender.equals(app.getUser().get())) { + drawMessage(message, AppConfig.CHAT_MESSAGE_ICON_SELF, AppConfig.COLOR_BLUE_10_OPACITY); + } else { + drawMessage(message, AppConfig.CHAT_MESSAGE_ICON, AppConfig.COLOR_BLUE_10_OPACITY); + } + }), userFilter::test); + + // Subscribe to left journey + app.getMessageManager().conditionalSubscribeLeftJourney(leftJourney -> Platform.runLater(() -> { + User sender = leftJourney.getUser(); + String message = sender.getName() + " a terminé son voyage !"; + if (sender.equals(app.getUser().get())) { + drawMessage(message, AppConfig.CHAT_MESSAGE_ICON_SELF, AppConfig.COLOR_BLUE_10_OPACITY); + } else { + drawMessage(message, AppConfig.CHAT_MESSAGE_ICON, AppConfig.COLOR_BLUE_10_OPACITY); + } + }), userFilter::test); + } + /** * Sets the form up * @param url The JavaFX URL handler @@ -383,6 +425,9 @@ public class MainWindowController implements Initializable { */ @Override public void initialize(URL url, ResourceBundle resourceBundle) { + LinkAPI transportApi = new LinkAPI(); + AppContext app = Main.getContext(); + currentJourneyLabel.setText(AppConfig.DEFAULT_JOURNEY_TEXT); startStopLabel.setText(""); // No text should be visible when no journey has been selected. messageTextBox.textProperty().addListener((ov, oldValue, newValue) -> { @@ -391,18 +436,15 @@ public class MainWindowController implements Initializable { } }); - LinkAPI transportApi = new LinkAPI(); - AppContext app = Main.getContext(); initializeTextFieldWithEnterBehaviour(app, transportApi); - try { currentJourney = new Connection.EmptyConnection(); } catch (ParseException e){ - e.printStackTrace(); showSadMessage("Impossible de démarrer proprement l'application."); + e.printStackTrace(); } - launchItinaryButton.setOnAction(event -> { + launchItineraryButton.setOnAction(event -> { try { connectionCanvas.getGraphicsContext2D() .clearRect(0, 0, connectionCanvas.getWidth(), connectionCanvas.getHeight()); @@ -410,6 +452,7 @@ public class MainWindowController implements Initializable { displayedConnections = transportApi.getConnections( originComboBox.getValue(), destinationComboBox.getValue()); startStopLabel.setText(originComboBox.getValue() + " - " + destinationComboBox.getValue()); + for (int i = 0; i < displayedConnections.size(); i++){ // Now iterating over connections drawConnection(displayedConnections.get(i), 30, 100 + 100 * i); @@ -422,7 +465,12 @@ public class MainWindowController implements Initializable { connectionContainer.getChildren().add(pane); pane.setOnMouseClicked(e -> { Pane pnl = (Pane) e.getSource(); - createJourney(app, pnl); + try { + createJourney(app, pnl); + } catch (IOException ex){ + showSadMessage("Une erreur s'est produite lors de la publication de cet évènement."); + ex.printStackTrace(); + } }); }); } @@ -431,40 +479,7 @@ public class MainWindowController implements Initializable { } }); - // Subscribe to chat message - app.getMessageManager().conditionalSubscribeChatMessage(chatMessage -> { - Platform.runLater(() -> { - User sender = chatMessage.getUser(); - String message = sender.getName() + ": " + chatMessage.getMessage(); - if (sender.equals(app.getUser().get())) { - drawMessage(message, AppConfig.CHAT_MESSAGE_ICON_SELF, AppConfig.COLOR_BLUE_10_OPACITY); - } else { - drawMessage(message, AppConfig.CHAT_MESSAGE_ICON, AppConfig.COLOR_BLUE_10_OPACITY); - } - }); - }, chatMessage -> !(app.getUser().get().getIgnoredUserList().contains(chatMessage.getUser()))); - - // Subscribe to joined journey - app.getMessageManager().conditionalSubscribeJoinedJourney(joinedJourney -> Platform.runLater(() -> { - User sender = joinedJourney.getUser(); - String message = sender.getName() + " voyage !"; - if (sender.equals(app.getUser().get())) { - drawMessage(message, AppConfig.CHAT_MESSAGE_ICON_SELF, AppConfig.COLOR_BLUE_10_OPACITY); - } else { - drawMessage(message, AppConfig.CHAT_MESSAGE_ICON, AppConfig.COLOR_BLUE_10_OPACITY); - } - }), joinedJourney -> !(app.getUser().get().getIgnoredUserList().contains(joinedJourney.getUser()))); - - // Subscribe to left journey - app.getMessageManager().conditionalSubscribeLeftJourney(leftJourney -> Platform.runLater(() -> { - User sender = leftJourney.getUser(); - String message = sender.getName() + " a terminé son voyage !"; - if (sender.equals(app.getUser().get())) { - drawMessage(message, AppConfig.CHAT_MESSAGE_ICON_SELF, AppConfig.COLOR_BLUE_10_OPACITY); - } else { - drawMessage(message, AppConfig.CHAT_MESSAGE_ICON, AppConfig.COLOR_BLUE_10_OPACITY); - } - }), leftJourney -> !(app.getUser().get().getIgnoredUserList().contains(leftJourney.getUser()))); + subscribeToEvents(app); // Login messages: drawCommands(); diff --git a/src/main/resources/fxml/MainWindow.fxml b/src/main/resources/fxml/MainWindow.fxml index bb483de30b6746b53187be5d4855c290c815550a..51f520a6b6813acd41db75cf9bfa0bda747c15a5 100644 --- a/src/main/resources/fxml/MainWindow.fxml +++ b/src/main/resources/fxml/MainWindow.fxml @@ -1,14 +1,11 @@ <?xml version="1.0" encoding="UTF-8"?> -<?import javafx.scene.canvas.*?> -<?import javafx.scene.text.*?> -<?import javafx.scene.paint.*?> -<?import javafx.scene.shape.*?> +<?import javafx.scene.canvas.Canvas?> <?import javafx.scene.control.*?> -<?import java.lang.*?> <?import javafx.scene.layout.*?> -<?import javafx.collections.FXCollections?> - +<?import javafx.scene.paint.*?> +<?import javafx.scene.shape.Line?> +<?import javafx.scene.text.Font?> <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="565.0" prefWidth="1000.0" style="-fx-background-color: #fafafa;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.hepia.ui.MainWindowController"> <top> <Pane maxHeight="48.0" maxWidth="1000.0" minHeight="48.0" minWidth="32.0" prefHeight="48.0" prefWidth="32.0" style="-fx-background-color: #35B1EE;" BorderPane.alignment="CENTER"> @@ -37,7 +34,7 @@ </Line> <Button id="searchOriginButton" fx:id="searchOriginButton" layoutX="293.0" layoutY="10.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="107.0" text="Chercher" /> <Button fx:id="searchDestinationButton" layoutX="711.0" layoutY="9.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="107.0" text="Chercher" /> - <Button id="launchItinaryButton" fx:id="launchItinaryButton" layoutX="836.0" layoutY="9.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="156.0" text="Itinéraire..." /> + <Button id="launchItinaryButton" fx:id="launchItineraryButton" layoutX="836.0" layoutY="9.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="156.0" text="Itinéraire..." /> </children> </Pane> </top>