From 730910c1fbfcf74ac7990b3fd85ce070fda8427c Mon Sep 17 00:00:00 2001 From: hneemann Date: Sun, 17 Apr 2016 08:36:26 +0200 Subject: [PATCH] added a export helper class --- .../neemann/digital/draw/graphics/Export.java | 56 +++++++++++++++++++ .../digital/draw/graphics/ExportFactory.java | 22 ++++++++ .../digital/draw/graphics/Exporter.java | 22 -------- .../digital/draw/graphics/GraphicSVG.java | 6 +- .../draw/graphics/GraphicSVGIndex.java | 6 +- .../draw/graphics/GraphicSVGLaTeX.java | 6 +- .../java/de/neemann/digital/gui/Main.java | 29 +++------- .../digital/integration/TestExport.java | 26 +-------- 8 files changed, 96 insertions(+), 77 deletions(-) create mode 100644 src/main/java/de/neemann/digital/draw/graphics/Export.java create mode 100644 src/main/java/de/neemann/digital/draw/graphics/ExportFactory.java delete mode 100644 src/main/java/de/neemann/digital/draw/graphics/Exporter.java diff --git a/src/main/java/de/neemann/digital/draw/graphics/Export.java b/src/main/java/de/neemann/digital/draw/graphics/Export.java new file mode 100644 index 000000000..93ee7ab09 --- /dev/null +++ b/src/main/java/de/neemann/digital/draw/graphics/Export.java @@ -0,0 +1,56 @@ +package de.neemann.digital.draw.graphics; + +import de.neemann.digital.draw.elements.Circuit; +import de.neemann.digital.draw.graphics.linemerger.GraphicLineCollector; +import de.neemann.digital.draw.graphics.linemerger.GraphicSkipLines; + +import java.io.Closeable; +import java.io.IOException; +import java.io.OutputStream; + +/** + * Helper to export graphics files + * + * @author hneemann + */ +public class Export { + + private final Circuit circuit; + private final ExportFactory factory; + + /** + * Creates a nw instance + * + * @param circuit the circuit to export + * @param factory the factory to create the graphics instance + */ + public Export(Circuit circuit, ExportFactory factory) { + this.circuit = circuit; + this.factory = factory; + } + + /** + * Export the file + * + * @param out stream to write the file to + * @throws IOException IOException + */ + public void export(OutputStream out) throws IOException { + GraphicMinMax minMax = new GraphicMinMax(); + circuit.drawTo(minMax); + + Graphic gr = factory.create(out, minMax.getMin(), minMax.getMax()); + try { + + GraphicLineCollector glc = new GraphicLineCollector(); + circuit.drawTo(glc); + glc.drawTo(gr); + + circuit.drawTo(new GraphicSkipLines(gr)); + + } finally { + if (gr instanceof Closeable) + ((Closeable) gr).close(); + } + } +} diff --git a/src/main/java/de/neemann/digital/draw/graphics/ExportFactory.java b/src/main/java/de/neemann/digital/draw/graphics/ExportFactory.java new file mode 100644 index 000000000..83e5b6e60 --- /dev/null +++ b/src/main/java/de/neemann/digital/draw/graphics/ExportFactory.java @@ -0,0 +1,22 @@ +package de.neemann.digital.draw.graphics; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * Factory to create a {@link Graphic} instance suited to create a file. + * + * @author hneemann + */ +public interface ExportFactory { + /** + * Creates a {@link Graphic} instance + * + * @param out the stream to write the graphic to + * @param min upper right corner of the circuit + * @param max lower left corner of the circuit + * @return the {@link Graphic} instance to use + * @throws IOException IOException + */ + Graphic create(OutputStream out, Vector min, Vector max) throws IOException; +} diff --git a/src/main/java/de/neemann/digital/draw/graphics/Exporter.java b/src/main/java/de/neemann/digital/draw/graphics/Exporter.java deleted file mode 100644 index fae470745..000000000 --- a/src/main/java/de/neemann/digital/draw/graphics/Exporter.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.neemann.digital.draw.graphics; - -import java.io.File; -import java.io.IOException; - -/** - * Factor to create a {@link Graphic} instance suited to create a file. - * - * @author hneemann - */ -public interface Exporter { - /** - * Creates a {@link Graphic} instance - * - * @param file the filename to use - * @param min upper right corner of the circuit - * @param max lower left corner of the circuit - * @return the {@link Graphic} instance to use - * @throws IOException IOException - */ - Graphic create(File file, Vector min, Vector max) throws IOException; -} diff --git a/src/main/java/de/neemann/digital/draw/graphics/GraphicSVG.java b/src/main/java/de/neemann/digital/draw/graphics/GraphicSVG.java index cbcb7f7af..b70608e4e 100644 --- a/src/main/java/de/neemann/digital/draw/graphics/GraphicSVG.java +++ b/src/main/java/de/neemann/digital/draw/graphics/GraphicSVG.java @@ -15,13 +15,13 @@ public class GraphicSVG implements Graphic, Closeable { /** * Creates a new instance. * - * @param file the file + * @param out the stream * @param min upper left corner * @param max lower right corner * @throws IOException IOException */ - public GraphicSVG(File file, Vector min, Vector max) throws IOException { - this(file, min, max, null, DEF_SCALE); + public GraphicSVG(OutputStream out, Vector min, Vector max) throws IOException { + this(out, min, max, null, DEF_SCALE); } /** diff --git a/src/main/java/de/neemann/digital/draw/graphics/GraphicSVGIndex.java b/src/main/java/de/neemann/digital/draw/graphics/GraphicSVGIndex.java index 89eae5a20..b94f9cda8 100644 --- a/src/main/java/de/neemann/digital/draw/graphics/GraphicSVGIndex.java +++ b/src/main/java/de/neemann/digital/draw/graphics/GraphicSVGIndex.java @@ -15,13 +15,13 @@ public class GraphicSVGIndex extends GraphicSVG { /** * Creates new instance * - * @param file the file + * @param out the file * @param min upper left corner * @param max lower right corner * @throws IOException IOException */ - public GraphicSVGIndex(File file, Vector min, Vector max) throws IOException { - super(file, min, max); + public GraphicSVGIndex(OutputStream out, Vector min, Vector max) throws IOException { + super(out, min, max); } /** diff --git a/src/main/java/de/neemann/digital/draw/graphics/GraphicSVGLaTeX.java b/src/main/java/de/neemann/digital/draw/graphics/GraphicSVGLaTeX.java index 0314708e9..53f919ee2 100644 --- a/src/main/java/de/neemann/digital/draw/graphics/GraphicSVGLaTeX.java +++ b/src/main/java/de/neemann/digital/draw/graphics/GraphicSVGLaTeX.java @@ -18,13 +18,13 @@ public class GraphicSVGLaTeX extends GraphicSVG { /** * Creates new instance * - * @param file the file + * @param out the file * @param min upper left corner * @param max lower right corner * @throws IOException IOException */ - public GraphicSVGLaTeX(File file, Vector min, Vector max) throws IOException { - super(file, min, max); + public GraphicSVGLaTeX(OutputStream out, Vector min, Vector max) throws IOException { + super(out, min, max); } /** diff --git a/src/main/java/de/neemann/digital/gui/Main.java b/src/main/java/de/neemann/digital/gui/Main.java index af068bf15..def5852f0 100644 --- a/src/main/java/de/neemann/digital/gui/Main.java +++ b/src/main/java/de/neemann/digital/gui/Main.java @@ -10,8 +10,6 @@ import de.neemann.digital.draw.elements.Circuit; import de.neemann.digital.draw.elements.ElementOrder; import de.neemann.digital.draw.elements.PinException; import de.neemann.digital.draw.graphics.*; -import de.neemann.digital.draw.graphics.linemerger.GraphicLineCollector; -import de.neemann.digital.draw.graphics.linemerger.GraphicSkipLines; import de.neemann.digital.draw.library.ElementLibrary; import de.neemann.digital.draw.model.ModelDescription; import de.neemann.digital.draw.model.RealTimeClock; @@ -31,7 +29,6 @@ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; -import java.io.Closeable; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -198,8 +195,8 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E JMenu export = new JMenu(Lang.get("menu_export")); export.add(new ExportAction(Lang.get("menu_exportSVG"), "svg", GraphicSVGIndex::new)); export.add(new ExportAction(Lang.get("menu_exportSVGLaTex"), "svg", GraphicSVGLaTeX::new)); - export.add(new ExportAction(Lang.get("menu_exportPNGSmall"), "png", (file, min, max) -> GraphicsImage.create(new FileOutputStream(file), min, max, "PNG", 1))); - export.add(new ExportAction(Lang.get("menu_exportPNGLarge"), "png", (file, min, max) -> GraphicsImage.create(new FileOutputStream(file), min, max, "PNG", 2))); + export.add(new ExportAction(Lang.get("menu_exportPNGSmall"), "png", (out, min, max) -> GraphicsImage.create(out, min, max, "PNG", 1))); + export.add(new ExportAction(Lang.get("menu_exportPNGLarge"), "png", (out, min, max) -> GraphicsImage.create(out, min, max, "PNG", 2))); JMenu file = new JMenu(Lang.get("menu_file")); bar.add(file); @@ -599,13 +596,13 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E private class ExportAction extends ToolTipAction { private final String name; private final String suffix; - private final Exporter exporter; + private final ExportFactory exportFactory; - ExportAction(String name, String suffix, Exporter exporter) { + ExportAction(String name, String suffix, ExportFactory exportFactory) { super(name); this.name = name; this.suffix = suffix; - this.exporter = exporter; + this.exportFactory = exportFactory; } @Override @@ -621,21 +618,9 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E } fc.addChoosableFileFilter(new FileNameExtensionFilter(name, suffix)); if (fc.showSaveDialog(Main.this) == JFileChooser.APPROVE_OPTION) { - Circuit circuit = circuitComponent.getCircuit(); - GraphicMinMax minMax = new GraphicMinMax(); - circuit.drawTo(minMax); try { - Graphic gr = null; - try { - GraphicLineCollector glc = new GraphicLineCollector(); - circuit.drawTo(glc); - gr = exporter.create(fc.getSelectedFile(), minMax.getMin(), minMax.getMax()); - glc.drawTo(gr); - circuit.drawTo(new GraphicSkipLines(gr)); - } finally { - if (gr instanceof Closeable) - ((Closeable) gr).close(); - } + new Export(circuitComponent.getCircuit(), exportFactory) + .export(new FileOutputStream(fc.getSelectedFile())); } catch (IOException e1) { new ErrorMessage(Lang.get("msg_errorWritingFile")).addCause(e1).show(Main.this); } diff --git a/src/test/java/de/neemann/digital/integration/TestExport.java b/src/test/java/de/neemann/digital/integration/TestExport.java index ad1a265b1..60809f0c3 100644 --- a/src/test/java/de/neemann/digital/integration/TestExport.java +++ b/src/test/java/de/neemann/digital/integration/TestExport.java @@ -4,14 +4,10 @@ import de.neemann.digital.core.NodeException; import de.neemann.digital.draw.elements.Circuit; import de.neemann.digital.draw.elements.PinException; import de.neemann.digital.draw.graphics.*; -import de.neemann.digital.draw.graphics.linemerger.GraphicLineCollector; -import de.neemann.digital.draw.graphics.linemerger.GraphicSkipLines; import junit.framework.TestCase; import java.io.ByteArrayOutputStream; -import java.io.Closeable; import java.io.IOException; -import java.io.OutputStream; /** * Only checks that something is written without an error @@ -20,28 +16,10 @@ import java.io.OutputStream; */ public class TestExport extends TestCase { - private interface Creator { - Graphic create(OutputStream out, Vector min, Vector max) throws IOException; - } - - private static ByteArrayOutputStream export(String file, Creator creator) throws NodeException, PinException, IOException { + private static ByteArrayOutputStream export(String file, ExportFactory creator) throws NodeException, PinException, IOException { Circuit circuit = new ToBreakRunner(file).getCircuit(); - - GraphicMinMax minMax = new GraphicMinMax(); - circuit.drawTo(minMax); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Graphic gr = creator.create(baos, minMax.getMin(), minMax.getMax()); - - GraphicLineCollector glc = new GraphicLineCollector(); - circuit.drawTo(glc); - glc.drawTo(gr); - - circuit.drawTo(new GraphicSkipLines(gr)); - - if (gr instanceof Closeable) - ((Closeable) gr).close(); - + new Export(circuit, creator).export(baos); return baos; }