Skip to content
Snippets Groups Projects
Verified Commit 0fee8c79 authored by Théo Pirkl's avatar Théo Pirkl :nail_care:
Browse files

Mostly refactoring

parent 22d875b0
No related branches found
No related tags found
1 merge request!21*poof* final version
package ch.hepia.events; package ch.hepia.events;
import ch.hepia.models.User; import ch.hepia.models.User;
import java.io.Serializable; import java.io.Serializable;
public class ChatMessage implements Serializable { public class ChatMessage implements Event, Serializable {
private static final long serialVersionUID = 0xAEF34565631L; private static final long serialVersionUID = 0xAEF34565631L;
private User user; private User user;
private String chatMessage; private String chatMessage;
......
package ch.hepia.events;
import ch.hepia.models.User;
/**
* Represents an event send into the MQ.
*/
public interface Event {
public User getUser();
}
...@@ -2,6 +2,7 @@ package ch.hepia.events; ...@@ -2,6 +2,7 @@ package ch.hepia.events;
import ch.hepia.api.transport.Connection; import ch.hepia.api.transport.Connection;
import ch.hepia.models.User; import ch.hepia.models.User;
import java.io.Serializable; import java.io.Serializable;
import java.util.Objects; import java.util.Objects;
...@@ -10,20 +11,22 @@ 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 * journey is composed of several sections (Genève-Vauderens : Genève-Morges is
* the first section, Morges-Vauderens is the second.) * 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 static final long serialVersionUID = 0xAEF34565673L;
private User user; private User user;
private Connection connection; private Connection connection;
private String weatherToDestination;
/** /**
* Main constructor * Main constructor
* *
* @param user The user triggering the event * @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.user = user;
this.connection = connection; this.connection = connection;
this.weatherToDestination = weatherToDestination;
} }
public User getUser() { public User getUser() {
...@@ -34,6 +37,10 @@ public class JoinedJourney implements Serializable { ...@@ -34,6 +37,10 @@ public class JoinedJourney implements Serializable {
return this.connection; return this.connection;
} }
public String getWeatherToDestination(){
return weatherToDestination;
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) { if (this == obj) {
......
...@@ -10,7 +10,7 @@ import java.io.Serializable; ...@@ -10,7 +10,7 @@ import java.io.Serializable;
* A journey is composed of several sections * A journey is composed of several sections
* (Genève-Vauderens : Genève-Morges is the first section, Morges-Vauderens is the second.) * (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 static final long serialVersionUID = 0xAEF34565674L;
private User user; private User user;
private Connection connection; private Connection connection;
...@@ -18,7 +18,7 @@ public class LeftJourney implements Serializable { ...@@ -18,7 +18,7 @@ public class LeftJourney implements Serializable {
/** /**
* Main constructor * Main constructor
* @param user The user triggering the event * @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){ public LeftJourney(User user, Connection connection){
this.user = user; this.user = user;
......
package ch.hepia.ui; package ch.hepia.ui;
import ch.hepia.Main; import ch.hepia.Main;
import ch.hepia.api.transport.Connection; import ch.hepia.api.transport.*;
import ch.hepia.api.transport.Journey; import ch.hepia.api.weather.WeatherAPI;
import ch.hepia.api.transport.LinkAPI;
import ch.hepia.api.transport.Location;
import ch.hepia.api.transport.Section;
import ch.hepia.config.AppConfig; import ch.hepia.config.AppConfig;
import ch.hepia.config.AppContext; import ch.hepia.config.AppContext;
import ch.hepia.events.ChatMessage; import ch.hepia.events.ChatMessage;
import ch.hepia.events.Event;
import ch.hepia.events.JoinedJourney; import ch.hepia.events.JoinedJourney;
import ch.hepia.events.LeftJourney; import ch.hepia.events.LeftJourney;
import ch.hepia.models.User; import ch.hepia.models.User;
...@@ -36,7 +34,7 @@ import java.util.ArrayList; ...@@ -36,7 +34,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import javafx.scene.layout.Pane; import java.util.function.Predicate;
public class MainWindowController implements Initializable { public class MainWindowController implements Initializable {
...@@ -62,7 +60,7 @@ public class MainWindowController implements Initializable { ...@@ -62,7 +60,7 @@ public class MainWindowController implements Initializable {
private Button searchDestinationButton; private Button searchDestinationButton;
@FXML @FXML
private Button launchItinaryButton; private Button launchItineraryButton;
@FXML @FXML
private Button sendMessageButton; private Button sendMessageButton;
...@@ -221,8 +219,8 @@ public class MainWindowController implements Initializable { ...@@ -221,8 +219,8 @@ public class MainWindowController implements Initializable {
), ),
new CornerRadii(5), new CornerRadii(5),
Insets.EMPTY))); Insets.EMPTY)));
p.setBorder(new Border(new BorderStroke(Color.color(0.6, 0.6, 0.6), BorderStrokeStyle.SOLID, new CornerRadii(5), p.setBorder(new Border(new BorderStroke(Color.color(0.6, 0.6, 0.6), BorderStrokeStyle.SOLID,
BorderWidths.DEFAULT))); new CornerRadii(5), BorderWidths.DEFAULT)));
p.setPrefWidth(AppConfig.APP_MAIN_VIEW_WIDTH + 10); p.setPrefWidth(AppConfig.APP_MAIN_VIEW_WIDTH + 10);
p.setPrefHeight(90); p.setPrefHeight(90);
} }
...@@ -232,8 +230,7 @@ public class MainWindowController implements Initializable { ...@@ -232,8 +230,7 @@ public class MainWindowController implements Initializable {
* @param message Message to print * @param message Message to print
*/ */
private void drawHelpMessage(String message) { private void drawHelpMessage(String message) {
drawMessage(message, AppConfig.HELP_MESSAGE_ICON, drawMessage(message, AppConfig.HELP_MESSAGE_ICON, Color.color(1, 0.9, 0.5, 0.3));
Color.color(1, 0.9, 0.5, 0.3));
} }
/** /**
...@@ -241,8 +238,9 @@ public class MainWindowController implements Initializable { ...@@ -241,8 +238,9 @@ public class MainWindowController implements Initializable {
*/ */
private void drawCommands(){ private void drawCommands(){
StringBuilder buffer = new StringBuilder("/"+AppConfig.CHAT_COMMANDS.get(0)); 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)); buffer.append(", /").append(AppConfig.CHAT_COMMANDS.get(i));
}
drawHelpMessage(buffer.toString()); drawHelpMessage(buffer.toString());
} }
...@@ -268,8 +266,8 @@ public class MainWindowController implements Initializable { ...@@ -268,8 +266,8 @@ public class MainWindowController implements Initializable {
currentJourneyLabel.setText(AppConfig.DEFAULT_JOURNEY_TEXT); currentJourneyLabel.setText(AppConfig.DEFAULT_JOURNEY_TEXT);
try { try {
currentJourney = new Connection.EmptyConnection(); currentJourney = new Connection.EmptyConnection();
showSadMessage("Quelque chose s'est mal passé en voulant quitter votre trajet.");
} catch (ParseException e){ } catch (ParseException e){
showSadMessage("Quelque chose s'est mal passé en voulant quitter votre trajet.");
e.printStackTrace(); e.printStackTrace();
} }
currentJourneyLabel.setUnderline(false); currentJourneyLabel.setUnderline(false);
...@@ -281,17 +279,21 @@ public class MainWindowController implements Initializable { ...@@ -281,17 +279,21 @@ public class MainWindowController implements Initializable {
* @param app App context * @param app App context
* @param pnl Panel containing the journey information * @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()); Integer pos = Integer.parseInt(pnl.getId());
if (currentJourney instanceof Connection.EmptyConnection){ WeatherAPI api = new WeatherAPI();
if (!(currentJourney instanceof Connection.EmptyConnection)){
leaveJourney(app); 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); currentJourney = displayedConnections.get(pos);
currentJourneyLabel.setText("Vous voyagez de " + currentJourney.getFrom().getLocation().getName() + " vers " currentJourneyLabel.setText("Vous voyagez de " + currentJourney.getFrom().getLocation().getName() + " vers "
+ currentJourney.getTo().getLocation().getName() + "."); + currentJourney.getTo().getLocation().getName() + ".");
currentJourneyLabel.setUnderline(true); currentJourneyLabel.setUnderline(true);
currentJourneyLabel.setOnMouseClicked(event -> { currentJourneyLabel.setOnMouseClicked(event -> {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION); Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Quitter le trajet"); alert.setTitle("Quitter le trajet");
...@@ -323,8 +325,7 @@ public class MainWindowController implements Initializable { ...@@ -323,8 +325,7 @@ public class MainWindowController implements Initializable {
case "ignore": case "ignore":
if (command.length > 1) { if (command.length > 1) {
Main.getContext().getUser().get().addIgnoredUser(new User(command[1])); Main.getContext().getUser().get().addIgnoredUser(new User(command[1]));
drawMessage(command[1] + " bloqué.", drawMessage(command[1] + " bloqué.", AppConfig.HELP_MESSAGE_ICON, c);
AppConfig.HELP_MESSAGE_ICON, c);
} }
break; break;
case "ignoredlist": case "ignoredlist":
...@@ -354,13 +355,13 @@ public class MainWindowController implements Initializable { ...@@ -354,13 +355,13 @@ public class MainWindowController implements Initializable {
UiUtils.buttonWhenEnter(destinationComboBox, searchDestinationButton); UiUtils.buttonWhenEnter(destinationComboBox, searchDestinationButton);
UiUtils.buttonWhenEnter(messageTextBox, sendMessageButton); UiUtils.buttonWhenEnter(messageTextBox, sendMessageButton);
searchOriginButton. searchOriginButton.setOnAction(event -> {
setOnAction(event -> { originComboBox.setValue(originComboBox.getEditor().getText()); originComboBox.setValue(originComboBox.getEditor().getText());
searchStops(originComboBox.getValue(), transportApi, originComboBox); searchStops(originComboBox.getValue(), transportApi, originComboBox);
}); });
searchDestinationButton. searchDestinationButton.setOnAction(event -> {
setOnAction(event -> { destinationComboBox.setValue(destinationComboBox.getEditor().getText()); destinationComboBox.setValue(destinationComboBox.getEditor().getText());
searchStops(destinationComboBox.getValue(), transportApi, destinationComboBox); searchStops(destinationComboBox.getValue(), transportApi, destinationComboBox);
}); });
...@@ -376,6 +377,47 @@ public class MainWindowController implements Initializable { ...@@ -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 * Sets the form up
* @param url The JavaFX URL handler * @param url The JavaFX URL handler
...@@ -383,6 +425,9 @@ public class MainWindowController implements Initializable { ...@@ -383,6 +425,9 @@ public class MainWindowController implements Initializable {
*/ */
@Override @Override
public void initialize(URL url, ResourceBundle resourceBundle) { public void initialize(URL url, ResourceBundle resourceBundle) {
LinkAPI transportApi = new LinkAPI();
AppContext app = Main.getContext();
currentJourneyLabel.setText(AppConfig.DEFAULT_JOURNEY_TEXT); currentJourneyLabel.setText(AppConfig.DEFAULT_JOURNEY_TEXT);
startStopLabel.setText(""); // No text should be visible when no journey has been selected. startStopLabel.setText(""); // No text should be visible when no journey has been selected.
messageTextBox.textProperty().addListener((ov, oldValue, newValue) -> { messageTextBox.textProperty().addListener((ov, oldValue, newValue) -> {
...@@ -391,18 +436,15 @@ public class MainWindowController implements Initializable { ...@@ -391,18 +436,15 @@ public class MainWindowController implements Initializable {
} }
}); });
LinkAPI transportApi = new LinkAPI();
AppContext app = Main.getContext();
initializeTextFieldWithEnterBehaviour(app, transportApi); initializeTextFieldWithEnterBehaviour(app, transportApi);
try { try {
currentJourney = new Connection.EmptyConnection(); currentJourney = new Connection.EmptyConnection();
} catch (ParseException e){ } catch (ParseException e){
e.printStackTrace();
showSadMessage("Impossible de démarrer proprement l'application."); showSadMessage("Impossible de démarrer proprement l'application.");
e.printStackTrace();
} }
launchItinaryButton.setOnAction(event -> { launchItineraryButton.setOnAction(event -> {
try { try {
connectionCanvas.getGraphicsContext2D() connectionCanvas.getGraphicsContext2D()
.clearRect(0, 0, connectionCanvas.getWidth(), connectionCanvas.getHeight()); .clearRect(0, 0, connectionCanvas.getWidth(), connectionCanvas.getHeight());
...@@ -410,6 +452,7 @@ public class MainWindowController implements Initializable { ...@@ -410,6 +452,7 @@ public class MainWindowController implements Initializable {
displayedConnections = transportApi.getConnections( displayedConnections = transportApi.getConnections(
originComboBox.getValue(), destinationComboBox.getValue()); originComboBox.getValue(), destinationComboBox.getValue());
startStopLabel.setText(originComboBox.getValue() + " - " + destinationComboBox.getValue()); startStopLabel.setText(originComboBox.getValue() + " - " + destinationComboBox.getValue());
for (int i = 0; i < displayedConnections.size(); i++){ for (int i = 0; i < displayedConnections.size(); i++){
// Now iterating over connections // Now iterating over connections
drawConnection(displayedConnections.get(i), 30, 100 + 100 * i); drawConnection(displayedConnections.get(i), 30, 100 + 100 * i);
...@@ -422,7 +465,12 @@ public class MainWindowController implements Initializable { ...@@ -422,7 +465,12 @@ public class MainWindowController implements Initializable {
connectionContainer.getChildren().add(pane); connectionContainer.getChildren().add(pane);
pane.setOnMouseClicked(e -> { pane.setOnMouseClicked(e -> {
Pane pnl = (Pane) e.getSource(); Pane pnl = (Pane) e.getSource();
try {
createJourney(app, pnl); 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 { ...@@ -431,40 +479,7 @@ public class MainWindowController implements Initializable {
} }
}); });
// Subscribe to chat message subscribeToEvents(app);
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())));
// Login messages: // Login messages:
drawCommands(); drawCommands();
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.*?> <?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.text.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.control.*?> <?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?> <?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"> <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> <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"> <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 @@ ...@@ -37,7 +34,7 @@
</Line> </Line>
<Button id="searchOriginButton" fx:id="searchOriginButton" layoutX="293.0" layoutY="10.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="107.0" text="Chercher" /> <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 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> </children>
</Pane> </Pane>
</top> </top>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment