added a export helper class

This commit is contained in:
hneemann 2016-04-17 08:36:26 +02:00
parent a756d26e54
commit 730910c1fb
8 changed files with 96 additions and 77 deletions

View File

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

View File

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

View File

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

View File

@ -15,13 +15,13 @@ public class GraphicSVG implements Graphic, Closeable {
/** /**
* Creates a new instance. * Creates a new instance.
* *
* @param file the file * @param out the stream
* @param min upper left corner * @param min upper left corner
* @param max lower right corner * @param max lower right corner
* @throws IOException IOException * @throws IOException IOException
*/ */
public GraphicSVG(File file, Vector min, Vector max) throws IOException { public GraphicSVG(OutputStream out, Vector min, Vector max) throws IOException {
this(file, min, max, null, DEF_SCALE); this(out, min, max, null, DEF_SCALE);
} }
/** /**

View File

@ -15,13 +15,13 @@ public class GraphicSVGIndex extends GraphicSVG {
/** /**
* Creates new instance * Creates new instance
* *
* @param file the file * @param out the file
* @param min upper left corner * @param min upper left corner
* @param max lower right corner * @param max lower right corner
* @throws IOException IOException * @throws IOException IOException
*/ */
public GraphicSVGIndex(File file, Vector min, Vector max) throws IOException { public GraphicSVGIndex(OutputStream out, Vector min, Vector max) throws IOException {
super(file, min, max); super(out, min, max);
} }
/** /**

View File

@ -18,13 +18,13 @@ public class GraphicSVGLaTeX extends GraphicSVG {
/** /**
* Creates new instance * Creates new instance
* *
* @param file the file * @param out the file
* @param min upper left corner * @param min upper left corner
* @param max lower right corner * @param max lower right corner
* @throws IOException IOException * @throws IOException IOException
*/ */
public GraphicSVGLaTeX(File file, Vector min, Vector max) throws IOException { public GraphicSVGLaTeX(OutputStream out, Vector min, Vector max) throws IOException {
super(file, min, max); super(out, min, max);
} }
/** /**

View File

@ -10,8 +10,6 @@ import de.neemann.digital.draw.elements.Circuit;
import de.neemann.digital.draw.elements.ElementOrder; import de.neemann.digital.draw.elements.ElementOrder;
import de.neemann.digital.draw.elements.PinException; import de.neemann.digital.draw.elements.PinException;
import de.neemann.digital.draw.graphics.*; 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.library.ElementLibrary;
import de.neemann.digital.draw.model.ModelDescription; import de.neemann.digital.draw.model.ModelDescription;
import de.neemann.digital.draw.model.RealTimeClock; import de.neemann.digital.draw.model.RealTimeClock;
@ -31,7 +29,6 @@ import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; 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")); 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_exportSVG"), "svg", GraphicSVGIndex::new));
export.add(new ExportAction(Lang.get("menu_exportSVGLaTex"), "svg", GraphicSVGLaTeX::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_exportPNGSmall"), "png", (out, min, max) -> GraphicsImage.create(out, 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_exportPNGLarge"), "png", (out, min, max) -> GraphicsImage.create(out, min, max, "PNG", 2)));
JMenu file = new JMenu(Lang.get("menu_file")); JMenu file = new JMenu(Lang.get("menu_file"));
bar.add(file); bar.add(file);
@ -599,13 +596,13 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E
private class ExportAction extends ToolTipAction { private class ExportAction extends ToolTipAction {
private final String name; private final String name;
private final String suffix; 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); super(name);
this.name = name; this.name = name;
this.suffix = suffix; this.suffix = suffix;
this.exporter = exporter; this.exportFactory = exportFactory;
} }
@Override @Override
@ -621,21 +618,9 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E
} }
fc.addChoosableFileFilter(new FileNameExtensionFilter(name, suffix)); fc.addChoosableFileFilter(new FileNameExtensionFilter(name, suffix));
if (fc.showSaveDialog(Main.this) == JFileChooser.APPROVE_OPTION) { if (fc.showSaveDialog(Main.this) == JFileChooser.APPROVE_OPTION) {
Circuit circuit = circuitComponent.getCircuit();
GraphicMinMax minMax = new GraphicMinMax();
circuit.drawTo(minMax);
try { try {
Graphic gr = null; new Export(circuitComponent.getCircuit(), exportFactory)
try { .export(new FileOutputStream(fc.getSelectedFile()));
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();
}
} catch (IOException e1) { } catch (IOException e1) {
new ErrorMessage(Lang.get("msg_errorWritingFile")).addCause(e1).show(Main.this); new ErrorMessage(Lang.get("msg_errorWritingFile")).addCause(e1).show(Main.this);
} }

View File

@ -4,14 +4,10 @@ import de.neemann.digital.core.NodeException;
import de.neemann.digital.draw.elements.Circuit; import de.neemann.digital.draw.elements.Circuit;
import de.neemann.digital.draw.elements.PinException; import de.neemann.digital.draw.elements.PinException;
import de.neemann.digital.draw.graphics.*; 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 junit.framework.TestCase;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
/** /**
* Only checks that something is written without an error * Only checks that something is written without an error
@ -20,28 +16,10 @@ import java.io.OutputStream;
*/ */
public class TestExport extends TestCase { public class TestExport extends TestCase {
private interface Creator { private static ByteArrayOutputStream export(String file, ExportFactory creator) throws NodeException, PinException, IOException {
Graphic create(OutputStream out, Vector min, Vector max) throws IOException;
}
private static ByteArrayOutputStream export(String file, Creator creator) throws NodeException, PinException, IOException {
Circuit circuit = new ToBreakRunner(file).getCircuit(); Circuit circuit = new ToBreakRunner(file).getCircuit();
GraphicMinMax minMax = new GraphicMinMax();
circuit.drawTo(minMax);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
Graphic gr = creator.create(baos, minMax.getMin(), minMax.getMax()); new Export(circuit, creator).export(baos);
GraphicLineCollector glc = new GraphicLineCollector();
circuit.drawTo(glc);
glc.drawTo(gr);
circuit.drawTo(new GraphicSkipLines(gr));
if (gr instanceof Closeable)
((Closeable) gr).close();
return baos; return baos;
} }