001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.view.save;
029:
030: import java.io.File;
031: import java.text.MessageFormat;
032:
033: import javax.swing.JOptionPane;
034:
035: import net.sf.jasperreports.engine.JRException;
036: import net.sf.jasperreports.engine.JRExporterParameter;
037: import net.sf.jasperreports.engine.JasperPrint;
038: import net.sf.jasperreports.engine.export.JRHtmlExporter;
039: import net.sf.jasperreports.view.JRSaveContributor;
040:
041: /**
042: * @author Teodor Danciu (teodord@users.sourceforge.net)
043: * @version $Id: JRHtmlSaveContributor.java 1229 2006-04-19 10:27:35Z teodord $
044: */
045: public class JRHtmlSaveContributor extends JRSaveContributor {
046:
047: /**
048: *
049: */
050: private static final String EXTENSION_HTM = ".htm";
051: private static final String EXTENSION_HTML = ".html";
052: public static final JRHtmlSaveContributor INSTANCE = new JRHtmlSaveContributor();
053:
054: /**
055: *
056: */
057: public static JRHtmlSaveContributor getInstance() {
058: return INSTANCE;
059: }
060:
061: /**
062: *
063: */
064: public boolean accept(File file) {
065: if (file.isDirectory()) {
066: return true;
067: }
068: String name = file.getName().toLowerCase();
069: return (name.endsWith(EXTENSION_HTM) || name
070: .endsWith(EXTENSION_HTML));
071: }
072:
073: /**
074: *
075: */
076: public String getDescription() {
077: return "HTML (*.htm, *.html)";
078: }
079:
080: /**
081: *
082: */
083: public void save(JasperPrint jasperPrint, File file)
084: throws JRException {
085: if (!file.getName().endsWith(EXTENSION_HTM)
086: && !file.getName().endsWith(EXTENSION_HTML)) {
087: file = new File(file.getAbsolutePath() + EXTENSION_HTML);
088: }
089:
090: if (!file.exists()
091: || JOptionPane.OK_OPTION == JOptionPane
092: .showConfirmDialog(
093: null,
094: MessageFormat
095: .format(
096: java.util.ResourceBundle
097: .getBundle(
098: "net/sf/jasperreports/view/viewer")
099: .getString(
100: "file.exists"),
101: new Object[] { file
102: .getName() }),
103: java.util.ResourceBundle
104: .getBundle(
105: "net/sf/jasperreports/view/viewer")
106: .getString("save"),
107: JOptionPane.OK_CANCEL_OPTION)) {
108: JRHtmlExporter exporter = new JRHtmlExporter();
109: exporter.setParameter(JRExporterParameter.JASPER_PRINT,
110: jasperPrint);
111: exporter
112: .setParameter(JRExporterParameter.OUTPUT_FILE, file);
113: exporter.exportReport();
114: }
115: }
116:
117: }
|