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.*;

/** Implements a GUI for manipulating images on a canvas.
  * @version 1.0  1 Nov 1999
  * @author Lawrence Rodrigues
  **/
 public class ManipUI extends JPanel{
   protected JTabbedPane jtp;
   protected  int startValue = 0;
   protected  int minValue = -360, maxValue = 360;
   protected  JTextField rotateValueField;
   protected  double rotateValue = 0.0;
   protected  int rotateStartValue = 0;
   protected  JSlider rotateSlider;

   protected int shearMinValue = -200;
   protected int shearMaxValue = 200;
   protected JTextField shearValueField;
   protected int shearValue = 0;
   protected int shearStartValue = 0;
   protected JSlider shearSlider;
   protected GeomManipController imageViewer;
   public ManipUI(GeomManipController manip) {
      imageViewer = manip;
      createUI();
   }

   private void createUI() {
      JPanel rpanel = createRotatePanel();
      JPanel spanel = createShearPanel();
      jtp = new JTabbedPane();
      jtp.addTab("Rotate", rpanel);
      jtp.addTab("Shear", spanel);
      add(jtp);
   }
 
   protected JPanel createRotatePanel() {
        JButton resetButton = new JButton("Reset Rotate");
        resetButton.addActionListener(
            new ActionListener() {
               public void actionPerformed(ActionEvent e){
                  rotateSlider.setValue(rotateStartValue);
                  rotateValueField.setText((new Double(rotateStartValue)).toString());
                  imageViewer.rotate(0.0);
               }
            }
        );

       rotateValueField = new JTextField(5);
       rotateValueField.addActionListener(
           new ActionListener() {
              public void actionPerformed(ActionEvent e){
                  try {
                    String str = ((JTextField)e.getSource()).getText();
                    rotateValue = (Double.valueOf(str)).doubleValue();
                    
                    rotateSlider.setValue((int)rotateValue);
                    imageViewer.rotate(rotateValue*(Math.PI/180.0));
                  } catch (Exception e1){}
              }
           }
       );

       rotateValueField.setText(Integer.toString(rotateStartValue));
       JLabel rotateLabel = new JLabel("Rotate");

       rotateSlider = new JSlider(minValue,maxValue,startValue);
       rotateSlider.setMajorTickSpacing(120);
       rotateSlider.setMinorTickSpacing(12);
       rotateSlider.setExtent(12);
       rotateSlider.setPaintTicks(true);
       rotateSlider.setPaintLabels(true);

       rotateSlider.addChangeListener(
            new ChangeListener() {
              public void stateChanged(ChangeEvent e){
                 double rotateValueOld = rotateValue;
                 rotateValue = ((JSlider)(e.getSource())).getValue();
                 imageViewer.rotate(rotateValue*(Math.PI/180.0));
                 rotateValueField.setText(Integer.toString((int)rotateValue));
              }
            }
        );

        JPanel rotatepan = new JPanel();
        GridBagLayout gb = new GridBagLayout() ;
        GridBagConstraints c = new GridBagConstraints();
        rotatepan.setLayout(gb);
        rotatepan.setBackground(Color.lightGray);

        c.weighty = 1.0; c.weightx = 1.0;
        JLabel  rotlab = new JLabel("Degrees ",SwingConstants.RIGHT);
        gb.setConstraints(rotlab, c);
        rotatepan.add(rotlab);
        gb.setConstraints(rotateValueField, c);
        rotatepan.add(rotateValueField);
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.HORIZONTAL;
        gb.setConstraints(rotateSlider, c);
        rotatepan.add(rotateSlider);
        rotatepan.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(),"Rotate"));
        return rotatepan;
   }

   protected JPanel createShearPanel() {
      JButton resetButton = new JButton("Reset Shear");
      resetButton.addActionListener(
            new ActionListener() {
               public void actionPerformed(ActionEvent e){
                  shearSlider.setValue(shearStartValue);
                  shearValueField.setText((new Double(shearStartValue/100.0)).toString());
                  imageViewer.shear(0.0, 0.0);
               }
            }
      );

      shearValueField = new JTextField(5);
      shearValueField.addActionListener(
           new ActionListener() {
              public void actionPerformed(ActionEvent e){
                  try {
                    String str = ((JTextField)e.getSource()).getText();
                    double shearValue = (Double.valueOf(str)).doubleValue();
                    shearSlider.setValue((int)(shearValue*100));
                    imageViewer.shear(shearValue, shearValue);
                  } catch (Exception e1){}
              }
           }
       );

       shearValueField.setText(Double.toString(shearStartValue/100.0));
       JLabel shearLabel = new JLabel("Shear");
        
       shearSlider = new JSlider(shearMinValue,shearMaxValue,shearStartValue);
       shearSlider.setMajorTickSpacing(100);
       shearSlider.setMinorTickSpacing(25);
       shearSlider.setExtent(10);
       shearSlider.setPaintTicks(true);
       shearSlider.setPaintLabels(true);

       shearSlider.addChangeListener(
            new ChangeListener() { 
              public void stateChanged(ChangeEvent e){
                 double shearValueOld = shearValue;
                 double shearValue = ((JSlider)(e.getSource())).getValue();
                 imageViewer.shear(shearValue/100.0, shearValue/100.0);
                 shearValueField.setText(Double.toString(shearValue/100.0));
              }
            }
      );

      JPanel shearpan = new JPanel();
      GridBagLayout gb = new GridBagLayout() ;
      GridBagConstraints c = new GridBagConstraints();
      shearpan.setLayout(gb);
      shearpan.setBackground(Color.lightGray);

      c.weighty = 1.0; c.weightx = 1.0;
      JLabel  rotlab = new JLabel("Shear factor ",SwingConstants.RIGHT);
      gb.setConstraints(rotlab, c);
      shearpan.add(rotlab);
      gb.setConstraints(shearValueField, c);
      shearpan.add(shearValueField);
      c.gridwidth = GridBagConstraints.REMAINDER;
      c.fill = GridBagConstraints.HORIZONTAL;
      gb.setConstraints(shearSlider, c);
      shearpan.add(shearSlider);
      shearpan.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(),"Shear"));
      return shearpan;
   }

   public void resetManipulation(){
      shearSlider.setValue((int)(shearStartValue*100));
      shearValueField.setText(Double.toString(shearStartValue));
      rotateValueField.setText(Integer.toString((int)rotateStartValue));
      rotateSlider.setValue((int)rotateStartValue);
   }
}
