Commit fd29aa38 authored by BAL DIT SOLLIER's avatar BAL DIT SOLLIER
Browse files

display and modify own user

parent 3d41a2e2
......@@ -18,6 +18,7 @@ import javafx.util.Callback;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
......@@ -61,8 +62,10 @@ public class FXHome {
String buttonSource = ((Button) e.getSource()).getId();
switch(buttonSource){
case "homeButtonMyProfile":
FXProfileConsult fxProfileConsult = new FXProfileConsult();
fxProfileConsult.initialize("../fxml/viewProfile.fxml", this.stage);
BorderPane layout = FXMLLoader.load(
new URL(FXHome.class.getResource("../fxml/viewProfile.fxml").toExternalForm())
);
this.stage.setScene(new Scene(layout));
break;
case "homeDeconnection":
FXConnection fxConnection = new FXConnection();
......
package main.java.client.main.controller;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import main.java.client.ClientApp;
import main.java.client.main.model.MainApplicationModel;
import main.java.common.dataModel.UserLight;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.ResourceBundle;
public class FXModifyProfile implements Initializable {
private MainApplicationModel model;
private Stage stage;
@FXML
private TextField modifyProfileTextFieldName;
@FXML
private TextField modifyProfileTextFieldFirstName;
@FXML
private PasswordField modifyProfilePasswordFieldPassword;
@FXML
private DatePicker modifyProfileTextFieldBirthDate;
@FXML
private TextField modifyProfileTextFieldPseudo;
public FXModifyProfile(){
this.model = ClientApp.mainCore.getFX().getModel();
stage = new Stage();
}
public void setStage(Stage stage){
this.stage = stage;
}
public void initialize(URL fxmlResource, ResourceBundle resource) {
UserLight user = this.model.getUserLight();
Date date = user.getDateOfBirth();
LocalDate localdate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
modifyProfileTextFieldName.setText(user.getLastName());
modifyProfileTextFieldFirstName.setText(user.getFirstName());
modifyProfileTextFieldPseudo.setText(user.getPseudo());
modifyProfileTextFieldBirthDate.setValue(localdate);
}
@FXML
private void handleButtonClick(MouseEvent e) throws IOException {
String buttonSource = ((Button) e.getSource()).getId();
switch(buttonSource){
case "modifyProfileButtonCancel":
setNewScene("../fxml/viewProfile.fxml", e);
break;
case "modifyProfileButtonValidate":
UpdateUserProfile();
setNewScene("../fxml/viewProfile.fxml", e);
break;
default:
System.out.println("["+buttonSource+"] A handler button was triggered, but no action was performed.");
}
}
private void UpdateUserProfile() {
try {
UserLight user = this.model.getUserLight();
LocalDate localDate = this.modifyProfileTextFieldBirthDate.getValue();
Date date = Date.from(localDate.atStartOfDay()
.atZone(ZoneId.systemDefault())
.toInstant());
user.setFirstName(this.modifyProfileTextFieldFirstName.getText());
user.setLastName(this.modifyProfileTextFieldName.getText());
user.setPseudo(this.modifyProfileTextFieldPseudo.getText());
user.setDateOfBirth(date);
ClientApp.mainCore.getMainToData().updatedProfile(user);
} catch (Exception exception){
System.out.println("Exception caught: " + exception.getMessage());
}
}
private void setNewScene(String path, MouseEvent e) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource(path));
Parent padre = loader.load();
Scene switcher = new Scene(padre);
this.stage = (Stage)((Node)e.getSource()).getScene().getWindow();
this.stage.setScene(switcher);
}
}
package main.java.client.main.controller;
import javafx.fxml.Initializable;
import main.java.client.ClientApp;
import main.java.client.main.model.MainApplicationModel;
import main.java.common.dataModel.UserHeavy;
import main.java.common.dataModel.UserLight;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.scene.Parent;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDate;
import java.time.Period;
import java.time.Year;
import java.time.ZoneId;
import java.util.Date;
import java.time.*;
import javafx.scene.control.DatePicker;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import java.io.IOException;
import java.util.ResourceBundle;
public class FXProfileConsult {
public class FXProfileConsult implements Initializable {
private MainApplicationModel model;
private Stage stage;
@FXML
private TextField modifyProfileTextFieldName;
private Text displayProfilePseudo;
@FXML
private Text displayProfileLastName;
@FXML
private TextField modifyProfileTextFieldFirstName;
private Text displayProfileFirstName;
@FXML
private PasswordField modifyProfilePasswordFieldPassword;
private Text displayProfileAge;
@FXML
private DatePicker modifyProfileTextFieldBirthDate;
private Text displayProfileNbVictories;
@FXML
private TextField modifyProfileTextFieldPseudo;
private Text displayProfileNbGames;
public FXProfileConsult(){
this.model = ClientApp.mainCore.getFX().getModel();
......@@ -50,106 +60,54 @@ public class FXProfileConsult {
this.stage = stage;
}
public void initialize(String fxmlResource, Stage stage) throws IOException {
public void initialize(URL fxmlResource, ResourceBundle resource) {
FXMLLoader fxmlLoader = new FXMLLoader(getClass()
.getResource(fxmlResource));
BorderPane root = (BorderPane) fxmlLoader.load();
FXProfileConsult controller = fxmlLoader.getController();
controller.setStage(stage);
UserLight user = this.model.getUserLight();
Scene scene = new Scene(root, ClientApp.APPLICATION_WIDTH, ClientApp.APPLICATION_HEIGHT);
scene.getStylesheets().add(ClientApp.CSS_FILE_PATH);
Date date = user.getDateOfBirth();
LocalDate birthdate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
Date now = new Date();
LocalDate today = now.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
int age = Period.between(birthdate, today).getYears();
displayProfileFirstName.setText(user.getFirstName());
displayProfileAge.setText(Integer.toString(age));
displayProfileLastName.setText(user.getLastName());
displayProfilePseudo.setText(user.getPseudo());
displayProfileNbGames.setText(Integer.toString(user.getPlayedGames()));
displayProfileNbVictories.setText(Integer.toString(user.getWonGames()));
stage.setScene(scene);
stage.show();
}
@FXML
private void handleButtonClick(MouseEvent e) throws IOException {
private void handleButtonClick(MouseEvent e) throws NullPointerException, IOException {
String buttonSource = ((Button) e.getSource()).getId();
switch(buttonSource){
case "viewProfileButtonClose":
this.stage = (Stage)((Node)e.getSource()).getScene().getWindow();
FXHome fxHome = new FXHome();
fxHome.initialize("../fxml/home.fxml", this.stage);
break;
case "viewProfileButtonEdit":
this.stage.setScene(ModifyProfileScene());
break;
case "modifyProfileCancel":
this.stage.setScene(MyProfileScene());
break;
case "modifyProfileValidate":
this.stage.setScene(UpdateUserProfile());
FXMLLoader loader = new FXMLLoader(getClass().getResource("../fxml/modifyProfile.fxml"));
Parent padre = loader.load();
Scene switcher = new Scene(padre);
this.stage = (Stage)((Node)e.getSource()).getScene().getWindow();
this.stage.setScene(switcher);
break;
default:
System.out.println("["+buttonSource+"] A handler button was triggered, but no action was performed.");
}
}
private Scene MyProfileScene() throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass()
.getResource("../fxml/viewProfile.fxml"));
BorderPane root = (BorderPane) fxmlLoader.load();
return setNewScene(fxmlLoader, root);
private void setNewScene(String path, MouseEvent e) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource(path));
Parent padre = loader.load();
Scene switcher = new Scene(padre);
this.stage = (Stage)((Node)e.getSource()).getScene().getWindow();
this.stage.setScene(switcher);
}
private Scene ModifyProfileScene() throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass()
.getResource("../fxml/modifyProfile.fxml"));
BorderPane root = (BorderPane) fxmlLoader.load();
UserLight user = this.model.getUserLight();
Date date = user.getDateOfBirth();
LocalDate localdate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
this.modifyProfileTextFieldName.setText(user.getLastName());
this.modifyProfileTextFieldFirstName.setText(user.getFirstName());
this.modifyProfileTextFieldPseudo.setText(user.getPseudo());
this.modifyProfileTextFieldBirthDate.setValue(localdate);
return setNewScene(fxmlLoader, root);
}
private Scene UpdateUserProfile() throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass()
.getResource("../fxml/viewProfile.fxml"));
BorderPane root = (BorderPane) fxmlLoader.load();
try {
UserLight user = this.model.getUserLight();
LocalDate localDate = this.modifyProfileTextFieldBirthDate.getValue();
Date date = java.util.Date.from(localDate.atStartOfDay()
.atZone(ZoneId.systemDefault())
.toInstant());
user.setFirstName(this.modifyProfileTextFieldFirstName.getText());
user.setLastName(this.modifyProfileTextFieldName.getText());
user.setPseudo(this.modifyProfileTextFieldPseudo.getText());
user.setDateOfBirth(date);
ClientApp.mainCore.getMainToData().updatedProfile(user);
} catch (Exception exception){
System.out.println("Exception caught: " + exception.getMessage());
}
return setNewScene(fxmlLoader, root);
}
private Scene setNewScene(FXMLLoader loader, Pane root){
FXProfileConsult controller = loader.getController();
controller.setStage(stage);
Scene scene = new Scene(root, ClientApp.APPLICATION_WIDTH, ClientApp.APPLICATION_HEIGHT);
scene.getStylesheets().add(ClientApp.CSS_FILE_PATH);
return scene;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment