/*
 * StationModeDialog.java
 *
 * Created on Aug 4, 2009, 12:12:54 PM
 */
package usda.weru.weps.location.chooser;

import java.awt.event.ItemEvent;
import java.awt.event.MouseEvent;
import java.awt.Component;
import java.awt.HeadlessException;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import usda.weru.weps.location.StationMode;

/**
 *
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public class StationModeChooser extends JPanel {

    private static final long serialVersionUID = 1L;

    /**
     * Return value if cancel is chosen.
     */
    public static final int CANCEL_OPTION = 1;

    /**
     * Return value if approve (yes, ok) is chosen.
     */
    public static final int APPROVE_OPTION = 0;

    /**
     * Return value if an error occured.
     */
    public static final int ERROR_OPTION = -1;

    private int c_returnValue;
    private StationMode c_selectedMode;
    private JDialog c_dialog;

    /** Creates new form StationModeDialog
     * @param allowedModes
     * @param selected
     */
    public StationModeChooser(StationMode[] allowedModes, StationMode selected) {

/**
                 * Note:  Assertions are not enabled.  These will be useless items
                 * unless assertions are enabled.  Thus, they will be commented out unless
                 * the user wishes to enable specific assertions (feed the virtual machine 
                 * the -ea argument).
                 */
//        assert allowedModes != null : "Must have a StaionMode array.";
        initComponents();

        okButton.setVisible(false);
        cancelButton.setVisible(false);
        label.setVisible(false);

        //prepare the selection choices that are allowed
        boolean first = true;
        for (final StationMode mode : allowedModes) {
            final JRadioButton option = new JRadioButton(mode.getDisplayName());

            option.addItemListener(new ItemListener() {

                @Override
                public void itemStateChanged(ItemEvent e) {
                    if (option.isSelected()) {
                        c_selectedMode = mode;
                    }
                }
            });

            option.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
                        if (c_dialog != null) {
                            c_selectedMode = mode;
                            okButton.doClick();
                        }
                    }
                }
            });

            //add to the panel
            optionsPanel.add(option);

            //add to the group
            optionsButtonGroup.add(option);

            if (first) {
                option.setSelected(true);
                option.requestFocusInWindow();
                first = false;
            }

            //is it selected?
            if (mode.equals(selected)) {
                option.setSelected(true);
                option.requestFocusInWindow();
            }
        }

        validate();

    }

    /**
     *
     * @param text
     */
    public void setLabelText(String text) {
        label.setVisible(true);
        label.setText(text);
    }

    /**
     *
     * @param parent
     * @return
     * @throws HeadlessException
     */
    public int showDialog(Component parent) throws HeadlessException {

        if (parent != null) {
            c_dialog = new JDialog(SwingUtilities.getWindowAncestor(parent));
        } else {
            c_dialog = new JDialog();
        }

        c_dialog.add(this);
        c_dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        c_dialog.setResizable(false);
        c_dialog.setModal(true);
        c_dialog.setTitle("Station Selection Mode");
        okButton.setVisible(true);
        cancelButton.setVisible(true);
        label.setVisible(false);
        c_dialog.pack();

        c_dialog.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                c_returnValue = CANCEL_OPTION;
            }
        });
        c_returnValue = ERROR_OPTION;

        c_dialog.setLocationRelativeTo(parent);
        c_dialog.setVisible(true);

        c_dialog.removeAll();
        c_dialog.dispose();
        c_dialog = null;
        return c_returnValue;
    }

    /** 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() {

        optionsButtonGroup = new javax.swing.ButtonGroup();
        label = new javax.swing.JTextPane();
        cancelButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        optionsPanel = new javax.swing.JPanel();

        label.setBorder(null);
        label.setEditable(false);
        label.setFocusable(false);
        label.setOpaque(false);

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        okButton.setText("Ok");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        optionsPanel.setLayout(new javax.swing.BoxLayout(optionsPanel, javax.swing.BoxLayout.Y_AXIS));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(optionsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton))
                    .addComponent(label, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(optionsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        if (optionsButtonGroup.getSelection() == null) {
            JOptionPane.showMessageDialog(this, "Please make a selection.", "Selection", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        c_returnValue = APPROVE_OPTION;

        c_dialog.dispose();
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        c_returnValue = CANCEL_OPTION;
        c_dialog.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    /**
     *
     * @return
     */
    public StationMode getSelectedStationMode() {
        return c_selectedMode;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextPane label;
    private javax.swing.JButton okButton;
    private javax.swing.ButtonGroup optionsButtonGroup;
    private javax.swing.JPanel optionsPanel;
    // End of variables declaration//GEN-END:variables

}
