/* @(#) CropImage.java 1.1 99/21/11
 * Copyright (c) 1999 Lawrence Rodrigues
 */

import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
import com.sun.media.jai.codec.*;

public class CropImage implements  MouseListener,MouseMotionListener{
   ImagePanel imageCanvas;
   private Point diff = new Point(0,0);
   private Point shapeAnchor  = new Point(0,0);
   boolean mousePressed = false;
   Rectangle currentShape, prevShape;
   boolean cropped = false;
   JMenuItem crop;

   public CropImage(ImagePanel c){
      imageCanvas = c;
      imageCanvas.addMouseListener(this);
      imageCanvas.addMouseMotionListener(this);
   }

   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(cropped) 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);
      Graphics g = imageCanvas.getDisplayedImageGC();
      g.setColor(Color.black);
      g.setXORMode(Color.white);
      if(prevShape != null)
      g.drawRect((int)prevShape.getX(), (int)prevShape.getY(),
                 (int)prevShape.getWidth(), (int)prevShape.getHeight());
      if(currentShape != null)
         g.drawRect((int)currentShape.getX(), (int)currentShape.getY(),
         (int)currentShape.getWidth(), (int)currentShape.getHeight());
      prevShape = currentShape;
      imageCanvas.repaint();
   }

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

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

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

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

   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(cropped) return;
              CropImageFilter crf = new CropImageFilter((int)currentShape.getX(),
                                                         (int)currentShape.getY(),
                                                         (int)currentShape.getWidth(),
                                                         (int)currentShape.getHeight());
              FilteredImageSource fis =
              new FilteredImageSource(imageCanvas.getImage().getSource(), crf);
              Image img = Toolkit.getDefaultToolkit().createImage(fis);
              imageCanvas.drawImage(img,(int)currentShape.getX(),(int)currentShape.getY() );
              cropped =true;
              crop.setEnabled(false);
           }
         }
      );
      JMenuItem reset = new JMenuItem("Reset");
      reset.addActionListener(
         new ActionListener(){
           public void actionPerformed(ActionEvent e){
              imageCanvas.resetImage();
              cropped = false;
              crop.setEnabled(true);
           }
         }
      );

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