#48 and #47 basics of gui

This commit is contained in:
giavaphi 2022-11-05 23:50:45 +01:00
parent f6ec411b7a
commit 3077e02b32
2 changed files with 122 additions and 38 deletions

View File

@ -1,16 +1,23 @@
package ch.zhaw.gartenverwaltung; package ch.zhaw.gartenverwaltung;
import ch.zhaw.gartenverwaltung.types.Crop;
import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Plant;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle; import java.util.ResourceBundle;
public class MyPlantsController implements Initializable { public class MyPlantsController implements Initializable {
@ -30,14 +37,14 @@ public class MyPlantsController implements Initializable {
@Override @Override
public void initialize(URL url, ResourceBundle resourceBundle) { public void initialize(URL url, ResourceBundle resourceBundle) {
//ToDo //ToDo
List<Plant> myPlants = getMyPlants(); List<Crop> cropList = getCropList();
createPlantView(myPlants); createPlantView(cropList);
} }
private void createPlantView(List<Plant> myPlants) { private void createPlantView(List<Crop> crops) {
//ToDo for(Crop crop : crops) {
for(Plant plant : myPlants) { HBox hBox = createPlantView(crop);
createPlantView(); myPlants_vbox.getChildren().add(hBox);
} }
} }
@ -45,14 +52,60 @@ public class MyPlantsController implements Initializable {
mainController = controller; mainController = controller;
} }
private List<Plant> getMyPlants() { private List<Crop> getCropList() {
//ToDo method to get myPlantList(scheduled) //ToDo method to get cropList
//Method to call all Plants saved //Method to get Crop List
List<Plant> myPlantList = new LinkedList<>(); List<Crop> cropList = new LinkedList<>();
return myPlantList; return cropList;
} }
private void createPlantView() { private HBox createPlantView(Crop crop) {
//ToDo FXML Panel with Plant data //ToDo add dynamic design
HBox hBox = new HBox();
Label label = new Label("Placeholder");
Button details = new Button("Details");
Button delete = new Button("delete");
details.setOnAction(getGoToCropDetailEvent(crop));
delete.setOnAction(getDeleteCropEvent(crop));
hBox.getChildren().addAll(label, details, delete);
return hBox;
}
private EventHandler<ActionEvent> getGoToCropDetailEvent(Crop crop) {
EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
//ToDo uncomment when new FXML exists
/*try {
mainController.loadPane("");
} catch (IOException e) {
e.printStackTrace();
}*/
}
};
return event;
}
private EventHandler<ActionEvent> getDeleteCropEvent(Crop crop) {
EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
showConfirmation(crop);
}
};
return event;
}
private void showConfirmation(Crop crop) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Delete Crop");
alert.setHeaderText("Are you sure want to delete this Crop?");
alert.setContentText("placeholder");
Optional<ButtonType> option = alert.showAndWait();
if (option.get() == ButtonType.OK) {
//ToDo Method to remove crop
}
} }
} }

View File

@ -1,14 +1,19 @@
package ch.zhaw.gartenverwaltung; package ch.zhaw.gartenverwaltung;
import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Crop;
import ch.zhaw.gartenverwaltung.types.Task;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.value.ChangeListener; import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue; import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.ListCell; import javafx.scene.control.ListCell;
import javafx.scene.control.ListView; import javafx.scene.control.ListView;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import java.net.URL; import java.net.URL;
import java.time.LocalDate; import java.time.LocalDate;
@ -17,7 +22,9 @@ import java.util.List;
import java.util.ResourceBundle; import java.util.ResourceBundle;
public class MyScheduleController implements Initializable { public class MyScheduleController implements Initializable {
private Plant selectedPlant = null; private Crop selectedCrop = null;
private final ListProperty<Crop> cropListProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
@FXML @FXML
private Label day1_label; private Label day1_label;
@ -65,28 +72,26 @@ public class MyScheduleController implements Initializable {
private Label information_label; private Label information_label;
@FXML @FXML
private ListView<Plant> scheduledPlants_listview; private ListView<Crop> scheduledPlants_listview;
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
List<Plant> plantList = new LinkedList<>(); //ToDo method to load crop list
fillListViewMyPlantsInSchedule(plantList); List<Crop> cropList = new LinkedList<>();
getSelectedPlantTask(); setCellFactoryListView();
scheduledPlants_listview.itemsProperty().bind(cropListProperty);
lookForSelectedListEntries();
setDayLabels(); setDayLabels();
information_label.setText(""); information_label.setText("");
loadTaskList();
} }
private void getSelectedPlantTask() { private void lookForSelectedListEntries() {
scheduledPlants_listview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Plant>() { scheduledPlants_listview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Crop>() {
@Override @Override
public void changed(ObservableValue<? extends Plant> observable, Plant oldValue, Plant newValue) { public void changed(ObservableValue<? extends Crop> observable, Crop oldValue, Crop newValue) {
if(newValue != null) { selectedCrop = newValue;
selectedPlant = newValue; loadTaskList();
//ToDo update day<x>_panel with task for the day
} else {
selectedPlant = null;
//ToDo update day<x>_panel with task for the day (all plants)
}
} }
}); });
} }
@ -102,23 +107,49 @@ public class MyScheduleController implements Initializable {
day7_label.setText(today.plusDays(6).getDayOfWeek().toString()); day7_label.setText(today.plusDays(6).getDayOfWeek().toString());
} }
private void fillListViewMyPlantsInSchedule(List<Plant> list) { private void setCellFactoryListView() {
for (Plant plant : list) { scheduledPlants_listview.setCellFactory(param -> new ListCell<Crop>() {
scheduledPlants_listview.getItems().add(plant);
}
scheduledPlants_listview.setCellFactory(param -> new ListCell<Plant>() {
@Override @Override
protected void updateItem(Plant plant, boolean empty) { protected void updateItem(Crop crop, boolean empty) {
super.updateItem(plant, empty); super.updateItem(crop, empty);
if (empty || plant == null || plant.name() == null) { if (empty || crop == null) {
setText(null); setText(null);
} else { } else {
setText(plant.name()); setText("placeholder");
} }
} }
}); });
} }
private void loadTaskList() {
//ToDo load task list according to selectedCrop
List<List<Task>> taskLists = new LinkedList<>();
if (selectedCrop != null) {
} else {
}
if (!taskLists.isEmpty()) {
viewTaskListOfDay(day1_pane, taskLists.get(0));
viewTaskListOfDay(day2_pane, taskLists.get(1));
viewTaskListOfDay(day3_pane, taskLists.get(2));
viewTaskListOfDay(day4_pane, taskLists.get(3));
viewTaskListOfDay(day5_pane, taskLists.get(4));
viewTaskListOfDay(day6_pane, taskLists.get(5));
viewTaskListOfDay(day7_pane, taskLists.get(6));
}
}
private void viewTaskListOfDay(Pane pane, List<Task> tasks) {
//ToDo update pane with task list
VBox vBox = new VBox();
for (Task task : tasks) {
Label label = new Label("placeholder");
vBox.getChildren().add(label);
}
pane.getChildren().add(vBox);
}
} }