/*
!=======================================================================
!
!  PROGRAM  PHASE-Viewer  (PHASE-Viewer 2014.01 ver.3.3.0)
!
!  Created on ----
!  AUTHOR(S): KOGA, Junichiro
!  File : InputPanel.java
!  
!  Contact address :  Phase System Consortium
!                     E-mail: phase_system@nims.go.jp URL https://azuma.nims.go.jp
!
!
!   Since 2002, this program set had been intensively developed as a part of the following 
!  national projects supported by the Ministry of Education, Culture, Sports, Science and
!  Technology (MEXT) of Japan; "Frontier Simulation Software for Industrial Science
!  (FSIS)" from 2002 to 2005, "Revolutionary Simulation Software (RSS21)" from 2006 to
!  2008. "Research and Development of Innovative Simulation Software (RISS)" from 2008
!  to 2013. These projects is lead by the Center for Research on Innovative Simulation 
!  Software (CISS), the Institute of Industrial Science (IIS), the University of Tokyo.
!   Since 2013, this program set has been further developed centering on PHASE System
!  Consortium. 
!   The activity of development of this program set has been supervised by Takahisa Ohno.
!
!=======================================================================
 */
package ciss.phase_viewer.common;

import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

public class InputPanel extends JPanel {
    JLabel m_tag;
    public JTextField m_value; /* !< このクラスが保持するJTextFieldオブジェクト */
    public JComboBox m_unit; /* !< このクラスが保持するJComboBoxオブジェクト */

    public static final int NONE = 0; /* !< 単位なしを指定する. */
    public static final int LONGTIME = 1; /*
                                           * !< PHASEプログラムの実行時間を指定するための単位 (day,
                                           * hour,...)を指定する.
                                           */
    public static final int TIME = 2; /* !< PHASE内部の時間 (au_time, fs, ... ). */
    public static final int LENGTH = 3; /* !< 長さの単位を指定. */
    public static final int ENERGY = 4; /* !< エネルギーの単位を指定. */
    public static final int FORCE = 5; /* !< 力の単位を指定. */

    private String[] units_longtime = { "day", "hour", "min", "sec" };
    private String[] units_time = { "au_time", "ns", "fs", };
    private String[] units_length = { "Bohr", "Angstrom", "nm" };
    private String[] units_energy = { "Hartree", "Rydberg", "eV" };
    private String[] units_force = { "Hartree/Bohr", "eV/Angstrom" };

    /**
     * このクラスが提供するGUIを作成. JTextFieldは具体的な値を 入力するため,
     * JComboBoxは対応する単位を編集するために配備される.
     * 
     * @param tag
     *            タイトル.
     * @param unit
     *            単位の種類.
     * @param textlen
     *            JTextFieldのカラム数.
     */
    public InputPanel(String tag, int unit, int textlen) {
        int pwidth;
        if (unit == NONE) {
            pwidth = textlen;
        } else {
            pwidth = textlen + 80;
        }
        // setLayout( new FlowLayout(FlowLayout.CENTER) );
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        setSize(pwidth, 20);
        // m_tag = new JLabel( tag, JLabel.LEFT);
        // m_tag.setPreferredSize( new Dimension(taglen,20));
        // add(m_tag);
        m_value = new JTextField(textlen);
        m_value.setSize(textlen, 20);
        add(m_value);
        if (unit == LONGTIME) {
            m_unit = new JComboBox(units_longtime);
        } else if (unit == TIME) {
            m_unit = new JComboBox(units_time);
        } else if (unit == LENGTH) {
            m_unit = new JComboBox(units_length);
        } else if (unit == ENERGY) {
            m_unit = new JComboBox(units_energy);
        } else if (unit == FORCE) {
            m_unit = new JComboBox(units_force);
        }
        if (unit != NONE) {
            m_unit.setSize(60, 20);
            add(m_unit);
        }
        setBorder(new TitledBorder(tag));
    }

    public InputPanel(String tag, int unit, int textlen, JTextField textfield) {
        int pwidth;
        if (unit == NONE) {
            pwidth = textlen;
        } else {
            pwidth = textlen + 80;
        }
        // setLayout( new FlowLayout(FlowLayout.CENTER) );
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        setSize(pwidth, 20);
        // m_tag = new JLabel( tag, JLabel.LEFT);
        // m_tag.setPreferredSize( new Dimension(taglen,20));
        // add(m_tag);
        // m_value = new JTextField(textlen);

        m_value = textfield;
        m_value.setSize(textlen, 20);
        add(m_value);
        if (unit == LONGTIME) {
            m_unit = new JComboBox(units_longtime);
        } else if (unit == TIME) {
            m_unit = new JComboBox(units_time);
        } else if (unit == LENGTH) {
            m_unit = new JComboBox(units_length);
        } else if (unit == ENERGY) {
            m_unit = new JComboBox(units_energy);
        } else if (unit == FORCE) {
            m_unit = new JComboBox(units_force);
        }
        if (unit != NONE) {
            m_unit.setSize(60, 20);
            add(m_unit);
        }
        setBorder(new TitledBorder(tag));
    }

    public InputPanel(String tag, int unit, int textlen, JTextField textfield,
            boolean flowLayout) {
        int pwidth;
        if (unit == NONE) {
            pwidth = textlen;
        } else {
            pwidth = textlen + 80;
        }
        // setLayout( new FlowLayout(FlowLayout.CENTER) );
        if (flowLayout)
            setLayout(new FlowLayout());
        else
            setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

        setSize(pwidth, 20);
        // m_tag = new JLabel( tag, JLabel.LEFT);
        // m_tag.setPreferredSize( new Dimension(taglen,20));
        // add(m_tag);
        // m_value = new JTextField(textlen);

        m_value = textfield;
        m_value.setSize(textlen, 20);
        add(m_value);
        if (unit == LONGTIME) {
            m_unit = new JComboBox(units_longtime);
        } else if (unit == TIME) {
            m_unit = new JComboBox(units_time);
        } else if (unit == LENGTH) {
            m_unit = new JComboBox(units_length);
        } else if (unit == ENERGY) {
            m_unit = new JComboBox(units_energy);
        } else if (unit == FORCE) {
            m_unit = new JComboBox(units_force);
        }
        if (unit != NONE) {
            m_unit.setSize(60, 20);
            add(m_unit);
        }
        setBorder(new TitledBorder(tag));
    }

    public InputPanel(String tag, int unit, int textlen, boolean flowLayout) {
        int pwidth;
        if (unit == NONE) {
            pwidth = textlen;
        } else {
            pwidth = textlen + 80;
        }
        if (flowLayout) {
            setLayout(new FlowLayout(FlowLayout.CENTER));
        } else {
            setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        }
        setSize(pwidth, 20);
        // m_tag = new JLabel( tag, JLabel.LEFT);
        // m_tag.setPreferredSize( new Dimension(taglen,20));
        // add(m_tag);
        m_value = new JTextField(textlen);
        m_value.setSize(textlen, 20);
        add(m_value);
        if (unit == LONGTIME) {
            m_unit = new JComboBox(units_longtime);
        } else if (unit == TIME) {
            m_unit = new JComboBox(units_time);
        } else if (unit == LENGTH) {
            m_unit = new JComboBox(units_length);
        } else if (unit == ENERGY) {
            m_unit = new JComboBox(units_energy);
        } else if (unit == FORCE) {
            m_unit = new JComboBox(units_force);
        }
        if (unit != NONE) {
            m_unit.setSize(60, 20);
            add(m_unit);
        }
        setBorder(new TitledBorder(tag));
    }

    /**
     * JTextFieldの値を取得する.
     * 
     * @return JTextFieldに表示してある値.
     */
    public String getValue() {
        return (String) m_value.getText();
    }

    /**
     * JComboBoxの値を取得する
     * 
     * @return JComboBoxの値.
     */
    public String getUnit() {
        return (String) m_unit.getSelectedItem();
    }

    /**
     * JTextFieldの値を設定する.
     * 
     * @param str
     *            JTextFieldに表示したい値.
     */
    public void setValue(String str) {
        m_value.setText(str);
    }

    /**
     * JComboBoxの値を設定する.
     * 
     * @param unit
     *            設定したいJComboBoxの値.
     */
    public void setUnit(String unit) {
        m_unit.setSelectedItem(unit);
    }

    public JTextField getTextField() {
        return this.m_value;
    }

    public void setTextField(JTextField m_value) {
        this.m_value = m_value;
    }

}
