From 55e74b5e4b0e22fc235def4cfb95ed683a144b40 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Wed, 19 Feb 2025 11:45:32 +0100 Subject: [PATCH] Add HTML export action --- src/main/java/ovh/gasser/newshapes/HTMLExporter.java | 11 +++++++---- .../ovh/gasser/newshapes/shapes/SCollection.java | 2 +- .../java/ovh/gasser/newshapes/ui/Controller.java | 12 ++++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/ovh/gasser/newshapes/HTMLExporter.java b/src/main/java/ovh/gasser/newshapes/HTMLExporter.java index 35767c2..c24f3d2 100644 --- a/src/main/java/ovh/gasser/newshapes/HTMLExporter.java +++ b/src/main/java/ovh/gasser/newshapes/HTMLExporter.java @@ -13,18 +13,21 @@ public class HTMLExporter { private final SCollection model; private HTMLExporter() throws FileNotFoundException { - model = SCollection.of( + this(SCollection.of( SRectangle.create(10, 10, 40, 60, Color.RED), SRectangle.create(70, 10, 40, 60), SCollection.of( SRectangle.create(100, 200, 40, 60, Color.MAGENTA), SRectangle.create(150, 200, 40, 60, Color.MAGENTA) ) - ); - export(); + )); } - private void export() throws FileNotFoundException { + public HTMLExporter(SCollection model){ + this.model = model; + } + + public void export() throws FileNotFoundException { try (final PrintWriter html = new PrintWriter("out.html")) { try (final PrintWriter css = new PrintWriter("style.css")) { HTMLDraftman draftman = new HTMLDraftman(html, css); diff --git a/src/main/java/ovh/gasser/newshapes/shapes/SCollection.java b/src/main/java/ovh/gasser/newshapes/shapes/SCollection.java index f945eb7..84c811a 100644 --- a/src/main/java/ovh/gasser/newshapes/shapes/SCollection.java +++ b/src/main/java/ovh/gasser/newshapes/shapes/SCollection.java @@ -16,7 +16,7 @@ import java.util.stream.Collectors; public class SCollection extends AbstractShape implements Streamable { private final static Logger logger = LoggerFactory.getLogger(SCollection.class); - private List children; + private final List children; private SCollection(Shape... shapes) { this.children = new ArrayList<>(List.of(shapes)); diff --git a/src/main/java/ovh/gasser/newshapes/ui/Controller.java b/src/main/java/ovh/gasser/newshapes/ui/Controller.java index a7cc8f1..9d79ee1 100644 --- a/src/main/java/ovh/gasser/newshapes/ui/Controller.java +++ b/src/main/java/ovh/gasser/newshapes/ui/Controller.java @@ -2,6 +2,7 @@ package ovh.gasser.newshapes.ui; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import ovh.gasser.newshapes.HTMLExporter; import ovh.gasser.newshapes.Selection; import ovh.gasser.newshapes.attributes.ColorAttributes; import ovh.gasser.newshapes.shapes.SCollection; @@ -12,6 +13,7 @@ import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.io.FileNotFoundException; import java.util.Optional; public class Controller { @@ -74,10 +76,20 @@ public class Controller { case KeyEvent.VK_DELETE -> deleteSelected(); case KeyEvent.VK_C -> copySelection(); case KeyEvent.VK_A -> changeSelectionColor(); + case KeyEvent.VK_H -> exportHtml(); default -> logger.warn("Pressed unhandled key: {}", evt.getKeyChar()); } } + private void exportHtml() { + logger.info("Exporting view to html"); + try { + new HTMLExporter(this.model).export(); + } catch (FileNotFoundException e) { + logger.error("Unable to export html: {}", e.getMessage()); + } + } + private void changeSelectionColor(){ if (selection == null) { logger.debug("No selection to change color of");