feat: Added logging and refactored code to accommodate

This commit is contained in:
David Guler 2022-11-08 07:36:31 +01:00
parent ad05e9e95a
commit 590049b9cf
7 changed files with 57 additions and 59 deletions

View File

@ -4,7 +4,6 @@ import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
@ -14,12 +13,15 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MainFXMLController implements Initializable {
/**
* Caching the panes
*/
private final Map<String, AnchorPane> panes = new HashMap<>();
private static final Logger LOG = Logger.getLogger(MainFXMLController.class.getName());
@FXML
private Button home_button;
@ -100,7 +102,7 @@ public class MainFXMLController implements Initializable {
loadPane("Home.fxml");
styleChangeButton(home_button);
} catch (IOException e) {
e.printStackTrace();
LOG.log(Level.SEVERE, "Failed to load FXML-Pane!", e);
}
}
}

View File

@ -7,8 +7,6 @@ import ch.zhaw.gartenverwaltung.types.Plant;
import ch.zhaw.gartenverwaltung.types.Seasons;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
@ -29,8 +27,11 @@ import java.net.URL;
import java.util.List;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PlantsController implements Initializable {
private static final Logger LOG = Logger.getLogger(PlantsController.class.getName());
private final PlantListModel plantListModel = new PlantListModel();
private Plant selectedPlant = null;
private final HardinessZone DEFAULT_HARDINESS_ZONE = HardinessZone.ZONE_8A;
@ -98,8 +99,10 @@ public class PlantsController implements Initializable {
lookForSelectedListEntry();
try {
viewFilteredListBySearch();
} catch (HardinessZoneNotSetException | IOException e) {
e.printStackTrace();
} catch (HardinessZoneNotSetException e) {
LOG.log(Level.WARNING, "Hardiness Zone not set!");
} catch (IOException e) {
LOG.log(Level.WARNING, "Could not retrieve data!", e);
}
}
@ -107,7 +110,7 @@ public class PlantsController implements Initializable {
* set text of list view to plant name
*/
private void setListCellFactory() {
list_plants.setCellFactory(param -> new ListCell<Plant>() {
list_plants.setCellFactory(param -> new ListCell<>() {
@Override
protected void updateItem(Plant plant, boolean empty) {
super.updateItem(plant, empty);
@ -143,13 +146,15 @@ public class PlantsController implements Initializable {
search_plants.textProperty().addListener((observable, oldValue, newValue) -> {
if (newValue.isEmpty()) {
fillPlantListWithHardinessZone();
}else {
} else {
try {
List<Plant> filteredPlants = plantListModel.getFilteredPlantListByString(DEFAULT_HARDINESS_ZONE, newValue);
clearListView();
plantListProperty.addAll(filteredPlants);
} catch (HardinessZoneNotSetException | IOException e) {
e.printStackTrace();
} catch (HardinessZoneNotSetException e) {
LOG.log(Level.WARNING, "Hardiness Zone not set!");
} catch (IOException e) {
LOG.log(Level.WARNING, "Could not retrieve data!", e);
}
}
});
@ -163,8 +168,10 @@ public class PlantsController implements Initializable {
try {
clearListView();
plantListProperty.addAll(plantListModel.getPlantList(plantListModel.getCurrentZone()));
} catch (HardinessZoneNotSetException | IOException e) {
e.printStackTrace();
} catch (HardinessZoneNotSetException e) {
LOG.log(Level.WARNING, "Hardiness Zone not set!");
} catch (IOException e) {
LOG.log(Level.WARNING, "Could not retrieve data!", e);
}
}
@ -182,12 +189,9 @@ public class PlantsController implements Initializable {
if (zone.equals(DEFAULT_HARDINESS_ZONE)) {
radioButton.setSelected(true);
}
radioButton.selectedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
radioButton.selectedProperty().addListener((observable, oldValue, newValue) -> {
plantListModel.setCurrentZone(zone);
fillPlantListWithHardinessZone();
}
});
climate_zones.getChildren().add(radioButton);
}
@ -207,20 +211,19 @@ public class PlantsController implements Initializable {
if (season.equals(Seasons.AllSEASONS)) {
radioButton.setSelected(true);
}
radioButton.selectedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
radioButton.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (season.equals(Seasons.AllSEASONS)) {
fillPlantListWithHardinessZone();
} else {
try {
viewFilteredListBySeason(season);
} catch (HardinessZoneNotSetException | IOException e) {
e.printStackTrace();
} catch (HardinessZoneNotSetException e) {
LOG.log(Level.WARNING, "Hardiness Zone not set!");
} catch (IOException e) {
LOG.log(Level.WARNING, "Could not retrieve data!", e);
}
}
}
});
seasons.getChildren().add(radioButton);
}
@ -237,27 +240,24 @@ public class PlantsController implements Initializable {
selectSowDay_button.setDisable(true);
Image img = new Image(String.valueOf(PlantsController.class.getResource("placeholder.png")));
img_plant.setImage(img);
list_plants.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Plant>() {
@Override
public void changed(ObservableValue<? extends Plant> observable, Plant oldValue, Plant newValue) {
list_plants.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if(newValue != null) {
selectedPlant = newValue;
description_plant.setText(selectedPlant.description());
selectSowDay_button.setDisable(false);
Image img;
Image img1;
if(selectedPlant.image() != null) {
img = selectedPlant.image();
img1 = selectedPlant.image();
} else {
img = new Image(String.valueOf(PlantsController.class.getResource("placeholder.png")));
img1 = new Image(String.valueOf(PlantsController.class.getResource("placeholder.png")));
}
img_plant.setImage(img);
img_plant.setImage(img1);
} else {
selectedPlant = null;
description_plant.setText("");
selectSowDay_button.setDisable(true);
Image img = new Image(String.valueOf(PlantsController.class.getResource("placeholder.png")));
img_plant.setImage(img);
}
Image img1 = new Image(String.valueOf(PlantsController.class.getResource("placeholder.png")));
img_plant.setImage(img1);
}
});
}

View File

@ -145,7 +145,6 @@ public class JsonGardenPlan implements GardenPlan {
.registerModule(new Jdk8Module())
.writeValue(new File(dataSource.toURI()), cropMap.values());
} catch (URISyntaxException e) {
// TODO: Log
throw new IOException(INVALID_DATASOURCE_MSG, e);
}
}

View File

@ -16,13 +16,12 @@ public class GrowthPhaseTypeDeserializer extends StdDeserializer<GrowthPhaseType
@Override
public GrowthPhaseType deserialize(JsonParser parser, DeserializationContext context) throws IOException {
GrowthPhaseType result = null;
GrowthPhaseType result;
String token = parser.getText();
try {
result = GrowthPhaseType.valueOf(token.toUpperCase());
} catch (IllegalArgumentException e) {
// TODO: Log
System.err.printf("Bad growth phase type \"%s\"\n", token);
throw new IOException(String.format("Bad growth phase type \"%s\"\n", token));
}
return result;
}

View File

@ -17,13 +17,12 @@ public class HardinessZoneDeserializer extends StdDeserializer<HardinessZone> {
@Override
public HardinessZone deserialize(JsonParser parser, DeserializationContext context) throws IOException {
HardinessZone result = null;
HardinessZone result;
String token = parser.getText();
try {
result = HardinessZone.valueOf(token.toUpperCase());
} catch (IllegalArgumentException e) {
// TODO: Log
System.err.printf("Unknown Hardiness Zone \"%s\"\n", token);
throw new IOException(String.format("Unknown Hardiness Zone \"%s\"\n", token), e);
}
return result;
}

View File

@ -23,9 +23,7 @@ public class PlantImageDeserializer extends JsonDeserializer<Image> {
try (InputStream is = new FileInputStream(new File(imageUrl.toURI()))) {
result = new Image(is);
} catch (IllegalArgumentException | URISyntaxException e) {
// TODO: Log
e.printStackTrace();
System.err.printf("Cannot find Image \"%s\"\n", imageUrl.getFile());
throw new IOException(String.format("Cannot find Image \"%s\"\n", imageUrl.getFile()));
}
}
return result;

View File

@ -4,6 +4,7 @@ module ch.zhaw.gartenverwaltung {
requires com.fasterxml.jackson.databind;
requires com.fasterxml.jackson.datatype.jsr310;
requires com.fasterxml.jackson.datatype.jdk8;
requires java.logging;
opens ch.zhaw.gartenverwaltung to javafx.fxml;
opens ch.zhaw.gartenverwaltung.types to com.fasterxml.jackson.databind;