🚀 Add hair color, go to bed

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

@ -26,7 +26,13 @@
</HBox>
<HBox>
<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>
</VBox>
</right>

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

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

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

@ -1,5 +1,7 @@
package model;
import utils.HairColor;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
@ -7,14 +9,18 @@ import java.io.Serializable;
public class Student implements Serializable {
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 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.firstname = !firstName.isEmpty() ? firstName : "_";
this.hairColor = hairColor != null ? hairColor : new HairColor(0.0, 0.0, 0.0);
}
public String getLastname() {
@ -25,6 +31,19 @@ public class Student implements Serializable {
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) {
if(firstname != null) {
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;
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.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import viewmodel.PromotionVM;
@ -18,21 +19,29 @@ public class MainWindow {
private Label lastnameTF;
@FXML
private TextField firstnameTF;
@FXML
private ColorPicker hairColorPicker;
@FXML
private void clickAddStudent() {
prepareCreationWindow();
displayAddWindow();
}
private void prepareCreationWindow() {
private void displayAddWindow() {
Stage stage = new Stage();
stage.initOwner(promLV.getScene().getWindow());
stage.initModality(Modality.WINDOW_MODAL);
StudentCreationWindow controller = initCreationWindow(stage);
if (controller.getLastname() != null && controller.getFirstname() != null) {
VM.addStudentVM(new StudentVM(controller.getLastname(), controller.getFirstname()));
if
(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.showAndWait();
} catch (IOException ex) {
ex.printStackTrace();
new Alert(Alert.AlertType.ERROR, ex.getMessage(), ButtonType.OK).showAndWait();
}
return controller;
@ -56,6 +64,7 @@ public class MainWindow {
VM.removeStudentVM(promLV.getSelectionModel().getSelectedItem());
lastnameTF.setText("");
firstnameTF.setText("");
hairColorPicker.setValue(Color.WHITE);
}
@FXML
@ -73,7 +82,7 @@ public class MainWindow {
private void initialize() {
promLV.itemsProperty().bind(VM.studentsVMProperty());
initPromLV();
promLV.setCellFactory(__ -> new StudentVMCell());
promLV.setCellFactory(__ -> new StudentCell());
}
private void initPromLV() {
@ -81,10 +90,12 @@ public class MainWindow {
if (oldV != null) {
lastnameTF.textProperty().unbind();
firstnameTF.textProperty().unbindBidirectional(oldV.firstnameProperty());
hairColorPicker.valueProperty().unbindBidirectional(oldV.hairColorProperty());
}
if (newV != null) {
lastnameTF.textProperty().bind(newV.lastnameProperty());
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.ListCell;
import javafx.scene.layout.BorderPane;
import javafx.scene.shape.Rectangle;
import viewmodel.StudentVM;
public class StudentVMCell extends ListCell<StudentVM> {
public class StudentCell extends ListCell<StudentVM> {
@Override
protected void updateItem(StudentVM item, boolean empty) {
super.updateItem(item, empty);
@ -13,12 +14,17 @@ public class StudentVMCell extends ListCell<StudentVM> {
Label lastnameLbl = new Label();
Label firstnameLbl = new Label();
Rectangle hairClrRect = new Rectangle();
lastnameLbl.textProperty().bind(item.lastnameProperty());
firstnameLbl.textProperty().bind(item.firstnameProperty());
hairClrRect.setHeight(10.0);
hairClrRect.setWidth(10.0);
hairClrRect.fillProperty().bind(item.hairColorProperty());
BorderPane pane = new BorderPane();
pane.setLeft(lastnameLbl);
pane.setCenter(firstnameLbl);
pane.setRight(hairClrRect);
setGraphic(pane);
}

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

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

Loading…
Cancel
Save