/* ParametersDialog.java */

package distributedwave;

import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;

/** Dialog used to get parameters from the user
 *
 * copyright (c) 2010, 2011 - Russell C. Bjork
 *
 */
public class ParametersDialog extends javax.swing.JDialog
                              implements java.awt.event.ActionListener
{
    /** Creates new form ParametersDialog 
     *
     *  @param parent the parent of this dialog
     *  @param modal true if this dialog should be modal
     *  @param offerSingleSystem true if "Test with Single System" option
     *         should be offered
     */
    public ParametersDialog(java.awt.Frame parent, 
                            boolean modal,
                            boolean offerSingleSystem)
    {
        super(parent, modal);
        initComponents();
        if (! offerSingleSystem)
        {
            singleSystemButton.setVisible(false);
            pack();
        }

        // Make clicking any field equivalent to pressing the enter button

        leftNeighborField.addActionListener(this);
        rightNeighborField.addActionListener(this);
    }

    /** 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() {
        java.awt.GridBagConstraints gridBagConstraints;

        orderGroup = new javax.swing.ButtonGroup();
        partGroup = new javax.swing.ButtonGroup();
        singleSystemButton = new javax.swing.JButton();
        neighborPanel = new javax.swing.JPanel();
        DirectionsLine1 = new javax.swing.JLabel();
        DirectionsLine2 = new javax.swing.JLabel();
        leftNeighborLabel = new javax.swing.JLabel();
        leftNeighborField = new javax.swing.JTextField();
        rightNeighborLabel = new javax.swing.JLabel();
        rightNeighborField = new javax.swing.JTextField();
        buttonPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());

        singleSystemButton.setText("Single System Test");
        singleSystemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                singleSystemButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(15, 15, 10, 15);
        getContentPane().add(singleSystemButton, gridBagConstraints);

        neighborPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Neighbors"));
        neighborPanel.setLayout(new java.awt.GridBagLayout());

        DirectionsLine1.setText("If systems A and B are neighbors, then either A");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        neighborPanel.add(DirectionsLine1, gridBagConstraints);

        DirectionsLine2.setText("must specify B as a neighbor or vice versa, but not both.");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        neighborPanel.add(DirectionsLine2, gridBagConstraints);

        leftNeighborLabel.setText("Left");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        neighborPanel.add(leftNeighborLabel, gridBagConstraints);

        leftNeighborField.setColumns(30);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.ipadx = 70;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        neighborPanel.add(leftNeighborField, gridBagConstraints);

        rightNeighborLabel.setText("Right");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        neighborPanel.add(rightNeighborLabel, gridBagConstraints);

        rightNeighborField.setColumns(30);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipadx = 70;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        neighborPanel.add(rightNeighborField, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 15);
        getContentPane().add(neighborPanel, gridBagConstraints);

        buttonPanel.setLayout(new java.awt.GridBagLayout());

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        buttonPanel.add(okButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 15, 15);
        getContentPane().add(buttonPanel, gridBagConstraints);

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

    /** Request parameters from the user.  This method will not return until
     *  the user either leaves the neighbor fields or enters legitimate
     *  addresses and presses "Enter Dining Room"; if a malformed address is
     *  entered an error will be reported and the method will wait until
     *  legitimate values are entered.  Information entered by the user can be
     *  accessed by the methods getName(), getLeftNeighbor(),
     *  getRightNeighbor(), and getPickupLeftFirst()
     */
    void requestParameters()
    {
        boolean ok;
        do
        {
            ok = true;
            
            setVisible(true);

            try
            {
                if (leftNeighborField.getText().length() == 0)
                    leftNeighbor = null;
                else
                    leftNeighbor = InetAddress.getByName(
                            leftNeighborField.getText());
            }
            catch(UnknownHostException e)
            {
                JOptionPane.showMessageDialog(this,
                    "Incorrect format address or network problem " +
                        leftNeighborField.getText());
                ok = false;
            }

            if (ok)
                try
                {
                    if (rightNeighborField.getText().length() == 0)
                        rightNeighbor = null;
                    else
                        rightNeighbor = InetAddress.getByName(
                                rightNeighborField.getText());

                }
                catch(UnknownHostException e)
                {
                    JOptionPane.showMessageDialog(this,
                        "Incorrect format address or network problem " +
                            rightNeighborField.getText());
                    ok = false;
                }
        } while (! ok);
    }

    /** Accessor for the address of the left neighbor
     *
     *  @return the left neighbor's address - null if the user left the field
     *          blank
     */
    InetAddress getLeftNeighbor()
    {
        return leftNeighbor;
    }

    /** Accessor for the address of the right neighbor
     *
     *  @return the right neighbor's address - null if the user left the field
     *          blank
     */
    InetAddress getRightNeighbor()
    {
        return rightNeighbor;
    }

    /** Accessor for whether signel system test was specified
     *
     *  @param true if single system test was specified
     */
    public boolean isSingleSystem()
    {
        return singleSystemTest;
    }

    // Make clicking return in any field equivalent to clicking the enter button
    public void actionPerformed(java.awt.event.ActionEvent evt)
    {
        okButtonActionPerformed(evt);
    }

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_okButtonActionPerformed

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

    private void singleSystemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_singleSystemButtonActionPerformed
        leftNeighborField.setText("localhost");
        rightNeighborField.setText("localhost");
        singleSystemTest = true;
        setVisible(false);
    }//GEN-LAST:event_singleSystemButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel DirectionsLine1;
    private javax.swing.JLabel DirectionsLine2;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JTextField leftNeighborField;
    private javax.swing.JLabel leftNeighborLabel;
    private javax.swing.JPanel neighborPanel;
    private javax.swing.JButton okButton;
    private javax.swing.ButtonGroup orderGroup;
    private javax.swing.ButtonGroup partGroup;
    private javax.swing.JTextField rightNeighborField;
    private javax.swing.JLabel rightNeighborLabel;
    private javax.swing.JButton singleSystemButton;
    // End of variables declaration//GEN-END:variables

    private InetAddress leftNeighbor, rightNeighbor;
    private boolean singleSystemTest = false;
}
