001    package org.LiveGraph.plot;
002    
003    import java.awt.Color;
004    import java.awt.Graphics;
005    import java.awt.image.BufferedImage;
006    import java.io.File;
007    import java.io.IOException;
008    import java.util.Iterator;
009    
010    import javax.imageio.ImageIO;
011    import javax.imageio.ImageWriter;
012    import javax.imageio.stream.ImageOutputStream;
013    import javax.swing.JOptionPane;
014    
015    import org.LiveGraph.LiveGraph;
016    import org.LiveGraph.gui.ExportImageDialog;
017    
018    import com.softnetConsult.utils.exceptions.ThrowableTools;
019    
020    /**
021     * Encapsulates the logic of graph image exports.
022     * 
023     * <p><strong>LiveGraph</strong> (http://www.live-graph.org).</p>
024     * <p>Copyright (c) 2007 by G. Paperin.</p>
025     * <p>File: GraphExporter.java</p> 
026     * <p style="font-size:smaller;">Redistribution and use in source and binary forms, with or
027     *    without modification, are permitted provided that the following terms and conditions are met:
028     * </p>
029     * <p style="font-size:smaller;">1. Redistributions of source code must retain the above
030     *    acknowledgement of the LiveGraph project and its web-site, the above copyright notice,
031     *    this list of conditions and the following disclaimer.<br />
032     *    2. Redistributions in binary form must reproduce the above acknowledgement of the
033     *    LiveGraph project and its web-site, the above copyright notice, this list of conditions
034     *    and the following disclaimer in the documentation and/or other materials provided with
035     *    the distribution.<br />
036     *    3. All advertising materials mentioning features or use of this software or any derived
037     *    software must display the following acknowledgement:<br />
038     *    <em>This product includes software developed by the LiveGraph project and its
039     *    contributors.<br />(http://www.live-graph.org)</em><br />
040     *    4. All advertising materials distributed in form of HTML pages or any other technology
041     *    permitting active hyper-links that mention features or use of this software or any
042     *    derived software must display the acknowledgment specified in condition 3 of this
043     *    agreement, and in addition, include a visible and working hyper-link to the LiveGraph
044     *    homepage (http://www.live-graph.org).
045     * </p>
046     * <p style="font-size:smaller;">THIS SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY
047     *    OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
048     *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT SHALL
049     *    THE AUTHORS, CONTRIBUTORS OR COPYRIGHT  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
050     *    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING  FROM, OUT OF OR
051     *    IN CONNECTION WITH THE SOFTWARE OR THE USE OR  OTHER DEALINGS IN THE SOFTWARE.
052     * </p>
053     * 
054     * @author Greg Paperin (http://www.paperin.org)
055     * @version {@value org.LiveGraph.LiveGraph#version}
056     */
057    public class GraphExporter {
058    
059    /**
060     * Graph plotter.
061     */
062    private Plotter plotter = null;
063    
064    /**
065     * Export settings dialog.
066     */
067    private ExportImageDialog dialog = null;
068    
069    /**
070     * Creates a new exporter.
071     * @param plotter Tghe plotter to use with this exporter.
072     */
073    public GraphExporter(Plotter plotter) {
074            this.plotter = plotter;
075            this.dialog = new ExportImageDialog(this); 
076    }
077    
078    /**
079     * Initiates an export by showing the options dialog.
080     * If the dialog is confirmed, it will call-back this exporter to finish the image creation.
081     */
082    public void exportGraph() {
083            dialog.setVisible(true);
084    }
085    
086    /**
087     * Plots the graph and exports the image to a file.
088     * 
089     * @param imgWidth Width of the exported image in pixel.
090     * @param imgHeight Height of the exported image in pixel.
091     * @param imgMIMEType MIME type of the exported image.
092     * @param imgFile File of the exported image.
093     */
094    public void doExportGraph(int imgWidth, int imgHeight, String imgMIMEType, File imgFile) {
095            
096            Iterator<ImageWriter> writers = ImageIO.getImageWritersByMIMEType(imgMIMEType);
097            if (!writers.hasNext()) {
098                    JOptionPane.showMessageDialog(null, "Cannot obtain image writer for MIME-type \""
099                                                                                      + imgMIMEType + "\".",
100                                                                              "Error during image export", JOptionPane.ERROR_MESSAGE);
101                    return;
102            }
103            ImageWriter writer = writers.next();
104            ImageOutputStream imgOut = null;
105            try {
106                    imgOut = ImageIO.createImageOutputStream(imgFile);
107                    writer.setOutput(imgOut);
108            } catch (IOException e) {
109                    LiveGraph.application().logErrorLn(ThrowableTools.stackTraceToString(e));
110                    JOptionPane.showMessageDialog(null, "Could not create image output stream.",
111                                                                              "Error during image export", JOptionPane.ERROR_MESSAGE);
112                    return;
113            }
114            
115            BufferedImage img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
116            Graphics g = img.getGraphics();
117            g.setColor(Color.WHITE);
118            g.fillRect(0, 0, imgWidth, imgHeight);
119            
120            int oldPlotW = plotter.getScreenWidth();        
121            int oldPlotH = plotter.getScreenHeight();
122            plotter.setScreenSize(imgWidth, imgHeight);
123            plotter.paint(g);
124            plotter.setScreenSize(oldPlotW, oldPlotH);
125            
126            try {
127                    writer.write(img);
128                    imgOut.close();
129                    writer = null;
130            } catch (IOException e) {
131                    LiveGraph.application().logErrorLn(ThrowableTools.stackTraceToString(e));
132                    JOptionPane.showMessageDialog(null, "Could not write to image.",
133                                                                              "Error during image export", JOptionPane.ERROR_MESSAGE);
134            } catch(IllegalArgumentException e) {
135                    LiveGraph.application().logErrorLn(ThrowableTools.stackTraceToString(e));
136                    JOptionPane.showMessageDialog(null, e.getMessage() + "\n\nTry choosing another image type.\n ",
137                                                                              "Error during image export", JOptionPane.ERROR_MESSAGE);
138            }
139    }
140    
141    /**
142     * Disposes of all GUI objects encapsulated in this exporter.
143     */
144    public void disposeInternalGUI() {
145            dialog.dispose();
146    }
147    
148    }