From: Robin Cheney Date: Fri, 21 Nov 2025 10:25:19 +0000 (+0100) Subject: Working better, many Bugfixes X-Git-Url: https://git.eternal.ddnss.de/?a=commitdiff_plain;h=20e0c139fc716b1e77e974232e88660a64415f77;p=tankstelle.git Working better, many Bugfixes --- diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/FuelType.java b/src/main/java/de/diejungsvondertanke/tankstelle/FuelType.java index 24c36b0..31e1fde 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/FuelType.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/FuelType.java @@ -9,22 +9,28 @@ public enum FuelType { /** * Classic Super 95 gasoline */ - SUPER, + SUPER("Super 95"), /** * Diesel (hopefully lead-free) */ - DIESEL, + DIESEL("Diesel"), /** * If you want to feel like saving the planet: Now with at most 10% ethanol in * your gasoline */ - SUPER_E10, + SUPER_E10("Super E10"), /** * Fancy premium diesel */ - PREMIUM_DIESEL, + PREMIUM_DIESEL("Premium Diesel"), /** * Gas, Gas, Gas, I'm gonna step on the gas */ - AUTOGAS + AUTOGAS("Car gas"); + + public String name; + + private FuelType(String name) { + this.name = name; + } } diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/Main.java b/src/main/java/de/diejungsvondertanke/tankstelle/Main.java index 0ab25e4..ed8c8bc 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/Main.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/Main.java @@ -31,18 +31,6 @@ public class Main { */ public static ArrayList fuelStations = new ArrayList(Arrays.asList(initialFuelStations)); - /** - * Main method - * - * @param args Program arguments (not in use) - */ - public static void main(String[] args) { - // javax.swing.SwingUtilities.invokeLater(() -> { - // FuelStationUI ui = new FuelStationUI(); - // ui.setVisible(true); - // }); - } - /** * Get an array of fuel stations which have a number of vending machines * that is equal to {@code number_of_vending_machines} diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/controllers/ControllerRegistry.java.old b/src/main/java/de/diejungsvondertanke/tankstelle/controllers/ControllerRegistry.java.old deleted file mode 100644 index 010b211..0000000 --- a/src/main/java/de/diejungsvondertanke/tankstelle/controllers/ControllerRegistry.java.old +++ /dev/null @@ -1,13 +0,0 @@ -package de.diejungsvondertanke.tankstelle.controllers; - -public class ControllerRegistry { - private static FuelStationUIController main; - - public static void registerMain(FuelStationUIController controller) { - main = controller; - } - - public static FuelStationUIController getMain() { - return main; - } -} diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/controllers/FuelStationUIController.java b/src/main/java/de/diejungsvondertanke/tankstelle/controllers/FuelStationUIController.java index 5b51804..5572535 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/controllers/FuelStationUIController.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/controllers/FuelStationUIController.java @@ -7,45 +7,21 @@ import de.diejungsvondertanke.tankstelle.*; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; -import javafx.scene.control.*; +import javafx.scene.control.Alert; +import javafx.scene.control.ComboBox; +import javafx.scene.control.ScrollPane; +import javafx.scene.control.TabPane; +import javafx.scene.control.TextArea; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; +/** + * Main controller class + */ public class FuelStationUIController { - // @FXML - // public ComboBox comboFuelStations; - - // @FXML - // public ComboBox comboFuelTypes; - - // @FXML - // private TabPane tabPane; - - // // child controllers - // @FXML - // private HBox resultTab; - - // @FXML - // private HBox priceTab; - - // @FXML - // private HBox stockTab; - - // @FXML - // private HBox searchTab; - - // @FXML - // private HBox newStationTab; - - // @FXML - // private HBox overviewTab; - - // @FXML - // private TextArea outputArea; - @FXML public ComboBox comboFuelStations; @FXML @@ -77,6 +53,19 @@ public class FuelStationUIController { private NewStationTabController newStationTabController; private OverviewTabController overviewTabController; + /** + * Automatically called initialize() function. + * + * This is the "main" UI setup routine. For timing reasons, this will/must load + * every subtab manually to get the controller object and set the + * {@link FuelStationUIController} controller in every subtab controller + * element. + * + * If not done correctly, using the controller in the subtabs WILL FAIL. + * + * USING THE CONTROLLER OBJECT INSIDE THE {@code initialize()} FUNCTION OF THE + * SUBTABS IS NOT PERMITTED AND WILL ALWAYS BE {@code null} DUE TO TIMING + */ @FXML public void initialize() { // Load all tabs manually and keep controllers @@ -86,6 +75,7 @@ public class FuelStationUIController { loadTab("/ui/ResultTab.fxml", resultTabContainer, (ResultTabController c) -> { resultTabController = c; resultTabController.setParentController(this); + resultTabController.refreshList(); }); loadTab("/ui/PriceTab.fxml", priceTabContainer, (PriceTabController c) -> { priceTabController = c; @@ -108,7 +98,7 @@ public class FuelStationUIController { (OverviewTabController c) -> { overviewTabController = c; overviewTabController.setParentController(this); - overviewTabController.refresh(); + // overviewTabController.refresh(); }); } catch (Exception e) { @@ -118,32 +108,31 @@ public class FuelStationUIController { refreshStationNames(); } - // Generic loader helper - // private void loadTab(String fxmlPath, Parent placeholder, - // java.util.function.Consumer controllerSetter) { - // try { - // FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlPath)); - // Parent content = loader.load(); - // T controller = loader.getController(); - // controllerSetter.accept(controller); - // placeholder.getChildren().setAll(content); - // } catch (IOException e) { - // e.printStackTrace(); - // } - // } - + /** + * Generic tab loader helper + * + * @param I have no idea what this does other than generalise + * the type for the consumer + * @param fxmlPath Path to the .fxml resource file + * @param container A container that inherits from {@link Parent}, for + * example {@link GridPane}, {@link HBoc} or + * {@link BorderPane} + * @param controllerConsumer A {@link Consumer} to + * @throws IOException If loading the Tab failed because the .fxml file was not + * found + */ private void loadTab(String fxmlPath, Parent container, Consumer controllerConsumer) throws IOException { FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlPath)); Parent loaded = loader.load(); T controller = loader.getController(); // Add loaded node to the container - if (container instanceof Pane pane) { - pane.getChildren().setAll(loaded); + if (container instanceof BorderPane border) { + border.setCenter(loaded); } else if (container instanceof ScrollPane scroll) { scroll.setContent(loaded); - } else if (container instanceof BorderPane border) { - border.setCenter(loaded); + } else if (container instanceof Pane pane) { + pane.getChildren().setAll(loaded); } else { throw new IllegalArgumentException("Unsupported container type: " + container.getClass()); } @@ -158,13 +147,23 @@ public class FuelStationUIController { return Main.fuelStations.get(idx); } + @FXML public void refreshStationNames() { comboFuelStations.getItems().setAll( Main.fuelStations.stream() .map(this::getDisplayName) .toList()); + + resultTabController.refreshList(); + overviewTabController.refresh(); } + /** + * Create some display String for the fuel station lists in the UI + * + * @param fs a {@link FuelStation} object to get the display name for + * @return the display name string + */ public String getDisplayName(FuelStation fs) { int index = Main.fuelStations.indexOf(fs); if (index >= 0) { diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/controllers/NewStationTabController.java b/src/main/java/de/diejungsvondertanke/tankstelle/controllers/NewStationTabController.java index 7b7a7ce..d709f9a 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/controllers/NewStationTabController.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/controllers/NewStationTabController.java @@ -2,7 +2,8 @@ package de.diejungsvondertanke.tankstelle.controllers; import de.diejungsvondertanke.tankstelle.Main; import javafx.fxml.FXML; -import javafx.scene.control.*; +import javafx.scene.control.RadioButton; +import javafx.scene.control.TextField; public class NewStationTabController { diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/controllers/OverviewTabController.java b/src/main/java/de/diejungsvondertanke/tankstelle/controllers/OverviewTabController.java index 799c57c..529caa8 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/controllers/OverviewTabController.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/controllers/OverviewTabController.java @@ -1,10 +1,15 @@ package de.diejungsvondertanke.tankstelle.controllers; -import de.diejungsvondertanke.tankstelle.*; +import de.diejungsvondertanke.tankstelle.Fuel; +import de.diejungsvondertanke.tankstelle.FuelStation; +import de.diejungsvondertanke.tankstelle.Main; +import javafx.application.Platform; import javafx.beans.property.SimpleFloatProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringProperty; import javafx.fxml.FXML; +import javafx.scene.Node; +import javafx.scene.Parent; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; @@ -35,20 +40,33 @@ public class OverviewTabController { @FXML public void initialize() { - colStation.setCellValueFactory(data -> new SimpleStringProperty(data.getValue().station())); - colType.setCellValueFactory(data -> new SimpleStringProperty(data.getValue().type())); - colFuel.setCellValueFactory(data -> new SimpleStringProperty(data.getValue().fuel())); - colAmount.setCellValueFactory(data -> new SimpleFloatProperty(data.getValue().amount()).asObject()); - colCapacity.setCellValueFactory(data -> new SimpleIntegerProperty(data.getValue().capacity()).asObject()); - colPrice.setCellValueFactory(data -> new SimpleFloatProperty(data.getValue().price()).asObject()); + colStation.setCellValueFactory(data -> new SimpleStringProperty(data.getValue().getStation())); + colType.setCellValueFactory(data -> new SimpleStringProperty(data.getValue().getType())); + colFuel.setCellValueFactory(data -> new SimpleStringProperty(data.getValue().getFuel())); + colAmount.setCellValueFactory(data -> new SimpleFloatProperty(data.getValue().getAmount()).asObject()); + colCapacity.setCellValueFactory(data -> new SimpleIntegerProperty(data.getValue().getCapacity()).asObject()); + colPrice.setCellValueFactory(data -> new SimpleFloatProperty(data.getValue().getPrice()).asObject()); } @FXML public void refresh() { table.getItems().clear(); + // ObservableList list = FXCollections.observableArrayList(); + // table.getItems().add(new FuelRow("TestStation", "TestType", "Diesel", 123, + + // 999, 1.23f)); + System.out.println("On FX thread? " + Platform.isFxApplicationThread()); for (FuelStation station : Main.fuelStations) { for (Fuel f : station.fuels) { + // System.out.println("Row: " + + // parentController.getDisplayName(station) + " | " + + // station.getClass().getSimpleName() + " | " + + // f.FUEL_TYPE + " | " + + // f.getStored_amount() + " | " + + // f.CAPACITY + " | " + + // f.getPrice()); + table.getItems().add( new FuelRow( parentController.getDisplayName(station), @@ -57,16 +75,94 @@ public class OverviewTabController { f.getStored_amount(), f.CAPACITY, f.getPrice())); + // System.out.println("Processed " + f.toString()); } } + // System.out.print(list); + // table.setItems(list); } - public record FuelRow( - String station, - String type, - String fuel, - float amount, - int capacity, - float price) { + public static void printNodeTree(Node node, int depth) { + // Indentation + String indent = " ".repeat(depth); + + // Print this node + System.out.println(indent + node.getClass().getSimpleName() + + (node.getId() != null ? " [id=" + node.getId() + "]" : "")); + + // If the node is a Parent, get its children and recurse + if (node instanceof Parent parent) { + for (Node child : parent.getChildrenUnmodifiable()) { + printNodeTree(child, depth + 1); + } + } + } + + public class FuelRow { + + String station; + String type; + String fuel; + float amount; + int capacity; + float price; + + public String getStation() { + return station; + } + + public void setStation(String station) { + this.station = station; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getFuel() { + return fuel; + } + + public void setFuel(String fuel) { + this.fuel = fuel; + } + + public float getAmount() { + return amount; + } + + public void setAmount(float amount) { + this.amount = amount; + } + + public int getCapacity() { + return capacity; + } + + public void setCapacity(int capacity) { + this.capacity = capacity; + } + + public float getPrice() { + return price; + } + + public void setPrice(float price) { + this.price = price; + } + + public FuelRow(String station, String type, String fuel, float amount, int capacity, float price) { + this.station = station; + this.type = type; + this.fuel = fuel; + this.amount = amount; + this.capacity = capacity; + this.price = price; + } + } } diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/controllers/PriceTabController.java b/src/main/java/de/diejungsvondertanke/tankstelle/controllers/PriceTabController.java index 1201e9b..4a4df4e 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/controllers/PriceTabController.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/controllers/PriceTabController.java @@ -36,6 +36,7 @@ public class PriceTabController { .formatted(type, parentController.getDisplayName(station), price)); txtPrice.clear(); + parentController.refreshStationNames(); } catch (NumberFormatException ex) { parentController.showError("Invalid number."); diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/controllers/ResultTabController.java b/src/main/java/de/diejungsvondertanke/tankstelle/controllers/ResultTabController.java index ffcbc12..194fe68 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/controllers/ResultTabController.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/controllers/ResultTabController.java @@ -1,9 +1,12 @@ package de.diejungsvondertanke.tankstelle.controllers; -import de.diejungsvondertanke.tankstelle.*; +import de.diejungsvondertanke.tankstelle.FuelStation; +import de.diejungsvondertanke.tankstelle.FuelType; +import de.diejungsvondertanke.tankstelle.Main; import de.diejungsvondertanke.tankstelle.error.NoSuchFuelTypeError; import javafx.fxml.FXML; import javafx.scene.control.ListView; +import javafx.scene.control.SelectionMode; public class ResultTabController { @@ -11,6 +14,11 @@ public class ResultTabController { private ListView listFuelStations; private FuelStationUIController parentController; + @FXML + public void initialize() { + listFuelStations.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); + } + public void setParentController(FuelStationUIController parentController) { this.parentController = parentController; } diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/controllers/SearchTabController.java b/src/main/java/de/diejungsvondertanke/tankstelle/controllers/SearchTabController.java index 65e5bf6..0feddfb 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/controllers/SearchTabController.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/controllers/SearchTabController.java @@ -1,6 +1,8 @@ package de.diejungsvondertanke.tankstelle.controllers; -import de.diejungsvondertanke.tankstelle.*; +import de.diejungsvondertanke.tankstelle.FuelStation; +import de.diejungsvondertanke.tankstelle.FuelType; +import de.diejungsvondertanke.tankstelle.Main; import de.diejungsvondertanke.tankstelle.error.NoSuchFuelTypeError; import javafx.fxml.FXML; diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/controllers/StockTabController.java b/src/main/java/de/diejungsvondertanke/tankstelle/controllers/StockTabController.java index 2a8aa96..01562ac 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/controllers/StockTabController.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/controllers/StockTabController.java @@ -1,9 +1,12 @@ package de.diejungsvondertanke.tankstelle.controllers; -import de.diejungsvondertanke.tankstelle.*; +import de.diejungsvondertanke.tankstelle.Fuel; +import de.diejungsvondertanke.tankstelle.FuelStation; +import de.diejungsvondertanke.tankstelle.FuelType; import de.diejungsvondertanke.tankstelle.error.NoSuchFuelTypeError; import javafx.fxml.FXML; -import javafx.scene.control.*; +import javafx.scene.control.RadioButton; +import javafx.scene.control.TextField; public class StockTabController { @@ -50,6 +53,7 @@ public class StockTabController { return; } station.set_stored_amount(value, type); + parentController.refreshStationNames(); parentController.appendOutput("Stock of %s at %s set to %.2f L" .formatted(type, parentController.getDisplayName(station), value)); } else { @@ -59,6 +63,7 @@ public class StockTabController { return; } station.add_stored_amount(value, type); + parentController.refreshStationNames(); parentController.appendOutput("Stock of %s at %s changed by %.2f L" .formatted(type, parentController.getDisplayName(station), value)); } diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/ui/JFX.java b/src/main/java/de/diejungsvondertanke/tankstelle/ui/JFX.java index 5e2d1cf..659572a 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/ui/JFX.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/ui/JFX.java @@ -8,6 +8,7 @@ import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; +import javafx.scene.image.Image; import javafx.stage.Stage; public class JFX extends Application { @@ -17,15 +18,18 @@ public class JFX extends Application { public static FuelStationUIController controller; static Parent parent; + static Scene scene; @Override public void start(Stage s) throws Exception { stage = s; Parent parent = loadFXML("FuelStationUI"); - Scene scene = new Scene(parent); + scene = new Scene(parent); stage.setScene(scene); stage.setTitle("Fuel Station Management System"); + stage.getIcons().add(new Image(getClass().getResourceAsStream("/icon/gasstation_4334.png"))); + initializeKeybinds(); stage.show(); } @@ -39,4 +43,60 @@ public class JFX extends Application { controller = fxmlLoader.getController(); return parent; } + + /** + * Set the keybinds and set the default closing behaviour to run the quit() + * function + */ + private void initializeKeybinds() { + // Override the default close behaviour + stage.setOnCloseRequest((e) -> { + exit(); + }); + stage.setFullScreenExitKeyCombination(null); + + // register keybinds here + scene.setOnKeyPressed((e) -> { + switch (e.getCode()) { + case ENTER: + if (e.isAltDown()) { + if (stage.isFullScreen()) + stage.setFullScreen(false); + else + stage.setFullScreen(true); + + } + break; + case F11: + + if (stage.isFullScreen()) + stage.setFullScreen(false); + else + stage.setFullScreen(true); + break; + + case F12: + if (stage.isMaximized()) + stage.setMaximized(false); + else + stage.setMaximized(true); + break; + + default: + break; + } + }); + } + + void exit() { + System.exit(0); + } + + void exit(int status) { + System.exit(status); + } + + private void quit() { + System.out.println("Exiting program, have a nice day :)"); + } } diff --git a/src/main/resources/icon/gasstation_4334.ico b/src/main/resources/icon/gasstation_4334.ico new file mode 100644 index 0000000..e2d0d80 Binary files /dev/null and b/src/main/resources/icon/gasstation_4334.ico differ diff --git a/src/main/resources/icon/gasstation_4334.png b/src/main/resources/icon/gasstation_4334.png new file mode 100644 index 0000000..484cc27 Binary files /dev/null and b/src/main/resources/icon/gasstation_4334.png differ diff --git a/src/main/resources/ui/NewStationTab.fxml b/src/main/resources/ui/NewStationTab.fxml index 6d91c64..b89ca72 100644 --- a/src/main/resources/ui/NewStationTab.fxml +++ b/src/main/resources/ui/NewStationTab.fxml @@ -7,12 +7,14 @@ fx:controller="de.diejungsvondertanke.tankstelle.controllers.NewStationTabController" hgap="10" vgap="10"> - +