🚀 Add hair color, go to bed

main
Alexis Drai 2 years ago
parent 8bcc0132af
commit 3c36fe3e61

@ -26,7 +26,13 @@
</HBox> </HBox>
<HBox> <HBox>
<Label text="first name: "/> <Label text="first name: "/>
<TextField fx:id="firstnameTF"/> <TextField fx:id="firstnameTF"
disable="${promLV.selectionModel.selectedIndex == minus1}"/>
</HBox>
<HBox>
<Label text="hair color: "/>
<ColorPicker fx:id="hairColorPicker"
disable="${promLV.selectionModel.selectedIndex == minus1}"/>
</HBox> </HBox>
</VBox> </VBox>
</right> </right>

@ -6,6 +6,7 @@
<?import javafx.scene.layout.HBox?> <?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?> <?import javafx.scene.control.Label?>
<?import javafx.scene.control.ColorPicker?>
<AnchorPane xmlns="http://javafx.com/javafx" <AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"> xmlns:fx="http://javafx.com/fxml">
<VBox> <VBox>
@ -17,6 +18,10 @@
<Label text="first name"/> <Label text="first name"/>
<TextField fx:id="firstnameTF"/> <TextField fx:id="firstnameTF"/>
</HBox> </HBox>
<HBox>
<Label text="hair color"/>
<ColorPicker fx:id="hairColorPicker"/>
</HBox>
<HBox> <HBox>
<Button text="OK" onAction="#clickCreate" disable="${lastnameTF.text.empty || firstnameTF.text.empty}"/> <Button text="OK" onAction="#clickCreate" disable="${lastnameTF.text.empty || firstnameTF.text.empty}"/>
<Button text="Cancel" onAction="#clickCancel"/> <Button text="Cancel" onAction="#clickCancel"/>

@ -1,6 +1,5 @@
package data; package data;
import model.Promotion;
import viewmodel.PromotionVM; import viewmodel.PromotionVM;
import java.io.FileInputStream; import java.io.FileInputStream;

@ -1,5 +1,6 @@
package data; package data;
import javafx.scene.paint.Color;
import viewmodel.PromotionVM; import viewmodel.PromotionVM;
import viewmodel.StudentVM; import viewmodel.StudentVM;
@ -9,10 +10,10 @@ public class Stub implements Loadable {
public PromotionVM load(){ public PromotionVM load(){
PromotionVM VM = new PromotionVM(); PromotionVM VM = new PromotionVM();
VM.addStudentVM(new StudentVM("ONE", "Alan")); VM.addStudentVM(new StudentVM("ONE", "Alan", Color.BLACK));
VM.addStudentVM(new StudentVM("TWO", "Allan")); VM.addStudentVM(new StudentVM("TWO", "Allan", Color.AQUAMARINE));
VM.addStudentVM(new StudentVM("THREE", "Alllan")); VM.addStudentVM(new StudentVM("THREE", "Alllan", Color.ORANGERED));
VM.addStudentVM(new StudentVM("FOUR", "Allllan")); VM.addStudentVM(new StudentVM("FOUR", "Allllan", Color.WHITESMOKE));
return VM; return VM;
} }

@ -1,5 +1,7 @@
package model; package model;
import utils.HairColor;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport; import java.beans.PropertyChangeSupport;
import java.io.Serializable; import java.io.Serializable;
@ -7,14 +9,18 @@ import java.io.Serializable;
public class Student implements Serializable { public class Student implements Serializable {
public static final String PROP_STUDENT_FIRSTNAME = "model.Student.firstname"; public static final String PROP_STUDENT_FIRSTNAME = "model.Student.firstname";
private static final String PROP_STUDENT_HAIR_COLOR = "model.Student.hairColor";
private final String lastname; private final String lastname;
private String firstname; private String firstname;
public Student(String lastname, String firstName) { private HairColor hairColor;
public Student(String lastname, String firstName, HairColor hairColor) {
this.lastname = !lastname.isEmpty() ? lastname : "_"; this.lastname = !lastname.isEmpty() ? lastname : "_";
this.firstname = !firstName.isEmpty() ? firstName : "_"; this.firstname = !firstName.isEmpty() ? firstName : "_";
this.hairColor = hairColor != null ? hairColor : new HairColor(0.0, 0.0, 0.0);
} }
public String getLastname() { public String getLastname() {
@ -25,6 +31,19 @@ public class Student implements Serializable {
return firstname; return firstname;
} }
public HairColor getHairColor() {
return hairColor;
}
public void setHairColor(HairColor hairColor) {
if(hairColor != null) {
HairColor oldV = getHairColor();
this.hairColor = hairColor;
// fire
getSupport().firePropertyChange(PROP_STUDENT_HAIR_COLOR, oldV, getHairColor());
}
}
public void setFirstname(String firstname) { public void setFirstname(String firstname) {
if(firstname != null) { if(firstname != null) {
String oldV = getFirstname(); String oldV = getFirstname();

@ -0,0 +1,27 @@
package utils;
import javafx.scene.paint.Color;
public class ColorConverter {
public static HairColor toHairColor(Color JFXColor) {
if (JFXColor == null) {return new HairColor(0.0, 0.0, 0.0);}
return new HairColor(JFXColor.getRed(), JFXColor.getGreen(), JFXColor.getBlue());
}
public static Color toJFXColor(HairColor hairColor) {
if (hairColor == null) {return Color.BLACK;}
return new Color(hairColor.r(), hairColor.g(), hairColor.b(), 1.0);
}
/**
* ACCEPTABLE TYPES: HairColor
*/
public static Color toJFXColor(Object o) {
if (o instanceof HairColor) {
return toJFXColor((HairColor) o);
} else {
throw new IllegalArgumentException("only acceptable param types");
}
}
}

@ -1,4 +1,5 @@
package utils; package utils;
public class HairColor { import java.io.Serializable;
}
public record HairColor(double r, double g, double b) implements Serializable {}

@ -4,6 +4,7 @@ import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.paint.Color;
import javafx.stage.Modality; import javafx.stage.Modality;
import javafx.stage.Stage; import javafx.stage.Stage;
import viewmodel.PromotionVM; import viewmodel.PromotionVM;
@ -18,21 +19,29 @@ public class MainWindow {
private Label lastnameTF; private Label lastnameTF;
@FXML @FXML
private TextField firstnameTF; private TextField firstnameTF;
@FXML
private ColorPicker hairColorPicker;
@FXML @FXML
private void clickAddStudent() { private void clickAddStudent() {
prepareCreationWindow(); displayAddWindow();
} }
private void prepareCreationWindow() { private void displayAddWindow() {
Stage stage = new Stage(); Stage stage = new Stage();
stage.initOwner(promLV.getScene().getWindow()); stage.initOwner(promLV.getScene().getWindow());
stage.initModality(Modality.WINDOW_MODAL); stage.initModality(Modality.WINDOW_MODAL);
StudentCreationWindow controller = initCreationWindow(stage); StudentCreationWindow controller = initCreationWindow(stage);
if (controller.getLastname() != null && controller.getFirstname() != null) { if
VM.addStudentVM(new StudentVM(controller.getLastname(), controller.getFirstname())); (controller.getLastname() != null &&
controller.getFirstname() != null &&
controller.getHairColor() != null)
{
VM.addStudentVM(new StudentVM(controller.getLastname(),
controller.getFirstname(),
controller.getHairColor()));
} }
} }
@ -45,7 +54,6 @@ public class MainWindow {
stage.setScene(new Scene(loader.load())); stage.setScene(new Scene(loader.load()));
stage.showAndWait(); stage.showAndWait();
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace();
new Alert(Alert.AlertType.ERROR, ex.getMessage(), ButtonType.OK).showAndWait(); new Alert(Alert.AlertType.ERROR, ex.getMessage(), ButtonType.OK).showAndWait();
} }
return controller; return controller;
@ -56,6 +64,7 @@ public class MainWindow {
VM.removeStudentVM(promLV.getSelectionModel().getSelectedItem()); VM.removeStudentVM(promLV.getSelectionModel().getSelectedItem());
lastnameTF.setText(""); lastnameTF.setText("");
firstnameTF.setText(""); firstnameTF.setText("");
hairColorPicker.setValue(Color.WHITE);
} }
@FXML @FXML
@ -73,7 +82,7 @@ public class MainWindow {
private void initialize() { private void initialize() {
promLV.itemsProperty().bind(VM.studentsVMProperty()); promLV.itemsProperty().bind(VM.studentsVMProperty());
initPromLV(); initPromLV();
promLV.setCellFactory(__ -> new StudentVMCell()); promLV.setCellFactory(__ -> new StudentCell());
} }
private void initPromLV() { private void initPromLV() {
@ -81,10 +90,12 @@ public class MainWindow {
if (oldV != null) { if (oldV != null) {
lastnameTF.textProperty().unbind(); lastnameTF.textProperty().unbind();
firstnameTF.textProperty().unbindBidirectional(oldV.firstnameProperty()); firstnameTF.textProperty().unbindBidirectional(oldV.firstnameProperty());
hairColorPicker.valueProperty().unbindBidirectional(oldV.hairColorProperty());
} }
if (newV != null) { if (newV != null) {
lastnameTF.textProperty().bind(newV.lastnameProperty()); lastnameTF.textProperty().bind(newV.lastnameProperty());
firstnameTF.textProperty().bindBidirectional(newV.firstnameProperty()); firstnameTF.textProperty().bindBidirectional(newV.firstnameProperty());
hairColorPicker.valueProperty().bindBidirectional(newV.hairColorProperty());
} }
}); });
} }

@ -3,9 +3,10 @@ package view;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.ListCell; import javafx.scene.control.ListCell;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.shape.Rectangle;
import viewmodel.StudentVM; import viewmodel.StudentVM;
public class StudentVMCell extends ListCell<StudentVM> { public class StudentCell extends ListCell<StudentVM> {
@Override @Override
protected void updateItem(StudentVM item, boolean empty) { protected void updateItem(StudentVM item, boolean empty) {
super.updateItem(item, empty); super.updateItem(item, empty);
@ -13,12 +14,17 @@ public class StudentVMCell extends ListCell<StudentVM> {
Label lastnameLbl = new Label(); Label lastnameLbl = new Label();
Label firstnameLbl = new Label(); Label firstnameLbl = new Label();
Rectangle hairClrRect = new Rectangle();
lastnameLbl.textProperty().bind(item.lastnameProperty()); lastnameLbl.textProperty().bind(item.lastnameProperty());
firstnameLbl.textProperty().bind(item.firstnameProperty()); firstnameLbl.textProperty().bind(item.firstnameProperty());
hairClrRect.setHeight(10.0);
hairClrRect.setWidth(10.0);
hairClrRect.fillProperty().bind(item.hairColorProperty());
BorderPane pane = new BorderPane(); BorderPane pane = new BorderPane();
pane.setLeft(lastnameLbl); pane.setLeft(lastnameLbl);
pane.setCenter(firstnameLbl); pane.setCenter(firstnameLbl);
pane.setRight(hairClrRect);
setGraphic(pane); setGraphic(pane);
} }

@ -1,12 +1,15 @@
package view; package view;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
public class StudentCreationWindow { public class StudentCreationWindow {
private String lastname; private String lastname;
private String firstname; private String firstname;
private Color hairColor;
public String getLastname() { public String getLastname() {
return lastname; return lastname;
@ -16,15 +19,22 @@ public class StudentCreationWindow {
return firstname; return firstname;
} }
public Color getHairColor() {
return hairColor;
}
@FXML @FXML
private TextField firstnameTF; private TextField firstnameTF;
@FXML @FXML
private TextField lastnameTF; private TextField lastnameTF;
@FXML
private ColorPicker hairColorPicker;
@FXML @FXML
private void clickCreate() { private void clickCreate() {
lastname = lastnameTF.getText(); lastname = lastnameTF.getText();
firstname = firstnameTF.getText(); firstname = firstnameTF.getText();
hairColor = hairColorPicker.getValue();
close(); close();
} }
@ -32,6 +42,7 @@ public class StudentCreationWindow {
private void clickCancel() { private void clickCancel() {
lastname = null; lastname = null;
firstname = null; firstname = null;
hairColor = null;
close(); close();
} }

@ -1,8 +1,12 @@
package viewmodel; package viewmodel;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.scene.paint.Color;
import model.Student; import model.Student;
import utils.ColorConverter;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
@ -24,13 +28,18 @@ public class StudentVM implements PropertyChangeListener {
public StringProperty firstnameProperty() {return firstname;} public StringProperty firstnameProperty() {return firstname;}
public void setFirstname(String firstname) {this.firstname.set(firstname);} public void setFirstname(String firstname) {this.firstname.set(firstname);}
private final ObjectProperty<Color> hairColor = new SimpleObjectProperty<>();
public Color getHairColor() {return hairColor.get();}
public ObjectProperty<Color> hairColorProperty() {return hairColor;}
public void setHairColor(Color hairColor) {this.hairColor.set(hairColor);}
public StudentVM(Object o) { public StudentVM(Object o) {
Student model; Student model;
if (o instanceof Student) { if (o instanceof Student) {
model = (Student) o; model = (Student) o;
} }
else { else {
model = new Student("", ""); model = new Student("", "", null);
} }
this.model = model; this.model = model;
@ -38,30 +47,31 @@ public class StudentVM implements PropertyChangeListener {
// load into self // load into self
setFirstname(model.getFirstname()); setFirstname(model.getFirstname());
setLastname(model.getLastname()); setLastname(model.getLastname());
setHairColor(ColorConverter.toJFXColor(model.getHairColor()));
// subscribe // subscribe
model.addListener(this); model.addListener(this);
// promise to update // promise to update
firstnameProperty().addListener((__, ___, newV) -> model.setFirstname(newV)); firstnameProperty().addListener((__, ___, newV) -> model.setFirstname(newV));
hairColorProperty().addListener((__, ___, newV) -> model.setHairColor(ColorConverter.toHairColor(newV)));
} }
public StudentVM(String lastname, String firstname) { public StudentVM(String lastname, String firstname, Color JFXHairColor) {
this(new Student(lastname, firstname)); this(new Student(lastname, firstname, ColorConverter.toHairColor(JFXHairColor)));
} }
public StudentVM(Student model) { public StudentVM(Student model) {
this((Object) model); this((Object) model);
} }
public StudentVM() {
this(null);
}
@Override @Override
public void propertyChange(PropertyChangeEvent evt) { public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(Student.PROP_STUDENT_FIRSTNAME)) { if (evt.getPropertyName().equals(Student.PROP_STUDENT_FIRSTNAME)) {
setFirstname((String) evt.getNewValue()); setFirstname((String) evt.getNewValue());
} }
if (evt.getPropertyName().equals(Student.PROP_STUDENT_FIRSTNAME)) {
setHairColor(ColorConverter.toJFXColor(evt.getNewValue()));
}
} }
} }

Loading…
Cancel
Save