1 28 package net.sf.jasperreports.view.save; 29 30 import java.io.File ; 31 import java.text.MessageFormat ; 32 33 import javax.swing.JOptionPane ; 34 35 import net.sf.jasperreports.engine.JRException; 36 import net.sf.jasperreports.engine.JRExporterParameter; 37 import net.sf.jasperreports.engine.JasperPrint; 38 import net.sf.jasperreports.engine.export.JRHtmlExporter; 39 import net.sf.jasperreports.view.JRSaveContributor; 40 41 45 public class JRHtmlSaveContributor extends JRSaveContributor 46 { 47 48 51 private static final String EXTENSION_HTM = ".htm"; 52 private static final String EXTENSION_HTML = ".html"; 53 public static final JRHtmlSaveContributor INSTANCE = new JRHtmlSaveContributor(); 54 55 58 public static JRHtmlSaveContributor getInstance() 59 { 60 return INSTANCE; 61 } 62 63 66 public boolean accept(File file) 67 { 68 if (file.isDirectory()) 69 { 70 return true; 71 } 72 String name = file.getName().toLowerCase(); 73 return (name.endsWith(EXTENSION_HTM) || name.endsWith(EXTENSION_HTML)); 74 } 75 76 79 public String getDescription() 80 { 81 return "HTML (*.htm, *.html)"; 82 } 83 84 87 public void save(JasperPrint jasperPrint, File file) throws JRException 88 { 89 if ( 90 !file.getName().endsWith(EXTENSION_HTM) 91 && !file.getName().endsWith(EXTENSION_HTML) 92 ) 93 { 94 file = new File (file.getAbsolutePath() + EXTENSION_HTML); 95 } 96 97 if ( 98 !file.exists() || 99 JOptionPane.OK_OPTION == 100 JOptionPane.showConfirmDialog( 101 null, 102 MessageFormat.format( 103 java.util.ResourceBundle.getBundle("net/sf/jasperreports/view/viewer").getString("file.exists"), 104 new Object []{file.getName()} 105 ), 106 java.util.ResourceBundle.getBundle("net/sf/jasperreports/view/viewer").getString("save"), 107 JOptionPane.OK_CANCEL_OPTION 108 ) 109 ) 110 { 111 JRHtmlExporter exporter = new JRHtmlExporter(); 112 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 113 exporter.setParameter(JRExporterParameter.OUTPUT_FILE, file); 114 exporter.exportReport(); 115 } 116 } 117 118 } 119 | Popular Tags |