diff --git a/src/main/java/ch/hepia/api/transport/Connection.java b/src/main/java/ch/hepia/api/transport/Connection.java index 5b4c9a798efce7f40cd12239a8bcfd91975b95fe..89dc99b8e029172a9383955b7cb86bc43102feb9 100644 --- a/src/main/java/ch/hepia/api/transport/Connection.java +++ b/src/main/java/ch/hepia/api/transport/Connection.java @@ -71,6 +71,13 @@ public class Connection implements Serializable { return new ArrayList<>(sections); } + public Stop getFrom(){ + return this.from; + } + + public Stop getTo(){ + return this.to; + } /** * Builder of Connection object diff --git a/src/main/java/ch/hepia/ui/MainWindowController.java b/src/main/java/ch/hepia/ui/MainWindowController.java index 49ff72a336a693f0d7eb4aaa5633b302ab706de4..04fbe5b361f755cda65b1826060f9919ff4e5b30 100644 --- a/src/main/java/ch/hepia/ui/MainWindowController.java +++ b/src/main/java/ch/hepia/ui/MainWindowController.java @@ -75,6 +75,8 @@ public class MainWindowController implements Initializable { private List<Connection> currentConnections; + private Connection currentJourney; + /** * Shows a sad message when the API crashes. */ @@ -252,6 +254,19 @@ public class MainWindowController implements Initializable { } } + /** + * Create a new journey and broadcast it + * @param app App context + * @param pnl Panel containing the journey information + */ + private void createJourney(AppContext app, Pane pnl) { + Integer pos = Integer.parseInt(pnl.getId()); + app.getMessageManager().sendJoinedJourney(new JoinedJourney(app.getUser().get(), currentConnections.get(pos))); + currentJourney = currentConnections.get(pos); + currentJourneyLabel.setText("Vous voyagez de " + currentJourney.getFrom().getLocation().getName() + " vers " + + currentJourney.getTo().getLocation().getName() + "."); + } + /** * Parse the line and search for chat command. * @param cmd The line to parse. @@ -356,36 +371,31 @@ public class MainWindowController implements Initializable { Platform.runLater(() -> { connectionContainer.getChildren().add(pane); pane.setOnMouseClicked(e -> { - Pane pnl = (Pane) e.getSource(); - Integer pos = Integer.parseInt(pnl.getId()); - app.getMessageManager().sendJoinedJourney(new JoinedJourney(app.getUser().get(), currentConnections.get(pos))); + Pane pnl = (Pane) e.getSource(); + createJourney(app, pnl); }); - }); + }); } } catch (IOException e) { showSadMessage(AppConfig.ERROR_API_UNREACHABLE); } }); - // 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())) - ); + 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(() -> { + app.getMessageManager().conditionalSubscribeJoinedJourney(joinedJourney -> Platform.runLater(() -> { User sender = joinedJourney.getUser(); String message = sender.getName() + " voyage !"; if (sender.equals(app.getUser().get())) { @@ -393,11 +403,10 @@ public class MainWindowController implements Initializable { } else { drawMessage(message, AppConfig.CHAT_MESSAGE_ICON, AppConfig.COLOR_BLUE_10_OPACITY); } - }), - joinedJourney -> !(app.getUser().get().getIgnoredUserList().contains(joinedJourney.getUser()))); + }), joinedJourney -> !(app.getUser().get().getIgnoredUserList().contains(joinedJourney.getUser()))); // Subscribe to left journey - app.getMessageManager().conditionalSubscribeLeftJourney( leftJourney -> Platform.runLater(() -> { + app.getMessageManager().conditionalSubscribeLeftJourney(leftJourney -> Platform.runLater(() -> { User sender = leftJourney.getUser(); String message = sender.getName() + " a terminé son voyage !"; if (sender.equals(app.getUser().get())) { @@ -405,10 +414,9 @@ public class MainWindowController implements Initializable { } else { drawMessage(message, AppConfig.CHAT_MESSAGE_ICON, AppConfig.COLOR_BLUE_10_OPACITY); } - }), - leftJourney -> !(app.getUser().get().getIgnoredUserList().contains(leftJourney.getUser()))); + }), leftJourney -> !(app.getUser().get().getIgnoredUserList().contains(leftJourney.getUser()))); - //Login messages: + // Login messages: drawCommands(); Platform.runLater(() -> { drawHelpMessage("Bonjour " + app.getUser().get().getName() + " ! Les commandes à dispositions sont :");