diff --git a/src/main/java/ch/hepia/ui/MainWindowController.java b/src/main/java/ch/hepia/ui/MainWindowController.java index a17417765009736841ba8cd602c7394410f4493d..5d2642d212ef5218db1f54cd844e1e6dbd5fadf8 100644 --- a/src/main/java/ch/hepia/ui/MainWindowController.java +++ b/src/main/java/ch/hepia/ui/MainWindowController.java @@ -291,46 +291,68 @@ public class MainWindowController implements Initializable { } /** - * Create a new journey and broadcast it - * Set the journey label and the current journey - * @param app App context - * @param pnl Panel containing the journey information + * Handle current connection UI reactivity events + * @param app The app context + * @param api The Weather API + * @throws IOException Whenever the Weather API goofs up */ - private void createJourney(AppContext app, Pane pnl) throws IOException { - Integer pos = Integer.parseInt(pnl.getId()); - // setConnectionPanelStyle(pnl, AppConfig.COLOR_GREEN_20_OPACITY); - WeatherAPI api = new WeatherAPI(); - Connection connection = displayedConnections.get(pos); - String wtd = api.getWeatherFrom(connection.getTo().getLocation().getCoordinates()).getConditionsIcon(); + private void setupCurrentConnection(AppContext app, WeatherAPI api) throws IOException { String wtcd = api.getWeatherFrom(currentJourney.getTo().getLocation().getCoordinates()).getConditionsIcon(); - - if (!(currentJourney instanceof Connection.EmptyConnection)){ - leaveJourney(app, wtcd); - } - - JoinedJourney joinedJourney = new JoinedJourney(app.getUser().get(), connection, wtd); - app.getMessageManager().sendJoinedJourney(joinedJourney); - - currentJourney = displayedConnections.get(pos); currentJourneyLabel.setText("Vous voyagez de " + currentJourney.getFrom().getLocation().getName() + " vers " - + currentJourney.getTo().getLocation().getName() + "."); + + currentJourney.getTo().getLocation().getName() + ". Cliquez ici pour quitter cet itinéraire."); currentJourneyLabel.setUnderline(true); currentJourneyLabel.setOnMouseClicked(event -> { + Alert alertQuit = new Alert(Alert.AlertType.CONFIRMATION); + alertQuit.setTitle("Quitter le trajet"); + alertQuit.setHeaderText("Quitter le trajet"); + alertQuit.setContentText("Souhaitez-vous quitter ce trajet ?"); + ButtonType ouiQuit = new ButtonType("Oui"); + ButtonType nonQuit = new ButtonType("Non"); + alertQuit.getButtonTypes().setAll(ouiQuit, nonQuit); + + Optional<ButtonType> resultQuit = alertQuit.showAndWait(); + if (resultQuit.get().equals(ouiQuit)){ + leaveJourney(app, wtcd); + } + }); + } + + /** + * Create a new journey and broadcast it + * Set the journey label and the current journey + * @param app App context + * @param pnl Panel containing the journey information + */ + private void createJourney(AppContext app, Pane pnl) throws IOException, ParseException { + if (currentJourney.equals(new Connection.EmptyConnection())){ Alert alert = new Alert(Alert.AlertType.CONFIRMATION); - alert.setTitle("Quitter le trajet"); - alert.setHeaderText("Quitter le trajet"); - alert.setContentText("Souhaitez-vous quitter ce trajet ?"); + alert.setTitle("Valider le trajet"); + alert.setHeaderText("Valider le trajet"); + alert.setContentText("Souhaitez-vous sélectionner ce trajet ?"); ButtonType oui = new ButtonType("Oui"); ButtonType non = new ButtonType("Non"); alert.getButtonTypes().setAll(oui, non); Optional<ButtonType> result = alert.showAndWait(); if (result.get().equals(oui)){ - leaveJourney(app, wtcd); + Integer pos = Integer.parseInt(pnl.getId()); + WeatherAPI api = new WeatherAPI(); + Connection connection = displayedConnections.get(pos); + String wtd = api.getWeatherFrom(connection.getTo().getLocation().getCoordinates()).getConditionsIcon(); + + JoinedJourney joinedJourney = new JoinedJourney(app.getUser().get(), connection, wtd); + app.getMessageManager().sendJoinedJourney(joinedJourney); + + currentJourney = displayedConnections.get(pos); + setupCurrentConnection(app, api); } - }); + } else { + UiUtils.dialog(Alert.AlertType.WARNING, "Note", "Vous avez un trajet en cours !", + "Veuillez s'il vous plait quitter le trajet actuel avant d'en choisir un autre."); + } } + /** * Parse the line and search for chat command. * @param cmd The line to parse. @@ -513,7 +535,7 @@ public class MainWindowController implements Initializable { Pane pnl = (Pane) e.getSource(); try { createJourney(app, pnl); - } catch (IOException ex){ + } catch (Exception ex){ showSadMessage(AppConfig.ERROR_API_MQ); ex.printStackTrace(); }