Add HTML export action

This commit is contained in:
Thibaud Gasser 2025-02-19 11:45:32 +01:00
parent 1b0284cb4d
commit 55e74b5e4b
3 changed files with 20 additions and 5 deletions

View File

@ -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);

View File

@ -16,7 +16,7 @@ import java.util.stream.Collectors;
public class SCollection extends AbstractShape implements Streamable<Shape> {
private final static Logger logger = LoggerFactory.getLogger(SCollection.class);
private List<Shape> children;
private final List<Shape> children;
private SCollection(Shape... shapes) {
this.children = new ArrayList<>(List.of(shapes));

View File

@ -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");