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            if (null == imgFile)
097                    throw new NullPointerException("Cannot export image to a null file");
098            
099            try {
100            
101                    Iterator<ImageWriter> writers = ImageIO.getImageWritersByMIMEType(imgMIMEType);
102                    if (!writers.hasNext()) {
103                            JOptionPane.showMessageDialog(null, "Cannot obtain image writer for MIME-type \""
104                                                                                              + imgMIMEType + "\".",
105                                                                                      "Error during image export", JOptionPane.ERROR_MESSAGE);
106                            return;
107                    }
108                    ImageWriter writer = writers.next();
109                    ImageOutputStream imgOut = null;
110                    try {
111                            imgOut = ImageIO.createImageOutputStream(imgFile);
112                            writer.setOutput(imgOut);
113                    } catch (IOException e) {
114                            LiveGraph.application().guiManager().logErrorLn(ThrowableTools.stackTraceToString(e));
115                            JOptionPane.showMessageDialog(null, "Could not create image output stream.",
116                                                                                      "Error during image export", JOptionPane.ERROR_MESSAGE);
117                            return;
118                    }
119                    
120                    BufferedImage img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
121                    Graphics g = img.getGraphics();
122                    g.setColor(Color.WHITE);
123                    g.fillRect(0, 0, imgWidth, imgHeight);
124                    
125                    plotter.setScreenSize(imgWidth, imgHeight);
126                    plotter.paint(g);
127                    
128                    try {
129                            writer.write(img);
130                            imgOut.close();
131                            writer = null;
132                    } catch (IOException e) {
133                            LiveGraph.application().guiManager().logErrorLn(ThrowableTools.stackTraceToString(e));
134                            JOptionPane.showMessageDialog(null, "Could not write to image.",
135                                                                                      "Error during image export", JOptionPane.ERROR_MESSAGE);
136                    } catch(IllegalArgumentException e) {
137                            LiveGraph.application().guiManager().logErrorLn(ThrowableTools.stackTraceToString(e));
138                            JOptionPane.showMessageDialog(null, e.getMessage() + "\n\nTry choosing another image type.\n ",
139                                                                                      "Error during image export", JOptionPane.ERROR_MESSAGE);
140                    }
141            } catch(SecurityException secE) {
142                    JOptionPane.showMessageDialog(null, "The Java security environment does not permit the"
143                                                    + " required access rights to export the graph image. \nYou may be running"
144                                                    + " LiveGraph in a restricted security environment. \n\nInfo: \n"
145                                                    + secE.getMessage());
146            }
147    }
148    
149    /**
150     * Disposes of all GUI objects encapsulated in this exporter.
151     */
152    public void disposeInternalGUI() {
153            dialog.dispose();
154    }
155    
156    }