Search in sources :

Example 1 with JRCsvSaveContributor

use of net.sf.jasperreports.view.save.JRCsvSaveContributor in project jgnash by ccavanaugh.

the class DynamicJasperReportPanel method saveAction.

private void saveAction() {
    Preferences p = Preferences.userNodeForPackage(DynamicJasperReportPanel.class);
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setMultiSelectionEnabled(false);
    saveContributors.forEach(fileChooser::addChoosableFileFilter);
    // restore the last save format
    if (p.get(LAST_CONTRIBUTOR, null) != null) {
        String last = p.get(LAST_CONTRIBUTOR, null);
        for (JRSaveContributor saveContributor : saveContributors) {
            if (saveContributor.getDescription().equals(last)) {
                fileChooser.setFileFilter(saveContributor);
                break;
            }
        }
    } else if (!saveContributors.isEmpty()) {
        fileChooser.setFileFilter(saveContributors.get(0));
    }
    if (p.get(LAST_DIRECTORY, null) != null) {
        fileChooser.setCurrentDirectory(new File(p.get(LAST_DIRECTORY, null)));
    }
    int retValue = fileChooser.showSaveDialog(this);
    if (retValue == JFileChooser.APPROVE_OPTION) {
        FileFilter fileFilter = fileChooser.getFileFilter();
        File file = fileChooser.getSelectedFile();
        p.put(LAST_DIRECTORY, file.getParent());
        JRSaveContributor contributor = null;
        if (fileFilter instanceof JRSaveContributor) {
            // save format chosen from the list
            contributor = (JRSaveContributor) fileFilter;
        } else {
            for (JRSaveContributor saveContributor : saveContributors) {
                // need to determine the best match
                if (saveContributor.accept(file)) {
                    contributor = saveContributor;
                    break;
                }
            }
            if (contributor == null) {
                JOptionPane.showMessageDialog(this, resourceBundle.getString("error.saving"));
            }
        }
        if (contributor != null) {
            p.put(LAST_CONTRIBUTOR, contributor.getDescription());
            try {
                if (contributor instanceof JRSingleSheetXlsSaveContributor) {
                    LOG.info("Formatting for xls file");
                    JasperPrint print = report.createJasperPrint(true);
                    contributor.save(print, file);
                } else if (contributor instanceof JRCsvSaveContributor) {
                    LOG.info("Formatting for csv file");
                    JasperPrint print = report.createJasperPrint(true);
                    contributor.save(print, file);
                } else {
                    contributor.save(jasperPrint, file);
                }
            } catch (final JRException ex) {
                LOG.log(Level.SEVERE, ex.getMessage(), ex);
                JOptionPane.showMessageDialog(this, resourceBundle.getString("error.saving"));
            }
        }
    }
}
Also used : JRSingleSheetXlsSaveContributor(net.sf.jasperreports.view.save.JRSingleSheetXlsSaveContributor) JFileChooser(javax.swing.JFileChooser) JRException(net.sf.jasperreports.engine.JRException) JasperPrint(net.sf.jasperreports.engine.JasperPrint) JRSaveContributor(net.sf.jasperreports.view.JRSaveContributor) Preferences(java.util.prefs.Preferences) FileFilter(javax.swing.filechooser.FileFilter) File(java.io.File) JasperPrint(net.sf.jasperreports.engine.JasperPrint) JRCsvSaveContributor(net.sf.jasperreports.view.save.JRCsvSaveContributor)

Example 2 with JRCsvSaveContributor

use of net.sf.jasperreports.view.save.JRCsvSaveContributor in project jgnash by ccavanaugh.

the class DynamicJasperReportPanel method initSaveContributors.

private void initSaveContributors() {
    saveContributors.add(new JRPdfSaveContributor(Locale.getDefault(), resourceBundle));
    saveContributors.add(new JROdtSaveContributor(Locale.getDefault(), resourceBundle));
    saveContributors.add(new JRDocxSaveContributor(Locale.getDefault(), resourceBundle));
    saveContributors.add(new JRRtfSaveContributor(Locale.getDefault(), resourceBundle));
    saveContributors.add(new JRCsvSaveContributor(Locale.getDefault(), resourceBundle));
    saveContributors.add(new JRSingleSheetXlsSaveContributor(Locale.getDefault(), resourceBundle));
}
Also used : JRSingleSheetXlsSaveContributor(net.sf.jasperreports.view.save.JRSingleSheetXlsSaveContributor) JRPdfSaveContributor(net.sf.jasperreports.view.save.JRPdfSaveContributor) JRRtfSaveContributor(net.sf.jasperreports.view.save.JRRtfSaveContributor) JROdtSaveContributor(net.sf.jasperreports.view.save.JROdtSaveContributor) JRDocxSaveContributor(net.sf.jasperreports.view.save.JRDocxSaveContributor) JRCsvSaveContributor(net.sf.jasperreports.view.save.JRCsvSaveContributor)

Aggregations

JRCsvSaveContributor (net.sf.jasperreports.view.save.JRCsvSaveContributor)2 JRSingleSheetXlsSaveContributor (net.sf.jasperreports.view.save.JRSingleSheetXlsSaveContributor)2 File (java.io.File)1 Preferences (java.util.prefs.Preferences)1 JFileChooser (javax.swing.JFileChooser)1 FileFilter (javax.swing.filechooser.FileFilter)1 JRException (net.sf.jasperreports.engine.JRException)1 JasperPrint (net.sf.jasperreports.engine.JasperPrint)1 JRSaveContributor (net.sf.jasperreports.view.JRSaveContributor)1 JRDocxSaveContributor (net.sf.jasperreports.view.save.JRDocxSaveContributor)1 JROdtSaveContributor (net.sf.jasperreports.view.save.JROdtSaveContributor)1 JRPdfSaveContributor (net.sf.jasperreports.view.save.JRPdfSaveContributor)1 JRRtfSaveContributor (net.sf.jasperreports.view.save.JRRtfSaveContributor)1