Package componenteTabla

Source Code of componenteTabla.ControlFiltro

package componenteTabla;

import java.util.Vector;
import java.util.Date;
import java.util.Enumeration;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import java.text.SimpleDateFormat;
/**
* Title:
* Description:
* Copyright:    Copyright (c)
* Company:
* @author
* @version 1.0
*/

public class ControlFiltro{

  private int[][] condicionCodificada; //tiene la informacion de las condiciones codificada convenientemente
  private Object[] valoresCondicion; //valores de las condiciones

  final int INTEGER = 1;
  final int FLOAT = 2;
  final int DOUBLE = 3;
  final int DATE = 4;
  final int BOOLEAN = 5;
  final int STRING = 6;
  final int LONG = 7;
  final int SHORT =8;

  final int IGUAL = 1; //    =                   1
  final int MAYORIGUAL = 2; //    >=                  2
  final int MENORIGUAL = 3; //    <=                  3
  final int DISTINTO = 4; //    <>                  4
  final int MAYOR = 5; //    >                   5
  final int MENOR = 6; //    <                   6
  final int CONTENIDO = 7; //    <                   6
  final int COMIENZA = 8;
  final int TERMINA = 9;

  public ControlFiltro(){
  }

  // tipos de las columnas
  //    tipo              codificacion
  //  Integer               1
  //  Float                 2
  //  Double                3
  //  Date                  4
  //  Boolean               5
  //  String                6
  //  Long                  7
  // Condiciones posibles
  //  condicion           codificacion
  //    =                   1
  //    >=                  2
  //    <=                  3
  //    <>                  4
  //    >                   5
  //    <                   6

  // Este metodo se encarga de filtrar las filas de una tabla de acuerdo a las
  // condiciones que se le pasan como parametro, tiene en cuenta el tipo de la columna

  private boolean comienza(String contiene, String contenido){
    if (contiene != null){
      return contiene.regionMatches(true, 0, contenido, 0, contenido.length());
    }
    else{
      return false;
    }
  }

  private boolean contenido(String contiene, String contenido){
    boolean b = true;
    if (contiene != null){
      for (int i = 0; i < contiene.length(); i++){
        b = contiene.regionMatches(true, i, contenido, 0, contenido.length());
        if (b){
          return true;
        }
      }
    }
    return false;

  }

  private int[][] tranformaCondicionACondicionCodificada(Object[][] condiciones,
          String[] nombreColumnas, String[] tiposColumnas){
    try{
      if (condiciones != null){
        int[][] retorno = new int[condiciones.length][3];

        for (int i = 0; i < condiciones.length; i++){
          int j = -1;

          do{
            j++;
            //la columna 0 contiene la columna de la condicion
          }
          while ( ( (String) condiciones[i][0]).compareTo(nombreColumnas[j]) !=
                  0);
          retorno[i][0] = j;

          if ( ( (String) tiposColumnas[j]).compareTo("Integer") == 0){
            retorno[i][2] = INTEGER;
          }
          else if ( ( (String) tiposColumnas[j]).compareTo("Float") == 0){
            retorno[i][2] = FLOAT;
          }
          else if ( ( (String) tiposColumnas[j]).compareTo("Double") == 0){
            retorno[i][2] = DOUBLE;
          }
          else if ( ( (String) tiposColumnas[j]).compareTo("Date") == 0){
            retorno[i][2] = DATE;
          }
          else if ( ( (String) tiposColumnas[j]).compareTo("Boolean") == 0){
            retorno[i][2] = BOOLEAN;
          }
          else  if ( ( (String) tiposColumnas[j]).compareTo("String") == 0){
            retorno[i][2] = STRING;
          }
          else if ( ( (String) tiposColumnas[j]).compareTo("Long") == 0){
            retorno[i][2] = LONG;
          }
          else if ( ( (String) tiposColumnas[j]).compareTo("Short") == 0){
            retorno[i][2] = SHORT;
          }

          retorno[i][1] = 0;
          //la collumna 1 contiene la condicion
          if ( ( (String) condiciones[i][1]).compareTo("=") == 0){
            retorno[i][1] = IGUAL;
          }
          else if ( ( (String) condiciones[i][1]).compareTo(">=") == 0){
            retorno[i][1] = MAYORIGUAL;
          }
          else if ( ( (String) condiciones[i][1]).compareTo("<=") == 0){
            retorno[i][1] = MENORIGUAL;
          }
          else if ( ( (String) condiciones[i][1]).compareTo("<>") == 0){
            retorno[i][1] = DISTINTO;
          }
          else if ( ( (String) condiciones[i][1]).compareTo(">") == 0){
            retorno[i][1] = MAYOR;
          }
          else if ( ( (String) condiciones[i][1]).compareTo("<") == 0){
            retorno[i][1] = MENOR;
          }
          else if ( ( (String) condiciones[i][1]).compareTo("contenga") == 0){
            retorno[i][1] = CONTENIDO;
          }
          else if ( ( (String) condiciones[i][1]).compareTo("comienza") == 0){
            retorno[i][1] = COMIENZA;
          }
          else if ( ( (String) condiciones[i][1]).compareTo("termina") == 0){
            retorno[i][1] = TERMINA;

          }
          else if (retorno[i][1] == 0){ //no operador seleecionado en la condicion
            throw new Exception();
          }
        } //end for
        return retorno;
      } //end if condiciones nulas
      return null;
    }
    catch (Exception e){
      mensageError("Hubo un error en la condici�n del filtro");
      e.printStackTrace();
      return null;
    }
  }

  private Object[] crearValoresCondicion(Object[][] condiciones,
          String[] tipoColumnas){
    try{
      Object[] retorno = new Object[condiciones.length];

      for (int i = 0; i < condiciones.length; i++){
        //columna 2 tiene el tipo de la condicion
        if (this.condicionCodificada[i][2] == INTEGER){
          retorno[i] = new Integer(Integer.parseInt( (String) condiciones[i][
                       2]));
        }
        else if (this.condicionCodificada[i][2] == FLOAT){
          retorno[i] = new Float(Float.parseFloat( (String) condiciones[i][2]));
        }
        else if (this.condicionCodificada[i][2] == DOUBLE){
          retorno[i] = new Double(Double.parseDouble( (String) condiciones[i][
                       2]));
        }
        else if (this.condicionCodificada[i][2] == DATE){
          SimpleDateFormat f=null ;
          if(((String) condiciones[i][2]).indexOf(":")!=-1 &&
      ((String) condiciones[i][2]).indexOf("/")!=-1)
            f = new SimpleDateFormat("d-M-yyyy");
          else
            if(((String) condiciones[i][2]).indexOf(":")==-1 &&
        ((String) condiciones[i][2]).indexOf("/")!=-1)
            f = new SimpleDateFormat("dd/MM/yyyy");
            else
  if(((String) condiciones[i][2]).indexOf(":")!=-1 &&
          ((String) condiciones[i][2]).indexOf("/")==-1)
                f = new SimpleDateFormat("HH:mm");
          retorno[i] = f.parse((String) condiciones[i][2]);
        }
        else if (this.condicionCodificada[i][2] == BOOLEAN){
          retorno[i] = Boolean.valueOf( (String) condiciones[i][2]);
        }
        else if (this.condicionCodificada[i][2] == STRING){
          retorno[i] = ( (String) condiciones[i][2]);
        }
       else if (this.condicionCodificada[i][2] == LONG){
          retorno[i] = new Long(Long.parseLong( (String) condiciones[i][2]));
        }
       else if (this.condicionCodificada[i][2] == SHORT){
          retorno[i] = new Short(Short.parseShort( (String) condiciones[i][2]));
       }
      }
      return retorno;
    }
    catch (Exception e){
      if (this.condicionCodificada != null){ //no hubo problemas con la condicion
        mensageError("Hubo un error en los valores de la condici�n");
      }
      e.printStackTrace();
      return null;
    }
  }

  private void mensageError(String mensage){
    JOptionPane pane = new JOptionPane();
    Object[] options = {"Aceptar"};
    pane.setOptions(options);
    pane.setOptionType(0);
    pane.setMessageType(JOptionPane.ERROR_MESSAGE);
    pane.setMessage(mensage);
    pane.setSize(260, 260);
    JDialog dialog = pane.createDialog(null, "Error");
    dialog.show();

  }

  private boolean cumpleCondicion(Object[] datos,
          int[][] condicionCodificada,
          Object[] valoresCondicion) throws Exception
  //  {
  //   try  comento el try paraque la excepcion la levante el metodo filtrar
  {
    boolean retorno = true;

    /*  final  int INTEGER=1;
     final  int FLOAT=2;
     final  int DOUBLE=3;
     final  int DATE=4;
     final  int BOOLEAN=5;
     final  int STRING=6;
     final int LONG=7;
     final  int IGUAL=1;//    =                   1
     final  int MAYORIGUAL=2;//    >=                  2
     final  int MENORIGUAL=3;//    <=                  3
     final  int DISTINTO=4;//    <>                  4
     final  int MAYOR=5;//    >                   5
     final  int MENOR=6;//    <                   6
     */
    for (int i = 0; i < condicionCodificada.length; i++){ //para cada condicion
      int columnaCondicion = condicionCodificada[i][0];

      switch (condicionCodificada[i][1]){ //columna de la condicion
        case IGUAL:
          switch (condicionCodificada[i][2]){ //columna del TIPO DE LA COLUMNA

            case INTEGER:
              if (datos[columnaCondicion] != null){
                if ( ( (Integer) datos[columnaCondicion]).compareTo((Integer)
                        valoresCondicion[i]) != 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case FLOAT:
              if (datos[columnaCondicion] != null){
                if ( ( (Float) datos[columnaCondicion]).compareTo((Float)
                        valoresCondicion[i]) != 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case DOUBLE:
              if (datos[columnaCondicion] != null){
                if ( ( (Double) datos[columnaCondicion]).compareTo(
                        (Double)valoresCondicion[i]) != 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case DATE:
              if (datos[columnaCondicion] != null){
    SimpleDateFormat f=null;
    try{
      //si es date
                if ( ( (Date) datos[columnaCondicion]).compareTo(
                        valoresCondicion[i]) != 0){
                  return false;
                }
                break;

    }
    catch (Exception ex){
      // si no es date sino string
      if(( (String) datos[columnaCondicion]).indexOf("/")!=-1 &&
       ( (String) datos[columnaCondicion]).indexOf(".")==-1)
            f = new SimpleDateFormat("dd/MM/yyyy");
      else
        if(( (String) datos[columnaCondicion]).indexOf("/")!=-1 &&
        ( (String) datos[columnaCondicion]).indexOf(".")!=-1)
             f = new SimpleDateFormat("dd/MM/yyyy HH:mm");
                   else
         if(( (String) datos[columnaCondicion]).indexOf("/")==-1 &&
                        ( (String) datos[columnaCondicion]).indexOf(".")!=-1)
             f = new SimpleDateFormat("HH:mm");
      Date d1 = f.parse( (String) datos[columnaCondicion]);
      if ( d1.compareTo(valoresCondicion[i]) != 0){
        return false;
      }
      break;

    }
              }
              else{
                return false;
              }

            case BOOLEAN:
              if (datos[columnaCondicion] != null){
                if ( ( (Boolean) datos[columnaCondicion]).booleanValue() !=
                        ( (Boolean) valoresCondicion[i]).booleanValue()){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case LONG:
              if (datos[columnaCondicion] != null){
                if ( ( (Long) datos[columnaCondicion]).compareTo(
                        valoresCondicion[i]) != 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case SHORT:
  if (datos[columnaCondicion] != null){
    if ( ( (Short) datos[columnaCondicion]).compareTo(
            valoresCondicion[i]) != 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case STRING:
              if (datos[columnaCondicion] != null){
                if ( ( (String) datos[columnaCondicion]).compareTo(
                        (String)valoresCondicion[i]) != 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }
          }
          break; //FIN SWITCH  TIPO DE COLUMNA

        case MAYORIGUAL:
          switch (condicionCodificada[i][2]){ //columna del TIPO DE LA COLUMNA

            case INTEGER:
              if (datos[columnaCondicion] != null){
                if ( ( (Integer) datos[columnaCondicion]).compareTo(
                        valoresCondicion[i]) < 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case FLOAT:
              if (datos[columnaCondicion] != null){
                if ( ( (Float) datos[columnaCondicion]).compareTo(
                        valoresCondicion[i]) < 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case DOUBLE:
              if (datos[columnaCondicion] != null){
                if ( ( (Double) datos[columnaCondicion]).compareTo(
                        valoresCondicion[i]) < 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case LONG:
              if (datos[columnaCondicion] != null){
                if ( ( (Long) datos[columnaCondicion]).compareTo(
                        valoresCondicion[i]) < 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case SHORT:
    if (datos[columnaCondicion] != null){
      if ( ( (Short) datos[columnaCondicion]).compareTo(
              valoresCondicion[i]) < 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case DATE:
              if (datos[columnaCondicion] != null){
    SimpleDateFormat f = null;
    try{
      //si es date
                if ( ( (Date) datos[columnaCondicion]).compareTo(
                        valoresCondicion[i]) < 0){
                  return false;
                }
                break;

    }
    catch (Exception ex){
      // si no es date sino string
      if(( (String) datos[columnaCondicion]).indexOf("/")!=-1 &&
       ( (String) datos[columnaCondicion]).indexOf(".")==-1)
            f = new SimpleDateFormat("dd/MM/yyyy");
      else
        if(( (String) datos[columnaCondicion]).indexOf("/")!=-1 &&
        ( (String) datos[columnaCondicion]).indexOf(".")!=-1)
             f = new SimpleDateFormat("dd/MM/yyyy HH:mm");
       else
         if(( (String) datos[columnaCondicion]).indexOf("/")==-1 &&
            ( (String) datos[columnaCondicion]).indexOf(".")!=-1)
             f = new SimpleDateFormat("HH:mm");

      Date d1 = f.parse( (String) datos[columnaCondicion]);
      if (d1.compareTo(valoresCondicion[i]) < 0){
        return false;
      }
      break;
    }
              }
              else{
                return false;
              }

            case BOOLEAN:
              throw new Exception();

            case STRING:
              if (datos[columnaCondicion] != null){
                if ( ( (String) datos[columnaCondicion]).compareTo(
                        (String)valoresCondicion[i]) < 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }
          }
          break; //FIN SWITCH  TIPO DE COLUMNA

        case MENORIGUAL:
          switch (condicionCodificada[i][2]){ //columna del TIPO DE LA COLUMNA
            case INTEGER:
              if (datos[columnaCondicion] != null){
                if ( ( (Integer) datos[columnaCondicion]).compareTo(
                        (Integer)valoresCondicion[i]) > 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case FLOAT:
              if (datos[columnaCondicion] != null){
                if ( ( (Float) datos[columnaCondicion]).compareTo(
                        (Float)valoresCondicion[i]) > 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case DOUBLE:
              if (datos[columnaCondicion] != null){
                if ( ( (Double) datos[columnaCondicion]).compareTo(
                        (Double)valoresCondicion[i]) > 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case LONG:
              if (datos[columnaCondicion] != null){
                if ( ( (Long) datos[columnaCondicion]).compareTo(
                        (Long)valoresCondicion[i]) > 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case SHORT:
  if (datos[columnaCondicion] != null){
    if ( ( (Short) datos[columnaCondicion]).compareTo(
            valoresCondicion[i]) > 0){
      return false;
    }
    break;
  }
  else{
    return false;
  }

            case DATE:
              if (datos[columnaCondicion] != null && valoresCondicion!=null){
    SimpleDateFormat f = null;
    try{
      //si es date
                if ( ( (Date) datos[columnaCondicion]).compareTo(
                        valoresCondicion[i]) > 0){
                  return false;
                }
                break;
              }
    catch (Exception ex){
      // si no es date sino string
      if(( (String) datos[columnaCondicion]).indexOf("/")!=-1 &&
       ( (String) datos[columnaCondicion]).indexOf(".")==-1)
            f = new SimpleDateFormat("dd/MM/yyyy");
      else
        if(( (String) datos[columnaCondicion]).indexOf("/")!=-1 &&
        ( (String) datos[columnaCondicion]).indexOf(".")!=-1)
             f = new SimpleDateFormat("dd/MM/yyyy HH:mm");
       else
         if(( (String) datos[columnaCondicion]).indexOf("/")==-1 &&
            ( (String) datos[columnaCondicion]).indexOf(".")!=-1)
             f = new SimpleDateFormat("HH:mm");

      Date d1 = f.parse( (String) datos[columnaCondicion]);
      if (d1.compareTo(valoresCondicion[i]) > 0){
        return false;
      }
      break;

    }
  }
              else{
                return false;
              }

            case BOOLEAN:
              throw new Exception();

            case STRING:
              if (datos[columnaCondicion] != null){
                if ( ( (String) datos[columnaCondicion]).compareTo(
                       (String) valoresCondicion[i]) > 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }
          }

          break; //FIN SWITCH  TIPO DE COLUMNA

        case DISTINTO:
          switch (condicionCodificada[i][2]){ //columna del TIPO DE LA COLUMNA
            case INTEGER:
              if (datos[columnaCondicion] != null){
                if ( ( (Integer) datos[columnaCondicion]).compareTo(
                        (Integer)valoresCondicion[i]) == 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case FLOAT:
              if (datos[columnaCondicion] != null){
                if ( ( (Float) datos[columnaCondicion]).compareTo(
                       (Float) valoresCondicion[i]) == 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case DOUBLE:
              if (datos[columnaCondicion] != null){
                if ( ( (Double) datos[columnaCondicion]).compareTo(
                       (Double) valoresCondicion[i]) == 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case LONG:
              if (datos[columnaCondicion] != null){
                if ( ( (Long) datos[columnaCondicion]).compareTo(
                        (Long)valoresCondicion[i]) == 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case SHORT:
  if (datos[columnaCondicion] != null){
    if ( ( (Short) datos[columnaCondicion]).compareTo(
            valoresCondicion[i]) == 0){
      return false;
    }
    break;
  }
  else{
    return false;
  }

            case DATE:
              if (datos[columnaCondicion] != null){
    SimpleDateFormat f = null;
    try{
      //si es date
                  if ( ( (Date) datos[columnaCondicion]).compareTo(
                          valoresCondicion[i]) == 0){
                    return false;
                  }
      break;
                }
    catch (Exception ex){
      // si no es date sino string
      if(( (String) datos[columnaCondicion]).indexOf("/")!=-1 &&
       ( (String) datos[columnaCondicion]).indexOf(".")==-1)
            f = new SimpleDateFormat("dd/MM/yyyy");
      else
        if(( (String) datos[columnaCondicion]).indexOf("/")!=-1 &&
        ( (String) datos[columnaCondicion]).indexOf(".")!=-1)
             f = new SimpleDateFormat("dd/MM/yyyy HH:mm");
       else
         if(( (String) datos[columnaCondicion]).indexOf("/")==-1 &&
            ( (String) datos[columnaCondicion]).indexOf(".")!=-1)
             f = new SimpleDateFormat("HH:mm");

      Date d1 = f.parse( (String) datos[columnaCondicion]);
      if (d1.compareTo(valoresCondicion[i]) == 0){
                  return false;
                }
                break;
              }
             }
              else{
                return false;
              }

            case BOOLEAN:
              if (datos[columnaCondicion] != null){
                if ( ( (Boolean) datos[columnaCondicion]).booleanValue() ==
                        ( (Boolean) valoresCondicion[i]).booleanValue()){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case STRING:
              if (datos[columnaCondicion] != null){
                if ( ( (String) datos[columnaCondicion]).compareTo(
                       (String) valoresCondicion[i]) == 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }
          }
          break; //FIN SWITCH  TIPO DE COLUMNA

        case MAYOR:
          switch (condicionCodificada[i][2]){ //columna del TIPO DE LA COLUMNA
            case INTEGER:
              if (datos[columnaCondicion] != null){
                if ( ( (Integer) datos[columnaCondicion]).compareTo(
                        (Integer)valoresCondicion[i]) <= 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case FLOAT:
              if (datos[columnaCondicion] != null){
                if ( ( (Float) datos[columnaCondicion]).compareTo(
                       (Float) valoresCondicion[i]) <= 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case DOUBLE:
              if (datos[columnaCondicion] != null){
                if ( ( (Double) datos[columnaCondicion]).compareTo(
                        (Double)valoresCondicion[i]) <= 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case LONG:
              if (datos[columnaCondicion] != null){
                if ( ( (Long) datos[columnaCondicion]).compareTo(
                      (LongvaloresCondicion[i]) <= 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case SHORT:
  if (datos[columnaCondicion] != null){
    if ( ( (Short) datos[columnaCondicion]).compareTo(
            valoresCondicion[i]) <= 0){
      return false;
    }
    break;
  }
  else{
    return false;
  }

            case DATE:
              if (datos[columnaCondicion] != null){
    SimpleDateFormat f = null;
    try{
      //si es date
                if ( ( (Date) datos[columnaCondicion]).compareTo(
                        valoresCondicion[i]) <= 0){
                  return false;
                }
                break;
              }
    catch (Exception ex){
      // si no es date sino string
      if(( (String) datos[columnaCondicion]).indexOf("/")!=-1 &&
       ( (String) datos[columnaCondicion]).indexOf(".")==-1)
            f = new SimpleDateFormat("dd/MM/yyyy");
      else
        if(( (String) datos[columnaCondicion]).indexOf("/")!=-1 &&
        ( (String) datos[columnaCondicion]).indexOf(".")!=-1)
             f = new SimpleDateFormat("dd/MM/yyyy HH:mm");
       else
         if(( (String) datos[columnaCondicion]).indexOf("/")==-1 &&
            ( (String) datos[columnaCondicion]).indexOf(".")!=-1)
             f = new SimpleDateFormat("HH:mm");

      Date d1 = f.parse( (String) datos[columnaCondicion]);
      if (d1.compareTo(valoresCondicion[i]) <= 0){
        return false;
      }
      break;
    }
              }
              else{
                return false;
              }

            case BOOLEAN:
              throw new Exception();

            case STRING:
              if (datos[columnaCondicion] != null){
                if ( ( (String) datos[columnaCondicion]).compareTo(
                       (String) valoresCondicion[i]) <= 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }
          }
          break; //FIN SWITCH  TIPO DE COLUMNA

        case MENOR:
          switch (condicionCodificada[i][2]){ //columna del TIPO DE LA COLUMNA
            case INTEGER:
              if (datos[columnaCondicion] != null){
                if ( ( (Integer) datos[columnaCondicion]).compareTo(
                        (Integer)valoresCondicion[i]) >= 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case FLOAT:
              if (datos[columnaCondicion] != null){
                if ( ( (Float) datos[columnaCondicion]).compareTo(
                      (FloatvaloresCondicion[i]) >= 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case DOUBLE:
              if (datos[columnaCondicion] != null){
                if ( ( (Double) datos[columnaCondicion]).compareTo(
                       (Double) valoresCondicion[i]) >= 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case LONG:
              if (datos[columnaCondicion] != null){
                if ( ( (Long) datos[columnaCondicion]).compareTo(
                        (Long)valoresCondicion[i]) >= 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }

            case SHORT:
  if (datos[columnaCondicion] != null){
    if ( ( (Short) datos[columnaCondicion]).compareTo(
            valoresCondicion[i]) >= 0){
      return false;
    }
    break;
  }
  else{
    return false;
  }

            case DATE:
              if (datos[columnaCondicion] != null){
    SimpleDateFormat f = null;
    try{
      //si es date
                if ( ( (Date) datos[columnaCondicion]).compareTo(
                        valoresCondicion[i]) >= 0){
                  return false;
                }
                break;
              }
    catch (Exception ex){
      // si no es date sino string
      if(( (String) datos[columnaCondicion]).indexOf("/")!=-1 &&
       ( (String) datos[columnaCondicion]).indexOf(".")==-1)
            f = new SimpleDateFormat("dd/MM/yyyy");
      else
        if(( (String) datos[columnaCondicion]).indexOf("/")!=-1 &&
        ( (String) datos[columnaCondicion]).indexOf(".")!=-1)
             f = new SimpleDateFormat("dd/MM/yyyy HH:mm");
       else
         if(( (String) datos[columnaCondicion]).indexOf("/")==-1 &&
            ( (String) datos[columnaCondicion]).indexOf(".")!=-1)
             f = new SimpleDateFormat("HH:mm");

      Date d1 = f.parse( (String) datos[columnaCondicion]);
      if (d1.compareTo(valoresCondicion[i]) >= 0){
        return false;
      }
      break;
    }
              }
              else{
                return false;
              }

            case BOOLEAN:
              throw new Exception();

            case STRING:
              if (datos[columnaCondicion] != null){
                if ( ( (String) datos[columnaCondicion]).compareTo(
                      (StringvaloresCondicion[i]) >= 0){
                  return false;
                }
                break;
              }
              else{
                return false;
              }
          }
          break; //FIN SWITCH  TIPO DE COLUMNA

        case CONTENIDO:
          switch (condicionCodificada[i][2]){ //columna del TIPO DE LA COLUMNA
            case INTEGER:
              throw new Exception();

            case FLOAT:
              throw new Exception();

            case DOUBLE:
              throw new Exception();

            case LONG:
              throw new Exception();

            case SHORT:
  throw new Exception();

            case DATE:
              throw new Exception();

            case BOOLEAN:
              throw new Exception();

            case STRING:
              if (!contenido( (String) datos[columnaCondicion],
                      (String) valoresCondicion[i])){
                return false;
              }
              break;

          }
          break; //FIN SWITCH  TIPO DE COLUMNA

        case COMIENZA:
          switch (condicionCodificada[i][2]){ //columna del TIPO DE LA COLUMNA
            case INTEGER:
              throw new Exception();

            case FLOAT:
              throw new Exception();

            case DOUBLE:
              throw new Exception();

            case LONG:
              throw new Exception();

            case SHORT:
  throw new Exception();

            case DATE:
              throw new Exception();

            case BOOLEAN:
              throw new Exception();

            case STRING:
              if (!comienza( (String) datos[columnaCondicion],
                      (String) valoresCondicion[i])){
                return false;
              }
              break;
          }
          break; //FIN SWITCH  TIPO DE COLUMNA

        case TERMINA:
          switch (condicionCodificada[i][2]){ //columna del TIPO DE LA COLUMNA
            case INTEGER:
              throw new Exception();

            case FLOAT:
              throw new Exception();

            case DOUBLE:
              throw new Exception();

            case LONG:
              throw new Exception();

            case SHORT:
  throw new Exception();

            case DATE:
              throw new Exception();

            case BOOLEAN:
              throw new Exception();

            case STRING:
              if (datos[columnaCondicion] != null){
                if (! ( (String) datos[columnaCondicion]).endsWith( (String)
                        valoresCondicion[i])){
                  return false;
                }
                break;
              }
              else{
                return false;
              }
          }
          break; //FIN SWITCH  TIPO DE COLUMNA

      } // Fin SWITCH  condicion
    }

    return true;

    /*    }
     catch (Exception ee)
     {
     mensageError("Hubo un error en la comparacion de valores");
     ee.printStackTrace();
     return false;
     }*/
  }

  public Object[][] filtrar(Object[][] datos, String[] tiposColumnas,
          String[] nombreColumnas, Object[][] condiciones){
    try{
      Vector resultado = new Vector();

      this.setCondicionCodificada(tranformaCondicionACondicionCodificada(
              condiciones, nombreColumnas, tiposColumnas));
      this.setValoresCondicion(crearValoresCondicion(condiciones,
              tiposColumnas));

      int w = this.getCondicionCodificada().length; //si es null es por que hubo un error en la transformacion de la condicion salta una execepcion

      //si es null es por que hubo un error en la transformacion de los valores de la condicion salta una execepcion
      Object[] s = this.getValoresCondicion();
      if (s != null){
        w = s.length;
      }
      else{
        w = 0;

      }
      if(datos!=null){
        for (int i = 0; i < datos.length; i++){ //por cada fila de datos
          if (cumpleCondicion(datos[i], condicionCodificada, valoresCondicion)){
            resultado.addElement( (Object[]) datos[i]);
          }
        }

      Object[][] datosRetorno = new Object[resultado.size()][datos[0].
                                length];
      Enumeration e = resultado.elements();
      int i = 0;

      while (e.hasMoreElements()){
        datosRetorno[i] = (Object[]) e.nextElement();
        i++;
      }
      if (resultado.size() == 0){
        return new Object[0][0];
      }
      else{
        return datosRetorno;
      }
    }
    return null;
    }
    catch (Exception e){
      if ( (this.getCondicionCodificada() != null) &&
              (this.getValoresCondicion() != null)){
        mensageError("Hubo un error en el filtro, la condicion seleccionada no es compatible con el tipo de la columna");

      }
      e.printStackTrace();
      return null;
    }

  }

  private void setCondicionCodificada(int[][] newCondicionCodificada){
    condicionCodificada = newCondicionCodificada;
  }

  private int[][] getCondicionCodificada(){
    return condicionCodificada;
  }

  private void setValoresCondicion(Object[] newValoresCondicion){
    valoresCondicion = newValoresCondicion;
  }

  private Object[] getValoresCondicion(){
    return valoresCondicion;
  }

}
TOP

Related Classes of componenteTabla.ControlFiltro

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.