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"));
}
}
}
}
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));
}
Aggregations