/*
 * RenameOptionDialog.java
 *
 */

package com.uvigo.tMaestro.SCOCreator.tool;

/**
 * Cuadro de dialogo que se utiliza para cambiar el nombre de las opciones
 * @author  raul
 * @version 0.1
 */
public class RenameOptionDialog extends javax.swing.JDialog {
    
    /**
     * Referencia al frame principal de la herramienta
     */
    private MainFrame oMainFrame;
    
    
    /**
     * Crea una nueva instancia del cuadro de dialogo.
     * Es aqui en donde se ponene las etiquetas de acuerdo
     * con el idioma elegido.
     */
    public RenameOptionDialog(java.awt.Frame parent) {
        super(parent, true);
        initComponents();
        oMainFrame = (MainFrame) parent;
        oJLabel1.setText(oMainFrame.getMessage("Text2"));
        oJLabel2.setText(oMainFrame.getMessage("Text3"));
        oJButton1.setText(oMainFrame.getMessage("Button1"));
        oJButton2.setText(oMainFrame.getMessage("Button2"));
        reloadOptions();
    }
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        oJPanel1 = new javax.swing.JPanel();
        oJLabel1 = new javax.swing.JLabel();
        oJComboBox1 = new javax.swing.JComboBox();
        oJLabel2 = new javax.swing.JLabel();
        oJTextField1 = new javax.swing.JTextField();
        oJPanel2 = new javax.swing.JPanel();
        oJButton1 = new javax.swing.JButton();
        oJButton2 = new javax.swing.JButton();

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

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        oJPanel1.setLayout(new java.awt.GridBagLayout());

        oJLabel1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 0, 10);
        oJPanel1.add(oJLabel1, gridBagConstraints);

        oJComboBox1.setMinimumSize(new java.awt.Dimension(140, 19));
        oJComboBox1.setPreferredSize(new java.awt.Dimension(140, 19));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 0, 0);
        oJPanel1.add(oJComboBox1, gridBagConstraints);

        oJLabel2.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 0, 10);
        oJPanel1.add(oJLabel2, gridBagConstraints);

        oJTextField1.setMinimumSize(new java.awt.Dimension(140, 19));
        oJTextField1.setPreferredSize(new java.awt.Dimension(140, 19));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 0, 0);
        oJPanel1.add(oJTextField1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 10);
        getContentPane().add(oJPanel1, gridBagConstraints);

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

        oJButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                oJButton1ActionPerformed(evt);
            }
        });

        oJPanel2.add(oJButton1, new java.awt.GridBagConstraints());

        oJButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                oJButton2ActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        oJPanel2.add(oJButton2, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(15, 10, 10, 10);
        getContentPane().add(oJPanel2, gridBagConstraints);

        setSize(new java.awt.Dimension(266, 159));
    }
    // </editor-fold>//GEN-END:initComponents
    
    /**
     * Cuando el usuario pulsa sobre el boton Cerrar
     */
    private void oJButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oJButton2ActionPerformed
        dispose();
    }//GEN-LAST:event_oJButton2ActionPerformed
    
    /**
     * Funcion llamada cuando el usuario pulsa sobre el boton de renombrar
     */
    private void oJButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oJButton1ActionPerformed
        String sNuevo = oJTextField1.getText();
        if (oMainFrame.containsOption(sNuevo)) {
            java.awt.Toolkit.getDefaultToolkit().beep();
            MyOptionPane.showMessageDialog(this,oMainFrame.getMessage("Error6"),oMainFrame.getMessage("Title12"),javax.swing.JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (sNuevo.equals("")) {
            java.awt.Toolkit.getDefaultToolkit().beep();
            MyOptionPane.showMessageDialog(this,oMainFrame.getMessage("Error5"),oMainFrame.getMessage("Title12"),javax.swing.JOptionPane.ERROR_MESSAGE);
            return;
        }
        oJTextField1.setText("");
        oMainFrame.setOption(oJComboBox1.getSelectedIndex(), sNuevo, (String) oJComboBox1.getSelectedItem());
        reloadOptions();
    }//GEN-LAST:event_oJButton1ActionPerformed
    
    /**
     * Coloca en el ComboBox las opciones
     * contempladas en la herramienta
     */
    private void reloadOptions() {
        oJComboBox1.removeAllItems();
        int i=0;
        for (i=0;i<oMainFrame.sizeOptions();i++) {
            oJComboBox1.addItem(oMainFrame.getOption(i));
        }
        oJComboBox1.setSelectedIndex(oMainFrame.getPSheet().getSelectedOption());
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton oJButton1;
    private javax.swing.JButton oJButton2;
    private javax.swing.JComboBox oJComboBox1;
    private javax.swing.JLabel oJLabel1;
    private javax.swing.JLabel oJLabel2;
    private javax.swing.JPanel oJPanel1;
    private javax.swing.JPanel oJPanel2;
    private javax.swing.JTextField oJTextField1;
    // End of variables declaration//GEN-END:variables
    
}
