use of net.sf.jasperreports.engine.JRException 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.engine.JRException in project jgnash by ccavanaugh.
the class JasperViewerDialogController method handleSaveAction.
@FXML
private void handleSaveAction() {
Preferences preferences = Preferences.userNodeForPackage(JasperViewerDialogController.class);
final String lastDir = preferences.get(LAST_DIR, null);
final FileChooser fileChooser = new FileChooser();
fileChooser.setTitle(ResourceUtils.getString("Title.SaveFile"));
if (lastDir != null) {
fileChooser.setInitialDirectory(new File(lastDir));
}
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("PDF Document", "*.pdf", "*.PDF"));
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("ODT Document", "*.odt", "*.ODT"));
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("DOCX Document", "*.docx", "*.DOCX"));
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("XLSX Document", "*.xlsx", "*.XLSX"));
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("CSV Document", "*.csv", "*.CSV"));
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("RTF Document", "*.rtf", "*.RTF"));
final File file = fileChooser.showSaveDialog(MainView.getPrimaryStage());
if (file != null) {
preferences.put(LAST_DIR, file.getParent());
switch(FileUtils.getFileExtension(file.getAbsolutePath()).toLowerCase(Locale.ROOT)) {
case "pdf":
try {
JasperExportManager.exportReportToPdfFile(jasperPrint.get(), file.getAbsolutePath());
} catch (final JRException e) {
StaticUIMethods.displayException(e);
}
break;
case "odt":
try {
final JROdtExporter exporter = new JROdtExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint.get()));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(file));
exporter.exportReport();
} catch (final JRException e) {
StaticUIMethods.displayException(e);
}
break;
case "docx":
try {
final JRDocxExporter exporter = new JRDocxExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint.get()));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(file));
exporter.exportReport();
} catch (final JRException e) {
StaticUIMethods.displayException(e);
}
break;
case "xlsx":
try {
final JRXlsExporter exporter = new JRXlsExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint.get()));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(file));
final SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
configuration.setOnePagePerSheet(false);
exporter.setConfiguration(configuration);
exporter.exportReport();
} catch (final JRException e) {
StaticUIMethods.displayException(e);
}
break;
case "csv":
try {
final JRCsvExporter exporter = new JRCsvExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint.get()));
exporter.setExporterOutput(new SimpleWriterExporterOutput(file));
exporter.exportReport();
} catch (final JRException e) {
StaticUIMethods.displayException(e);
}
break;
case "rtf":
try {
final JRRtfExporter exporter = new JRRtfExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint.get()));
exporter.setExporterOutput(new SimpleWriterExporterOutput(file));
exporter.exportReport();
} catch (final JRException e) {
StaticUIMethods.displayException(e);
}
break;
default:
}
}
}
use of net.sf.jasperreports.engine.JRException in project jgnash by ccavanaugh.
the class JasperViewerDialogController method refresh.
private void refresh() {
final List<Node> children = pagePane.getChildren();
children.clear();
for (int i = 0; i < pageCount.get(); i++) {
try {
final BufferedImage bufferedImage = (BufferedImage) JasperPrintManager.printPageToImage(jasperPrint.get(), i, (float) zoom);
final ImageView imageView = new ImageView(SwingFXUtils.toFXImage(bufferedImage, null));
imageView.setEffect(dropShadow);
children.add(imageView);
} catch (final JRException ex) {
StaticUIMethods.displayException(ex);
}
}
setPageIndex(0);
}
use of net.sf.jasperreports.engine.JRException in project opennms by OpenNMS.
the class CustomJRJdtCompiler method getNameEnvironment.
protected INameEnvironment getNameEnvironment(final JRCompilationUnit[] units) {
final INameEnvironment env = new INameEnvironment() {
@Override
public NameEnvironmentAnswer findType(char[][] compoundTypeName) {
StringBuilder result = new StringBuilder();
String sep = "";
for (int i = 0; i < compoundTypeName.length; i++) {
result.append(sep);
result.append(compoundTypeName[i]);
sep = ".";
}
return findType(result.toString());
}
@Override
public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) {
StringBuilder result = new StringBuilder();
String sep = "";
for (int i = 0; i < packageName.length; i++) {
result.append(sep);
result.append(packageName[i]);
sep = ".";
}
result.append(sep);
result.append(typeName);
return findType(result.toString());
}
private int getClassIndex(String className) {
int classIdx;
for (classIdx = 0; classIdx < units.length; ++classIdx) {
if (className.equals(units[classIdx].getName())) {
break;
}
}
if (classIdx >= units.length) {
classIdx = -1;
}
return classIdx;
}
private NameEnvironmentAnswer findType(String className) {
try {
int classIdx = getClassIndex(className);
if (classIdx >= 0) {
ICompilationUnit compilationUnit = new CompilationUnit(units[classIdx].getSourceCode(), className);
return new NameEnvironmentAnswer(compilationUnit, null);
}
String resourceName = className.replace('.', '/') + ".class";
InputStream is = getResource(resourceName);
if (is != null) {
try {
byte[] classBytes = JRLoader.loadBytes(is);
char[] fileName = className.toCharArray();
ClassFileReader classFileReader = new ClassFileReader(classBytes, fileName, true);
return new NameEnvironmentAnswer(classFileReader, null);
} finally {
try {
is.close();
} catch (IOException e) {
// ignore
}
}
}
} catch (JRException e) {
LOG.error("Compilation error", e);
} catch (org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException exc) {
LOG.error("Compilation error", exc);
} catch (IllegalArgumentException e) {
throw new JRRuntimeException(EXCEPTION_MESSAGE_KEY_NAME_ENVIRONMENT_ANSWER_INSTANCE_ERROR, (Object[]) null, e);
}
return null;
}
private boolean isPackage(String result) {
int classIdx = getClassIndex(result);
if (classIdx >= 0) {
return false;
}
String resourceName = result.replace('.', '/') + ".class";
boolean isPackage = true;
InputStream is = getResource(resourceName);
if (is != null) {
// 1.5 plugin
try {
isPackage = (is.read() > 0);
} catch (IOException e) {
// ignore
} finally {
try {
is.close();
} catch (IOException e) {
// ignore
}
}
}
return isPackage;
}
@Override
public boolean isPackage(char[][] parentPackageName, char[] packageName) {
StringBuilder result = new StringBuilder();
String sep = "";
if (parentPackageName != null) {
for (int i = 0; i < parentPackageName.length; i++) {
result.append(sep);
result.append(parentPackageName[i]);
sep = ".";
}
}
if (Character.isUpperCase(packageName[0])) {
if (!isPackage(result.toString())) {
return false;
}
}
result.append(sep);
result.append(packageName);
return isPackage(result.toString());
}
@Override
public void cleanup() {
}
};
return env;
}
use of net.sf.jasperreports.engine.JRException in project opennms by OpenNMS.
the class ReportCompiler method main.
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
if (args.length > 1 && new File(args[0]).exists()) {
System.setProperty("opennms.home", args[0]);
}
initializeSingleInstanceDatabase();
// To trigger Spring Initialization
BeanUtils.getBeanFactory("jasperReportContext");
File reportsDirectory = new File(ConfigFileConstants.getFilePathString(), "report-templates");
System.out.printf("Analyzing jasper reports located at %s\n", reportsDirectory);
for (File report : FileUtils.listFiles(reportsDirectory, new String[] { "jrxml" }, true)) {
System.out.printf("Compiling report template %s\n", report.getAbsolutePath());
try {
JasperCompileManager.compileReportToFile(report.getAbsolutePath());
} catch (JRException e) {
System.err.println("Error: cannot compile report because: " + e.getMessage());
e.printStackTrace();
}
}
}
Aggregations