long (long pagesAmountInFile) { if (documentOfPagesModel.getPagesAmount() == 0) { return targetVisiblePosition.pageNumber == 0 ? targetVisiblePosition.pageNumber : targetVisiblePosition.pageNumber - 1; } int visibleTargetPageIndex = documentOfPagesModel.tryGetIndexOfNeededPageInList(targetVisiblePosition.pageNumber); if (visibleTargetPageIndex == -1) { // some pages before visible one exist and are located in list => just need to get next to last in list return tryGetNumberOfNextToDocumentPage(pagesAmountInFile); } // check if we really need some extra pages or already not int offsetToFirstVisibleSymbolOfTargetVisiblePage = getSymbolOffsetToStartOfPage(visibleTargetPageIndex); int topOfTargetVisiblePage = offsetToY(offsetToFirstVisibleSymbolOfTargetVisiblePage); int topOfTargetVisibleArea = topOfTargetVisiblePage + targetVisiblePosition.verticalScrollOffset; Rectangle visibleArea = editor.getScrollingModel().getVisibleArea(); int bottomOfTargetVisibleArea = topOfTargetVisibleArea + visibleArea.height; int lastVisiblePageIndex = visibleTargetPageIndex; int offsetToEndOfLastVisiblePage = getSymbolOffsetToStartOfPage(lastVisiblePageIndex + 1); while (lastVisiblePageIndex + 1 < documentOfPagesModel.getPagesAmount() && offsetToY(offsetToEndOfLastVisiblePage) <= bottomOfTargetVisibleArea) { lastVisiblePageIndex++; offsetToEndOfLastVisiblePage = getSymbolOffsetToStartOfPage(lastVisiblePageIndex + 1); } if (documentOfPagesModel.getPagesAmount() - 1 == lastVisiblePageIndex) { // we need at least 1 invisible page after visible ones, if it's not the end return tryGetNumberOfNextToDocumentPage(pagesAmountInFile); } return -1; }