Example #1
0
 private void ensureRectIsInBounds(final Rectangle rectangle, final Dimension bounds) {
   if (rectangle.x < 0) {
     rectangle.x = 0;
   }
   if (rectangle.x + rectangle.width > bounds.width) {
     final JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar();
     final int scrollBarWidth;
     if (verticalScrollBar != null) {
       scrollBarWidth = verticalScrollBar.getWidth();
     } else {
       scrollBarWidth = 0;
     }
     rectangle.x = bounds.width - rectangle.width - scrollBarWidth;
   }
   if (rectangle.y < 0) {
     rectangle.y = 0;
   }
   if (rectangle.y + rectangle.height > bounds.height) {
     final JScrollBar horizontalScrollBar = scrollPane.getHorizontalScrollBar();
     final int scrollBarHeight;
     if (horizontalScrollBar != null) {
       scrollBarHeight = horizontalScrollBar.getHeight();
     } else {
       scrollBarHeight = 0;
     }
     rectangle.y = bounds.height - rectangle.height - scrollBarHeight;
   }
 }
Example #2
0
  protected JScrollPane createScrollPane(
      JComponent component, String title, boolean horizontalScrollbar) {
    JScrollPane scrollPane =
        new JScrollPane(
            component,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            horizontalScrollbar
                ? JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
                : JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    Color color = component.getBackground();
    if (color == null) {
      color = Color.white;
    }
    scrollPane.setBackground(color);
    scrollPane.getViewport().setBackground(color);
    if (title != null) {
      scrollPane.setBorder(BorderFactory.createTitledBorder(title));
    }

    JScrollBar scrollBar = scrollPane.getVerticalScrollBar();
    scrollBar.setPreferredSize(new Dimension(10, scrollBar.getHeight()));
    if (horizontalScrollbar) {
      scrollBar = scrollPane.getHorizontalScrollBar();
      scrollBar.setPreferredSize(new Dimension(scrollBar.getWidth(), 10));
    }
    return scrollPane;
  }
  public float getTargetZoom(int zoommode, PDFPage page) {
    float outzoom = zoom;
    if (page != null) {
      int pagenumber = page.getPageNumber() - 1;
      int pagecount = pdf.getNumberOfPages();
      boolean isOdd = (pagenumber % 2) == 0; // even pages have odd numbers :)
      boolean onRight = handedness == ODD_PAGES_ON_RIGHT ? isOdd : !isOdd;
      PDFPage leftPage, rightPage;
      Rectangle2D.Float leftPageRect = null, rightPageRect = null;
      if (onRight) {
        leftPage = (pagenumber > 0) ? pdf.getPage(pagenumber - 1) : null;
        rightPage = page;
      } else {
        leftPage = page;
        rightPage = (pagenumber < (pagecount - 1)) ? pdf.getPage(pagenumber + 1) : null;
      }
      if (leftPage != null) {
        leftPageRect = (Rectangle2D.Float) PagePanel.getFullPageView(leftPage);
      }
      if (rightPage != null) {
        rightPageRect = (Rectangle2D.Float) PagePanel.getFullPageView(rightPage);
      }
      if (leftPageRect == null) {
        leftPageRect = (Rectangle2D.Float) rightPageRect.clone();
      }
      if (rightPageRect == null) {
        rightPageRect = (Rectangle2D.Float) leftPageRect.clone();
      }
      JScrollBar hsb = scrollPane.getHorizontalScrollBar();
      JScrollBar vsb = scrollPane.getVerticalScrollBar();
      float prw = leftPageRect.width + rightPageRect.width;
      float prh = Math.max(leftPageRect.height, rightPageRect.height);

      Dimension size = getSize();
      float w =
          (float) Math.ceil(prw * Util.getScreenResolution(this) / 72)
              + (margin * 4)
              + interpagemargin
              + vsb.getWidth();
      float h =
          (float) Math.ceil(prh * Util.getScreenResolution(this) / 72)
              + (margin * 2)
              + hsb.getHeight();
      switch (zoommode) {
        case ZOOM_FITWIDTH:
          outzoom = (float) size.width / w;
          break;
        case ZOOM_FITHEIGHT:
          outzoom = (float) size.height / h;
          break;
        case ZOOM_FIT:
          float zw = (float) size.width / w;
          float zh = (float) size.height / h;
          outzoom = zw < zh ? zw : zh;
          break;
        default:
          outzoom = zoom == 0 ? 1 : zoom;
      }
    }
    return outzoom;
  }