From 7a300d2870b4cdbb82f507125fb242f2a1e213e3 Mon Sep 17 00:00:00 2001 From: Dmitry Batrak Date: Wed, 20 May 2015 17:09:22 +0300 Subject: [PATCH] IDEA-76915 Printing out page ranges prints something unbeleivable --- .../codeEditor/printing/TextPainter.java | 54 ++++++++++++------- .../src/messages/CodeEditorBundle.properties | 1 + 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeEditor/printing/TextPainter.java b/platform/lang-impl/src/com/intellij/codeEditor/printing/TextPainter.java index 801bfdc5f819..53c970a6f6bd 100644 --- a/platform/lang-impl/src/com/intellij/codeEditor/printing/TextPainter.java +++ b/platform/lang-impl/src/com/intellij/codeEditor/printing/TextPainter.java @@ -67,7 +67,7 @@ class TextPainter extends BasePainter { private final PrintSettings myPrintSettings; private final String myFullFileName; private final String myShortFileName; - private int myPageIndex; + private int myPageIndex = -1; private int myNumberOfPages = -1; private int mySegmentEnd; private final LineMarkerInfo[] myMethodSeparators; @@ -178,6 +178,8 @@ class TextPainter extends BasePainter { @Override public int print(final Graphics g, final PageFormat pageFormat, final int pageIndex) throws PrinterException { + myPerformActualDrawing = false; + if (myProgress.isCanceled()) { return NO_SUCH_PAGE; } @@ -187,34 +189,46 @@ class TextPainter extends BasePainter { if (myNumberOfPages < 0) { myProgress.setText(CodeEditorBundle.message("print.file.calculating.number.of.pages.progress")); - myPerformActualDrawing = false; - if (!calculateNumberOfPages(g2d, pageFormat)) { return NO_SUCH_PAGE; } } - myPerformActualDrawing = true; + if (pageIndex >= myNumberOfPages) { + return NO_SUCH_PAGE; + } - return ApplicationManager.getApplication().runReadAction(new Computable() { - @Override - public Integer compute() { - if (!isValidRange(myRangeToPrint)) { + isPrintingPass = !isPrintingPass; + if (!isPrintingPass) { + while(++myPageIndex < pageIndex) { + if (!printPageInReadAction(g2d, pageFormat, "print.skip.page.progress")) { return NO_SUCH_PAGE; } - - isPrintingPass = !isPrintingPass; - if (!isPrintingPass) { - return PAGE_EXISTS; + } + return ApplicationManager.getApplication().runReadAction(new Computable() { + @Override + public Integer compute() { + return isValidRange(myRangeToPrint) ? PAGE_EXISTS : NO_SUCH_PAGE; } - - myProgress.setText(CodeEditorBundle.message("print.file.page.progress", myShortFileName, (pageIndex + 1), myNumberOfPages)); - myPageIndex = pageIndex; - - RangeMarker newRange = printPage(g2d, pageFormat, myRangeToPrint); - setSegment(newRange); - - return PAGE_EXISTS; + }); + } + else { + myPerformActualDrawing = true; + printPageInReadAction(g2d, pageFormat, "print.file.page.progress"); + return PAGE_EXISTS; + } + } + + private boolean printPageInReadAction(final Graphics2D g2d, final PageFormat pageFormat, final String progressMessageKey) { + return ApplicationManager.getApplication().runReadAction(new Computable() { + @Override + public Boolean compute() { + if (!isValidRange(myRangeToPrint)) { + return false; + } + myProgress.setText(CodeEditorBundle.message(progressMessageKey, myShortFileName, (myPageIndex + 1), myNumberOfPages)); + setSegment(printPage(g2d, pageFormat, myRangeToPrint)); + return true; } }); } diff --git a/platform/platform-resources-en/src/messages/CodeEditorBundle.properties b/platform/platform-resources-en/src/messages/CodeEditorBundle.properties index a1c47c6de185..bf6fa5ebef76 100644 --- a/platform/platform-resources-en/src/messages/CodeEditorBundle.properties +++ b/platform/platform-resources-en/src/messages/CodeEditorBundle.properties @@ -60,4 +60,5 @@ print.header.alignment.center=Center print.header.alignment.right=Right print.file.calculating.number.of.pages.progress=Calculating number of pages... print.file.page.progress=Printing {0}. Page {1} of {2} +print.skip.page.progress=Printing {0}. Skipping page {1} of {2} file.not.found=File not found: {0}