This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository faxtomail. See https://gitlab.nuiton.org/codelutin/faxtomail.git commit 405c9d062e6ebc5ac10a76faa9b292f10fba9d69 Author: Jean Couteau <jean.couteau@gmail.com> Date: Fri Apr 27 14:37:01 2018 +0200 fixes #9786 : Pendant l’Impression on ne peut plus rien faire tant que le document n’est pas sorti -> Passage en SwingWorker --- .../faxtomail/ui/swing/util/FaxToMailUIUtil.java | 236 ++++++++++----------- 1 file changed, 114 insertions(+), 122 deletions(-) diff --git a/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailUIUtil.java b/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailUIUtil.java index 84fbd5a8..66ed42f8 100644 --- a/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailUIUtil.java +++ b/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailUIUtil.java @@ -56,7 +56,6 @@ import jaxx.runtime.JAXXObject; import jaxx.runtime.JAXXUtil; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.Predicate; -import org.apache.commons.io.Charsets; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; @@ -83,6 +82,7 @@ import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.JTree; import javax.swing.SwingUtilities; +import javax.swing.SwingWorker; import javax.swing.event.HyperlinkEvent; import javax.swing.event.HyperlinkListener; import javax.swing.tree.DefaultMutableTreeNode; @@ -440,7 +440,6 @@ public final class FaxToMailUIUtil extends ApplicationUIUtil { */ public static boolean print(AttachmentFile attachmentFile, boolean defaultPrinter, FaxToMailConfiguration config) { boolean result; - FileInputStream fileInputStream = null; try { File file; if (!FaxToMailUIUtil.isFileAPDF(attachmentFile)) { @@ -449,16 +448,12 @@ public final class FaxToMailUIUtil extends ApplicationUIUtil { file = attachmentFile.getFile(); } - fileInputStream = new FileInputStream(file); - - result = printWithPdfRenderer(attachmentFile.getFilename(), fileInputStream, defaultPrinter, config); + result = printWithPdfRenderer(attachmentFile.getFilename(), file, defaultPrinter, config); } catch (IOException | DocumentException e) { throw new ApplicationTechnicalException( t("jaxx.application.error.cannot.print"), e); - } finally { - IOUtils.closeQuietly(fileInputStream); } return result; } @@ -471,153 +466,150 @@ public final class FaxToMailUIUtil extends ApplicationUIUtil { * @param defaultPrinter if {@code true}, do not display print dialog and print with default printer * @return true if file has been printed, false otherwise */ - public static boolean printText(String printName, String text, boolean defaultPrinter, FaxToMailConfiguration config) { - boolean result; - FileInputStream fileInputStream = null; - ByteArrayInputStream byteArrayInputStream = null; - - try { - // convert text content to inputstream - byte[] content = text.getBytes(Charsets.UTF_8); - byteArrayInputStream = new ByteArrayInputStream(content); + public static void printText(String printName, String text, boolean defaultPrinter, FaxToMailConfiguration config) { + // convert text content to inputstream + byte[] content = text.getBytes(StandardCharsets.UTF_8); + try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content)){ File file = convertFileToPdf(byteArrayInputStream, FileType.TEXT); - fileInputStream = new FileInputStream(file); - result = printWithPdfRenderer(printName, fileInputStream, defaultPrinter, config); + printWithPdfRenderer(printName, file, defaultPrinter, config); } catch (IOException | DocumentException e) { throw new ApplicationTechnicalException( t("jaxx.application.error.cannot.print"), e); - - } finally { - IOUtils.closeQuietly(fileInputStream); - IOUtils.closeQuietly(byteArrayInputStream); } - return result; } /** * Imprime un attachment file. * * @param printName print job name - * @param fis stream to print + * @param file file to print * @param defaultPrinter if {@code true}, do not display print dialog and print with default printer * @return true if file has been printed, false otherwise */ - //TODO kmorin print in an action - protected static boolean printWithPdfRenderer(String printName, FileInputStream fis, boolean defaultPrinter, + protected static boolean printWithPdfRenderer(final String printName, final File file, + final boolean defaultPrinter, final FaxToMailConfiguration config) { - boolean result = false; - PDDocument pdDocument = null; - - try { - - PrinterJob printJob = PrinterJob.getPrinterJob(); - printJob.setJobName(printName); - // Send print job to default printer - PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet(); - result = defaultPrinter || printJob.printDialog(attributes); + final PrinterJob printJob = PrinterJob.getPrinterJob(); + printJob.setJobName(printName); - if (result) { - pdDocument = PDDocument.load(fis); - final PDFRenderer renderer = new PDFRenderer(pdDocument); - final int numOfPages = pdDocument.getNumberOfPages(); + // Send print job to default printer + final PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet(); + final boolean result = defaultPrinter || printJob.printDialog(attributes); - printJob.setPrintable(new Printable() { - public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { - try { - if (pageIndex >= numOfPages) { - return NO_SUCH_PAGE; - } - - //get back margin - int marginMillimeter = config.getDefaultPrintMargin(); - - //Init pageFormat with margin from conf (do not use default params) - Paper paper = pageFormat.getPaper(); - double margin = marginMillimeter*2.83465; // convert millimeters to points - paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - - margin * 2); - pageFormat.setPaper(paper); - - double paperWidth = (int) pageFormat.getImageableWidth(); - double paperHeight = (int) pageFormat.getImageableHeight(); - int paperX = (int) pageFormat.getImageableX(); - int paperY = (int) pageFormat.getImageableY(); - - if (log.isDebugEnabled()) { - log.debug("page width " + paperWidth); - log.debug("page height " + paperHeight); - log.debug("page x " + paperX); - log.debug("page y " + paperY); - } - - BufferedImage image = renderer.renderImageWithDPI(pageIndex, 300); - - int imageWidth = image.getWidth(); - int imageHeight = image.getHeight(); - double widthRatio = paperWidth / imageWidth; - double heightRatio = paperHeight / imageHeight; - - if (log.isDebugEnabled()) { - log.debug("width ratio : " + widthRatio); - log.debug("height ratio : " + heightRatio); - } - - int width; - int height; - // if the image is smaller than the page - if (widthRatio >= 1 && heightRatio >= 1) { - width = imageWidth; - height = imageHeight; - - } else { - double minRatio = Math.min(widthRatio, heightRatio); - width = (int) (minRatio * imageWidth); - height = (int) (minRatio * imageHeight); - } + //Use SwingWorker so that printing does not block UI (cf. #9786) + SwingWorker worker = new SwingWorker<Void, Void>() { + @Override + public Void doInBackground() { + + if (result) { + try (FileInputStream fis = new FileInputStream(file)) { + try (PDDocument pdDocument = PDDocument.load(fis);) { + + final PDFRenderer renderer = new PDFRenderer(pdDocument); + final int numOfPages = pdDocument.getNumberOfPages(); + + printJob.setPrintable(new Printable() { + public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { + try { + if (pageIndex >= numOfPages) { + return NO_SUCH_PAGE; + } + + //get back margin + int marginMillimeter = config.getDefaultPrintMargin(); + + //Init pageFormat with margin from conf (do not use default params) + Paper paper = pageFormat.getPaper(); + double margin = marginMillimeter * 2.83465; // convert millimeters to points + paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() + - margin * 2); + pageFormat.setPaper(paper); + + double paperWidth = (int) pageFormat.getImageableWidth(); + double paperHeight = (int) pageFormat.getImageableHeight(); + int paperX = (int) pageFormat.getImageableX(); + int paperY = (int) pageFormat.getImageableY(); + + if (log.isDebugEnabled()) { + log.debug("page width " + paperWidth); + log.debug("page height " + paperHeight); + log.debug("page x " + paperX); + log.debug("page y " + paperY); + } + + BufferedImage image = renderer.renderImageWithDPI(pageIndex, 300); + + int imageWidth = image.getWidth(); + int imageHeight = image.getHeight(); + double widthRatio = paperWidth / imageWidth; + double heightRatio = paperHeight / imageHeight; + + if (log.isDebugEnabled()) { + log.debug("width ratio : " + widthRatio); + log.debug("height ratio : " + heightRatio); + } + + int width; + int height; + // if the image is smaller than the page + if (widthRatio >= 1 && heightRatio >= 1) { + width = imageWidth; + height = imageHeight; + + } else { + double minRatio = Math.min(widthRatio, heightRatio); + width = (int) (minRatio * imageWidth); + height = (int) (minRatio * imageHeight); + } + + if (log.isDebugEnabled()) { + log.debug("image width : " + width); + log.debug("image height : " + height); + } + + //use full size + graphics.setClip(paperX, paperY, width, height); + graphics.drawImage(image, paperX, paperY, width, height, null); + + return PAGE_EXISTS; + + } catch (Exception e) { + if (log.isErrorEnabled()) { + log.error("error while printing", e); + } + return NO_SUCH_PAGE; + } + } + }); - if (log.isDebugEnabled()) { - log.debug("image width : " + width); - log.debug("image height : " + height); + printJob.print(attributes); + } catch (PrinterException ex) { + if (log.isErrorEnabled()) { + log.error("can't print", ex); } - //use full size - graphics.setClip(paperX,paperY, width, height); - graphics.drawImage(image, paperX, paperY, width, height, null); - - return PAGE_EXISTS; - - } catch (Exception e) { + } catch (IOException e) { if (log.isErrorEnabled()) { - log.error("error while printing", e); + log.error("", e); } - return NO_SUCH_PAGE; + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("", e); } } - }); + } - printJob.print(attributes); + return null; } + }; - } catch (PrinterException ex) { - if (log.isErrorEnabled()) { - log.error("can't print", ex); - } - - } catch (IOException e) { - if (log.isErrorEnabled()) { - log.error("", e); - } - } finally { - if (log.isDebugEnabled()) { - log.debug("finally close the pdf file"); - } - IOUtils.closeQuietly(pdDocument); - } + worker.execute(); return result; } -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.