public boolean setFitMode(final int fitMode) {

        if (documentViewModel == null) {
            return false;
        }

        boolean changed = fitMode != viewportFitMode;
        viewportFitMode = fitMode;

        if (document != null) {

            // update fit
            float newZoom = documentViewModel.getViewZoom();
            if (viewportFitMode == PAGE_FIT_ACTUAL_SIZE) {
                newZoom = 1.0f;
            } else if (viewportFitMode == PAGE_FIT_WINDOW_HEIGHT) {
                if (documentView != null && documentViewScrollPane != null) {
                    float viewportHeight = documentViewScrollPane.getViewport().getViewRect().height;
                    float pageViewHeight = documentView.getDocumentSize().height;

                    // pageViewHeight insert padding on each side.
                    pageViewHeight += AbstractDocumentView.layoutInserts *2;

                    if (viewportHeight > 0) {
                        newZoom = (viewportHeight / pageViewHeight);
                    } else {
                        newZoom = 1.0f;
                    }
                }
            } else if (viewportFitMode == PAGE_FIT_WINDOW_WIDTH) {
                if (documentView != null && documentViewScrollPane != null) {
                    float viewportWidth = documentViewScrollPane.getViewport().getViewRect().width;
                    float pageViewWidth = documentView.getDocumentSize().width;

                    // add insert padding on each side.
                    pageViewWidth += AbstractDocumentView.layoutInserts *2;

                    if (viewportWidth > 0) {
                        newZoom = (viewportWidth / pageViewWidth);
                    } else {
                        newZoom = 1.0f;
                    }
                }
            }

            // If we're scrolled all the way to the top, center to top of document when zoom,
            //  otherwise the view will zoom into the general center of the page
            if (getVerticalScrollBar().getValue() == 0) {
                setZoom(newZoom, new Point(0, 0), true);
            }
            else {
                setZoom(newZoom, null, true);
            }
        }

        return changed;
    }