/* @(#) LensPanel.java 1.1 99/01/12
 * Copyright (c) 1999 Lawrence Rodrigues
 */
package com.vistech.imageviewer;
import java.io.*;
import java.beans.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.lang.reflect.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;
import com.vistech.events.*;
import com.vistech.imageloader.*;
import com.sun.image.codec.jpeg.*;

/** Implements a panel for setting the lens size and magnification factor.
 * @version 1.0  1 Dec 1999
 * @author Lawrence Rodrigues
 **/
public class LensPanel extends JPanel {
   protected ImageManipulator imageCanvas;
   protected Lens lens;
   protected boolean lensOn = false;
   public LensPanel(ImageManipulator manip) {
      imageCanvas = manip;
      if(manip != null) {
         manip.resetManipulation();
         lens = new Lens(manip);
         createUI();
      }
   }

   public void setLensOn(boolean onOrOff){
      if(lensOn ==  onOrOff) return;
      lensOn = onOrOff;
      if(lensOn) {
         imageCanvas.addMouseListener(lens);
         imageCanvas.addMouseMotionListener(lens);
      } else {
         imageCanvas.removeMouseListener(lens);
         imageCanvas.removeMouseMotionListener(lens);
      }
      lens.setLensOn(lensOn);
   }

   private void createUI() {
      JButton sizeincr = new JButton("+");
      sizeincr.addActionListener(new LensSizeIncrAdapter(true));

      JButton sizedecr = new JButton("-");
      sizedecr.addActionListener(new LensSizeIncrAdapter(false));

      JButton magincr = new JButton("+");
      magincr.addActionListener(new LensMagIncrAdapter(true));

      JButton magdecr = new JButton("-");
      magdecr.addActionListener(new LensMagIncrAdapter(false));

      JPanel sinc = new JPanel();
      JLabel lab = new JLabel("Lens Size");
      sinc.add(lab);
      sinc.add(sizeincr);
      sinc.add(sizedecr);

      JPanel minc = new JPanel();
      JLabel mlab = new JLabel("Lens mag");
      minc.add(mlab);
      minc.add(magincr);
      minc.add(magdecr);

      add(sinc);
      add(minc);
      setBorder(BorderFactory.createTitledBorder("Lens"));
   }

   class LensSizeIncrAdapter implements ActionListener {
      boolean incrDecr;
      public LensSizeIncrAdapter(boolean incrDecr) {this.incrDecr = incrDecr;}
      public void actionPerformed(ActionEvent e) {
           if(incrDecr) lens.incrementLensSize();
           else lens.decrementLensSize();
      }
   }
   class LensMagIncrAdapter implements ActionListener {
      boolean incrDecr;
      public LensMagIncrAdapter(boolean incrDecr) {this.incrDecr = incrDecr;}
      public void actionPerformed(ActionEvent e) {
           if(incrDecr) lens.incrementLensMag();
           else lens.decrementLensMag();
      }
   }
}
