001    package org.LiveGraph.gui;
002    
003    import javax.imageio.ImageIO;
004    import javax.swing.JButton;
005    import javax.swing.JComboBox;
006    import javax.swing.JFileChooser;
007    import javax.swing.JLabel;
008    import javax.swing.JOptionPane;
009    import javax.swing.JPanel;
010    import javax.swing.JTextField;
011    import javax.swing.WindowConstants;
012    
013    import java.awt.Dimension;
014    import java.awt.FlowLayout;
015    import java.awt.BorderLayout;
016    import java.awt.GridBagLayout;
017    import java.awt.Toolkit;
018    import java.awt.event.ActionEvent;
019    import java.awt.event.ActionListener;
020    import java.io.File;
021    
022    import javax.swing.JDialog;
023    
024    import org.LiveGraph.plot.GraphExporter;
025    
026    import com.softnetConsult.utils.files.FileTools;
027    import com.softnetConsult.utils.swing.DisEnablingPanel;
028    
029    
030    /**
031     * The modal dialog for graph image export.
032     * 
033     * <p><strong>LiveGraph</strong> (http://www.live-graph.org).</p>
034     * <p>Copyright (c) 2007 by G. Paperin.</p>
035     * <p>File: ExportImageDialog.java</p> 
036     * <p style="font-size:smaller;">Redistribution and use in source and binary forms, with or
037     *    without modification, are permitted provided that the following terms and conditions are met:
038     * </p>
039     * <p style="font-size:smaller;">1. Redistributions of source code must retain the above
040     *    acknowledgement of the LiveGraph project and its web-site, the above copyright notice,
041     *    this list of conditions and the following disclaimer.<br />
042     *    2. Redistributions in binary form must reproduce the above acknowledgement of the
043     *    LiveGraph project and its web-site, the above copyright notice, this list of conditions
044     *    and the following disclaimer in the documentation and/or other materials provided with
045     *    the distribution.<br />
046     *    3. All advertising materials mentioning features or use of this software or any derived
047     *    software must display the following acknowledgement:<br />
048     *    <em>This product includes software developed by the LiveGraph project and its
049     *    contributors.<br />(http://www.live-graph.org)</em><br />
050     *    4. All advertising materials distributed in form of HTML pages or any other technology
051     *    permitting active hyper-links that mention features or use of this software or any
052     *    derived software must display the acknowledgment specified in condition 3 of this
053     *    agreement, and in addition, include a visible and working hyper-link to the LiveGraph
054     *    homepage (http://www.live-graph.org).
055     * </p>
056     * <p style="font-size:smaller;">THIS SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY
057     *    OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
058     *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT SHALL
059     *    THE AUTHORS, CONTRIBUTORS OR COPYRIGHT  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
060     *    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING  FROM, OUT OF OR
061     *    IN CONNECTION WITH THE SOFTWARE OR THE USE OR  OTHER DEALINGS IN THE SOFTWARE.
062     * </p>
063     * 
064     * @author Greg Paperin (http://www.paperin.org)
065     * @version {@value org.LiveGraph.LiveGraph#version}
066     */
067    public class ExportImageDialog extends JDialog {
068    
069    /**
070     * The export worker.
071     */
072    private GraphExporter exporter = null;
073    
074    /**
075     * Constructs a new dialog.
076     * 
077     * @param exporter Graph exporter.
078     */
079    public ExportImageDialog(GraphExporter exporter) {
080            super();
081            this.exporter = exporter;
082            initialize();
083    }
084    
085    /**
086     * This method initializes this dialog's view.
087     */
088    private void initialize() {
089            
090            // Window size and position:
091            final int WIN_WIDTH = 470;
092            final int WIN_HEIGHT = 220;
093            Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
094            setBounds((scr.width - WIN_WIDTH) / 2, (scr.height - WIN_HEIGHT) / 2, WIN_WIDTH, WIN_HEIGHT);
095            setTitle("Export graph to file");
096            setModal(true);
097            this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
098            final ExportImageDialog EXPORT_DIALOG = this;
099            
100            
101            // Layout:
102            
103            getContentPane().setLayout(new BorderLayout());
104            JButton button = null;
105            JPanel panel = null;
106            
107            // Options:
108            
109            JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER));
110            panel = (JPanel) p.add(new JPanel(new GridBagLayout()));
111            getContentPane().add(p, BorderLayout.CENTER);
112            
113            panel.add(new JLabel("Image width in pixel:"), Tools.createGridBagConstraints(0, 0, 1, 1));
114            panel.add(new JLabel("Image height in pixel:"), Tools.createGridBagConstraints(0, 1, 1, 1));
115            panel.add(new JLabel("Image type:"), Tools.createGridBagConstraints(0, 2, 1, 1));
116            panel.add(new JLabel("Image file:"), Tools.createGridBagConstraints(0, 3, 1, 1));
117                    
118            final JComboBox imgWidthBox = new JComboBox(new Integer[] {200, 300, 400, 500, 600, 700, 800, 900,
119                                                                                                                               1000, 1100, 1200, 1300, 1400, 1500, 1600,
120                                                                                                                               1700, 1800, 1900, 2000});
121            panel.add(imgWidthBox, Tools.createGridBagConstraints(1, 0, 2, 1));
122            
123            final JComboBox imgHeightBox = new JComboBox(new Integer[] {200, 300, 400, 500, 600, 700, 800, 900,
124                                                                                                                                    1000, 1100, 1200, 1300, 1400, 1500, 1600,
125                                                                                                                                    1700, 1800, 1900, 2000});
126            panel.add(imgHeightBox, Tools.createGridBagConstraints(1, 1, 2, 1));
127            
128            final JComboBox imgTypeBox = new JComboBox(ImageIO.getWriterMIMETypes());
129            panel.add(imgTypeBox, Tools.createGridBagConstraints(1, 2, 2, 1));
130            
131            final JTextField imgFileField = new JTextField();
132            panel.add(imgFileField, Tools.createGridBagConstraints(1, 3, 1, 1));
133            imgFileField.setPreferredSize(new Dimension(200, 0));
134            
135            final JFileChooser imgFileDlg = new JFileChooser();
136            imgFileDlg.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
137            imgFileDlg.setCurrentDirectory(new File(System.getProperty("user.dir")));       
138            button = new JButton("Browse...");
139            button.addActionListener(new ActionListener() {
140                    public void actionPerformed(ActionEvent e) {
141                            if (JFileChooser.APPROVE_OPTION != imgFileDlg.showOpenDialog(EXPORT_DIALOG))
142                                    return;
143                            
144                            String selFName = imgFileDlg.getSelectedFile().getAbsolutePath();
145                            if (!imgFileDlg.getSelectedFile().isDirectory() && 0==FileTools.getExtension(selFName).length()) {
146                                    String mimeType = (String) imgTypeBox.getSelectedItem();
147                                    int p = mimeType.indexOf('/');
148                                    selFName = selFName + "." + mimeType.substring(p + 1);
149                            }
150                            imgFileField.setText(selFName);
151                    }
152            });
153            panel.add(button, Tools.createGridBagConstraints(2, 3, 1, 1));
154            
155            // Buttons:
156            
157            panel = new DisEnablingPanel(new FlowLayout(FlowLayout.CENTER));
158            getContentPane().add(panel, BorderLayout.SOUTH);
159            button = new JButton("Export");
160            button.addActionListener(new ActionListener() {
161                    public void actionPerformed(ActionEvent e) {
162                            if (exportConfirmed((Integer) imgWidthBox.getSelectedItem(),
163                                                                    (Integer) imgHeightBox.getSelectedItem(),
164                                                                    (String) imgTypeBox.getSelectedItem(),
165                                                                    imgFileField.getText())) {
166                                    EXPORT_DIALOG.setVisible(false);
167                            }                       
168                    }
169            });
170            panel.add(button);
171            button = new JButton("Cancel");
172            button.addActionListener(new ActionListener() {
173                    public void actionPerformed(ActionEvent e) { EXPORT_DIALOG.setVisible(false); }
174            });
175            panel.add(button);
176    }
177    
178    /**
179     * Verifies the validity of the selected user options and initiates the export.
180     * 
181     * @param imgWidth Width of the image to export.
182     * @param imgHeight Height of the image to export.
183     * @param imgType MIME type of the image to export.
184     * @param imgFile File of the image to export.
185     * @return Whether export has been undertaken.
186     */
187    private boolean exportConfirmed(int imgWidth, int imgHeight, String imgType, String imgFile) {
188            
189            File file = (new File(imgFile)).getAbsoluteFile();
190            
191            if (file.isDirectory()) {
192                    JOptionPane.showMessageDialog(this, "You have specified a directory.\nPlease specify a file.");
193                    return false;
194            }
195            
196            if (!file.exists()) {
197                    File parent = file.getParentFile();
198                    if (null == parent || !parent.isDirectory() || !parent.exists()) {
199                            JOptionPane.showMessageDialog(this, "Please specify a filename within an existing directory.");
200                            return false;
201                    }               
202            }       
203            
204            if (file.exists()) {
205                    int opt = JOptionPane.showConfirmDialog(this, "Do you want to overwrite the file\n"
206                                                                                                            + file.getAbsolutePath() + "?\n ",
207                                                                                                    "Overwrite file?", JOptionPane.YES_NO_OPTION);
208                    if (JOptionPane.YES_OPTION != opt)
209                            return false;                   
210            }
211            
212            exporter.doExportGraph(imgWidth, imgHeight, imgType, file);
213            return true;
214    }
215    
216    }