package cl.jtv;
/*
 * DialogParametros.java
 *
 * Created on 5 de diciembre de 2002, 08:22 PM
 */

/**
 *
 * @author  Administrador
 */
import java.util.*;


public class DialogParametros extends javax.swing.JDialog {
    
    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;
    
    private Hashtable parametros;
    private Submaquina submaquina;
    private boolean addComando;
    /** Creates new form DialogParametros */
    public DialogParametros(java.awt.Frame parent, boolean modal,Submaquina submaquina,boolean addComando) {
        super(parent, modal);
        this.parametros = submaquina.parametros;
        this.submaquina = submaquina;
        this.addComando = addComando;
        initComponents();
        Object[][] data = new Object[parametros.size()][2];
        Set en = parametros.keySet();
        java.util.List list = new java.util.ArrayList(en);
        java.util.Collections.sort(list);      
        java.util.Iterator iter = list.iterator();
        int j = 0;
        while(iter.hasNext()){
            String nombre = (String)iter.next();
            Parametro parametro = (Parametro)parametros.get(nombre);
            data[j][0] = nombre;
            try{
                data[j][1] = parametro.isSet()?parametro.getNombre():"";
            }
            catch(Exception ex){
                System.out.println("Excepcion");
            }
            j++;
        }
        /*Object[][] data = {
        {"A","a"},
        {"B","b"},
        {"C","c"},
        };*/
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
        data,
        new String [] {
            "Parámetro", "Valor"
        }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };
            
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
            
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        
        javax.swing.table.TableColumn valores = this.jTable1.getColumnModel().getColumn(1);
        javax.swing.JComboBox comboBox = new javax.swing.JComboBox();
        comboBox.setFont(new java.awt.Font("TIMESROMAN",java.awt.Font.ITALIC,14));
        comboBox.setBackground(java.awt.Color.white);
        
        comboBox.addItem("");
        comboBox.addItem("#");
        for(char i='a';i<='z';i++)
           comboBox.addItem(String.valueOf(i));
        for(char i='0';i<='9';i++)
           comboBox.addItem(String.valueOf(i));
        valores.setCellEditor(new javax.swing.DefaultCellEditor(comboBox));
        
        this.jTable1.setRowHeight(20);
        
        javax.swing.JScrollPane scrollPane = new javax.swing.JScrollPane(jTable1,javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setAutoscrolls(true);
        jTable1.setPreferredScrollableViewportSize(new java.awt.Dimension(150, 300));
        this.jPanel7.add(scrollPane);
    }
    
     public DialogParametros(java.awt.Frame parent, boolean modal,Hashtable parametros) {
        super(parent, modal);
        this.parametros = parametros;
        this.addComando = false;
        initComponents();
        Object[][] data = new Object[parametros.size()][2];
        Set en = parametros.keySet();
        java.util.List list = new java.util.ArrayList(en);
        java.util.Collections.sort(list);      
        java.util.Iterator iter = list.iterator();
        int j = 0;
        while(iter.hasNext()){
            String nombre = (String)iter.next();
            Parametro parametro = (Parametro)parametros.get(nombre);
            data[j][0] = nombre;
            try{
                data[j][1] = parametro.isSet()?parametro.getNombre():"";
            }
            catch(Exception ex){
                System.out.println("Excepcion");
            }
            j++;
        }
        /*Object[][] data = {
        {"A","a"},
        {"B","b"},
        {"C","c"},
        };*/
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
        data,
        new String [] {
            "Parámetro", "Valor"
        }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };
            
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
            
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        
        javax.swing.table.TableColumn valores = this.jTable1.getColumnModel().getColumn(1);
        javax.swing.JComboBox comboBox = new javax.swing.JComboBox();
        comboBox.setFont(new java.awt.Font("TIMESROMAN",java.awt.Font.ITALIC,14));
        comboBox.setBackground(java.awt.Color.white);
        
        comboBox.addItem("");
        comboBox.addItem("#");
        for(char i='a';i<='z';i++)
           comboBox.addItem(String.valueOf(i));
        for(char i='0';i<='9';i++)
           comboBox.addItem(String.valueOf(i));
        valores.setCellEditor(new javax.swing.DefaultCellEditor(comboBox));
        
        this.jTable1.setRowHeight(20);
        
        javax.swing.JScrollPane scrollPane = new javax.swing.JScrollPane(jTable1,javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setAutoscrolls(true);
        jTable1.setPreferredScrollableViewportSize(new java.awt.Dimension(150, 300));
        this.jPanel7.add(scrollPane);
    }
    /** 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() {
        jPanel1 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel7 = new javax.swing.JPanel();
        jTable1 = new javax.swing.JTable();

        setTitle("Asignaci\u00f3n de Par\u00e1metros");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel1.add(jPanel5, java.awt.BorderLayout.NORTH);

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel6.add(jButton1);

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel6.add(jButton2);

        jPanel1.add(jPanel6, java.awt.BorderLayout.SOUTH);

        jTable1.setFont(new java.awt.Font("Times New Roman", 2, 15));
        jPanel7.add(jTable1);

        jPanel1.add(jPanel7, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-300)/2, (screenSize.height-200)/2, 300, 200);
    }
    // </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // Add your handling code here:
         doClose (RET_CANCEL);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // Add your handling code here:
        try{
        Enumeration en = parametros.elements();
        Vector comandosParametrosOld = null;
        if(addComando)
            comandosParametrosOld = new Vector();
        int i=0;
        while(en.hasMoreElements()){
            Parametro parametro = (Parametro)en.nextElement();
            if(addComando) comandosParametrosOld.add(new ComandoEPR(parametro,null,parametro.getValor())); 
            parametro.setValor(Simbolo.getSimbolo((String)jTable1.getValueAt(i,1)));
            i++;
        }
        if(addComando){
            ComandoEPS comandoEPS = new ComandoEPS(submaquina,null);
            en = null;
            en = parametros.elements();
            while(en.hasMoreElements()){
                Parametro parametro = (Parametro)en.nextElement();
                comandoEPS.addComando(new ComandoEPR(parametro,null,parametro.getValor()));
            }
            
            ComandoEPS comandoEPSInverse = new ComandoEPS(submaquina,null);
            en = null;
            en = comandosParametrosOld.elements();
            while(en.hasMoreElements())
                comandoEPSInverse.addComando((ComandoEPR)en.nextElement());
            comandoEPS.setInverse(comandoEPSInverse);
            Comando.addComando2UR(comandoEPS);
        }
        }
        catch(Exception ex){
            System.out.println("Excepción en DialogParametros");
        }
        doClose (RET_OK);
    }//GEN-LAST:event_jButton1ActionPerformed

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog
    
    private void doClose (int retStatus) {
        returnStatus = retStatus;
        setVisible (false);
        dispose ();
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        //new DialogParametros(new javax.swing.JFrame(), true,null,false).show();
        Hashtable h1 = new Hashtable();
        Integer i1 = new Integer(1);
        Integer i2 = new Integer(2);
        h1.put(i1,i1);
        h1.put(i2,i2);
        
        Hashtable h2 = (Hashtable)h1.clone();
        
        if(h1.get(i1)==h2.get(i1))
            System.out.println("Son iguales");
        else
            System.out.println("Son diferentes");
    }

    /** Getter for property returnStatus.
     * @return Value of property returnStatus.
     */
    public int getReturnStatus() {
        return returnStatus;
    }    

    /** Setter for property returnStatus.
     * @param returnStatus New value of property returnStatus.
     */
    public void setReturnStatus(int returnStatus) {
        this.returnStatus = returnStatus;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
    private int returnStatus = RET_CANCEL;
}
