diff --git a/chat/src/main/java/rtgre/chat/ChatController.java b/chat/src/main/java/rtgre/chat/ChatController.java index 7a14321..8fd7bda 100644 --- a/chat/src/main/java/rtgre/chat/ChatController.java +++ b/chat/src/main/java/rtgre/chat/ChatController.java @@ -204,7 +204,8 @@ public class ChatController implements Initializable { * @param e L'évènement associé à un clic droit sur la PostListView */ private void handleContextMenu(ContextMenuEvent e) { - if (postListView.getSelectionModel().getSelectedItem().getFrom().equals(contact.getLogin())) { + Post post = postListView.getSelectionModel().getSelectedItem(); + if (post.getFrom().equals(contact.getLogin()) && post.isEditable()) { contextMenu.show(postListView, e.getScreenX(), e.getScreenY()); } } @@ -242,14 +243,18 @@ public class ChatController implements Initializable { String from = postListView.getSelectionModel().getSelectedItem().getFrom(); String to = postListView.getSelectionModel().getSelectedItem().getTo(); try { - ModifyMessageController controller = showNewStage(i18nBundle.getString("messageEdit"), "modifymessage-view.fxml"); - - Post post = new Post(postUUID, timestamp, from, to, controller.hostTextField.getText()); - client.sendPostEvent(post); - postVector.remove(postListView.getSelectionModel().getSelectedItem()); - postsObservableList.remove(postListView.getSelectionModel().getSelectedItem()); - postListView.refresh(); - + ModifyMessageController controller = showModifyMessageStage( + i18nBundle.getString("messageEdit"), + "modifymessage-view.fxml", + postListView.getSelectionModel().getSelectedItem().getBody() + ); + if (controller.isOk()) { + Post post = new Post(postUUID, timestamp, from, to, controller.hostTextField.getText()); + client.sendPostEvent(post); + postVector.remove(postListView.getSelectionModel().getSelectedItem()); + postsObservableList.remove(postListView.getSelectionModel().getSelectedItem()); + postListView.refresh(); + } } catch (IOException e) { LOGGER.warning("Can't open modify message view!"); } @@ -266,7 +271,9 @@ public class ChatController implements Initializable { long timestamp = postListView.getSelectionModel().getSelectedItem().getTimestamp(); String from = postListView.getSelectionModel().getSelectedItem().getFrom(); String to = postListView.getSelectionModel().getSelectedItem().getTo(); - client.sendPostEvent(new Post(postUUID, timestamp, from, to, "Ce message a été supprimé.")); + Post post = new Post(postUUID, timestamp, from, to, "Ce message a été supprimé."); + post.setEditable(false); + client.sendPostEvent(post); postVector.remove(postListView.getSelectionModel().getSelectedItem()); postsObservableList.remove(postListView.getSelectionModel().getSelectedItem()); postListView.refresh(); @@ -312,6 +319,31 @@ public class ChatController implements Initializable { return fxmlLoader.getController(); } + + /** + * Ouvre une fenêtre de modification de message et attend qu'elle se ferme. + * + * @param title titre de la fenêtre + * @param fxmlFileName nom du fichier FXML décrivant l'interface graphique + * @param parameter paramètre éventuel au format textuel + * @return l'objet contrôleur associé à la fenêtre + * @throws IOException si le fichier FXML n'est pas trouvé dans les ressources + */ + public T showModifyMessageStage(String title, String fxmlFileName, String parameter) throws IOException { + FXMLLoader fxmlLoader = new FXMLLoader(ChatApplication.class.getResource(fxmlFileName), i18nBundle); + Scene scene = new Scene(fxmlLoader.load()); + Stage stage = new Stage(); + ModifyMessageController controller = fxmlLoader.getController(); + controller.setOldBody(parameter); + stage.initModality(Modality.APPLICATION_MODAL); + stage.setTitle(title); + stage.setResizable(false); + stage.setScene(scene); + stage.showAndWait(); + return fxmlLoader.getController(); + } + + /** * Initialise RoomListView avec sa CelFactory et sa liste observable */ diff --git a/chat/src/main/java/rtgre/chat/ModifyMessageController.java b/chat/src/main/java/rtgre/chat/ModifyMessageController.java index d1e1a76..97ba39e 100644 --- a/chat/src/main/java/rtgre/chat/ModifyMessageController.java +++ b/chat/src/main/java/rtgre/chat/ModifyMessageController.java @@ -1,5 +1,6 @@ package rtgre.chat; +import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.fxml.Initializable; import javafx.scene.control.Button; @@ -22,7 +23,24 @@ public class ModifyMessageController implements Initializable { public Button submitButton; /** Si la vue possède une valeur de retour */ private Boolean ok; + /** Valeur de l'ancien post */ + private String oldBody = ""; + /** + * Getter de oldBody + * @return Renvoie le contenu de l'ancien Post + */ + public String getOldBody() { + return oldBody; + } + + /** + * Setter de oldBody + * @param oldBody le contenu de l'ancien post + */ + public void setOldBody(String oldBody) { + this.oldBody = oldBody; + } /** * Initialisation du composant graphique @@ -34,6 +52,7 @@ public class ModifyMessageController implements Initializable { resetButton.setOnAction(this::onActionReset); submitButton.setOnAction(this::onActionSubmit); hostTextField.setOnAction(this::onActionSubmit); + Platform.runLater(() -> hostTextField.setText(oldBody)); } /** @@ -42,7 +61,7 @@ public class ModifyMessageController implements Initializable { * @param actionEvent L'évènement associé au clic sur le bouton Reset */ private void onActionReset(ActionEvent actionEvent) { - hostTextField.setText(""); + hostTextField.setText(oldBody); } /** diff --git a/chat/src/main/java/rtgre/chat/graphisme/PostListViewCell.java b/chat/src/main/java/rtgre/chat/graphisme/PostListViewCell.java index 5dd0b81..fbe11ed 100644 --- a/chat/src/main/java/rtgre/chat/graphisme/PostListViewCell.java +++ b/chat/src/main/java/rtgre/chat/graphisme/PostListViewCell.java @@ -97,6 +97,9 @@ public class PostListViewCell extends ListCell { tf.setBackground(Background.fill(Color.web("#FEE"))); hBox.setAlignment(Pos.CENTER_LEFT); } + if (!post.isEditable()) { + tf.setBackground(Background.fill(Color.web("#808080"))); + } setGraphic(hBox); getListView().scrollTo(getListView().getItems().size() - 1); } diff --git a/chat/src/main/java/rtgre/modeles/DatabaseApi.java b/chat/src/main/java/rtgre/modeles/DatabaseApi.java index 60b8e2c..f127108 100644 --- a/chat/src/main/java/rtgre/modeles/DatabaseApi.java +++ b/chat/src/main/java/rtgre/modeles/DatabaseApi.java @@ -43,7 +43,8 @@ public class DatabaseApi { + " `timestamp` long," + " `from` text," + " `to` text," - + " `body` text" + + " `body` text," + + " `connected` boolean" + ");"; stmt = con.createStatement(); stmt.execute(sql); @@ -105,7 +106,7 @@ public class DatabaseApi { * @return `true` si le post a bien été ajouté, `false` si une erreur est survenue */ public boolean addPost(Post post) { - String query = "INSERT INTO posts VALUES (?, ?, ?, ?, ?)"; + String query = "INSERT INTO posts VALUES (?, ?, ?, ?, ?, ?)"; try { PreparedStatement pstmt = con.prepareStatement(query); pstmt.setString(1, post.getId().toString()); @@ -113,6 +114,7 @@ public class DatabaseApi { pstmt.setString(3, post.getFrom()); pstmt.setString(4, post.getTo()); pstmt.setString(5, post.getBody()); + pstmt.setBoolean(6, post.isEditable()); pstmt.executeUpdate(); return true; diff --git a/chat/src/main/java/rtgre/modeles/Post.java b/chat/src/main/java/rtgre/modeles/Post.java index c018c7b..52f84d1 100644 --- a/chat/src/main/java/rtgre/modeles/Post.java +++ b/chat/src/main/java/rtgre/modeles/Post.java @@ -17,6 +17,24 @@ public class Post extends Message { /** Login du contact qui a envoyé ce post */ protected String from; + /** + * Getter de `editable` + * @return Le post est-il éditable/supprimé ? + */ + public boolean isEditable() { + return editable; + } + + /** + * Setter de `editable` + * @param editable Le statut d'éditabilité du message + */ + public void setEditable(boolean editable) { + this.editable = editable; + } + + /** Post éditable ? */ + protected boolean editable = true; /** * Constructeur par défaut @@ -130,7 +148,8 @@ public class Post extends Message { .put("timestamp", this.timestamp) .put("from", this.from) .put("to", this.to) - .put("body", this.body); + .put("body", this.body) + .put("editable", this.editable); } /** @@ -148,12 +167,16 @@ public class Post extends Message { * @return Le post créé */ public static Post fromJson(JSONObject jsonObject) { - return new Post( + Post post = new Post( UUID.fromString(jsonObject.getString("id")), jsonObject.getLong("timestamp"), jsonObject.getString("from"), jsonObject.getString("to"), jsonObject.getString("body") ); + if (jsonObject.has("editable")) { + post.setEditable(jsonObject.getBoolean("editable")); + } + return post; } }