/* @(#) Crop.java 1.1 2000/10/11
 * Copyright (c) 1999 Lawrence Rodrigues
 */
package com.vistech.jai.render;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
import javax.media.jai.*;
import com.sun.media.jai.codec.*;
import com.vistech.util.*;

import java.awt.image.renderable.*;
import com.vistech.imageviewer.*;
import com.vistech.jai.util.*;
import com.vistech.jai.render.*;

/** Crops the image drawn over a canvas.
  * @version 1.0 11 October 2000
  * @author Lawrence Rodrigues
  **/
public class Crop implements  MouseListener,MouseMotionListener{
   protected CompositeCanvasJAI imageCanvas;
   protected Point diff = new Point(0,0);
   protected Point shapeAnchor  = new Point(0,0);
   protected boolean mousePressed = false;
   protected Rectangle currentShape, prevShape;
   protected boolean cropOn = false;
   protected boolean cropped = false;
   protected JMenuItem crop;

   public Crop(CompositeCanvasJAI c){
      imageCanvas = c;
      imageCanvas.setDrawOn(true);
   }

   public void setCropOn(boolean onOrOff){ cropOn = onOrOff;}

   public void startDraw(int x, int y) {
      shapeAnchor = new Point(x,y);
      diff = new Point(0,0);
   }

   public void draw(int x, int y) {
      if(!cropOn) return;
      diff.x = x - shapeAnchor.x;
      diff.y = y - shapeAnchor.y;
      int wid = diff.x;
      int ht = diff.y;
      Point ulhc = new Point(shapeAnchor);
      if(diff.x <0) {
         wid = -diff.x;
         ulhc.x = x;
      }
      if(diff.y <0){
         ht = -diff.y;
         ulhc.y = y;
      }
      currentShape = new Rectangle(ulhc.x, ulhc.y, wid, ht);
      Graphics2D g = imageCanvas.getDisplayedImageGC();
      g.setColor(Color.black);
      g.setXORMode(Color.red);
      if(prevShape != null) g.draw(prevShape);
      g.draw(currentShape);
      prevShape = currentShape;
      imageCanvas.repaint();
   }

   public void mousePressed(MouseEvent e) {
      if(!cropOn) return;
      imageCanvas.eraseShapes();
      mousePressed = true;
      if(SwingUtilities.isLeftMouseButton(e)){
         startDraw(e.getX(), e.getY());
      }
   }

   public void mouseReleased(MouseEvent e) {
      if(!mousePressed) return;
      if(SwingUtilities.isLeftMouseButton(e)){
         Graphics2D g = imageCanvas.getDisplayedImageGC();
         draw(e.getX(), e.getY());
         prevShape = null;
         crop();
      }
      mousePressed = false;
   }

   public void mouseClicked(MouseEvent e){}
   public void mouseEntered(MouseEvent e){}
   public void mouseExited(MouseEvent e){}

   public void mouseDragged(MouseEvent e){
      if(SwingUtilities.isLeftMouseButton(e)){
          draw(e.getX(), e.getY());
      }
   }
   public void mouseMoved(MouseEvent e){}

   public void crop() {
      if(currentShape == null) return;
      PlanarImage image = imageCanvas.getImage();
      AffineTransform atx = imageCanvas.getTransform();
      Shape shape = AffineTransformUtil.inverseTransform(currentShape,atx);
      if(shape == null) return;
      Rectangle2D bounds = shape.getBounds2D();
      RenderedOp op = JAIOpUtil.cropImage(image, bounds);
      PlanarImage img = op.createInstance();
      imageCanvas.paintImage(img);
      cropped = true;
   }

   public void reset() {
      imageCanvas.eraseShapes();
      imageCanvas.reset();
      imageCanvas.paintImage(imageCanvas.getImage());
      imageCanvas.repaint();
  }

   protected void popupMenu(JComponent comp,int x, int y){
      JPopupMenu jp = new JPopupMenu("");
      jp.setLightWeightPopupEnabled(true);
      comp.add(jp);
      crop = new JMenuItem("Crop");
      crop.addActionListener(
         new ActionListener(){
           public void actionPerformed(ActionEvent e){
              if(!cropOn) return;
              PlanarImage image = imageCanvas.getImage();
              RenderedOp op = JAIOpUtil.cropImage(image, currentShape);
              PlanarImage img = op.createInstance();
              imageCanvas.paintImage(img);
              crop.setEnabled(false);
           }
         }
      );
      JMenuItem reset = new JMenuItem("Reset");
      reset.addActionListener(
         new ActionListener(){
           public void actionPerformed(ActionEvent e){
              imageCanvas.reset();
              imageCanvas.paintImage(imageCanvas.getImage());;
              //cropped = false;
              crop.setEnabled(true);
           }
         }
      );

      jp.add(crop);
      jp.add(reset);
      jp.show(comp,x,y);
   }
 }
