Author: kmorin Date: 2014-12-18 10:47:18 +0000 (Thu, 18 Dec 2014) New Revision: 732 Url: http://forge.codelutin.com/projects/faxtomail/repository/revisions/732 Log: fixes #6260 Document rogn?\195?\169 ?\195?\160 l'impression catch ClassCastException (c'est moche, ?\195?\160 voir comment faire mieux) Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/DemandeTableModel.java trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailExceptionHandler.java trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailUIUtil.java Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/DemandeTableModel.java =================================================================== --- trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/DemandeTableModel.java 2014-12-17 16:29:42 UTC (rev 731) +++ trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/DemandeTableModel.java 2014-12-18 10:47:18 UTC (rev 732) @@ -330,11 +330,14 @@ return result; } + // cf #5944 java.lang.ArrayIndexOutOfBoundsException: the rowIndex was 15, but should be int [0,14] public final DemandeUIModel getEntry(int rowIndex) { SwingUtil.ensureRowIndex(this, rowIndex); int rowCount = getRowCount(); if (rowIndex > rowCount) { - log.error("Error getting entry " + rowIndex + " int table model with " + rowCount + " items"); + if (log.isErrorEnabled()) { + log.error("Error getting entry " + rowIndex + " int table model with " + rowCount + " items"); + } return null; } List<DemandeUIModel> data = getRows(); Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailExceptionHandler.java =================================================================== --- trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailExceptionHandler.java 2014-12-17 16:29:42 UTC (rev 731) +++ trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailExceptionHandler.java 2014-12-18 10:47:18 UTC (rev 732) @@ -50,16 +50,26 @@ } protected void handleException(String tname, Throwable ex) { + + Throwable cause = getCause(ex); if (log.isErrorEnabled()) { - log.error("Global application exception [" + tname + "]", ex); + log.error("Global application exception [" + tname + "] : " + (cause != null ? cause.getMessage() : ""), ex); } - Throwable cause = getCause(ex); + // FIXME 20141218 kmorin + // catch ClassCastException to avoid errors like these : + // java.lang.ClassCastException: javax.swing.plaf.FontUIResource cannot be cast to java.awt.Color + if (cause instanceof ClassCastException) { + if (log.isErrorEnabled()) { + log.error("ClassCastException caught", cause); + } + return; + } - boolean backToScreen = false; - - AbstractFaxToMailAction action = null; - +// boolean backToScreen = false; +// +// AbstractFaxToMailAction action = null; +// if (cause instanceof ApplicationActionException) { ApplicationActionException actionException = (ApplicationActionException) cause; @@ -69,19 +79,19 @@ log.debug("Action error cause:", cause); } - action = (AbstractFaxToMailAction) actionException.getAction(); - - if (action instanceof AbstractChangeScreenAction) { - backToScreen = true; - } +// action = (AbstractFaxToMailAction) actionException.getAction(); +// +// if (action instanceof AbstractChangeScreenAction) { +// backToScreen = true; +// } } showErrorDialog(cause != null ? cause.getMessage() : "", cause); - if (backToScreen) { - -// action.getContext().setFallBackScreen(); - } +// if (backToScreen) { +// +//// action.getContext().setFallBackScreen(); +// } } } Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailUIUtil.java =================================================================== --- trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailUIUtil.java 2014-12-17 16:29:42 UTC (rev 731) +++ trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailUIUtil.java 2014-12-18 10:47:18 UTC (rev 732) @@ -511,19 +511,22 @@ PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setJobName(printName); - PageFormat pf = printJob.defaultPage(); - Paper paper = new Paper(); - paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight()); - pf.setPaper(paper); + // Send print job to default printer + PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet(); + result = defaultPrinter || printJob.printDialog(attributes); -// PaperSizes ps = new PaperSizes("A4"); -// log.info(ps.getAvailablePaperSizes()); + PageFormat pageFormat = printJob.getPageFormat(attributes); + final double paperWidth = (int) pageFormat.getImageableWidth(); + final double paperHeight = (int) pageFormat.getImageableHeight(); + final int paperX = (int) pageFormat.getImageableX(); + final int paperY = (int) pageFormat.getImageableY(); -// Paper paper = pf.getPaper(); -// final int paperWidth = (int) paper.getImageableWidth(); -// final int paperHeight = (int) paper.getImageableHeight(); -// final int paperX = (int) paper.getImageableX(); -// final int paperY = (int) paper.getImageableY(); + if (log.isDebugEnabled()) { + log.debug("page width " + paperWidth); + log.debug("page height " + paperHeight); + log.debug("page x " + paperX); + log.debug("page y " + paperY); + } printJob.setPrintable(new Printable() { public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { @@ -532,36 +535,49 @@ return NO_SUCH_PAGE; } BufferedImage image = pdfDecoder.getPageAsImage(pageIndex + 1); - graphics.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null); -// graphics.drawImage(image, -// paperX, -// paperY, -// Math.min(paperWidth, image.getWidth()), -// Math.min(paperHeight, image.getHeight()), -// null); + 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); + } + graphics.drawImage(image, paperX, paperY, width, height, null); + return PAGE_EXISTS; } catch (Exception e) { - log.error("error while printing", e); + if (log.isErrorEnabled()) { + log.error("error while printing", e); + } return NO_SUCH_PAGE; } } }); - // Send print job to default printer -// PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet(); -// float xmargin = 0.5f; -// float ymargin = 0.5f; -// float w = 595; -// float h = 842; -// attributes.add(new MediaPrintableArea(xmargin, ymargin, w - 2*xmargin, h - 2*ymargin, MediaPrintableArea.INCH)); -// result = defaultPrinter || printJob.printDialog(attributes); - result = defaultPrinter || printJob.printDialog(); if (result) { - printJob.print(); -// printJob.print(attributes); + printJob.print(attributes); } @@ -571,7 +587,9 @@ } } finally { - log.info("finally close the pdf file"); + if (log.isDebugEnabled()) { + log.debug("finally close the pdf file"); + } pdfDecoder.closePdfFile(); }