/** * Create a default character sheet output file name based on the export template type and the * character file name. The output file will be in the same folder as the character file. * * @param characterFilename The path to the character PCG file. * @return The default output file name. */ private String generateOutputFilename(String characterFilename) { File charFile = new File(characterFilename); String charname = charFile.getName(); String extension = ExportUtilities.getOutputExtension(exportTemplateFilename, isPdf); String outputName = charname.substring(0, charname.lastIndexOf('.')) + "." + extension; return new File(charFile.getParent(), outputName).getAbsolutePath(); }
/** * Get a temporary file name for outputting a character using a particular output template. * * @param templateFile The output template that will be used. * @return The temporary file, or null if it could not be created. */ public static File getTempOutputFilename(File templateFile) { String extension = ExportUtilities.getOutputExtension( templateFile.getName(), ExportUtilities.isPdfTemplate(templateFile)); try { // create a temporary file to view the character output return File.createTempFile( Constants.TEMPORARY_FILE_NAME, "." + extension, SettingsHandler.getTempPath()); } catch (IOException ioe) { ShowMessageDelegate.showMessageDialog( "Could not create temporary preview file.", "PCGen", MessageType.ERROR); Logging.errorPrint("Could not create temporary preview file.", ioe); return null; } }
@Override public void previewSpells() { boolean aBool = SettingsHandler.getPrintSpellsWithPC(); SettingsHandler.setPrintSpellsWithPC(true); String templateFileName = PCGenSettings.getInstance().getProperty(PCGenSettings.SELECTED_SPELL_SHEET_PATH); if (StringUtils.isEmpty(templateFileName)) { delegate.showErrorMessage( Constants.APPLICATION_NAME, LanguageBundle.getString("in_spellNoSheet")); // $NON-NLS-1$ return; } File templateFile = new File(templateFileName); File outputFile = BatchExporter.getTempOutputFilename(templateFile); boolean success; if (ExportUtilities.isPdfTemplate(templateFile)) { success = BatchExporter.exportCharacterToPDF(pcFacade, outputFile, templateFile); } else { success = BatchExporter.exportCharacterToNonPDF(pcFacade, outputFile, templateFile); } if (success) { try { Utility.viewInBrowser(outputFile); } catch (IOException e) { Logging.errorPrint("SpellSupportFacadeImpl.previewSpells failed", e); delegate.showErrorMessage( Constants.APPLICATION_NAME, LanguageBundle.getString("in_spellPreviewFail")); // $NON-NLS-1$ } } SettingsHandler.setPrintSpellsWithPC(aBool); }
/** * Write a PDF party sheet for the characters in the party to the output file. The party sheet * will be built according to the template file. If the output file exists it will be overwritten. * * @param party The already loaded party of characters to be output. * @param outFile The file to which the party sheet is to be written. * @param templateFile The file that has the export template definition. * @return true if the export was successful, false if it failed in some way. */ public static boolean exportPartyToPDF(PartyFacade party, File outFile, File templateFile) { // We want the non pdf extension here for the intermediate file. String templateExtension = ExportUtilities.getOutputExtension(templateFile.getName(), false); boolean isTransformTemplate = "xslt".equalsIgnoreCase(templateExtension) || "xsl".equalsIgnoreCase(templateExtension); boolean useTempFile = PCGenSettings.OPTIONS_CONTEXT.initBoolean( PCGenSettings.OPTION_GENERATE_TEMP_FILE_WITH_PDF, false); String outFileName = FilenameUtils.removeExtension(outFile.getAbsolutePath()); File tempFile = isTransformTemplate ? new File(outFileName + ".xml") : new File(outFileName + ".fo"); try (BufferedOutputStream fileStream = new BufferedOutputStream(new FileOutputStream(outFile)); ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); OutputStream exportOutput = useTempFile // Output to both the byte stream and to the temp file. ? new TeeOutputStream(byteOutputStream, new FileOutputStream(tempFile)) : byteOutputStream) { FopTask task; if (isTransformTemplate) { exportParty(party, exportOutput); ByteArrayInputStream inputStream = new ByteArrayInputStream(byteOutputStream.toByteArray()); task = FopTask.newFopTask(inputStream, templateFile, fileStream); } else { SettingsHandler.setSelectedPartyPDFOutputSheet(templateFile.getAbsolutePath()); exportParty(party, templateFile, exportOutput); ByteArrayInputStream inputStream = new ByteArrayInputStream(byteOutputStream.toByteArray()); task = FopTask.newFopTask(inputStream, null, fileStream); } task.run(); } catch (IOException e) { Logging.errorPrint("BatchExporter.exportPartyToPDF failed", e); return false; } catch (ExportException e) { Logging.errorPrint("BatchExporter.exportPartyToPDF failed", e); return false; } return true; }
/** * Create a new instance of BatchExporter for use with a particular export template. * * @param exportTemplateFilename The path to the export template. * @param uiDelegate The object through which to report any issues to the user. */ BatchExporter(String exportTemplateFilename, UIDelegate uiDelegate) { this.exportTemplateFilename = exportTemplateFilename; this.uiDelegate = uiDelegate; isPdf = ExportUtilities.isPdfTemplate(exportTemplateFilename); }
@Override public void exportSpells() { final String template = PCGenSettings.getInstance().getProperty(PCGenSettings.SELECTED_SPELL_SHEET_PATH); if (StringUtils.isEmpty(template)) { delegate.showErrorMessage( Constants.APPLICATION_NAME, LanguageBundle.getString("in_spellNoSheet")); // $NON-NLS-1$ return; } String ext = template.substring(template.lastIndexOf('.')); // Get the name of the file to output to. JFileChooser fcExport = new JFileChooser(); fcExport.setCurrentDirectory(new File(PCGenSettings.getPcgDir())); fcExport.setDialogTitle( LanguageBundle.getString("InfoSpells.export.spells.for") + charDisplay.getDisplayName()); // $NON-NLS-1$ if (fcExport.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) { return; } final String aFileName = fcExport.getSelectedFile().getAbsolutePath(); if (aFileName.length() < 1) { delegate.showErrorMessage( Constants.APPLICATION_NAME, LanguageBundle.getString("InfoSpells.must.set.filename")); // $NON-NLS-1$ return; } try { final File outFile = new File(aFileName); if (outFile.isDirectory()) { delegate.showErrorMessage( Constants.APPLICATION_NAME, LanguageBundle.getString("InfoSpells.can.not.overwrite.directory")); // $NON-NLS-1$ return; } if (outFile.exists()) { int reallyClose = JOptionPane.showConfirmDialog( null, LanguageBundle.getFormattedString( "InfoSpells.confirm.overwrite", outFile.getName()), // $NON-NLS-1$ LanguageBundle.getFormattedString("InfoSpells.overwriting", outFile.getName()), JOptionPane.YES_NO_OPTION); // $NON-NLS-1$ if (reallyClose != JOptionPane.YES_OPTION) { return; } } // Output the file File templateFile = new File(template); boolean success; if (ExportUtilities.isPdfTemplate(templateFile)) { success = BatchExporter.exportCharacterToPDF(pcFacade, outFile, templateFile); } else { success = BatchExporter.exportCharacterToNonPDF(pcFacade, outFile, templateFile); } if (!success) { delegate.showErrorMessage( Constants.APPLICATION_NAME, LanguageBundle.getFormattedString( "InfoSpells.export.failed", charDisplay.getDisplayName())); // $NON-NLS-1$ } } catch (Exception ex) { Logging.errorPrint( LanguageBundle.getFormattedString( "InfoSpells.export.failed", charDisplay.getDisplayName()), ex); //$NON-NLS-1$ delegate.showErrorMessage( Constants.APPLICATION_NAME, LanguageBundle.getFormattedString( "InfoSpells.export.failed.retry", charDisplay.getDisplayName())); // $NON-NLS-1$ } }