/*
 * ElectPotPropertyEditor.java
 *
 */
 
package ovt.object.editor;

import javax.swing.*;
import ovt.mag.*;

public class ElectPotPropertyEditor extends JFrame {

    public ElectPotPropertyEditor(MagProps magProps) {
    this.magProps = magProps;
    initComponents ();
    pack ();
  }

  //data, for which this editor is designed
  public int rangeSelector = 0;
  public double Bz = 7, By = 7, Tilt = 0, SWVel = 400, Resolution = 2;
  
  //this is probably not needed here (?)
  private MagProps magProps = null;

  private JPanel paramsPanel;
  private JLabel p1Lbl,p2Lbl,p3Lbl,p4Lbl,p5Lbl;
  private JEditorPane p1Edit,p2Edit,p3Edit,p4Edit,p5Edit;
/*
  private JPanel optionsPanel;
  private JLabel optLbl;
  private ButtonGroup optGroup;
  private JRadioButton o1RButt,o2RButt;
*/
  private JPanel buttonsPanel;
  private JButton OKBtn;
  private JButton cancelBtn;
  private JButton applyBtn;

  private void initComponents () {
//    String txt1 = "...,-30,-20,-10,-3,+3,+10,+20,+30,...";
//    String txt2 = "...,-15,-10,-5,-1,+1,+5,+10,+15,...";
    //
    paramsPanel = new javax.swing.JPanel ();
    p1Lbl = new JLabel ();
    p2Lbl = new JLabel ();
    p3Lbl = new JLabel ();
    p4Lbl = new JLabel ();
    p5Lbl = new JLabel ();
    p1Edit = new JEditorPane ();
    p2Edit = new JEditorPane ();
    p3Edit = new JEditorPane ();
    p4Edit = new JEditorPane ();
    p5Edit = new JEditorPane ();
    //
/*
    optionsPanel = new JPanel ();
    optLbl = new JLabel ();
    optGroup = new ButtonGroup();
    o1RButt = new JRadioButton(txt1);
    o2RButt = new JRadioButton(txt2);
*/
    //
    buttonsPanel = new JPanel ();
    OKBtn = new JButton ();
    cancelBtn = new JButton ();
    applyBtn = new JButton ();
    //
    setTitle ("ElectPot Property Editor");
    addWindowListener (new java.awt.event.WindowAdapter () {
      public void windowClosing (java.awt.event.WindowEvent evt) {
        exitForm (evt);
      }
    }
    );

    paramsPanel.setLayout (new java.awt.GridLayout (5, 2));

      p1Lbl.setText ("Bz            [nT]");
      paramsPanel.add (p1Lbl);
      p1Edit.setText(new Double(Bz).toString());
      paramsPanel.add (p1Edit);
  
      p2Lbl.setText ("By            [nT]");
      paramsPanel.add (p2Lbl);
      p2Edit.setText(new Double(By).toString());
      paramsPanel.add (p2Edit);
  
      p3Lbl.setText ("SW Velocity [km/s]");
      paramsPanel.add (p3Lbl);
      p3Edit.setText(new Double(SWVel).toString());
      paramsPanel.add (p3Edit);
  
      p4Lbl.setText ("Tilt         [deg]");
      paramsPanel.add (p4Lbl);
      p4Edit.setText(new Double(Tilt).toString());
      paramsPanel.add (p4Edit);
  
      p5Lbl.setText ("Resolution    [kV]");
      paramsPanel.add (p5Lbl);
      p5Edit.setText(new Double(Resolution).toString());
      paramsPanel.add (p5Edit);
  
    getContentPane ().add (paramsPanel, java.awt.BorderLayout.NORTH);

/*
    optionsPanel.setLayout (new java.awt.GridLayout (3, 1));

      optLbl.setText ("Levels of isolines:");
      optionsPanel.add(optLbl);

      o1RButt.setMnemonic(java.awt.event.KeyEvent.VK_F);
      o1RButt.setActionCommand(txt1);
      o1RButt.setSelected(true);
      optGroup.add(o1RButt);
      o1RButt.addActionListener (new java.awt.event.ActionListener () {
        public void actionPerformed (java.awt.event.ActionEvent evt) {
          //o1RButtActionPerformed (evt);
          o1RButt.setSelected(true);
          rangeSelector = 0;
        }
      }
      );
      optionsPanel.add(o1RButt);

      o2RButt.setMnemonic(java.awt.event.KeyEvent.VK_R);
      o2RButt.setActionCommand(txt2);
      optGroup.add(o2RButt);
      o2RButt.addActionListener (new java.awt.event.ActionListener () {
        public void actionPerformed (java.awt.event.ActionEvent evt) {
          //o2RButtActionPerformed (evt);
          o2RButt.setSelected(true);
          rangeSelector = 1;
        }
      }
      );
      optionsPanel.add(o2RButt);

    getContentPane ().add (optionsPanel, java.awt.BorderLayout.CENTER);
*/

    buttonsPanel.setLayout (new java.awt.GridLayout (1, 3));

      OKBtn.setText ("OK");
      OKBtn.addActionListener (new java.awt.event.ActionListener () {
        public void actionPerformed (java.awt.event.ActionEvent evt) {
          OKBtnActionPerformed (evt);
        }
      }
      );
      buttonsPanel.add (OKBtn);
  
      cancelBtn.setText ("Cancel");
      cancelBtn.addActionListener (new java.awt.event.ActionListener () {
        public void actionPerformed (java.awt.event.ActionEvent evt) {
          cancelBtnActionPerformed (evt);
        }
      }
      );
      buttonsPanel.add (cancelBtn);
  
      applyBtn.setText ("Apply");
      applyBtn.addActionListener (new java.awt.event.ActionListener () {
        public void actionPerformed (java.awt.event.ActionEvent evt) {
          applyBtnActionPerformed (evt);
        }
      }
      );
      buttonsPanel.add (applyBtn);

    getContentPane ().add (buttonsPanel, java.awt.BorderLayout.SOUTH);
  }

  private void applyBtnActionPerformed (java.awt.event.ActionEvent evt) {
    // Add your handling code here:
    try{
      double x1,x2,x3,x4,x5;
      x1 = new Double(p1Edit.getText().trim()).doubleValue();  
      x2 = new Double(p2Edit.getText().trim()).doubleValue();
      x3 = new Double(p3Edit.getText().trim()).doubleValue();
      x4 = new Double(p4Edit.getText().trim()).doubleValue();
      x5 = new Double(p5Edit.getText().trim()).doubleValue();
      //here one could check validity of data and throw NumberFormatException.
      //the following only if the data are correct.
      Bz = x1;
      By = x2;
      SWVel = x3;
      Tilt = x4;
      Resolution = x5;
    }catch(NumberFormatException e){
      //the message window should be shown here.
      return;
    }
    magProps.fireMagPropsChange();
    magProps.getCore().Render();
  }

  private void OKBtnActionPerformed (java.awt.event.ActionEvent evt) {
    this.setVisible(false);
    applyBtnActionPerformed(evt);
  }

  private void cancelBtnActionPerformed (java.awt.event.ActionEvent evt) {
    doCancel();
  }

  private void exitForm(java.awt.event.WindowEvent evt) {
    doCancel();
  }

  public void doCancel() {
    this.setVisible(false);
  }
}