🎨 Add creation window, make last name final, prettify

main
Alexis Drai 3 years ago
parent ba93364bed
commit 8bcc0132af

@ -22,7 +22,7 @@
<VBox>
<HBox>
<Label text="last name: "/>
<TextField fx:id="lastnameTF"/>
<Label fx:id="lastnameTF"/>
</HBox>
<HBox>
<Label text="first name: "/>

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml">
<VBox>
<HBox>
<Label text="LAST NAME"/>
<TextField fx:id="lastnameTF"/>
</HBox>
<HBox>
<Label text="first name"/>
<TextField fx:id="firstnameTF"/>
</HBox>
<HBox>
<Button text="OK" onAction="#clickCreate" disable="${lastnameTF.text.empty || firstnameTF.text.empty}"/>
<Button text="Cancel" onAction="#clickCancel"/>
</HBox>
</VBox>
</AnchorPane>

@ -1,9 +1,6 @@
package launcher;
import data.Loadable;
import data.Loader;
import data.Saver;
import data.Stub;
import data.*;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
@ -42,10 +39,12 @@ public class Launcher extends Application {
@Override
public void stop() throws Exception {
Savable saver;
try {
new Saver().save(VM);
saver = new Saver();
saver.save(VM);
} catch (IOException __) {
System.err.println("couls not save...");
System.err.println("could not save...");
}
super.stop();

@ -6,16 +6,15 @@ import java.io.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 final String lastname;
private String firstname;
public Student(String lastname, String firstName) {
this.lastname = !lastname.isEmpty() ? lastname : "DOE";
this.firstname = !firstName.isEmpty() ? firstName : "John";
this.lastname = !lastname.isEmpty() ? lastname : "_";
this.firstname = !firstName.isEmpty() ? firstName : "_";
}
public String getLastname() {
@ -26,15 +25,6 @@ public class Student implements Serializable {
return firstname;
}
public void setLastname(String lastname) {
if(lastname != null) {
String oldV = getLastname();
this.lastname = lastname;
// fire
getSupport().firePropertyChange(PROP_STUDENT_LASTNAME, oldV, getLastname());
}
}
public void setFirstname(String firstname) {
if(firstname != null) {
String oldV = getFirstname();

@ -1,27 +1,61 @@
package view;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Modality;
import javafx.stage.Stage;
import viewmodel.PromotionVM;
import viewmodel.StudentVM;
import java.io.IOException;
public class MainWindow {
@FXML
private ListView<StudentVM> promLV;
@FXML
private TextField lastnameTF;
private Label lastnameTF;
@FXML
private TextField firstnameTF;
@FXML
private void clickAddStudent() {
VM.addStudentVM();
prepareCreationWindow();
}
private void prepareCreationWindow() {
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()));
}
}
private StudentCreationWindow initCreationWindow(Stage stage) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/StudentCreationWindow.fxml"));
StudentCreationWindow controller = new StudentCreationWindow();
loader.setController(controller);
try {
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;
}
@FXML
private void clickRemoveStudent() {
VM.removeStudentVM(promLV.getSelectionModel().getSelectedItem());
lastnameTF.setText("");
firstnameTF.setText("");
}
@FXML
@ -45,11 +79,11 @@ public class MainWindow {
private void initPromLV() {
promLV.getSelectionModel().selectedItemProperty().addListener((__, oldV, newV) -> {
if (oldV != null) {
lastnameTF.textProperty().unbindBidirectional(oldV.lastnameProperty());
lastnameTF.textProperty().unbind();
firstnameTF.textProperty().unbindBidirectional(oldV.firstnameProperty());
}
if (newV != null) {
lastnameTF.textProperty().bindBidirectional(newV.lastnameProperty());
lastnameTF.textProperty().bind(newV.lastnameProperty());
firstnameTF.textProperty().bindBidirectional(newV.firstnameProperty());
}
});

@ -0,0 +1,41 @@
package view;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
public class StudentCreationWindow {
private String lastname;
private String firstname;
public String getLastname() {
return lastname;
}
public String getFirstname() {
return firstname;
}
@FXML
private TextField firstnameTF;
@FXML
private TextField lastnameTF;
@FXML
private void clickCreate() {
lastname = lastnameTF.getText();
firstname = firstnameTF.getText();
close();
}
@FXML
private void clickCancel() {
lastname = null;
firstname = null;
close();
}
private void close(){
firstnameTF.getScene().getWindow().hide();
}
}

@ -47,8 +47,6 @@ public class PromotionVM implements PropertyChangeListener {
model.addStudent(toAdd.getModel());
}
public void addStudentVM() {addStudentVM(new StudentVM());}
public void removeStudentVM(StudentVM toRemove) {
model.removeStudent(toRemove.getModel());
}

@ -17,7 +17,7 @@ public class StudentVM implements PropertyChangeListener {
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 void setLastname(String lastname) {this.lastname.set(lastname);}
private final StringProperty firstname = new SimpleStringProperty();
public String getFirstname() {return firstname.get();}
@ -44,7 +44,6 @@ public class StudentVM implements PropertyChangeListener {
// promise to update
firstnameProperty().addListener((__, ___, newV) -> model.setFirstname(newV));
lastnameProperty().addListener((__, ___, newV) -> model.setLastname(newV));
}
public StudentVM(String lastname, String firstname) {
@ -61,16 +60,8 @@ public class StudentVM implements PropertyChangeListener {
@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()));
setFirstname((String) evt.getNewValue());
}
}
private String isNotEmptyOrToDashes(String stringEvt) {
return stringEvt.isEmpty() ? "---" : stringEvt;
}
}

Loading…
Cancel
Save