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

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


public class DialogPropiedades 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;
    
    public static boolean editingMode=false;
    
    private Vector2 vector2;
    private NodeTreeMachine node;
    private Hashtable dicOfMachines;
    private String nombreLargoOld;
    private boolean edit,edit2;
    /** Creates new form DialogParametros */
    public DialogPropiedades(java.awt.Frame parent, boolean modal, Vector2 newVector2,Hashtable newDicOfMachines,boolean newEdit) {
        super(parent, modal);
        vector2 = newVector2;
        dicOfMachines = newDicOfMachines;
        edit = newEdit;
        edit2 = newEdit;
        nombreLargoOld = vector2.getNombreLargo();
        initComponents();
        if(edit){
            this.jTextField3.setText(vector2.getNombreLargo());
            this.jTextField4.setText(vector2.getNombreCorto());
            this.jTextPane4.setText(vector2.getDescripcion());        
        }
    }
    
   /* public DialogPropiedades(java.awt.Frame parent, boolean modal,NodeTreeMachine node,Hashtable newDicOfMachines) {
        super(parent, modal);
        node = node;
        vector2 = node.vector2;
        dicOfMachines = newDicOfMachines;
        initComponents();
        if(node!=null){
            this.jTextField3.setText(vector2.getNombreLargo());
            this.jTextField4.setText(vector2.getNombreCorto());
            this.jTextPane4.setText(vector2.getDescripcion());        
        }
        
        this.nombreLargoOld = new String(vector2.getNombreLargo()); 
        
    }*/
    

    /** 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.
     */
    private void initComponents() {//GEN-BEGIN: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();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextPane4 = new javax.swing.JTextPane();
        
        setTitle("Propiedades");
        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);
        
        jPanel7.setLayout(new java.awt.GridLayout(2, 1));
        
        jPanel2.setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints1;
        
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText("Nombre");
        jLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
        jPanel2.add(jLabel3, gridBagConstraints1);
        
        jTextField3.setColumns(30);
        jTextField3.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        jTextField3.setMaximumSize(new java.awt.Dimension(180, 20));
        jTextField3.setMinimumSize(new java.awt.Dimension(180, 20));
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
        jPanel2.add(jTextField3, gridBagConstraints1);
        
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel4.setText("Nombre Corto");
        jLabel4.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = 1;
        gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
        jPanel2.add(jLabel4, gridBagConstraints1);
        
        jTextField4.setColumns(4);
        jTextField4.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        jTextField4.setMaximumSize(new java.awt.Dimension(50, 20));
        jTextField4.setMinimumSize(new java.awt.Dimension(50, 20));
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 1;
        gridBagConstraints1.gridy = 1;
        gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
        jPanel2.add(jTextField4, gridBagConstraints1);
        
        jPanel7.add(jPanel2);
        
        jScrollPane2.setBorder(new javax.swing.border.TitledBorder(null, "Descripci\u00f3n", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 1, 12)));
        jScrollPane2.setViewportView(jTextPane4);
        
        jPanel7.add(jScrollPane2);
        
        jPanel1.add(jPanel7, java.awt.BorderLayout.CENTER);
        
        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
        
        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setSize(new java.awt.Dimension(300, 300));
        setLocation((screenSize.width-300)/2,(screenSize.height-300)/2);
    }//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:
        String nombreLargo = new String(this.jTextField3.getText().toUpperCase());
        String nombreCorto = new String(this.jTextField4.getText().toUpperCase());
        String descripcion = new String(this.jTextPane4.getText());
        
        nombreLargo = StringUtil.trimLeft(nombreLargo);
        nombreLargo = StringUtil.trimRight(nombreLargo);
        nombreCorto = StringUtil.trimLeft(nombreCorto);
        nombreCorto = StringUtil.trimRight(nombreCorto);
        
        if(nombreLargo.length()==0){
            JOptionPane.showMessageDialog(null, "Debe ingresar un nombre no vacio.","JTV",JOptionPane.WARNING_MESSAGE);
            this.jTextField3.grabFocus();
            return;
        }
        
        if(nombreLargo.length()>20){
            JOptionPane.showMessageDialog(null, "El nombre debe tener un máximo de 20 caracteres.","JTV",JOptionPane.WARNING_MESSAGE);
            this.jTextField3.grabFocus();
            return;
        }
        
        if(nombreCorto.length()==0){
            JOptionPane.showMessageDialog(null, "Debe ingresar un nombre corto no vacio.","JTV",JOptionPane.WARNING_MESSAGE);
            this.jTextField4.grabFocus();
            return;
        }
        
        if(nombreCorto.length()>4){
            JOptionPane.showMessageDialog(null, "El nombre corto debe tener un máximo de 4 caracteres.","JTV",JOptionPane.WARNING_MESSAGE);
            this.jTextField4.grabFocus();
            return;
        }
        
        
        if(dicOfMachines.containsKey(nombreLargo)&&!edit){
            JOptionPane.showMessageDialog(null, "El nombre '"+nombreLargo+"' ya existe 1.","JTV",JOptionPane.WARNING_MESSAGE);
            return;
        }
        
        if(edit&&(!nombreLargo.equals(nombreLargoOld))&&dicOfMachines.containsKey(nombreLargo)){
            JOptionPane.showMessageDialog(null, "El nombre '"+nombreLargo+"' ya existe 2.","JTV",JOptionPane.WARNING_MESSAGE);
            return;
        }
        /*vector2.setNombreLargo(this.jTextField3.getText().toUpperCase());
        vector2.setNombreCorto(this.jTextField4.getText().toUpperCase());
        vector2.setDescripcion(this.jTextPane4.getText());*/
        vector2.setNombreLargo(nombreLargo);
        vector2.setNombreCorto(nombreCorto);
        vector2.setDescripcion(descripcion);
        if(!edit2){
            
            vector2.setPrincipal(false);
            vector2.setMonocinta(false);
        }
        /*else{
            if(nombreLargo.equals(nombreLargoOld)) return;
             //node.refreshUserObject();
             node.refreshChildDependences();
             DialogPropiedades.editingMode=true;
             this.dicOfMachines.remove(this.nombreLargoOld);
             System.out.println("nombreLargoOld"+nombreLargoOld);
             System.out.println("vector2.getNombreLargo()"+vector2.getNombreLargo());
             this.dicOfMachines.put(vector2.getNombreLargo(),vector2);          
             DialogPropiedades.editingMode=false;
        }*/
            
        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 DialogPropiedades(new javax.swing.JFrame(), true,new Vector2(),new Hashtable()).show();
    }

    /** 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;
    }
    
    /** Getter for property nombreLargoOld.
     * @return Value of property nombreLargoOld.
     */
    public java.lang.String getNombreLargoOld() {
        return nombreLargoOld;
    }    
    
    /** Setter for property nombreLargoOld.
     * @param nombreLargoOld New value of property nombreLargoOld.
     */
    public void setNombreLargoOld(java.lang.String nombreLargoOld) {
        this.nombreLargoOld = nombreLargoOld;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextPane jTextPane4;
    // End of variables declaration//GEN-END:variables
    private int returnStatus = RET_CANCEL;
}
