|
| 1 | +package net.imagej.plot.io; |
| 2 | + |
| 3 | +import net.imagej.plot.AbstractPlot; |
| 4 | +import org.jfree.chart.JFreeChart; |
| 5 | +import org.jfree.graphics2d.svg.SVGGraphics2D; |
| 6 | +import org.jfree.graphics2d.svg.SVGUtils; |
| 7 | +import org.scijava.convert.ConvertService; |
| 8 | +import org.scijava.io.AbstractIOPlugin; |
| 9 | +import org.scijava.io.IOPlugin; |
| 10 | +import org.scijava.plugin.Parameter; |
| 11 | +import org.scijava.plugin.Plugin; |
| 12 | + |
| 13 | +import java.awt.*; |
| 14 | +import java.io.File; |
| 15 | +import java.io.IOException; |
| 16 | + |
| 17 | +/** |
| 18 | + * Plugin that can write {@link AbstractPlot} as SVG file. |
| 19 | + * |
| 20 | + * @author Matthias Arzt |
| 21 | + */ |
| 22 | +@Plugin(type = IOPlugin.class) |
| 23 | +public class AbstractPlotSvgIOPlugin extends AbstractIOPlugin<AbstractPlot> { |
| 24 | + |
| 25 | + @Parameter |
| 26 | + ConvertService convertService; |
| 27 | + |
| 28 | + @Override |
| 29 | + public boolean supportsOpen(String source) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public boolean supportsSave(String destination) { |
| 35 | + return destination.endsWith(".svg"); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public boolean supportsSave(Object data, String destination) { |
| 40 | + return supportsSave(destination) && |
| 41 | + data instanceof AbstractPlot && |
| 42 | + convertService.supports(data, JFreeChart.class); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public AbstractPlot open(String source) throws IOException { |
| 47 | + throw new UnsupportedOperationException(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void save(AbstractPlot data, String destination) throws IOException { |
| 52 | + if(!supportsSave(data, destination)) |
| 53 | + throw new IllegalArgumentException(); |
| 54 | + JFreeChart chart = convertService.convert(data, JFreeChart.class); |
| 55 | + SVGGraphics2D g = new SVGGraphics2D(data.getPreferredWidth(), data.getPreferredWidth()); |
| 56 | + chart.draw(g, new Rectangle(0, 0, g.getWidth(), g.getHeight())); |
| 57 | + SVGUtils.writeToSVG(new File(destination), g.getSVGElement()); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Class<AbstractPlot> getDataType() { |
| 62 | + return AbstractPlot.class; |
| 63 | + } |
| 64 | +} |
0 commit comments