Advertisement
Guest User

Untitled

a guest
Jun 30th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 12.14 KB | None | 0 0
  1. package controllers.validaciones;
  2.  
  3. import components.defaultStyles;
  4.  
  5. import javax.swing.*;
  6. import java.awt.event.FocusAdapter;
  7. import java.awt.event.FocusEvent;
  8. import java.awt.event.KeyAdapter;
  9. import java.awt.event.KeyEvent;
  10.  
  11. public class controllerValidacion {
  12. // ------------------------------ FIELDS ------------------------------
  13.  
  14.     private defaultStyles style;
  15.  
  16. // --------------------------- CONSTRUCTORS ---------------------------
  17.  
  18.     public controllerValidacion() {
  19.         style = new defaultStyles();
  20.     }
  21.  
  22. // -------------------------- OTHER METHODS --------------------------
  23.  
  24.     public boolean esEntero(String cad) {
  25.         boolean comprobacion = false;
  26.         for (int i = 0; i < cad.length(); i++) {
  27.             if (!Character.isDigit(cad.charAt(i))) {
  28.                 comprobacion = true;
  29.             }
  30.         }
  31.  
  32.         return comprobacion;
  33.     }
  34.  
  35.     public boolean esLetra(String cad) {
  36.         boolean comprobacion = false;
  37.         for (int i = 0; i < cad.length(); i++) {
  38.             if (!Character.isLetter(cad.charAt(i))) {
  39.                 comprobacion = true;
  40.             }
  41.         }
  42.  
  43.         return comprobacion;
  44.     }
  45.  
  46.     public void validarFocusSoloLetras(JTextField imput, JLabel alertNotificacion, JLabel lblalertNotificacion, JLabel alertExito, JLabel alertError) {
  47.         imput.addFocusListener(new FocusAdapter() {
  48.             @Override
  49.             public void focusGained(FocusEvent e) {
  50.                 if ((esLetra(imput.getText()) == true)) {
  51.                     lblalertNotificacion.setText("Ingrese solo letras");
  52.                     lblalertNotificacion.setVisible(true);
  53.                     alertNotificacion.setVisible(true);
  54.                     alertError.setVisible(true);
  55.                     alertExito.setVisible(false);
  56.                     imput.setBorder(style.getBorderWarning());
  57.                 } else if (imput.getText().length() == 0) {
  58.                     lblalertNotificacion.setText("Complete este campo");
  59.                     lblalertNotificacion.setVisible(true);
  60.                     alertNotificacion.setVisible(true);
  61.                     alertError.setVisible(true);
  62.                     alertExito.setVisible(false);
  63.                     System.out.println("vacio");
  64.                     imput.setBorder(style.getBorderWarning());
  65.                 } else {
  66.                     lblalertNotificacion.setVisible(false);
  67.                     alertNotificacion.setVisible(false);
  68.                     alertError.setVisible(false);
  69.                     alertExito.setVisible(true);
  70.                     System.out.println("lleno");
  71.                     imput.setBorder(style.getBorderSuccess());
  72.                 }
  73.             }
  74.  
  75.             @Override
  76.             public void focusLost(FocusEvent e) {
  77.                 if ((esLetra(imput.getText()) == true)) {
  78.                     lblalertNotificacion.setText("Ingrese solo Letras");
  79.                     lblalertNotificacion.setVisible(true);
  80.                     alertNotificacion.setVisible(true);
  81.                     alertError.setVisible(true);
  82.                     alertExito.setVisible(false);
  83.                     imput.setBorder(style.getBorderWarning());
  84.                 } else if (imput.getText().length() == 0) {
  85.                     lblalertNotificacion.setText("Complete este campo");
  86.                     lblalertNotificacion.setVisible(true);
  87.                     alertNotificacion.setVisible(true);
  88.                     alertError.setVisible(true);
  89.                     alertExito.setVisible(false);
  90.                     System.out.println("vacio");
  91.                     imput.setBorder(style.getBorderWarning());
  92.                 } else {
  93.                     lblalertNotificacion.setVisible(false);
  94.                     alertNotificacion.setVisible(false);
  95.                     alertError.setVisible(false);
  96.                     alertExito.setVisible(true);
  97.                     System.out.println("lleno");
  98.                     imput.setBorder(style.getBorderSuccess());
  99.                 }
  100.             }
  101.         });
  102.     }
  103.  
  104.     public void validarFocusSoloNumeros(JTextField imput, JLabel alertNotificacion, JLabel lblalertNotificacion, JLabel alertExito, JLabel alertError) {
  105.         imput.addFocusListener(new FocusAdapter() {
  106.             @Override
  107.             public void focusGained(FocusEvent e) {
  108.                 if ((esEntero(imput.getText()) == true)) {
  109.                     lblalertNotificacion.setText("Ingrese solo números");
  110.                     lblalertNotificacion.setVisible(true);
  111.                     alertNotificacion.setVisible(true);
  112.                     alertError.setVisible(true);
  113.                     alertExito.setVisible(false);
  114.                     imput.setBorder(style.getBorderWarning());
  115.                 } else if (imput.getText().length() == 0) {
  116.                     lblalertNotificacion.setText("Complete este campo");
  117.                     lblalertNotificacion.setVisible(true);
  118.                     alertNotificacion.setVisible(true);
  119.                     alertError.setVisible(true);
  120.                     alertExito.setVisible(false);
  121.                     System.out.println("vacio");
  122.                     imput.setBorder(style.getBorderWarning());
  123.                 } else {
  124.                     lblalertNotificacion.setVisible(false);
  125.                     alertNotificacion.setVisible(false);
  126.                     alertError.setVisible(false);
  127.                     alertExito.setVisible(true);
  128.                     System.out.println("lleno");
  129.                     imput.setBorder(style.getBorderSuccess());
  130.                 }
  131.                 validarForma();
  132.             }
  133.  
  134.             @Override
  135.             public void focusLost(FocusEvent e) {
  136.                 if ((esEntero(imput.getText()) == true)) {
  137.                     lblalertNotificacion.setText("Ingrese solo números");
  138.                     lblalertNotificacion.setVisible(true);
  139.                     alertNotificacion.setVisible(true);
  140.                     alertError.setVisible(true);
  141.                     alertExito.setVisible(false);
  142.                     imput.setBorder(style.getBorderWarning());
  143.                 } else if (imput.getText().length() == 0) {
  144.                     lblalertNotificacion.setText("Complete este campo");
  145.                     lblalertNotificacion.setVisible(true);
  146.                     alertNotificacion.setVisible(true);
  147.                     alertError.setVisible(true);
  148.                     alertExito.setVisible(false);
  149.                     System.out.println("vacio");
  150.                     imput.setBorder(style.getBorderWarning());
  151.                 } else {
  152.                     lblalertNotificacion.setVisible(false);
  153.                     alertNotificacion.setVisible(false);
  154.                     alertError.setVisible(false);
  155.                     alertExito.setVisible(true);
  156.                     System.out.println("lleno");
  157.                     imput.setBorder(style.getBorderSuccess());
  158.                 }
  159.                 validarForma();
  160.             }
  161.         });
  162.     }
  163.  
  164.     public void validarForma() {
  165.     }
  166.  
  167.     public void validarSoloLetras(JTextField imput, JLabel alertNotificacion, JLabel lblalertNotificacion, JLabel alertExito, JLabel alertError) {
  168.         imput.addKeyListener(new KeyAdapter() {
  169.                                  @Override
  170.                                  public void keyReleased(KeyEvent e) {
  171.                                      char caracter = e.getKeyChar();
  172.  
  173.                                      if ((esLetra(imput.getText()) == true)) {
  174.                                          lblalertNotificacion.setText("Ingrese solo letras");
  175.                                          lblalertNotificacion.setVisible(true);
  176.                                          alertNotificacion.setVisible(true);
  177.                                          alertError.setVisible(true);
  178.                                          alertExito.setVisible(false);
  179.                                          imput.setBorder(style.getBorderWarning());
  180.                                          System.out.println("no es letra");
  181.                                      } else if ((esLetra(imput.getText()) == false) || (caracter != KeyEvent.VK_BACK_SPACE)) {
  182.                                          if ((imput.getText().equals(""))) {
  183.                                              lblalertNotificacion.setText("Complete este campo");
  184.                                              lblalertNotificacion.setVisible(true);
  185.                                              alertNotificacion.setVisible(true);
  186.                                              alertError.setVisible(true);
  187.                                              alertExito.setVisible(false);
  188.                                              imput.setBorder(style.getBorderWarning());
  189.                                          } else {
  190.                                              lblalertNotificacion.setVisible(false);
  191.                                              alertNotificacion.setVisible(false);
  192.                                              alertError.setVisible(false);
  193.                                              alertExito.setVisible(true);
  194.                                              imput.setBorder(style.getBorderSuccess());
  195.                                          }
  196.                                          System.out.println("es letra");
  197.                                      }
  198.                                  }
  199.                              }
  200.         );
  201.     }
  202.  
  203.     public void validarSoloNumeros(JTextField imput, JLabel alertNotificacion, JLabel lblalertNotificacion, JLabel alertExito, JLabel alertError) {
  204.         imput.addKeyListener(new KeyAdapter() {
  205.                                  @Override
  206.                                  public void keyReleased(KeyEvent e) {
  207.                                      char caracter = e.getKeyChar();
  208.                                      boolean comprobacion = false;
  209.  
  210.                                      if ((esEntero(imput.getText()) == true)) {
  211.                                          lblalertNotificacion.setText("Ingrese solo números");
  212.                                          lblalertNotificacion.setVisible(true);
  213.                                          alertNotificacion.setVisible(true);
  214.                                          alertError.setVisible(true);
  215.                                          alertExito.setVisible(false);
  216.                                          imput.setBorder(style.getBorderWarning());
  217.                                          //  btnAgregar.setEnabled(false);
  218.  
  219.                                          System.out.println("no es numero");
  220.                                      } else if ((esEntero(imput.getText()) == false) || (caracter != KeyEvent.VK_BACK_SPACE)) {
  221.                                          if ((imput.getText().equals(""))) {
  222.                                              lblalertNotificacion.setText("Complete este campo");
  223.                                              lblalertNotificacion.setVisible(true);
  224.                                              alertNotificacion.setVisible(true);
  225.                                              alertError.setVisible(true);
  226.                                              alertExito.setVisible(false);
  227.                                              imput.setBorder(style.getBorderWarning());
  228.                                              // btnAgregar.setEnabled(false);
  229.                                          } else {
  230.                                              lblalertNotificacion.setVisible(false);
  231.                                              alertNotificacion.setVisible(false);
  232.                                              alertError.setVisible(false);
  233.                                              alertExito.setVisible(true);
  234.                                              imput.setBorder(style.getBorderSuccess());
  235.                                              //  btnAgregar.setEnabled(true);
  236.                                          }
  237.                                          System.out.println("es numero");
  238.                                      }
  239.                                      validarForma();
  240.                                  }
  241.                              }
  242.         );
  243.     }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement