package SesameGUI;
import java.awt.*;
import oracle.jdeveloper.layout.XYLayout;
import javax.swing.JLabel;
import oracle.jdeveloper.layout.XYConstraints;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JEditorPane;
import javax.swing.JTextField;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.*;

public class ScaleDlg  extends javax.swing.JDialog
{
  private XYLayout xYLayout1 = new XYLayout();
  private JLabel jLabel1 = new JLabel();
  private JPanel horizontalPane = new JPanel();
  private XYLayout xYLayout2 = new XYLayout();
  private JLabel jLabel2 = new JLabel();
  private JLabel jLabel3 = new JLabel();
  private FloatField minV = new FloatField(0.0f , 2);
  private FloatField maxV = new FloatField(0.0f , 2);
  private JPanel verticalPane = new JPanel();
  private XYLayout xYLayout3 = new XYLayout();
  private FloatField maxV1 = new FloatField(0.0f , 2);
  private FloatField minV1 = new FloatField(0.0f , 2);
  private JLabel jLabel4 = new JLabel();
  private JLabel jLabel5 = new JLabel();
  private ButtonGroup bg = new ButtonGroup();
  private ButtonGroup bg1 = new ButtonGroup();
  private JRadioButton log1 = new JRadioButton();
  private JRadioButton linear1 = new JRadioButton();
  private JLabel jLabel6 = new JLabel();

  public ScaleDlg(Dialog owner)
  {
    super(owner);
    try
    {
      jbInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception
  {
    this.getContentPane().setLayout(xYLayout1);
    this.setTitle("Scale properties");
    this.addWindowListener(new java.awt.event.WindowAdapter()
      {
        public void windowDeactivated(WindowEvent e)
        {
          desactivated(e);
        }
      });
    xYLayout1.setWidth(167);
    xYLayout1.setHeight(243);
    jLabel1.setText("Horizontal scale");
    horizontalPane.setLayout(xYLayout2);
    horizontalPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    jLabel2.setText("Min:");
    jLabel3.setText("Max:");
    verticalPane.setLayout(xYLayout3);
    verticalPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    jLabel4.setText("Max:");
    jLabel5.setText("Min:");

    bg1.add(log2);
    bg1.add(linear2);
    
    log2.setText("Log");
    log2.setActionCommand("linear");
    log2.addItemListener(new ItemListener()
      {
        public void itemStateChanged(ItemEvent e)
        {
          logSelected1(e);
        }
      });
    linear2.setText("Linear");
    linear2.setActionCommand("linear");
    linear2.setSelected(true);
    linear2.addItemListener(new ItemListener()
      {
        public void itemStateChanged(ItemEvent e)
        {
          linearSelected1(e);
        }
      });

    bg.add(log1);
    bg.add(linear1);
    
    log1.setText("Log");
    log1.setActionCommand("linear");
    log1.addItemListener(new ItemListener()
      {
        public void itemStateChanged(ItemEvent e)
        {
          logSelected(e);
        }
      });
    linear1.setText("Linear");
    linear1.setActionCommand("linear");
    linear1.setSelected(true);
    linear1.addItemListener(new ItemListener()
      {
        public void itemStateChanged(ItemEvent e)
        {
          linearSelected(e);
        }
      });
    jLabel6.setText("Vertical scale");

    verticalPane.add(maxV1, new XYConstraints(35, 55, 105, 20));
    verticalPane.add(minV1, new XYConstraints(35, 30, 105, 20));
    verticalPane.add(jLabel4, new XYConstraints(10, 58, 35, 15));
    verticalPane.add(jLabel5, new XYConstraints(10, 33, 35, 15));
    verticalPane.add(log1, new XYConstraints(80, 0, 65, 25));
    verticalPane.add(linear1, new XYConstraints(5, 0, 70, 25));
    horizontalPane.add(maxV, new XYConstraints(35, 55, 105, 20));
    horizontalPane.add(minV, new XYConstraints(35, 30, 105, 20));
    horizontalPane.add(jLabel3, new XYConstraints(10, 55, 35, 15));
    horizontalPane.add(jLabel2, new XYConstraints(10, 30, 35, 15));
    horizontalPane.add(log2, new XYConstraints(80, 0, 65, 25));
    horizontalPane.add(linear2, new XYConstraints(5, 0, 70, 25));
    this.getContentPane().add(jLabel6, new XYConstraints(5, 115, 165, 15));
    this.getContentPane().add(verticalPane, new XYConstraints(5, 130, 150, 85));
    this.getContentPane().add(horizontalPane, new XYConstraints(5, 20, 150, 85));
    this.getContentPane().add(jLabel1, new XYConstraints(5, 5, 165, 15));

    setSize(170 , 254);
  }

  public Scale      horizontal;
  public Scale      vertical;
  private JRadioButton log2 = new JRadioButton();
  private JRadioButton linear2 = new JRadioButton();

  private void desactivated(WindowEvent e)
  {
    Scale           newVertical;
    Scale           newHorizontal;
    double          off;
    double          off1;
    double          scV , scH;

    scV = 1.0;
    scH = 1.0;
    
    if (linear1.isSelected())
      {
      off = 0.0;
      newVertical = new AbsoluteVerticalScale (vertical.getView());
      }
    else
      {
      if (Double.valueOf(minV1.getText()).doubleValue() >= 1.0)
        off = 0.001;
      else
        off = -0.00001;

      if (Double.valueOf(minV1.getText()).doubleValue() + off < 0.0001)
        {
        minV1.setText("0.001");
        }
        
      newVertical = new LogarithmicVerticalScale (vertical.getView());
      }

    if (linear2.isSelected())
      {
      off1 = 0.0;
      newHorizontal = new AbsoluteHorizontalScale (horizontal.getView());
      }
    else
      {
      if (Double.valueOf(minV1.getText()).doubleValue() >= 1.0)
        off1 = 0.001;
      else
        off1 = -0.0001;

      if (Double.valueOf(minV.getText()).doubleValue() + off1 < 0.0001)
        {
        minV.setText("0.001");
        }
      
      newHorizontal = new LogarithmicHorizontalScale (horizontal.getView());
      }

    newVertical.copyFrom (vertical);
    newHorizontal.copyFrom (horizontal);

    horizontal.getView().setScaleX(newHorizontal);
    
    newHorizontal.setLowerBound((Double.valueOf(minV.getText()).doubleValue() / scH) + off1);    
    newHorizontal.setUpperBound(Double.valueOf(maxV.getText()).doubleValue());
    newHorizontal.setLowerVisibleBound((Double.valueOf(minV.getText()).doubleValue() / scH) + off1);    
    newHorizontal.setUpperVisibleBound(Double.valueOf(maxV.getText()).doubleValue());

    vertical.getView().setScaleY(newVertical);

    newVertical.setLowerBound((Double.valueOf(minV1.getText()).doubleValue() / scV) + off);    
    newVertical.setUpperBound(Double.valueOf(maxV1.getText()).doubleValue());
    newVertical.setLowerVisibleBound((Double.valueOf(minV1.getText()).doubleValue() / scV) + off);    
    newVertical.setUpperVisibleBound(Double.valueOf(maxV1.getText()).doubleValue());

    if (linear2.isSelected())
      newHorizontal.calcStep(10);
    else
      newHorizontal.calcStep();
    
    newVertical.calcStep();

    newVertical.getView().getParent().repaint();
  }

  public void load()
  {
    minV.setText(String.valueOf(horizontal.getLowerBound()));
    maxV.setText(String.valueOf(horizontal.getUpperBound()));

    minV1.setText(String.valueOf(vertical.getLowerBound()));
    maxV1.setText(String.valueOf(vertical.getUpperBound()));

    minV1.setMinVal((float)vertical.getView().getSpectrum().getYMin());
    minV1.setMaxVal((float)vertical.getView().getSpectrum().getYMax());
    maxV1.setMinVal((float)vertical.getView().getSpectrum().getYMin());
    maxV1.setMaxVal((float)vertical.getView().getSpectrum().getYMax());

    minV.setMinVal((float)horizontal.getLowerBound());
    minV.setMaxVal((float)horizontal.getUpperBound());
    maxV.setMinVal((float)horizontal.getLowerBound());
    maxV.setMaxVal((float)horizontal.getUpperBound());

    log1.setSelected(vertical.toString() == "LogarithmicVerticalScale");
    log2.setSelected(horizontal.toString() == "LogarithmicHorizontalScale");
  }

  private void linearSelected(ItemEvent e)
  {
  }

  private void logSelected(ItemEvent e)
  {
  }

  private void linearSelected1(ItemEvent e)
  {
  }

  private void logSelected1(ItemEvent e)
  {
  }
}