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


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;

   public ManipUI() {
      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.addMouseListener(new java.awt.event.MouseAdapter() {
               public void mouseReleased(java.awt.event.MouseEvent e){
                      resetButtonMouseReleased(e);
               }
            }
        );

       rotateValueField = new JTextField(5);
       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);

       // saveMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
//             public void mouseReleased(java.awt.event.MouseEvent evt) {
//                 saveMenuItemMouseReleased(evt);
//             }
//         });

        rotateSlider.addMouseListener(new java.awt.event.MouseAdapter() {
              public void mouseReleased(java.awt.event.MouseEvent e){
                 rotateSliderMouseReleased(e);
              }
            }
        );


        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.add(resetButton);
        rotatepan.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(),"Rotate"));
        return rotatepan;
   }

   protected JPanel createShearPanel() {
      JButton resetButton = new JButton("Reset Shear");
      resetButton.addMouseListener(
            new MouseAdapter() {
               public void mouseReleased(MouseEvent e){
                  shearSlider.setValue(shearStartValue);
                  shearValueField.setText((new Double(shearStartValue/100.0)).toString());
                  opart1.imagePanel.shear(0.0, 0.0);
               }
            }
      );

      shearValueField = new JTextField(5);
      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.addMouseListener(
            new MouseAdapter() {
              public void mouseReleased(MouseEvent e){
                 double shearValueOld = shearValue;
                 double shearValue = ((JSlider)(e.getSource())).getValue();
                 opart1.imagePanel.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.add(resetButton);
      shearpan.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(),"Shear"));
      return shearpan;
   }

   private void rotateSliderMouseReleased(java.awt.event.MouseEvent evt){
                 
                 double rotateValueOld = rotateValue;
                 rotateValue = rotateSlider.getValue();
                 opart1.imagePanel.rotate(rotateValue*(Math.PI/180.0));
                 rotateValueField.setText(Integer.toString((int)rotateValue));
   }

   private void resetButtonMouseReleased(java.awt.event.MouseEvent evt){
                  double rotateValueOld = rotateValue;
                  rotateSlider.setValue(rotateStartValue);
                  rotateValueField.setText((new Double(rotateStartValue)).toString());
                  opart1.imagePanel.rotate(0.0);
   }

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