int () { if (documentOfPagesModel.getPagesAmount() == 0) { return 0; } int offset = tryGetTargetCaretOffsetInDocument(); if (offset == -1) return -1; int startOfAllowedRange = tryGetStartMarginForTargetCaretInDocument(); int endOfAllowedRange = getSymbolOffsetToStartOfPage(documentOfPagesModel.getPagesAmount()); try { if (documentOfPagesModel.getLastPage().getPageNumber() != dataProvider.getPagesAmount() - 1) { endOfAllowedRange -= documentOfPagesModel.getLastPage().getText().length() / 2; } } catch (IOException e) { LOG.warn(e); } if (offset >= startOfAllowedRange && offset <= endOfAllowedRange) { return offset; } else { return -1; } }