Commit 197237c8 authored by Peining's avatar Peining
Browse files

Add functions for modifying avatar and for changing content of popup...

Add functions for modifying avatar and for changing content of popup automatically to generate a date picker for updating birthday.
parent e04851bc
......@@ -5,10 +5,13 @@
*/
package com.utclo23.ihmmain.controller;
import com.utclo23.data.module.DataException;
import com.utclo23.data.structure.PublicUser;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.time.ZoneId;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.concurrent.Task;
......@@ -18,14 +21,19 @@ import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Cursor;
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.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
/**
* Object: display all info of player profile.
......@@ -55,6 +63,7 @@ public class PlayerProfileController extends AbstractController{
private boolean isOther = false;
private String attribut;
private Image avatarImage;
private PopupController control;
@FXML
@Override
......@@ -110,25 +119,84 @@ public class PlayerProfileController extends AbstractController{
attribut="Password";
popup(attribut);
}
@FXML
private void editAvatar(ActionEvent event) throws IOException, DataException{
String avatarPath;
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open avatar file");
File selectedFile = fileChooser.showOpenDialog(getIhmmain().primaryStage);
if (selectedFile != null){
avatarPath = selectedFile.getPath();
Logger.getLogger(CreateUserController.class.getName()).log(
Level.INFO, "The chosen file is : {0}", avatarPath);
getFacade().iDataIHMMain.updateFileImage(avatarPath);
refresh();
}
}
/**
* Generate a pop-up
* The content of pop-up is generated dynamically.
* For updating the birthday, the date picker replace the text field in pop-up.
* @param attribut:name of info that user would like to modify
* @throws IOException
*/
private void popup(String attribut) throws IOException{
final Stage primaryStage = getIhmmain().primaryStage;
String path = "/fxml/ihmmain/popup.fxml";
FXMLLoader loader = new FXMLLoader(getClass().getResource(path));
Parent sceneLoader = loader.load();
PopupController controller=loader.getController();
controller.setFacade(getFacade());
controller.setIhmmain(getIhmmain());
controller.setAttribut(attribut);
Scene newScene;
newScene = new Scene(sceneLoader);
Stage popup = new Stage();
popup.initOwner(primaryStage);
popup.setScene(newScene);
if(attribut == "Birthday"){
final DatePicker date = new DatePicker();
Button back = new Button();
Button submit = new Button();
back.setText("Back");
back.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
((Node) (event.getSource())).getScene().getWindow().hide();
}
});
submit.setText("Submit");
submit.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
try {
Date birthDate = Date.from(date.getValue().atStartOfDay(ZoneId.systemDefault()).toInstant());
getFacade().iDataIHMMain.updateBirthdate(birthDate);
refresh();
((Node) (event.getSource())).getScene().getWindow().hide();
} catch (DataException ex) {
Logger.getLogger(PlayerProfileController.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
Pane root = new Pane();
root.setId("root");
Scene display = new Scene(root,400,150);
display.getStylesheets().add(getClass().getResource("/styles/ihmmain.css").toExternalForm());
popup.setScene(display);
root.getChildren().add(back);
root.getChildren().add(submit);
back.setLayoutX(60);
back.setLayoutY(91);
submit.setLayoutX(231);
submit.setLayoutY(91);
root.getChildren().add(date);
date.setLayoutX(90);
date.setLayoutY(35);
}
else{
String path = "/fxml/ihmmain/popup.fxml";
FXMLLoader loader = new FXMLLoader(getClass().getResource(path));
Parent sceneLoader = loader.load();
PopupController controller=loader.getController();
controller.setFacade(getFacade());
controller.setIhmmain(getIhmmain());
controller.setAttribut(attribut);
Scene newScene;
newScene = new Scene(sceneLoader);
popup.setScene(newScene);
}
popup.show();
}
/**
......@@ -201,11 +269,21 @@ public class PlayerProfileController extends AbstractController{
new Thread(wait).start();
}
}
public void getAvatar(){
byte[] thumbnail = getFacade().iDataIHMMain.getMyPublicUserProfile().getLightPublicUser().getAvatarThumbnail();
//System.out.println(thumbnail);
ByteArrayInputStream inputStream = new ByteArrayInputStream(thumbnail);
avatarImage = new Image(inputStream);
/**
* Get user's avatar
*/
public void getAvatar(PublicUser player){
byte[] thumbnail = player.getLightPublicUser().getAvatarThumbnail();
try{
ByteArrayInputStream inputStream = new ByteArrayInputStream(thumbnail);
avatarImage = new Image(inputStream);
}catch(NullPointerException e){
Logger.getLogger(
PlayerProfileController.class.getName()).log(Level.INFO,
"[PlayerProfile] - error - avatar is null."
);
}
}
@Override
/**
......@@ -214,9 +292,9 @@ public class PlayerProfileController extends AbstractController{
public void refresh(){
if (!isOther){
try{
getAvatar();
image.setImage(avatarImage);
me = getFacade().iDataIHMMain.getMyPublicUserProfile();
getAvatar(me);
image.setImage(avatarImage);
userID .setText(me.getLightPublicUser().getPlayerName());
firstName.setText(me.getFirstName());
lastName.setText(me.getLastName());
......@@ -232,6 +310,8 @@ public class PlayerProfileController extends AbstractController{
}
else{
try{
getAvatar(other);
image.setImage(avatarImage);
userID.setText(other.getLightPublicUser().getPlayerName());
firstName.setText(other.getFirstName());
lastName.setText(other.getLastName());
......
......@@ -28,7 +28,7 @@ public class PopupController extends AbstractController{
((Node) (event.getSource())).getScene().getWindow().hide();
}
@FXML
private void update(ActionEvent event) throws IOException, DataException{
public void update(ActionEvent event) throws IOException, DataException{
String text;
text = field.getText();
if (text.isEmpty()){
......@@ -46,10 +46,6 @@ public class PopupController extends AbstractController{
case "LastName":
getFacade().iDataIHMMain.updateLastname(text);
break;
/* To-do: change popup*/
case "Birthday":
getFacade().iDataIHMMain.updateLastname(text);
break;
case "Password":
getFacade().iDataIHMMain.updatePassword(text);
break;
......
......@@ -27,7 +27,7 @@
</HBox>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0">
<children>
<Button mnemonicParsing="false" text="Modify">
<Button mnemonicParsing="false" onAction="#editAvatar" text="Modify">
<opaqueInsets>
<Insets />
</opaqueInsets>
......@@ -116,9 +116,9 @@
<RowConstraints maxHeight="261.6125183105469" minHeight="10.0" prefHeight="67.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Statistics" GridPane.halignment="CENTER">
<Label text="Statistics" GridPane.halignment="LEFT">
<GridPane.margin>
<Insets left="10.0" />
<Insets left="90.0" />
</GridPane.margin>
</Label>
<GridPane prefHeight="125.0" prefWidth="368.0" GridPane.halignment="RIGHT" GridPane.rowIndex="1" GridPane.valignment="CENTER">
......@@ -141,7 +141,7 @@
<Label text="Played" GridPane.columnIndex="1" />
<Label text="Win" GridPane.columnIndex="2" />
<Label text="Loss" GridPane.columnIndex="3" />
<Label text="Abondon" GridPane.columnIndex="4" />
<Label text="Abandoned" GridPane.columnIndex="4" />
<Label prefHeight="17.0" prefWidth="0.0" text="0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label text="0" GridPane.columnIndex="1" GridPane.rowIndex="3" />
......@@ -164,9 +164,9 @@
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="30.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="30.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="54.0" minHeight="10.0" prefHeight="54.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="74.0" minHeight="10.0" prefHeight="55.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="63.0" minHeight="10.0" prefHeight="61.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="68.0" minHeight="10.0" prefHeight="53.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="74.0" minHeight="10.0" prefHeight="57.0" vgrow="SOMETIMES" />
</rowConstraints>
<GridPane.margin>
<Insets left="80.0" />
......@@ -181,7 +181,10 @@
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button fx:id="backButton" mnemonicParsing="false" onAction="#back" text="Return" GridPane.halignment="CENTER" GridPane.valignment="TOP" />
<Button fx:id="backButton" mnemonicParsing="false" onAction="#back" text="Return" GridPane.halignment="LEFT" GridPane.valignment="TOP">
<GridPane.margin>
<Insets left="80.0" />
</GridPane.margin></Button>
<Button fx:id="playerList" mnemonicParsing="false" onAction="#toPlayerList" text="Other Player" GridPane.columnIndex="1" GridPane.valignment="TOP" />
</children>
</GridPane>
......
......@@ -9,7 +9,7 @@
<AnchorPane id="AnchorPane" prefHeight="150.0" prefWidth="300.0" stylesheets="@../../styles/ihmmain.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.utclo23.ihmmain.controller.PopupController">
<children>
<Pane prefHeight="151.0" prefWidth="400.0">
<Pane prefHeight="151.0" prefWidth="400.0" style="-fx-background-color: #061f30;">
<children>
<GridPane layoutX="51.0" layoutY="50.0" prefHeight="54.0" prefWidth="277.0">
<columnConstraints>
......
......@@ -6,7 +6,9 @@
#generalGrid{
-fx-background-color: #061f30;
}
#root{
-fx-background-color: #061f30;
}
.scroll-pane {
-fx-base: transparent;
-fx-background-color: transparent;
......
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