Exemplo n.º 1
1
  public void filterBase(String ft) {
    if (ft.contains("-i")) {
      RowFilter<Storage, Object> tmpFilter = null;
      try {
        tmpFilter = RowFilter.regexFilter("^new$", Storage.COL_TAGS);
      } catch (Exception e) {
        System.err.println("Failed to filter new ones");
      }
      tableSorter.setRowFilter(tmpFilter);
      return;
    }

    boolean names = false;
    boolean bads = false;
    boolean exclusive = true;
    if (ft.contains("-n")) {
      names = true;
      ft = ft.substring(0, ft.indexOf("-n")) + ft.substring(ft.indexOf("-n") + 2);
    }
    if (ft.contains("-z")) {
      bads = true;
      ft = ft.substring(0, ft.indexOf("-z")) + ft.substring(ft.indexOf("-z") + 2);
    }
    if (ft.contains("-x")) {
      exclusive = false;
      ft = ft.substring(0, ft.indexOf("-x")) + ft.substring(ft.indexOf("-x") + 2);
    }

    ft = ft.replaceAll("[^a-zA-Z0-9]", " ").replaceAll("  *", " ").trim().toLowerCase();
    String[] terms = ft.split(" ");
    ArrayList<RowFilter<Storage, Object>> termFilters = new ArrayList<RowFilter<Storage, Object>>();
    for (int i = 0; i < terms.length; i++) {
      if (terms[i].length() > 0) {
        try {
          RowFilter<Storage, Object> tmp =
              RowFilter.regexFilter(".*" + terms[i] + ".*", Storage.COL_TAGS);
          if (names) tmp = RowFilter.regexFilter(".*" + terms[i] + ".*", Storage.COL_NAME);
          termFilters.add(tmp);
        } catch (Exception e) {
          // do nothing
          System.err.println("Term filter error for term " + terms[i]);
        }
      }
    }

    RowFilter<Storage, Object> badFilter = RowFilter.regexFilter(".*zz.*", Storage.COL_TAGS);
    if (!bads) badFilter = RowFilter.notFilter(badFilter);

    RowFilter<Storage, Object> omniFilter = badFilter;
    if (termFilters.size() != 0) {
      ArrayList<RowFilter<Storage, Object>> tmpFilters =
          new ArrayList<RowFilter<Storage, Object>>();
      RowFilter<Storage, Object> orFilter =
          (exclusive ? RowFilter.andFilter(termFilters) : RowFilter.orFilter(termFilters));
      tmpFilters.add(orFilter);
      tmpFilters.add(badFilter);
      omniFilter = RowFilter.andFilter(tmpFilters);
    }

    tableSorter.setRowFilter(omniFilter);
  }
Exemplo n.º 2
0
  private void jBPesquisarActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jBPesquisarActionPerformed
    // TODO add your handling code here:

    String text = jTFPesquisar.getText();

    if (text.length() == 0) {
      sorter.setRowFilter(null);
    } else {
      try {
        sorter.setRowFilter(RowFilter.regexFilter(text));
      } catch (PatternSyntaxException pse) {
        JOptionPane.showMessageDialog(null, "Bad regex pattern");

      } catch (RuntimeException ex2) {
        JOptionPane.showMessageDialog(null, "Erro: " + ex2.getMessage());
      }
    }
  } // GEN-LAST:event_jBPesquisarActionPerformed
  /** Filters table by name, by code, by price. */
  private void applyFilters() {
    List<RowFilter<DiscountCardsTableModel, Integer>> andFilters = new ArrayList<>();
    for (final FilterUnit filterUnit : filters) {
      final int columnIndex = tableModel.findColumn(filterUnit.columnTitle);

      RowFilter<DiscountCardsTableModel, Integer> filter = null;
      if (FilterType.PART_TEXT == filterUnit.filterType) {
        filter =
            RowFilter.regexFilter(("(?iu)" + filterUnit.textField.getText().trim()), columnIndex);
      } else if (FilterType.FULL_TEXT == filterUnit.filterType) {
        filter = RowFilter.regexFilter(filterUnit.textField.getText().trim(), columnIndex);
      } else if (FilterType.NUMBER == filterUnit.filterType
          || FilterType.NUMBER_DIAPASON == filterUnit.filterType
              && !filterUnit.textField.getText().contains("-")) {
        if (filterUnit.textField.getText().length() > 0) {
          String numberStr =
              filterUnit.textField.getText().trim().replace(",", ".").replaceAll("[^0-9.]", "");
          Double number = Double.parseDouble(numberStr);
          filter = RowFilter.numberFilter(RowFilter.ComparisonType.EQUAL, number, columnIndex);
        }
      } else if (FilterType.NUMBER_DIAPASON == filterUnit.filterType) {
        if (filterUnit.textField.getText().length() > 0) {
          String[] numbers = filterUnit.textField.getText().split("-", 2);
          if (numbers.length > 1 && !numbers[0].trim().isEmpty() && !numbers[1].trim().isEmpty()) {
            String numbers0 = numbers[0].replace(",", ".").replaceAll("[^0-9.]", "");
            String numbers1 = numbers[1].replace(",", ".").replaceAll("[^0-9.]", "");
            final Double number1 = Double.parseDouble(numbers0);
            final Double number2 = Double.parseDouble(numbers1);
            filter =
                new RowFilter<DiscountCardsTableModel, Integer>() {
                  @Override
                  public boolean include(
                      Entry<? extends DiscountCardsTableModel, ? extends Integer> entry) {
                    Double number =
                        (Double) tableModel.getRawValueAt(entry.getIdentifier(), columnIndex);
                    return number > number1 && number < number2;
                  }
                };
          }
        }
      } else if (FilterType.NUMBER_MORE == filterUnit.filterType) {
        if (filterUnit.textField.getText().length() > 0) {
          String numberStr =
              filterUnit.textField.getText().trim().replace(",", ".").replaceAll("[^0-9.]", "");
          final Double number = Double.parseDouble(numberStr);
          filter =
              new RowFilter<DiscountCardsTableModel, Integer>() {
                @Override
                public boolean include(
                    Entry<? extends DiscountCardsTableModel, ? extends Integer> entry) {
                  Double numberEntry =
                      (Double) tableModel.getRawValueAt(entry.getIdentifier(), columnIndex);
                  return numberEntry >= number;
                }
              };
        }

      } else if (FilterType.DATE == filterUnit.filterType
          || FilterType.DATE_DIAPASON == filterUnit.filterType
              && !filterUnit.textField.getText().contains("-")) {
        if (filterUnit.textField.getText().length() > 0) {
          final Date correctedDate = getCorrectedDate(filterUnit.textField.getText().trim());
          filter =
              new RowFilter<DiscountCardsTableModel, Integer>() {
                @Override
                public boolean include(
                    Entry<? extends DiscountCardsTableModel, ? extends Integer> entry) {
                  Object dateObject = tableModel.getRawValueAt(entry.getIdentifier(), columnIndex);
                  if (dateObject != null) {
                    Date date;
                    if (dateObject instanceof Date) {
                      date = (Date) dateObject;
                    } else if (dateObject instanceof Calendar) {
                      date = ((Calendar) dateObject).getTime();
                    } else {
                      throw new RuntimeException(
                          "Unsupported class for DATE filterType! " + dateObject.getClass());
                    }
                    return date.after(addDays(correctedDate, -1))
                        && date.before(addDays(correctedDate, 1));
                  } else {
                    return false;
                  }
                }
              };
        }
      } else if (FilterType.DATE_DIAPASON == filterUnit.filterType) {
        if (filterUnit.textField.getText().length() > 0) {
          String[] dates = filterUnit.textField.getText().split("-", 2);
          if (dates.length > 1 && !dates[0].trim().isEmpty() && !dates[1].trim().isEmpty()) {
            final Date correctedDate1 = getCorrectedDate(dates[0]);
            final Date correctedDate2 = getCorrectedDate(dates[1]);
            filter =
                new RowFilter<DiscountCardsTableModel, Integer>() {
                  @Override
                  public boolean include(
                      Entry<? extends DiscountCardsTableModel, ? extends Integer> entry) {
                    Object saleDateObj =
                        tableModel.getRawValueAt(entry.getIdentifier(), columnIndex);
                    if (saleDateObj != null) {
                      Date date =
                          ((Calendar) tableModel.getRawValueAt(entry.getIdentifier(), columnIndex))
                              .getTime();
                      return date.after(addDays(correctedDate1, -1))
                          && date.before(addDays(correctedDate2, 1));
                    } else {
                      return false;
                    }
                  }
                };
          }
        }
      } else if (FilterType.CHECKBOXES == filterUnit.filterType) {
        if (anyDeselected(filterUnit.checkBoxes)) {
          filter =
              new RowFilter<DiscountCardsTableModel, Integer>() {
                @Override
                public boolean include(
                    Entry<? extends DiscountCardsTableModel, ? extends Integer> entry) {
                  Object columnObject =
                      tableModel.getRawValueAt(entry.getIdentifier(), columnIndex);
                  return correspondsToCheckBoxes(filterUnit.checkBoxes, columnObject);
                }
              };
        }
      }
      if (filter != null) {
        andFilters.add(filter);
      }
      sorter.setRowFilter(RowFilter.andFilter(andFilters));
    }
  }
Exemplo n.º 4
-1
  private void jBPesquisarKeyTyped(
      java.awt.event.KeyEvent evt) { // GEN-FIRST:event_jBPesquisarKeyTyped

    if (evt.getKeyChar() == KeyEvent.VK_ENTER) {
      if (evt.getSource() == jBPesquisar) {

        String text = jTFPesquisar.getText();

        if (text.length() == 0) {
          sorter.setRowFilter(null);
        } else {
          try {
            sorter.setRowFilter(RowFilter.regexFilter(text));
            int total = calculaTotalPecas(text);
            jLabelTotalPecas.setText(String.valueOf(total));
          } catch (PatternSyntaxException pse) {
            JOptionPane.showMessageDialog(this, "Erro na pesquisa");
          } catch (RuntimeException ex2) {
            JOptionPane.showMessageDialog(null, "Erro: " + ex2.getMessage());
          }
        }
      }
    }
    tabela.grabFocus();
  } // GEN-LAST:event_jBPesquisarKeyTyped