/* @(#) PanZoom.java 1.1 99/12/1
 * Copyright (c) 1999 Lawrence Rodrigues
 */
package com.vistech.imageviewer;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.*;
import javax.swing.*;

/** Implements the pan and zoom features together.
  * @see com.vistech.imageviewer.Zoom
  * @see com.vistech.imageviewer.Scroll
  * @see com.vistech.imageviewer.ZoomController
  * @see com.vistech.imageviewer.ScrollController
  * @version 1.0  1 Dec 1999
  * @author Lawrence Rodrigues
  **/

public class PanZoom extends MouseAdapter{
   protected ImageManipulator imageCanvas;
   protected ScrollGUI pan;
   protected Scroll scroll;
   protected ZoomGUI zoomGUI;
   protected Zoom zoom;
   protected boolean panOn,zoomOn, zoomOut, panZoomOn = true;
   /** @param manip the component on which the image is drawn.
     */
   public PanZoom(ImageManipulator manip) {
      imageCanvas = manip;
      scroll = new Scroll(imageCanvas);
      pan = new ScrollGUI(scroll);

      zoom = new Zoom(imageCanvas);
      zoomGUI = new ZoomGUI(zoom);
      init();
   }
   protected void init() {
      if(imageCanvas == null) return;
      imageCanvas.addMouseListener(pan);
      imageCanvas.addMouseListener(zoomGUI);
      imageCanvas.addMouseListener(this);
      imageCanvas.addMouseMotionListener(pan);
      panOn = true;
      zoomOn = false;
      zoomOut = false;
   }

   protected void setStates() {
      pan.setScrollOn(panOn);
      zoomGUI.setZoomOn(zoomOn);
      zoomGUI.setZoomOut(zoomOut);
   }
   public void setPanZoomOn(boolean onOrOff){
      if(panZoomOn == onOrOff) return;
      panZoomOn = onOrOff;
      if(panZoomOn){
         imageCanvas.addMouseListener(pan);
         imageCanvas.addMouseListener(zoomGUI);
         imageCanvas.addMouseListener(this);
         imageCanvas.addMouseMotionListener(pan);
      }else {
         imageCanvas.removeMouseListener(pan);
         imageCanvas.removeMouseListener(zoomGUI);
         imageCanvas.removeMouseListener(this);
         imageCanvas.removeMouseMotionListener(pan);
      }
   }
   public boolean getZoomOut(){ return zoomOut; }
   public boolean getZoomOn(){ return zoomOn; }
   public double getZoomFactor(){
       return zoomGUI.getZoomFactor();
   }

   public boolean getPanOn(){ return panOn; }
   public void reset(){
      zoomGUI.reset();
      pan.reset();
   }
   protected void popupMenu(JComponent comp,int x, int y){
      JPopupMenu jp = new JPopupMenu("");
      jp.setLightWeightPopupEnabled(true);
      comp.add(jp);
      JMenuItem zout = new JMenuItem("Zoom out");
      zout.addActionListener(
         new ActionListener(){
           public void actionPerformed(ActionEvent e){
              zoomOut = true; zoomOn = true; panOn = false;
              setStates();
           }
         }
      );
      JMenuItem zin = new JMenuItem("Zoom in");
      zin.addActionListener(
         new ActionListener(){
           public void actionPerformed(ActionEvent e){
              zoomOut = false; zoomOn = true; panOn = false;
              setStates();
           }
         }
      );

      JMenuItem pn = new JMenuItem("Pan");
      pn.addActionListener(
         new ActionListener(){
           public void actionPerformed(ActionEvent e){
              zoomOn = false; panOn = true;
              setStates();
           }
         }
      );
      jp.add(zin);
      jp.add(zout);
      jp.add(pn);
      jp.show(comp,x,y);
    }

    public void mousePressed(MouseEvent e) {
       if(!SwingUtilities.isLeftMouseButton(e)){
          popupMenu((JComponent)e.getSource(), e.getX(), e.getY());
       }
    }
}