public void calculateCurrentPage() { if (pageComponents != null) { Rectangle viewport = scrollpane.getViewport().getViewRect(); // find visible pages ArrayList<PageViewComponent> visiblePages = new ArrayList<PageViewComponent>(10); Rectangle pageBounds; int pageCount = 0; for (AbstractPageViewComponent pageComponent : pageComponents) { if (pageComponent != null) { pageBounds = documentViewModel.getPageBounds(pageCount); if (pageBounds != null && pageComponent.isShowing()) { visiblePages.add(pageComponent); } } pageCount++; } // find center point of view port int x = viewport.x + (viewport.width / 2); int y = viewport.y + (viewport.height / 2); Point centerView = new Point(x, y); // find out which page center is closest to center and thus the new current page double minLength = Double.MAX_VALUE; int minPage = -1; double tmpDistance; for (PageViewComponent pageComponent : visiblePages) { if (pageComponent != null) { pageBounds = documentViewModel.getPageBounds(pageComponent.getPageIndex()); x = pageBounds.x + (pageBounds.width / 2); y = pageBounds.y + (pageBounds.height / 2); // find minimum page. tmpDistance = centerView.distance(x, y); if (tmpDistance < minLength) { minLength = tmpDistance; minPage = pageComponent.getPageIndex(); } } } // clean up visiblePages.clear(); visiblePages.trimToSize(); // finally send out event to update page number int oldCurrentPage = documentViewModel.getViewCurrentPageIndex(); documentViewModel.setViewCurrentPageIndex(minPage); DocumentViewControllerImpl documentViewController = (DocumentViewControllerImpl) documentView.getParentViewController(); documentViewController.firePropertyChange( PropertyConstants.DOCUMENT_CURRENT_PAGE, oldCurrentPage, minPage); } }
public void mouseClicked(MouseEvent e) { if ((e.getModifiers() & MouseEvent.MOUSE_PRESSED) != 0) { if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) { // zoom in Point pageOffset = documentViewModel.getPageBounds(pageViewComponent.getPageIndex()).getLocation(); Point mouse = e.getPoint(); mouse.setLocation(pageOffset.x + mouse.x, pageOffset.y + mouse.y); documentViewController.setZoomOut(mouse); } } if (pageViewComponent != null) { pageViewComponent.requestFocus(); } }