public void updatePanelSize() {

    // Set panel size.
    if (stmt != null && stmt.getWidth() > 0 && stmt.getHeight() > 0) {
      int width, height;
      switch (panel.getFitMode()) {
        case FIT_VERTICAL:
          height = getViewport().getHeight();
          width = stmt.getWidth() * height / stmt.getHeight();
          break;

        case FIT_HORIZONTAL:
          width = getViewport().getWidth();
          height = stmt.getHeight() * width / stmt.getWidth();
          break;

        case FIT_BOTH:
          width = getViewport().getWidth();
          height = getViewport().getHeight();
          break;

        case ORIGINAL:
        default:
          width = stmt.getWidth();
          height = stmt.getHeight();
          break;
      }
      panel.setPreferredSize(new Dimension(width, height));
      panel.setSize(new Dimension(width, height));
    }
  }
 public VsketchPreviewPane() {
   panel = new VsketchPreviewPanel();
   setViewportView(panel);
   fitModeIcon = VsketchFrame.getImageIcon("glyphicons_215_resize_full.png");
   MyMouseListener listener = new MyMouseListener();
   panel.addMouseListener(listener);
   panel.addMouseMotionListener(listener);
 }
 public int imageToViewY(int y) {
   switch (panel.getFitMode()) {
     case FIT_BOTH:
     case FIT_VERTICAL:
       return y * panel.getHeight() / stmt.getHeight();
     case FIT_HORIZONTAL:
       return y * panel.getWidth() / stmt.getWidth();
     default:
       return y;
   }
 }
 public int viewToImageX(int x) {
   switch (panel.getFitMode()) {
     case FIT_BOTH:
     case FIT_HORIZONTAL:
       return x * stmt.getWidth() / panel.getWidth();
     case FIT_VERTICAL:
       return x * stmt.getHeight() / panel.getHeight();
     default:
       return x;
   }
 }
  public void setFitMode(FitMode fitMode) {
    panel.setFitMode(fitMode);

    // Set scroll bar policy.
    setHorizontalScrollBarPolicy(
        fitMode == FitMode.FIT_VERTICAL || fitMode == FitMode.ORIGINAL
            ? ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED
            : ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    setVerticalScrollBarPolicy(
        fitMode == FitMode.FIT_VERTICAL || fitMode == FitMode.FIT_BOTH
            ? ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER
            : ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
    updatePanelSize();
    panel.repaint();
  }
 private void changeFitMode() {
   switch (panel.getFitMode()) {
     case ORIGINAL:
       setFitMode(FitMode.FIT_VERTICAL);
       break;
     case FIT_VERTICAL:
       setFitMode(FitMode.FIT_HORIZONTAL);
       break;
     case FIT_HORIZONTAL:
       setFitMode(FitMode.FIT_BOTH);
       break;
     case FIT_BOTH:
     default:
       setFitMode(FitMode.ORIGINAL);
       break;
   }
 }
 public void setStmt(Stmt stmt) {
   this.stmt = stmt;
   panel.setStmt(stmt);
   setFitMode(panel.getFitMode());
   repaint();
 }