package spana;

import lib.common.Util;
import lib.huvud.ProgramConf;
import lib.kemi.chem.Chem;

/** Dialog to change logK.
 * <br>
 * Copyright (C) 2014-2020 I.Puigdomenech.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/
 * 
 * @author Ignasi Puigdomenech */
public class LogKchange extends javax.swing.JFrame {
    private ProgramConf pc;
    private final java.awt.Dimension windowSize;
    private boolean finished = false;
    private Chem.ChemSystem cs = null;
    private Chem.ChemSystem.NamesEtc namn = null;
    private final int species;
    private boolean deactivated_old;
    private double lBeta_old;
    private boolean modified;
    /** when a jTextField containing a number is edited,
     * the old text is saved in this variable. If the user makes a mistake, for example
     * enters "0.7e", then the old text is restored from this variable. */
    private String oldTextLogK = "0";
    /** New-line character(s) to substitute "\n" */
    private static final String nl = System.getProperty("line.separator");

  //<editor-fold defaultstate="collapsed" desc="Constructor">
  /** Creates new form logKchange
   * @param chemS
   * @param complex
   * @param pc0 */
  public LogKchange(Chem.ChemSystem chemS, int complex, ProgramConf pc0) {
    initComponents();
    this.cs = chemS;
    this.namn = cs.namn;
    this.species = complex;
    this.pc = pc0;
    modified = false;
    setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
    //center Window on Screen
    windowSize = this.getSize();
    int left; int top;
    left = Math.max(0, (MainFrame.screenSize.width  - windowSize.width ) / 2);
    top = Math.max(0, (MainFrame.screenSize.height - windowSize.height) / 2);
    this.setLocation(Math.min(MainFrame.screenSize.width-100, left),
                     Math.min(MainFrame.screenSize.height-100, top));
    //--- close window on ESC key
    javax.swing.KeyStroke escKeyStroke = javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE,0, false);
    getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(escKeyStroke,"ESCAPE");
    javax.swing.Action escAction = new javax.swing.AbstractAction() {
        @Override public void actionPerformed(java.awt.event.ActionEvent e) {
            quitDialog();
        }};
    getRootPane().getActionMap().put("ESCAPE", escAction);
    //--- Alt-X eXit
    javax.swing.KeyStroke altXKeyStroke = javax.swing.KeyStroke.getKeyStroke(
            java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.ALT_MASK, false);
    getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(altXKeyStroke,"ALT_X");
    javax.swing.Action altXAction = new javax.swing.AbstractAction() {
        @Override public void actionPerformed(java.awt.event.ActionEvent e) {
            jButtonOK.doClick();
        }};
    getRootPane().getActionMap().put("ALT_X", altXAction);
    //--- F1 for help
    javax.swing.KeyStroke f1KeyStroke = javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1,0, false);
    getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(f1KeyStroke,"F1");
    javax.swing.Action f1Action = new javax.swing.AbstractAction() {
        @Override public void actionPerformed(java.awt.event.ActionEvent e) {
            jButtonHelp.doClick();
        }};
    getRootPane().getActionMap().put("F1", f1Action);

    //--- Title
    this.setTitle("Change logK");
    //---- Icon
    String iconName = "images/LogK_32x32.gif";
    java.net.URL imgURL = this.getClass().getResource(iconName);
    if (imgURL != null) {this.setIconImage(new javax.swing.ImageIcon(imgURL).getImage());}
    else {System.out.println("Error: Could not load image = \""+iconName+"\"");}

    // --- set up frame
    jLabelReaction.setText(spana.ModifyChemSyst.reactionText(cs, species, false));
    lBeta_old = cs.lBeta[species];
    jLabelLogKOld.setText(Util.formatDbl4(lBeta_old));
    jTextFieldLogK.setText(Util.formatDbl4(lBeta_old));
    setCheckBox(namn.ident[cs.Na + species]);
    deactivated_old = jRadioButtonDeact.isSelected();

    setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    this.setVisible(true);
  }
  //</editor-fold>

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabelReaction = new javax.swing.JLabel();
        jLabelWarning = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabelLogKOld = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldLogK = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jRadioButtonAct = new javax.swing.JRadioButton();
        jRadioButtonDeact = new javax.swing.JRadioButton();
        jPanelButtons = new javax.swing.JPanel();
        jButtonQuit = new javax.swing.JButton();
        jButtonOK = new javax.swing.JButton();
        jButtonHelp = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 0, 204));
        jLabel1.setText("Reaction:"); // NOI18N

        jLabelReaction.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabelReaction.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabelReaction.setText("8 Cu 2+ + 8 H3(urid)  =  Cu8(urid)8 + 24 H+"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelReaction, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelReaction)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabelWarning.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabelWarning.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spana/images/Warn_RedTriangl.gif"))); // NOI18N
        jLabelWarning.setText("<html>Warning! This species is not active.<br>This may result in wrong diagrams!</html>"); // NOI18N
        jLabelWarning.setAlignmentY(0.0F);
        jLabelWarning.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 0, 204));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("old logK = "); // NOI18N

        jLabelLogKOld.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabelLogKOld.setText("jLabel5"); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 0, 204));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Enter new logK:"); // NOI18N

        jTextFieldLogK.setText("-999.999"); // NOI18N
        jTextFieldLogK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldLogKActionPerformed(evt);
            }
        });
        jTextFieldLogK.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldLogKFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldLogKFocusLost(evt);
            }
        });
        jTextFieldLogK.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldLogKKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextFieldLogKKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 99, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 33, Short.MAX_VALUE)
        );

        buttonGroup.add(jRadioButtonAct);
        jRadioButtonAct.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jRadioButtonAct.setForeground(new java.awt.Color(0, 0, 204));
        jRadioButtonAct.setMnemonic('a');
        jRadioButtonAct.setText("activate");
        jRadioButtonAct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonActActionPerformed(evt);
            }
        });

        buttonGroup.add(jRadioButtonDeact);
        jRadioButtonDeact.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jRadioButtonDeact.setForeground(new java.awt.Color(0, 0, 204));
        jRadioButtonDeact.setMnemonic('d');
        jRadioButtonDeact.setText("deactivate");
        jRadioButtonDeact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonDeactActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(157, 157, 157)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButtonDeact)
                    .addComponent(jRadioButtonAct))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelLogKOld)
                    .addComponent(jTextFieldLogK, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabelLogKOld))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTextFieldLogK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(4, 4, 4)
                .addComponent(jRadioButtonAct)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButtonDeact)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButtonQuit.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButtonQuit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spana/images/Quit_32x32.gif"))); // NOI18N
        jButtonQuit.setMnemonic('Q');
        jButtonQuit.setText("<html><u>Q</u>uit</html>"); // NOI18N
        jButtonQuit.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButtonQuit.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
        jButtonQuit.setIconTextGap(8);
        jButtonQuit.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jButtonQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonQuitActionPerformed(evt);
            }
        });

        jButtonOK.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButtonOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spana/images/Save_32x32.gif"))); // NOI18N
        jButtonOK.setMnemonic('O');
        jButtonOK.setText("OK"); // NOI18N
        jButtonOK.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButtonOK.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
        jButtonOK.setIconTextGap(8);
        jButtonOK.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jButtonHelp.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButtonHelp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spana/images/Help_32x32.gif"))); // NOI18N
        jButtonHelp.setMnemonic('H');
        jButtonHelp.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButtonHelp.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jButtonHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonHelpActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelButtonsLayout = new javax.swing.GroupLayout(jPanelButtons);
        jPanelButtons.setLayout(jPanelButtonsLayout);
        jPanelButtonsLayout.setHorizontalGroup(
            jPanelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelButtonsLayout.createSequentialGroup()
                .addGroup(jPanelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanelButtonsLayout.createSequentialGroup()
                        .addGap(93, 93, 93)
                        .addComponent(jButtonHelp))
                    .addGroup(jPanelButtonsLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButtonOK, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonQuit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelButtonsLayout.setVerticalGroup(
            jPanelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelButtonsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonOK)
                    .addComponent(jButtonQuit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonHelp)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanelButtons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelButtons, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addGap(2, 2, 2))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelWarning, javax.swing.GroupLayout.PREFERRED_SIZE, 354, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(jLabelWarning, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

  //<editor-fold defaultstate="collapsed" desc="Events">
    private void jButtonQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonQuitActionPerformed
        quitDialog();
}//GEN-LAST:event_jButtonQuitActionPerformed

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        validateLogK();
        lBeta_old = cs.lBeta[species];
        deactivated_old = jRadioButtonDeact.isSelected();
        quitDialog();
}//GEN-LAST:event_jButtonOKActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        quitDialog();
    }//GEN-LAST:event_formWindowClosing

    private void jTextFieldLogKFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldLogKFocusLost
        validateLogK();
    }//GEN-LAST:event_jTextFieldLogKFocusLost

    private void jTextFieldLogKKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldLogKKeyPressed
        if(evt.getKeyChar() == java.awt.event.KeyEvent.VK_ENTER) {
            validateLogK();
        }
    }//GEN-LAST:event_jTextFieldLogKKeyPressed

    private void jTextFieldLogKKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldLogKKeyTyped
        char key = evt.getKeyChar();
        if(!isCharOKforNumberInput(key)) {evt.consume();}
    }//GEN-LAST:event_jTextFieldLogKKeyTyped

    private void jTextFieldLogKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldLogKActionPerformed
        validateLogK();
    }//GEN-LAST:event_jTextFieldLogKActionPerformed

    private void jTextFieldLogKFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldLogKFocusGained
        oldTextLogK = jTextFieldLogK.getText();
        jTextFieldLogK.selectAll();
    }//GEN-LAST:event_jTextFieldLogKFocusGained

    private void jRadioButtonActActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonActActionPerformed
        if(jRadioButtonAct.isSelected()) {
          if(namn.ident[cs.Na+species].startsWith("*")) {
            namn.ident[cs.Na+species] = namn.ident[cs.Na+species].substring(1);
          }
        } else { // radiobutton not selected
          if(!namn.ident[cs.Na+species].startsWith("*")) {
            namn.ident[cs.Na+species] = "*"+namn.ident[cs.Na+species];
          }
        }
        jLabelReaction.setText(spana.ModifyChemSyst.reactionText(cs, species, false));
        setCheckBox(namn.ident[cs.Na+species]);
    }//GEN-LAST:event_jRadioButtonActActionPerformed

    private void jRadioButtonDeactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonDeactActionPerformed
        if(jRadioButtonDeact.isSelected()) {
          if(!namn.ident[cs.Na+species].startsWith("*")) {
            namn.ident[cs.Na+species] = "*"+namn.ident[cs.Na+species];
          }
        } else { // radiobutton not selected
          if(namn.ident[cs.Na+species].startsWith("*")) {
            namn.ident[cs.Na+species] = namn.ident[cs.Na+species].substring(1);
          }
        }
        jLabelReaction.setText(spana.ModifyChemSyst.reactionText(cs, species, false));
        setCheckBox(namn.ident[cs.Na+species]);
    }//GEN-LAST:event_jRadioButtonDeactActionPerformed

    private void jButtonHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonHelpActionPerformed
            setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
            Thread hlp = new Thread() {@Override public void run(){
                String[] a = {"S_Modify_Chem_System_htm_Deactivate"};
                lib.huvud.RunProgr.runProgramInProcess(LogKchange.this,ProgramConf.HELP_JAR,a,false,pc.dbg,pc.pathAPP);
                try{Thread.sleep(2000);}   //show the "wait" cursor for 2 sec
                catch (InterruptedException e) {}
                setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
            }};//new Thread
            hlp.start();

    }//GEN-LAST:event_jButtonHelpActionPerformed

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
      if(windowSize != null) {
        int w = windowSize.width;
        int h = windowSize.height;
        if(this.getHeight()<h){this.setSize(this.getWidth(), h);}
        if(this.getWidth()<w){this.setSize(w,this.getHeight());}
      }
    }//GEN-LAST:event_formComponentResized

  //</editor-fold>

  //<editor-fold defaultstate="collapsed" desc="Methods">
  private void quitDialog() {
    if(Math.abs(cs.lBeta[species] - lBeta_old) > 1e-8 ||
       deactivated_old != jRadioButtonDeact.isSelected()) {modified = true;}
    if(modified) {
      int n;
      Object[] options = {"Cancel", "Yes"};
          n = javax.swing.JOptionPane.
                  showOptionDialog(this,"Discard your changes?",
                  "Modify Chemical System", javax.swing.JOptionPane.YES_NO_OPTION,
                  javax.swing.JOptionPane.WARNING_MESSAGE, null, options, null);
      if(n != javax.swing.JOptionPane.NO_OPTION) { // second button = "yes"
          return; // oops!
      } else { // yes: discard changes
          cs.lBeta[species] = lBeta_old;
          if(deactivated_old) {
            if(!namn.ident[cs.Na + species].startsWith("*")) {
              namn.ident[cs.Na + species] = "*"+namn.ident[cs.Na + species];
            }
          } else {
            if(namn.ident[cs.Na + species].startsWith("*")) {
              namn.ident[cs.Na + species] = namn.ident[cs.Na + species].substring(1);
            }
          }
      } // discard changes?
    } //if modified
    finished = true;
    this.notify_All();
    this.dispose();
  } // quitForm_Gen_Options

  private synchronized void notify_All() {notifyAll();}

  /** this method will wait for this dialog frame to be closed
   * @return "modified"
   */
  public synchronized boolean  waitForLogKchange() {
    while(!finished) {
        try {wait();} catch (InterruptedException ex) {}
    } // while
    return modified;
  } // waitForLogKchange()

  private void setCheckBox(String name) {
    if(name.startsWith("*")) {
        jRadioButtonDeact.setSelected(true);
        jRadioButtonDeact.setText("deactivated");
        jRadioButtonAct.setText("activate");
        jLabelWarning.setVisible(true);
    } else {
        jRadioButtonAct.setSelected(true);
        jRadioButtonDeact.setText("deactivate");
        jRadioButtonAct.setText("activated");
        jLabelWarning.setVisible(false);
    }
  }

  private void validateLogK(){
    cs.lBeta[species] = readLogK();
    String t = Util.formatNum(cs.lBeta[species]);
    if(Double.parseDouble(t) != cs.lBeta[species]) {
        jTextFieldLogK.setText(t);
    }
  }

  private double readLogK() {
    String t = jTextFieldLogK.getText().trim();
    if(t.length() <= 0) {return 0;}
    double w;
    try{w = Double.valueOf(t);
        w = Math.min(1e50,Math.max(w, -1e50));
    } catch (NumberFormatException ex) {
        try{w = Double.valueOf(oldTextLogK); jTextFieldLogK.setText(oldTextLogK);}
        catch (NumberFormatException e) {w = 0.;}
        String msg = "Error (NumberFormatException)";
        msg = msg +nl+"with text: \""+t+"\"";
        System.out.println("---- "+msg);
        javax.swing.JOptionPane.showMessageDialog(this, msg,
                pc.progName, javax.swing.JOptionPane.ERROR_MESSAGE);
    }
    return w;
  } //readLogK()

  /** @param key a character
   * @return true if the character is ok, that is, it is either a number,
   * or a dot, or a minus sign, or an "E" (such as in "2.5e-6") */
  private boolean isCharOKforNumberInput(char key) {
        return Character.isDigit(key)
                || key == '-' || key == '+' || key == '.' || key == 'E' || key == 'e';
  } // isCharOKforNumberInput(char)

  //</editor-fold>

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JButton jButtonHelp;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JButton jButtonQuit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabelLogKOld;
    private javax.swing.JLabel jLabelReaction;
    private javax.swing.JLabel jLabelWarning;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanelButtons;
    private javax.swing.JRadioButton jRadioButtonAct;
    private javax.swing.JRadioButton jRadioButtonDeact;
    private javax.swing.JTextField jTextFieldLogK;
    // End of variables declaration//GEN-END:variables

}
