Archived
1
0
Fork 0

Can switch between scene

This commit is contained in:
Ziedelth 2022-01-05 08:37:40 +01:00
parent 0e36273ab9
commit cc3a9033ff
8 changed files with 104 additions and 52 deletions

View file

@ -3,15 +3,16 @@ package fr.univ.lyon1.gui;
import fr.univ.lyon1.client.Client; import fr.univ.lyon1.client.Client;
import fr.univ.lyon1.client.ClientReceive; import fr.univ.lyon1.client.ClientReceive;
import fr.univ.lyon1.common.Message; import fr.univ.lyon1.common.Message;
import fr.univ.lyon1.gui.handlers.MainHandler;
import java.io.IOException; import java.io.IOException;
public class ClientGUI extends Client { public class ClientGUI extends Client {
private final MainGui gui; private final MainHandler gui;
public ClientGUI(MainGui gui, String address, int port) throws IOException, InterruptedException { public ClientGUI(MainHandler handler, String address, int port) throws IOException, InterruptedException {
super(address, port); super(address, port);
this.gui = gui; this.gui = handler;
} }
@Override @Override

View file

@ -1,5 +1,6 @@
package fr.univ.lyon1.gui; package fr.univ.lyon1.gui;
import fr.univ.lyon1.gui.handlers.MainHandler;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.scene.Parent; import javafx.scene.Parent;
@ -14,9 +15,9 @@ import javafx.scene.text.TextFlow;
public class ClientPanel extends Parent { public class ClientPanel extends Parent {
private final TextArea textToSend = new TextArea(); private final TextArea textToSend = new TextArea();
private final TextFlow receivedText = new TextFlow(); private final TextFlow receivedText = new TextFlow();
private final MainGui gui; private final MainHandler gui;
ClientPanel(MainGui gui) { public ClientPanel(MainHandler gui) {
this.gui = gui; this.gui = gui;
ScrollPane scrollReceivedText = new ScrollPane(); ScrollPane scrollReceivedText = new ScrollPane();
scrollReceivedText.setLayoutX(20); scrollReceivedText.setLayoutX(20);

View file

@ -1,47 +1,22 @@
package fr.univ.lyon1.gui; package fr.univ.lyon1.gui;
import fr.univ.lyon1.common.ServerConfiguration; import fr.univ.lyon1.gui.handlers.MainHandler;
import fr.univ.lyon1.gui.handlers.ServerConfigurationHandler;
import javafx.application.Application; import javafx.application.Application;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage; import javafx.stage.Stage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException; import java.io.IOException;
public class MainGui extends Application { public class MainGui extends Application {
private ClientPanel clientPanel;
@Nullable
private ClientGUI client;
@Override @Override
public void start(Stage stage) { public void start(Stage stage) {
try { try {
@NotNull new MainHandler().launch(stage);
final ServerConfiguration serverConfiguration = ServerConfiguration.load();
this.client = new ClientGUI(this, serverConfiguration.getAddress(), serverConfiguration.getPort());
stage.setTitle("Chat client");
stage.setWidth(440);
this.clientPanel = new ClientPanel(this);
Group root = new Group();
root.getChildren().add(this.clientPanel);
Scene scene = new Scene(root, 600, 500);
stage.setScene(scene);
stage.show();
if (this.client != null) {
this.client.run();
}
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
// Launch server configuration // Launch server configuration
try { try {
launchServerConfiguration(stage); new ServerConfigurationHandler().launch(stage);
} catch (IOException ex) { } catch (IOException ex) {
// Can not launch server configuration, stop application // Can not launch server configuration, stop application
System.exit(1); System.exit(1);
@ -54,25 +29,7 @@ public class MainGui extends Application {
}); });
} }
private void launchServerConfiguration(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(ClassLoader.getSystemClassLoader().getResource("connect_gui.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Configuration du serveur");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) { public static void main(String[] args) {
Application.launch(MainGui.class, args); Application.launch(MainGui.class, args);
} }
public void sendMessage(String msg) {
if (client != null) {
client.sendMessage(msg);
}
}
public void receiveMessage(String msg) {
clientPanel.addMessage(msg);
}
} }

View file

@ -1,16 +1,22 @@
package fr.univ.lyon1.gui.controller; package fr.univ.lyon1.gui.controller;
import fr.univ.lyon1.common.ServerConfiguration; import fr.univ.lyon1.common.ServerConfiguration;
import fr.univ.lyon1.gui.handlers.MainHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
public class ConnectGuiController { public class ConnectGuiController {
@FXML
public TextField addressTextField; public TextField addressTextField;
@FXML
public TextField portTextField; public TextField portTextField;
@FXML
public Button connectButton; public Button connectButton;
/** /**
@ -40,10 +46,19 @@ public class ConnectGuiController {
new ServerConfiguration(address, iPort).save(); new ServerConfiguration(address, iPort).save();
Dialog.showSuccessDialog("Connecté", "Vous êtes bien connecté au serveur"); Dialog.showSuccessDialog("Connecté", "Vous êtes bien connecté au serveur");
System.out.println("File saved, next step..."); System.out.println("File saved, next step...");
@NotNull
final Stage stage = (Stage) this.connectButton.getScene().getWindow();
// TODO: Change application scene
new MainHandler().launch(stage);
System.out.println("Get title : " + stage.getTitle());
} catch (IOException e) { } catch (IOException e) {
Dialog.showErrorDialog("Erreur", "Impossible de sauvegarder les informations de connexion au serveur"); Dialog.showErrorDialog("Erreur", "Impossible de sauvegarder les informations de connexion au serveur");
this.connectButton.setDisable(false); this.connectButton.setDisable(false);
System.out.println("Failed to save file, error: " + e.getMessage()); System.out.println("Failed to save file, error: " + e.getMessage());
} catch (InterruptedException e) {
e.printStackTrace();
} }
} else { } else {
System.out.println("Connection not available"); System.out.println("Connection not available");

View file

@ -0,0 +1,9 @@
package fr.univ.lyon1.gui.handlers;
import javafx.stage.Stage;
import java.io.IOException;
public interface Handler {
void launch(Stage stage) throws IOException, InterruptedException;
}

View file

@ -0,0 +1,50 @@
package fr.univ.lyon1.gui.handlers;
import fr.univ.lyon1.common.ServerConfiguration;
import fr.univ.lyon1.gui.ClientGUI;
import fr.univ.lyon1.gui.ClientPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
public class MainHandler implements Handler {
private ClientPanel clientPanel;
@Nullable
private ClientGUI client;
@Override
public void launch(Stage stage) throws IOException, InterruptedException {
@NotNull
final ServerConfiguration serverConfiguration = ServerConfiguration.load();
this.client = new ClientGUI(this, serverConfiguration.getAddress(), serverConfiguration.getPort());
stage.setTitle("Chat client");
stage.setWidth(440);
this.clientPanel = new ClientPanel(this);
Group root = new Group();
root.getChildren().add(this.clientPanel);
Scene scene = new Scene(root, 600, 500);
stage.setScene(scene);
stage.show();
if (this.client != null) {
this.client.run();
}
}
public void sendMessage(String msg) {
if (client != null) {
client.sendMessage(msg);
}
}
public void receiveMessage(String msg) {
clientPanel.addMessage(msg);
}
}

View file

@ -0,0 +1,18 @@
package fr.univ.lyon1.gui.handlers;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class ServerConfigurationHandler implements Handler {
@Override
public void launch(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(ClassLoader.getSystemClassLoader().getResource("connect_gui.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Configuration du serveur");
stage.setScene(scene);
stage.show();
}
}

View file

@ -10,4 +10,5 @@ module fr.univ.lyon {
exports fr.univ.lyon1.common; exports fr.univ.lyon1.common;
exports fr.univ.lyon1.gui; exports fr.univ.lyon1.gui;
exports fr.univ.lyon1.gui.controller; exports fr.univ.lyon1.gui.controller;
exports fr.univ.lyon1.gui.handlers;
} }