/*
 * ProfileSelector2.java
 *
 * Created on 3 april 2009, 14:36
 */

package net.i2p.desktopgui.gui;

import java.awt.Point;
import java.util.Enumeration;
import java.util.Properties;
import javax.swing.AbstractButton;
import net.i2p.desktopgui.persistence.PropertyManager;

/**
 *
 * @author  mathias
 */
public class SpeedSelector2 extends javax.swing.JFrame {
    Properties props;

    /** Creates new form ProfileSelector2 */
    public SpeedSelector2(Point point) {
        this.props = PropertyManager.getProps();
        initComponents();
        this.setLocation(point);
        loadButtonSelection();
        this.setVisible(true);
        this.requestFocus();
    }

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

        buttonGroup1 = new javax.swing.ButtonGroup();
        nextButton = new javax.swing.JButton();
        returnButton = new javax.swing.JButton();
        questionLabel = new javax.swing.JLabel();
        browseButton = new javax.swing.JRadioButton();
        downloadButton = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(net.i2p.desktopgui.desktopgui.Main.class).getContext().getResourceMap(SpeedSelector2.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setMinimumSize(new java.awt.Dimension(610, 330));
        setName("Form"); // NOI18N
        setResizable(false);
        getContentPane().setLayout(null);

        nextButton.setText(resourceMap.getString("nextButton.text")); // NOI18N
        nextButton.setName("nextButton"); // NOI18N
        nextButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nextButtonMouseClicked(evt);
            }
        });
        getContentPane().add(nextButton);
        nextButton.setBounds(440, 250, 90, 29);

        returnButton.setText(resourceMap.getString("returnButton.text")); // NOI18N
        returnButton.setName("returnButton"); // NOI18N
        returnButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                returnButtonMouseClicked(evt);
            }
        });
        getContentPane().add(returnButton);
        returnButton.setBounds(336, 250, 90, 29);

        questionLabel.setText(resourceMap.getString("questionLabel.text")); // NOI18N
        questionLabel.setName("questionLabel"); // NOI18N
        getContentPane().add(questionLabel);
        questionLabel.setBounds(30, 40, 265, 17);

        buttonGroup1.add(browseButton);
        browseButton.setText(resourceMap.getString("browseButton.text")); // NOI18N
        browseButton.setActionCommand(resourceMap.getString("browseButton.actionCommand")); // NOI18N
        browseButton.setName("browseButton"); // NOI18N
        getContentPane().add(browseButton);
        browseButton.setBounds(40, 120, 520, 40);

        buttonGroup1.add(downloadButton);
        downloadButton.setText(resourceMap.getString("downloadButton.text")); // NOI18N
        downloadButton.setActionCommand(resourceMap.getString("downloadButton.actionCommand")); // NOI18N
        downloadButton.setName("downloadButton"); // NOI18N
        getContentPane().add(downloadButton);
        downloadButton.setBounds(40, 70, 499, 40);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(30, 170, 530, 70);

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

private void returnButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_returnButtonMouseClicked
    saveButtonSelection();
    PropertyManager.saveProps(props);
    new SpeedSelector(this.getLocationOnScreen());
    this.dispose();
}//GEN-LAST:event_returnButtonMouseClicked

private void nextButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nextButtonMouseClicked
    saveButtonSelection();
    PropertyManager.saveProps(props);
    new SpeedSelector3(this.getLocationOnScreen(), this.getSize());
    this.dispose();
}//GEN-LAST:event_nextButtonMouseClicked

private void loadButtonSelection() {
    
    Enumeration<AbstractButton> elements = buttonGroup1.getElements();
    while(elements.hasMoreElements()) {
        AbstractButton button = elements.nextElement();
        if(button == null)
            continue;
        if(props.getProperty(SpeedSelectorConstants.USERTYPE) == null)
            break;
        String type = button.getActionCommand();
        if(type.equals(props.getProperty(SpeedSelectorConstants.USERTYPE))) {
            button.setSelected(true);
            break;
        }
    }
}

private void saveButtonSelection() {
    Enumeration<AbstractButton> elements = buttonGroup1.getElements();
    while(elements.hasMoreElements()) {
        AbstractButton button = elements.nextElement();
        if(button == null)
            continue;
        if(button.isSelected()) {
            String type = button.getActionCommand();
            props.setProperty(SpeedSelectorConstants.USERTYPE, type);
            break;
        }
    }
}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton browseButton;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JRadioButton downloadButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton nextButton;
    private javax.swing.JLabel questionLabel;
    private javax.swing.JButton returnButton;
    // End of variables declaration//GEN-END:variables

}
