parent
3505f380ec
commit
ba93364bed
@ -1,14 +1,62 @@
|
|||||||
package model;
|
package model;
|
||||||
|
|
||||||
|
import java.beans.PropertyChangeListener;
|
||||||
|
import java.beans.PropertyChangeSupport;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Promotion implements Serializable {
|
public class Promotion implements Serializable {
|
||||||
|
public static final String PROP_PROMOTION_ADD_STUDENT = "model.Promotion.addStudent";
|
||||||
|
public static final String PROP_PROMOTION_RMV_STUDENT = "model.Promotion.removeStudent";
|
||||||
|
|
||||||
private final List<Student> students = new ArrayList<>();
|
private final List<Student> students = new ArrayList<>();
|
||||||
|
|
||||||
public List<Student> getStudents() {
|
public List<Student> getStudents() {
|
||||||
return Collections.unmodifiableList(students);
|
return Collections.unmodifiableList(students);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addStudent(Student toAdd) {
|
||||||
|
if (toAdd != null) {
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
students.add(i, toAdd);
|
||||||
|
// fire
|
||||||
|
getSupport().fireIndexedPropertyChange(
|
||||||
|
PROP_PROMOTION_ADD_STUDENT,
|
||||||
|
i,
|
||||||
|
students.size() > i + 1 ? students.get(i + 1) : null,
|
||||||
|
toAdd
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeStudent(Student toRemove) {
|
||||||
|
if (toRemove != null) {
|
||||||
|
int i = students.indexOf(toRemove);
|
||||||
|
|
||||||
|
students.remove(i);
|
||||||
|
// fire
|
||||||
|
getSupport().fireIndexedPropertyChange(
|
||||||
|
PROP_PROMOTION_RMV_STUDENT,
|
||||||
|
i,
|
||||||
|
toRemove,
|
||||||
|
students.size() > i ? students.get(i) : null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PropertyChangeSupport support = null;
|
||||||
|
|
||||||
|
public PropertyChangeSupport getSupport() {
|
||||||
|
if (support == null) {
|
||||||
|
support = new PropertyChangeSupport(this);
|
||||||
|
}
|
||||||
|
return support;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addListener(PropertyChangeListener listener) {
|
||||||
|
getSupport().addPropertyChangeListener(listener);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,31 +1,59 @@
|
|||||||
package model;
|
package model;
|
||||||
|
|
||||||
|
import java.beans.PropertyChangeListener;
|
||||||
|
import java.beans.PropertyChangeSupport;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
public class Student implements Serializable {
|
public class Student implements Serializable {
|
||||||
|
|
||||||
|
public static final String PROP_STUDENT_LASTNAME = "model.Student.lastname";
|
||||||
|
public static final String PROP_STUDENT_FIRSTNAME = "model.Student.firstname";
|
||||||
|
|
||||||
private String lastname;
|
private String lastname;
|
||||||
|
|
||||||
private String firstName;
|
private String firstname;
|
||||||
|
|
||||||
public Student(String lastname, String firstName) {
|
public Student(String lastname, String firstName) {
|
||||||
this.lastname = !lastname.isEmpty() ? lastname : "DOE";
|
this.lastname = !lastname.isEmpty() ? lastname : "DOE";
|
||||||
this.firstName = !firstName.isEmpty() ? firstName : "John";
|
this.firstname = !firstName.isEmpty() ? firstName : "John";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLastname() {
|
public String getLastname() {
|
||||||
return lastname;
|
return lastname;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFirstName() {
|
public String getFirstname() {
|
||||||
return firstName;
|
return firstname;
|
||||||
}
|
|
||||||
|
|
||||||
public void setFirstName(String firstName) {
|
|
||||||
this.firstName = firstName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLastname(String lastname) {
|
public void setLastname(String lastname) {
|
||||||
|
if(lastname != null) {
|
||||||
|
String oldV = getLastname();
|
||||||
this.lastname = lastname;
|
this.lastname = lastname;
|
||||||
|
// fire
|
||||||
|
getSupport().firePropertyChange(PROP_STUDENT_LASTNAME, oldV, getLastname());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstname(String firstname) {
|
||||||
|
if(firstname != null) {
|
||||||
|
String oldV = getFirstname();
|
||||||
|
this.firstname = firstname;
|
||||||
|
// fire
|
||||||
|
getSupport().firePropertyChange(PROP_STUDENT_FIRSTNAME, oldV, getFirstname());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private PropertyChangeSupport support = null;
|
||||||
|
|
||||||
|
public PropertyChangeSupport getSupport() {
|
||||||
|
if (support == null) {
|
||||||
|
support = new PropertyChangeSupport(this);
|
||||||
|
}
|
||||||
|
return support;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addListener(PropertyChangeListener listener) {
|
||||||
|
getSupport().addPropertyChangeListener(listener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,29 @@
|
|||||||
|
package view;
|
||||||
|
|
||||||
|
import javafx.scene.control.Label;
|
||||||
|
import javafx.scene.control.ListCell;
|
||||||
|
import javafx.scene.layout.BorderPane;
|
||||||
|
import viewmodel.StudentVM;
|
||||||
|
|
||||||
|
public class StudentVMCell extends ListCell<StudentVM> {
|
||||||
|
@Override
|
||||||
|
protected void updateItem(StudentVM item, boolean empty) {
|
||||||
|
super.updateItem(item, empty);
|
||||||
|
if (!empty) {
|
||||||
|
|
||||||
|
Label lastnameLbl = new Label();
|
||||||
|
Label firstnameLbl = new Label();
|
||||||
|
lastnameLbl.textProperty().bind(item.lastnameProperty());
|
||||||
|
firstnameLbl.textProperty().bind(item.firstnameProperty());
|
||||||
|
|
||||||
|
BorderPane pane = new BorderPane();
|
||||||
|
pane.setLeft(lastnameLbl);
|
||||||
|
pane.setCenter(firstnameLbl);
|
||||||
|
|
||||||
|
setGraphic(pane);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
setGraphic(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,28 +1,70 @@
|
|||||||
package viewmodel;
|
package viewmodel;
|
||||||
|
|
||||||
|
import javafx.beans.property.ListProperty;
|
||||||
|
import javafx.beans.property.ReadOnlyListProperty;
|
||||||
|
import javafx.beans.property.SimpleListProperty;
|
||||||
|
import javafx.collections.FXCollections;
|
||||||
|
import javafx.collections.ObservableList;
|
||||||
import model.Promotion;
|
import model.Promotion;
|
||||||
|
|
||||||
public class PromotionVM {
|
import java.beans.IndexedPropertyChangeEvent;
|
||||||
|
import java.beans.PropertyChangeEvent;
|
||||||
|
import java.beans.PropertyChangeListener;
|
||||||
|
|
||||||
private Promotion model;
|
public class PromotionVM implements PropertyChangeListener {
|
||||||
|
|
||||||
|
private final Promotion model;
|
||||||
|
|
||||||
|
public Promotion getModel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
public PromotionVM(Object o) {
|
public PromotionVM(Object o) {
|
||||||
|
Promotion model;
|
||||||
|
if (o instanceof Promotion) {
|
||||||
|
model = (Promotion) o;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
model = new Promotion();
|
||||||
|
}
|
||||||
|
this.model = model;
|
||||||
|
|
||||||
|
// load
|
||||||
|
model.getStudents().forEach(s -> studentsVMObs.add(new StudentVM(s)));
|
||||||
|
|
||||||
|
// subscribe
|
||||||
|
model.addListener(this);
|
||||||
|
|
||||||
|
// promise to update
|
||||||
|
// ... in methods
|
||||||
}
|
}
|
||||||
|
|
||||||
public PromotionVM() {
|
public PromotionVM() {
|
||||||
|
this(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Promotion getModel() {
|
public void addStudentVM(StudentVM toAdd) {
|
||||||
return model;
|
model.addStudent(toAdd.getModel());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addStudent(StudentVM toAdd) {
|
public void addStudentVM() {addStudentVM(new StudentVM());}
|
||||||
|
|
||||||
|
public void removeStudentVM(StudentVM toRemove) {
|
||||||
|
model.removeStudent(toRemove.getModel());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addStudent() { addStudent(new StudentVM()); }
|
private final ObservableList<StudentVM> studentsVMObs = FXCollections.observableArrayList();
|
||||||
|
private final ListProperty<StudentVM> studentsVM = new SimpleListProperty<>(studentsVMObs);
|
||||||
|
public ObservableList<StudentVM> getStudentsVM() {return FXCollections.unmodifiableObservableList(studentsVM.get());}
|
||||||
|
public ReadOnlyListProperty<StudentVM> studentsVMProperty() {return studentsVM;}
|
||||||
|
|
||||||
public void removeStudent(StudentVM toRemove) {
|
@Override
|
||||||
|
public void propertyChange(PropertyChangeEvent evt) {
|
||||||
|
if (evt.getPropertyName().equals(Promotion.PROP_PROMOTION_ADD_STUDENT)) {
|
||||||
|
studentsVMObs.add(((IndexedPropertyChangeEvent) evt).getIndex(), new StudentVM(evt.getNewValue()));
|
||||||
|
}
|
||||||
|
if (evt.getPropertyName().equals(Promotion.PROP_PROMOTION_RMV_STUDENT)) {
|
||||||
|
studentsVMObs.remove(((IndexedPropertyChangeEvent) evt).getIndex());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,76 @@
|
|||||||
package viewmodel;
|
package viewmodel;
|
||||||
|
|
||||||
public class StudentVM {
|
import javafx.beans.property.SimpleStringProperty;
|
||||||
|
import javafx.beans.property.StringProperty;
|
||||||
|
import model.Student;
|
||||||
|
|
||||||
|
import java.beans.PropertyChangeEvent;
|
||||||
|
import java.beans.PropertyChangeListener;
|
||||||
|
|
||||||
|
public class StudentVM implements PropertyChangeListener {
|
||||||
|
private final Student model;
|
||||||
|
|
||||||
|
public Student getModel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final StringProperty lastname = new SimpleStringProperty();
|
||||||
|
public String getLastname() {return lastname.get();}
|
||||||
|
public StringProperty lastnameProperty() {return lastname;}
|
||||||
|
public void setLastname(String lastname) {this.lastname.set(lastname);}
|
||||||
|
|
||||||
|
private final StringProperty firstname = new SimpleStringProperty();
|
||||||
|
public String getFirstname() {return firstname.get();}
|
||||||
|
public StringProperty firstnameProperty() {return firstname;}
|
||||||
|
public void setFirstname(String firstname) {this.firstname.set(firstname);}
|
||||||
|
|
||||||
|
public StudentVM(Object o) {
|
||||||
|
Student model;
|
||||||
|
if (o instanceof Student) {
|
||||||
|
model = (Student) o;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
model = new Student("", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.model = model;
|
||||||
|
|
||||||
|
// load into self
|
||||||
|
setFirstname(model.getFirstname());
|
||||||
|
setLastname(model.getLastname());
|
||||||
|
|
||||||
|
// subscribe
|
||||||
|
model.addListener(this);
|
||||||
|
|
||||||
|
// promise to update
|
||||||
|
firstnameProperty().addListener((__, ___, newV) -> model.setFirstname(newV));
|
||||||
|
lastnameProperty().addListener((__, ___, newV) -> model.setLastname(newV));
|
||||||
|
}
|
||||||
|
|
||||||
|
public StudentVM(String lastname, String firstname) {
|
||||||
|
this(new Student(lastname, firstname));
|
||||||
|
}
|
||||||
|
|
||||||
|
public StudentVM(Student model) {
|
||||||
|
this((Object) model);
|
||||||
|
}
|
||||||
|
|
||||||
|
public StudentVM() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void propertyChange(PropertyChangeEvent evt) {
|
||||||
|
if (evt.getPropertyName().equals(Student.PROP_STUDENT_LASTNAME)) {
|
||||||
|
setLastname(isNotEmptyOrToDashes((String) evt.getNewValue()));
|
||||||
|
}
|
||||||
|
if (evt.getPropertyName().equals(Student.PROP_STUDENT_FIRSTNAME)) {
|
||||||
|
setFirstname(isNotEmptyOrToDashes((String) evt.getNewValue()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String isNotEmptyOrToDashes(String stringEvt) {
|
||||||
|
return stringEvt.isEmpty() ? "---" : stringEvt;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue