Commit 7ca04d65 authored by Louis Bal Dit Sollier's avatar Louis Bal Dit Sollier
Browse files

Merge branch 'feature/main/displayOwnUser' into 'int/v2'

display and modify own user

See merge request !34
parents 360616cf 8b92ffbc
...@@ -19,6 +19,9 @@ import javafx.scene.control.ListCell; ...@@ -19,6 +19,9 @@ import javafx.scene.control.ListCell;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Date; import java.util.Date;
public class FXHome { public class FXHome {
...@@ -96,8 +99,10 @@ public class FXHome { ...@@ -96,8 +99,10 @@ public class FXHome {
String buttonSource = ((Button) e.getSource()).getId(); String buttonSource = ((Button) e.getSource()).getId();
switch(buttonSource){ switch(buttonSource){
case "homeButtonMyProfile": case "homeButtonMyProfile":
FXProfileConsult fxProfileConsult = new FXProfileConsult(); BorderPane layout = FXMLLoader.load(
fxProfileConsult.initialize("../fxml/viewProfile.fxml", this.stage); new URL(FXHome.class.getResource("../fxml/viewProfile.fxml").toExternalForm())
);
this.stage.setScene(new Scene(layout));
break; break;
case "homeButtonCreateGame": case "homeButtonCreateGame":
BorderPane layout = FXMLLoader.load( BorderPane layout = FXMLLoader.load(
......
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; package main.java.client.main.controller;
import javafx.fxml.Initializable;
import main.java.client.ClientApp; import main.java.client.ClientApp;
import main.java.client.main.model.MainApplicationModel; import main.java.client.main.model.MainApplicationModel;
import main.java.common.dataModel.UserHeavy;
import main.java.common.dataModel.UserLight; import main.java.common.dataModel.UserLight;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent; import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import javafx.stage.Stage; 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.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.io.IOException;
import java.util.ResourceBundle;
public class FXProfileConsult { public class FXProfileConsult implements Initializable {
private MainApplicationModel model; private MainApplicationModel model;
private Stage stage; private Stage stage;
@FXML @FXML
private TextField modifyProfileTextFieldName; private Text displayProfilePseudo;
@FXML
private Text displayProfileLastName;
@FXML @FXML
private TextField modifyProfileTextFieldFirstName; private Text displayProfileFirstName;
@FXML @FXML
private PasswordField modifyProfilePasswordFieldPassword; private Text displayProfileAge;
@FXML @FXML
private DatePicker modifyProfileTextFieldBirthDate; private Text displayProfileNbVictories;
@FXML @FXML
private TextField modifyProfileTextFieldPseudo; private Text displayProfileNbGames;
public FXProfileConsult(){ public FXProfileConsult(){
this.model = ClientApp.mainCore.getFX().getModel(); this.model = ClientApp.mainCore.getFX().getModel();
...@@ -50,106 +60,54 @@ public class FXProfileConsult { ...@@ -50,106 +60,54 @@ public class FXProfileConsult {
this.stage = stage; this.stage = stage;
} }
public void initialize(String fxmlResource, Stage stage) throws IOException { public void initialize(URL fxmlResource, ResourceBundle resource) {
FXMLLoader fxmlLoader = new FXMLLoader(getClass() UserLight user = this.model.getUserLight();
.getResource(fxmlResource));
BorderPane root = (BorderPane) fxmlLoader.load();
FXProfileConsult controller = fxmlLoader.getController();
controller.setStage(stage);
Scene scene = new Scene(root, ClientApp.APPLICATION_WIDTH, ClientApp.APPLICATION_HEIGHT); Date date = user.getDateOfBirth();
scene.getStylesheets().add(ClientApp.CSS_FILE_PATH); 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 @FXML
private void handleButtonClick(MouseEvent e) throws IOException { private void handleButtonClick(MouseEvent e) throws NullPointerException, IOException {
String buttonSource = ((Button) e.getSource()).getId(); String buttonSource = ((Button) e.getSource()).getId();
switch(buttonSource){ switch(buttonSource){
case "viewProfileButtonClose": case "viewProfileButtonClose":
this.stage = (Stage)((Node)e.getSource()).getScene().getWindow();
FXHome fxHome = new FXHome(); FXHome fxHome = new FXHome();
fxHome.initialize("../fxml/home.fxml", this.stage); fxHome.initialize("../fxml/home.fxml", this.stage);
break; break;
case "viewProfileButtonEdit": case "viewProfileButtonEdit":
this.stage.setScene(ModifyProfileScene()); FXMLLoader loader = new FXMLLoader(getClass().getResource("../fxml/modifyProfile.fxml"));
break; Parent padre = loader.load();
case "modifyProfileCancel": Scene switcher = new Scene(padre);
this.stage.setScene(MyProfileScene()); this.stage = (Stage)((Node)e.getSource()).getScene().getWindow();
break; this.stage.setScene(switcher);
case "modifyProfileValidate":
this.stage.setScene(UpdateUserProfile());
break; break;
default: default:
System.out.println("["+buttonSource+"] A handler button was triggered, but no action was performed."); System.out.println("["+buttonSource+"] A handler button was triggered, but no action was performed.");
} }
} }
private Scene MyProfileScene() throws IOException { private void setNewScene(String path, MouseEvent e) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass() FXMLLoader loader = new FXMLLoader(getClass().getResource(path));
.getResource("../fxml/viewProfile.fxml")); Parent padre = loader.load();
BorderPane root = (BorderPane) fxmlLoader.load(); Scene switcher = new Scene(padre);
this.stage = (Stage)((Node)e.getSource()).getScene().getWindow();
return setNewScene(fxmlLoader, root); 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