diff --git a/src/main/java/ch/hepia/ui/MainWindowController.java b/src/main/java/ch/hepia/ui/MainWindowController.java index 8dfdd6080d77fddb55d36639968d53e161ef9ed0..6f94addc3ffe3c5bf344ef16356298b1e2bee1dc 100644 --- a/src/main/java/ch/hepia/ui/MainWindowController.java +++ b/src/main/java/ch/hepia/ui/MainWindowController.java @@ -152,7 +152,7 @@ public class MainWindowController implements Initializable { * Draws a newly received message * @param message The message to draw */ - private void drawMessage(User user, String message, String image, Color color){ + private void drawMessage(String message, String image, Color color){ Pane p = new Pane(); setChatPanelStyle(p, color); Image lblImg = new Image(Main.class.getResourceAsStream(image)); @@ -186,7 +186,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), + p.setBorder(new Border(new BorderStroke( + Color.color(0.6, 0.6, 0.6), BorderStrokeStyle.SOLID, new CornerRadii(5), BorderWidths.DEFAULT))); p.setPrefWidth(312); p.setMaxWidth(320); @@ -197,7 +198,8 @@ public class MainWindowController implements Initializable { * @param message Message to print */ private void drawHelpMessage(String message) { - drawMessage(Main.getContext().getUser().get(), 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)); } /** @@ -206,7 +208,7 @@ 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++) - buffer.append(", /" + AppConfig.CHAT_COMMANDS.get(i)); + buffer.append(", /").append(AppConfig.CHAT_COMMANDS.get(i)); drawHelpMessage(buffer.toString()); } @@ -229,26 +231,66 @@ public class MainWindowController implements Initializable { */ private void parseChatCommand(String cmd){ String[] command = cmd.split(" ", 2); + Color c = Color.color(1, 0.9, 0.5, 0.3); if (AppConfig.CHAT_COMMANDS.contains(command[0])){ - if (command[0].equals("help")) { - drawCommands(); - } else if (command[0].equals("ignore")){ - if (command.length > 1){ - Main.getContext().getUser().get().addIgnoredUser(new User(command[1])); - drawMessage(Main.getContext().getUser().get(), command[1] + " bloqué.", "/img/help.png", Color.color(1, 0.9, 0.5, 0.3)); - } - } else if (command[0].equals("ignoredlist")) { + switch (command[0]) { + case "help": + drawCommands(); + break; + case "ignore": + if (command.length > 1) { + Main.getContext().getUser().get().addIgnoredUser(new User(command[1])); + drawMessage(command[1] + " bloqué.", + "/img/help.png", c); + } + break; + case "ignoredlist": List<User> list = Main.getContext().getUser().get().getIgnoredUserList(); StringBuilder buffer = new StringBuilder(list.get(0).getName()); - for(int i = 1; i < list.size(); i++) - buffer.append(", " + list.get(i).getName()); - drawMessage(Main.getContext().getUser().get(), "Bloqués : " + buffer.toString(), "/img/help.png", Color.color(1, 0.9, 0.5, 0.3)); + for (int i = 1; i < list.size(); i++){ + buffer.append(", ").append(list.get(i).getName()); + } + drawMessage("Bloqués : " + buffer.toString(), + "/img/help.png", c); + break; } } else { drawCommands(); } } + /** + * Initializes the event handler for every textbox/combofield that has a "press enter" behaviour. + * @param app The app context + * @param transportApi The transport API + */ + private void initializeTextFieldWithEnterBehaviour(AppContext app, LinkAPI transportApi){ + UiUtils.buttonWhenEnter(originComboBox, searchOriginButton); + UiUtils.buttonWhenEnter(destinationComboBox, searchDestinationButton); + UiUtils.buttonWhenEnter(messageTextBox, sendMessageButton); + + 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); + }); + + sendMessageButton.setOnAction(event -> { + if (app.getUser().isPresent() && !messageTextBox.getText().isEmpty()){ + if ( messageTextBox.getText().charAt(0) != '/') { + app.getMessageManager().sendChatMessage(new ChatMessage(app.getUser().get(), messageTextBox.getText())); + } else { + parseChatCommand(messageTextBox.getText().substring(1)); + } + messageTextBox.clear(); + } + }); + } + /** * Sets the form up * @param url The JavaFX URL handler @@ -266,19 +308,7 @@ public class MainWindowController implements Initializable { LinkAPI transportApi = new LinkAPI(); AppContext app = Main.getContext(); - UiUtils.buttonWhenEnter(originComboBox, searchOriginButton); - UiUtils.buttonWhenEnter(destinationComboBox, searchDestinationButton); - UiUtils.buttonWhenEnter(messageTextBox, sendMessageButton); - - 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); - }); + initializeTextFieldWithEnterBehaviour(app, transportApi); launchItinaryButton.setOnAction(event -> { try { @@ -297,16 +327,7 @@ public class MainWindowController implements Initializable { } }); - sendMessageButton.setOnAction(event -> { - if (app.getUser().isPresent() && !messageTextBox.getText().isEmpty()){ - if ( messageTextBox.getText().charAt(0) != '/') { - app.getMessageManager().sendChatMessage(new ChatMessage(app.getUser().get(), messageTextBox.getText())); - } else { - parseChatCommand(messageTextBox.getText().substring(1, messageTextBox.getText().length())); - } - messageTextBox.clear(); - } - }); + // Subscribe to chat message app.getMessageManager().conditionalSubscribeChatMessage( chatMessage -> { @@ -314,9 +335,9 @@ public class MainWindowController implements Initializable { User sender = chatMessage.getUser(); String message = sender.getName() + ": " + chatMessage.getMessage(); if (sender.equals(app.getUser().get())) { - drawMessage(sender, message, AppConfig.CHAT_MESSAGE_ICON_SELF, AppConfig.COLOR_BLUE_10_OPACITY); + drawMessage(message, AppConfig.CHAT_MESSAGE_ICON_SELF, AppConfig.COLOR_BLUE_10_OPACITY); } else { - drawMessage(sender, message, AppConfig.CHAT_MESSAGE_ICON, AppConfig.COLOR_BLUE_10_OPACITY); + drawMessage(message, AppConfig.CHAT_MESSAGE_ICON, AppConfig.COLOR_BLUE_10_OPACITY); } }); }, @@ -324,34 +345,28 @@ public class MainWindowController implements Initializable { ); // 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(sender, message, AppConfig.CHAT_MESSAGE_ICON_SELF, AppConfig.COLOR_BLUE_10_OPACITY); - } else { - drawMessage(sender, message, AppConfig.CHAT_MESSAGE_ICON, AppConfig.COLOR_BLUE_10_OPACITY); - } - }); - }, - joinedJourney -> !(app.getUser().get().getIgnoredUserList().contains(joinedJourney.getUser())) - ); + 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(sender, message, AppConfig.CHAT_MESSAGE_ICON_SELF, AppConfig.COLOR_BLUE_10_OPACITY); - } else { - drawMessage(sender, message, AppConfig.CHAT_MESSAGE_ICON, AppConfig.COLOR_BLUE_10_OPACITY); - } - }); - }, - leftJourney -> !(app.getUser().get().getIgnoredUserList().contains(leftJourney.getUser())) - ); + 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: drawCommands();