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 "AS IS", 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, imgFileField.getPreferredSize().height)); 134 135 try { 136 final JFileChooser imgFileDlg = new JFileChooser(""); 137 imgFileDlg.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 138 try { 139 imgFileDlg.setCurrentDirectory(new File(System.getProperty("user.dir"))); 140 } catch(SecurityException e) { 141 imgFileDlg.setCurrentDirectory(new File(System.getProperty("user.home"))); 142 } 143 button = null; 144 button = new JButton("Browse..."); 145 button.addActionListener(new ActionListener() { 146 public void actionPerformed(ActionEvent e) { 147 if (JFileChooser.APPROVE_OPTION != imgFileDlg.showOpenDialog(EXPORT_DIALOG)) 148 return; 149 150 String selFName = imgFileDlg.getSelectedFile().getAbsolutePath(); 151 if (!imgFileDlg.getSelectedFile().isDirectory() && 0==FileTools.getExtension(selFName).length()) { 152 String mimeType = (String) imgTypeBox.getSelectedItem(); 153 int p = mimeType.indexOf('/'); 154 selFName = selFName + "." + mimeType.substring(p + 1); 155 } 156 imgFileField.setText(selFName); 157 } 158 }); 159 panel.add(button, Tools.createGridBagConstraints(2, 3, 1, 1)); 160 } catch(SecurityException e) { 161 if (null != button) 162 button.setEnabled(false); 163 } 164 165 // Buttons: 166 167 panel = new DisEnablingPanel(new FlowLayout(FlowLayout.CENTER)); 168 getContentPane().add(panel, BorderLayout.SOUTH); 169 button = new JButton("Export"); 170 button.addActionListener(new ActionListener() { 171 public void actionPerformed(ActionEvent e) { 172 if (exportConfirmed((Integer) imgWidthBox.getSelectedItem(), 173 (Integer) imgHeightBox.getSelectedItem(), 174 (String) imgTypeBox.getSelectedItem(), 175 imgFileField.getText())) { 176 EXPORT_DIALOG.setVisible(false); 177 } 178 } 179 }); 180 panel.add(button); 181 button = new JButton("Cancel"); 182 button.addActionListener(new ActionListener() { 183 public void actionPerformed(ActionEvent e) { EXPORT_DIALOG.setVisible(false); } 184 }); 185 panel.add(button); 186 } 187 188 /** 189 * Verifies the validity of the selected user options and initiates the export. 190 * 191 * @param imgWidth Width of the image to export. 192 * @param imgHeight Height of the image to export. 193 * @param imgType MIME type of the image to export. 194 * @param imgFile File of the image to export. 195 * @return Whether export has been undertaken. 196 */ 197 private boolean exportConfirmed(int imgWidth, int imgHeight, String imgType, String imgFile) { 198 199 File file = null; 200 try { 201 file = (new File(imgFile)).getAbsoluteFile(); 202 203 if (file.isDirectory()) { 204 JOptionPane.showMessageDialog(this, "You have specified a directory.\nPlease specify a file."); 205 return false; 206 } 207 208 if (!file.exists()) { 209 File parent = file.getParentFile(); 210 if (null == parent || !parent.isDirectory() || !parent.exists()) { 211 JOptionPane.showMessageDialog(this, "Please specify a filename within an existing directory."); 212 return false; 213 } 214 } 215 216 if (file.exists()) { 217 int opt = JOptionPane.showConfirmDialog(this, "Do you want to overwrite the file\n" 218 + file.getAbsolutePath() + "?\n ", 219 "Overwrite file?", JOptionPane.YES_NO_OPTION); 220 if (JOptionPane.YES_OPTION != opt) 221 return false; 222 } 223 } catch(SecurityException e) { 224 JOptionPane.showMessageDialog(this, "The Java security environment does not permit access" 225 + " to the specified file. \nYou may be running LiveGraph in" 226 + " a restricted security environment."); 227 return false; 228 } 229 230 if (null != file) 231 exporter.doExportGraph(imgWidth, imgHeight, imgType, file); 232 233 return true; 234 } 235 236 }