update json files for crop and task with ui
This commit is contained in:
parent
e22cb0b24d
commit
b3f839e4a3
|
@ -87,7 +87,7 @@ public class CropDetailController {
|
|||
private ListView<Pest> pests_listView;
|
||||
|
||||
@FXML
|
||||
void addTask() throws IOException {
|
||||
void addTask() throws IOException, HardinessZoneNotSetException {
|
||||
createTaskDialog(true, null);
|
||||
}
|
||||
|
||||
|
@ -136,7 +136,7 @@ public class CropDetailController {
|
|||
if (plant.image() != null) {
|
||||
imageView.setImage(plant.image());
|
||||
}
|
||||
area_label.setText("");
|
||||
area_label.setText(String.valueOf(crop.getArea()));
|
||||
location_label.setText("");
|
||||
|
||||
setTaskListProperty(crop);
|
||||
|
@ -255,7 +255,7 @@ public class CropDetailController {
|
|||
return (event) -> {
|
||||
try {
|
||||
createTaskDialog(false, task);
|
||||
} catch (IOException e) {
|
||||
} catch (IOException | HardinessZoneNotSetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
};
|
||||
|
@ -267,7 +267,7 @@ public class CropDetailController {
|
|||
};
|
||||
}
|
||||
|
||||
private void createTaskDialog(boolean newTask, Task givenTask) throws IOException {
|
||||
private void createTaskDialog(boolean newTask, Task givenTask) throws IOException, HardinessZoneNotSetException {
|
||||
Dialog<Task> dialog = new Dialog<>();
|
||||
dialog.setTitle("Set Task");
|
||||
dialog.setHeaderText("Add/Edit Task:");
|
||||
|
@ -289,7 +289,7 @@ public class CropDetailController {
|
|||
if (!newTask) {
|
||||
controller.setTaskValue(givenTask);
|
||||
}
|
||||
dialog.setResultConverter(button -> button.equals(saveTask) ? controller.returnResult() : null);
|
||||
dialog.setResultConverter(button -> button.equals(saveTask) ? controller.returnResult(this.crop) : null);
|
||||
|
||||
dialog.showAndWait()
|
||||
.ifPresent(task -> {
|
||||
|
@ -334,8 +334,11 @@ public class CropDetailController {
|
|||
//ToDo method to set location
|
||||
location_label.setText(string);
|
||||
} else {
|
||||
System.out.println(string);
|
||||
//ToDo method to set area of crop in garden
|
||||
try {
|
||||
garden.updateCrop(this.crop.withArea(Double.parseDouble(string)));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
area_label.setText(string);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -51,7 +51,6 @@ public class MyGardenController {
|
|||
@AfterInject
|
||||
@SuppressWarnings("unused")
|
||||
public void init() {
|
||||
System.out.println("once");
|
||||
setIconToButton(addPlant_button, "addIcon.png");
|
||||
myGarden_listView.itemsProperty().bind(garden.getPlantedCrops());
|
||||
setCellFactory();
|
||||
|
|
|
@ -1,23 +1,35 @@
|
|||
package ch.zhaw.gartenverwaltung;
|
||||
|
||||
import ch.zhaw.gartenverwaltung.bootstrap.AfterInject;
|
||||
import ch.zhaw.gartenverwaltung.bootstrap.Inject;
|
||||
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
|
||||
import ch.zhaw.gartenverwaltung.io.PlantList;
|
||||
import ch.zhaw.gartenverwaltung.models.Garden;
|
||||
import ch.zhaw.gartenverwaltung.models.GardenSchedule;
|
||||
import ch.zhaw.gartenverwaltung.types.Crop;
|
||||
import ch.zhaw.gartenverwaltung.types.Plant;
|
||||
import ch.zhaw.gartenverwaltung.types.Task;
|
||||
import javafx.beans.binding.Binding;
|
||||
import javafx.beans.binding.Bindings;
|
||||
import javafx.beans.binding.BooleanBinding;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.Initializable;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.util.Callback;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class TaskFormularController implements Initializable {
|
||||
private Crop crop;
|
||||
private Plant plant;
|
||||
private Task task = null;
|
||||
|
||||
@Inject
|
||||
private GardenSchedule gardenSchedule;
|
||||
@Inject
|
||||
private Garden garden;
|
||||
@Inject
|
||||
private PlantList plantList;
|
||||
|
||||
@FXML
|
||||
private TextArea description_area;
|
||||
|
@ -34,19 +46,28 @@ public class TaskFormularController implements Initializable {
|
|||
@FXML
|
||||
private TextField taskName_field;
|
||||
|
||||
@AfterInject
|
||||
@SuppressWarnings("unused")
|
||||
|
||||
public Task returnResult() {
|
||||
public Task returnResult(Crop crop) {
|
||||
int interval = 0;
|
||||
if (!(interval_field.getText().isEmpty() || interval_field.getText().equals(""))) {
|
||||
interval = Integer.parseInt(interval_field.getText());
|
||||
}
|
||||
Task task = new Task(taskName_field.getText(), description_area.getText(),
|
||||
start_datePicker.getValue(), end_datePicker.getValue(),
|
||||
Integer.parseInt(interval_field.getText()), crop.getCropId().get());
|
||||
interval, crop.getCropId().get());
|
||||
if (this.task != null) return this.task.updateTask(task);
|
||||
return task;
|
||||
}
|
||||
|
||||
public void setCorp(Crop crop) {
|
||||
public void setCorp(Crop crop) throws HardinessZoneNotSetException, IOException {
|
||||
this.crop = crop;
|
||||
this.plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get();
|
||||
}
|
||||
|
||||
public void setTaskValue(Task task) {
|
||||
this.task = task;
|
||||
taskName_field.setText(task.getName());
|
||||
description_area.setText(task.getDescription());
|
||||
start_datePicker.setValue(task.getStartDate());
|
||||
|
@ -69,7 +90,7 @@ public class TaskFormularController implements Initializable {
|
|||
setDisable(true);
|
||||
setStyle("-fx-background-color: #ffc0cb;");
|
||||
|
||||
if (item.compareTo(today) > 0 && item.compareTo(crop.getStartDate()) > 0) {
|
||||
if (item.compareTo(today) > 0 && item.compareTo(crop.getStartDate()) > 0 && item.compareTo(crop.getStartDate().plusDays(plant.timeToHarvest(0))) < 0) {
|
||||
setDisable(false);
|
||||
setStyle("-fx-background-color: #32CD32;");
|
||||
}
|
||||
|
@ -93,7 +114,7 @@ public class TaskFormularController implements Initializable {
|
|||
setDisable(true);
|
||||
setStyle("-fx-background-color: #ffc0cb;");
|
||||
|
||||
if (item.compareTo(today) > 0 && item.compareTo(crop.getStartDate()) > 0) {
|
||||
if (item.compareTo(today) > 0 && item.compareTo(crop.getStartDate()) > 0 && item.compareTo(crop.getStartDate().plusDays(plant.timeToHarvest(0))) < 0) {
|
||||
setDisable(false);
|
||||
setStyle("-fx-background-color: #32CD32;");
|
||||
}
|
||||
|
@ -114,12 +135,8 @@ public class TaskFormularController implements Initializable {
|
|||
});
|
||||
|
||||
button.disableProperty().bind(start_datePicker.valueProperty().isNull()
|
||||
.or(end_datePicker.valueProperty().isNull())
|
||||
.or(taskName_field.textProperty().isEmpty())
|
||||
.or(description_area.textProperty().isEmpty())
|
||||
.or(interval_field.textProperty().isEmpty()));
|
||||
|
||||
|
||||
.or(description_area.textProperty().isEmpty()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -27,6 +27,15 @@ public class TextFieldFormularController {
|
|||
}
|
||||
|
||||
public void initSaveButton(Button button) {
|
||||
text_area.textProperty().addListener((observable, oldValue, newValue) -> {
|
||||
if (newValue.matches("\\d*\\.?\\d*")) {
|
||||
text_area.setText(newValue);
|
||||
} else {
|
||||
text_area.setText(oldValue);
|
||||
}
|
||||
});
|
||||
|
||||
button.disableProperty().bind(text_area.textProperty().isEmpty());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -70,6 +70,19 @@ public class Garden {
|
|||
plantedCrops.clear();
|
||||
plantedCrops.addAll(cropList.getCrops());
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the {@link Crop} from the file and the cache
|
||||
*
|
||||
* @param crop The crop which is being updated
|
||||
* @throws IOException If the database cannot be accessed
|
||||
*/
|
||||
public void updateCrop(Crop crop) throws IOException {
|
||||
cropList.saveCrop(crop);
|
||||
plantedCrops.clear();
|
||||
plantedCrops.addAll(cropList.getCrops());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of {@link Crop}s which are currently in the gardenplan.
|
||||
*
|
||||
|
|
|
@ -10,9 +10,9 @@ import java.util.Optional;
|
|||
*/
|
||||
public class Task {
|
||||
private Long id;
|
||||
private final String name;
|
||||
private final String description;
|
||||
private final LocalDate startDate;
|
||||
private String name;
|
||||
private String description;
|
||||
private LocalDate startDate;
|
||||
private Integer interval;
|
||||
private LocalDate endDate;
|
||||
private long cropId;
|
||||
|
@ -74,4 +74,14 @@ public class Task {
|
|||
public Optional<LocalDate> getEndDate() {
|
||||
return Optional.ofNullable(endDate);
|
||||
}
|
||||
|
||||
public Task updateTask(Task task) {
|
||||
this.name = task.getName();
|
||||
this.description = task.getDescription();
|
||||
this.startDate = task.getStartDate();
|
||||
this.endDate = task.getEndDate().orElse(null);
|
||||
this.interval = task.getInterval().orElse(0);
|
||||
this.cropId = task.getCropId();
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue