diff --git a/chat/src/main/java/rtgre/chat/ChatApplication.java b/chat/src/main/java/rtgre/chat/ChatApplication.java index 039b5e8..35c92c2 100644 --- a/chat/src/main/java/rtgre/chat/ChatApplication.java +++ b/chat/src/main/java/rtgre/chat/ChatApplication.java @@ -16,15 +16,13 @@ import java.util.logging.Logger; public class ChatApplication extends Application { public static final Logger LOGGER = Logger.getLogger(ChatApplication.class.getCanonicalName()); - public class EssaiLogger { - static { - try { - InputStream is = EssaiLogger.class.getClassLoader() - .getResource("logging.properties").openStream(); - LogManager.getLogManager().readConfiguration(is); - } catch (Exception e) { - LOGGER.log(Level.INFO, "Cannot read configuration file", e); - } + static { + try { + InputStream is = ChatApplication.class + .getResource("logging.properties").openStream(); + LogManager.getLogManager().readConfiguration(is); + } catch (Exception e) { + LOGGER.log(Level.INFO, "Cannot read configuration file", e); } } diff --git a/chat/src/main/java/rtgre/chat/ChatController.java b/chat/src/main/java/rtgre/chat/ChatController.java index 1b5ee81..6375c58 100644 --- a/chat/src/main/java/rtgre/chat/ChatController.java +++ b/chat/src/main/java/rtgre/chat/ChatController.java @@ -10,7 +10,9 @@ import javafx.embed.swing.SwingFXUtils; import javafx.event.ActionEvent; import javafx.event.Event; import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; +import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; @@ -19,6 +21,7 @@ import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.FileChooser; +import javafx.stage.Modality; import javafx.stage.Stage; import net.synedra.validatorfx.Check; import net.synedra.validatorfx.TooltipWrapper; @@ -98,6 +101,7 @@ public class ChatController implements Initializable { loginTextField.disableProperty().bind(connectionButton.selectedProperty()); hostComboBox.disableProperty().bind(connectionButton.selectedProperty()); + hostAddMenuItem.setOnAction(this::handleHostAdd); avatarMenuItem.setOnAction(this::handleAvatarChange); avatarImageView.setOnMouseClicked(this::handleAvatarChange); sendButton.setOnAction(this::onActionSend); @@ -128,6 +132,39 @@ public class ChatController implements Initializable { } + private void handleHostAdd(ActionEvent actionEvent) { + try { + ChatHostAddController controller = showNewStage("Add host", "chathostadd-view.fxml"); + if (controller.isOk()) { + hostComboBox.getItems().add(controller.hostTextField.getText()); + hostComboBox.setValue(controller.hostTextField.getText()); + } + } catch (IOException e) { + LOGGER.warning("Impossible d'ouvrir la fenêtre de dialogue: fxml introuvable \n" + e.getMessage()); + e.printStackTrace(); + } + } + + /** + * Ouvre une fenêtre modale et attend qu'elle se ferme. + * + * @param title titre de la fenêtre + * @param fxmlFileName nom du fichier FXML décrivant l'interface graphique + * @return l'objet contrôleur associé à la fenêtre + * @throws IOException si le fichier FXML n'est pas trouvé dans les ressources + */ + public T showNewStage(String title, String fxmlFileName) throws IOException { + FXMLLoader fxmlLoader = new FXMLLoader(ChatApplication.class.getResource(fxmlFileName)); + Scene scene = new Scene(fxmlLoader.load()); + Stage stage = new Stage(); + stage.initModality(Modality.APPLICATION_MODAL); + stage.setTitle(title); + stage.setResizable(false); + stage.setScene(scene); + stage.showAndWait(); + return fxmlLoader.getController(); + } + private void initRoomListView() { try { roomsListView.setCellFactory(roomListView -> new RoomListViewCell()); diff --git a/chat/src/main/java/rtgre/chat/ChatHostAddController.java b/chat/src/main/java/rtgre/chat/ChatHostAddController.java new file mode 100644 index 0000000..1ec5d58 --- /dev/null +++ b/chat/src/main/java/rtgre/chat/ChatHostAddController.java @@ -0,0 +1,70 @@ +package rtgre.chat; + +import javafx.beans.binding.Bindings; +import javafx.event.ActionEvent; +import javafx.fxml.Initializable; +import javafx.scene.control.Button; +import javafx.scene.control.TextField; +import javafx.scene.layout.HBox; +import javafx.stage.Stage; +import net.synedra.validatorfx.Check; +import net.synedra.validatorfx.TooltipWrapper; +import net.synedra.validatorfx.Validator; + +import java.net.URL; +import java.util.ResourceBundle; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ChatHostAddController implements Initializable { + + + public TextField hostTextField; + public Button resetButton; + public HBox submitWrapper; + public Button submitButton; + private boolean ok = false; + public static final Pattern HOST_PORT_REGEX = Pattern.compile("^([-.a-zA-Z0-9]+)(?::([0-9]{1,5}))?$"); + private Validator validatorHost = new Validator(); + + @Override + public void initialize(URL url, ResourceBundle resourceBundle) { + submitButton.setOnAction(this::onActionSubmit); + resetButton.setOnAction(this::onActionReset); + + submitButton.disableProperty().bind(validatorHost.containsErrorsProperty()); + TooltipWrapper