<commit_before>/* Copyright (C) 2003-2019 <NAME>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  You may obtain a copy of the License at
 *
 *     https://www.gnu.org/licenses/agpl-3.0.txt
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 */
package bzh.plealog.bioinfo.ui.util;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;

import com.plealog.genericapp.api.EZEnvironment;

/**
 * This is a utility class aims at providing a search component. It can be used
 * to hightlight rows containing a keyword. By default the component scans the
 * entire table, but the method setColumnsToScan() can be used to set the list
 * of columns to scan.
 * 
 * @author <NAME>
 */
@SuppressWarnings("serial")
public class TableSearcherComponent extends JPanel
    implements TableSearcherComponentAPI {

  private JButton                                 _searchNext;
  private JButton                                 _searchPrev;
  private int                                     _lastMatchRow            = -1;
  private String                                  _helper                  = "Enter a pattern to search for";
  private SearchField                             _sf;
  private JTable                                  _table;
  private int[]                                   _colsIdx;
  private int[]                                   _indices;
  private Pattern                                 _pattern;
  private ArrayList<TableSearcherComponentAction> _userActions;
  private boolean                                 _searching;

  private static final Color                      LOCATE_NO_MATCH_BG_COLOR = new Color(
      255, 102, 102);
  private static final Color                      LOCATE_NO_MATCH_FG_COLOR = Color.white;
  private static final Color                      LOCATE_MATCH_FG_COLOR    = Color.black;
  private static final Color                      LOCATE_MATCH_BG_COLOR    = Color.white;

  @SuppressWarnings("unused")
  private TableSearcherComponent() {
  }

  /**
   * Constructor.
   * 
   * @param table
   *          the table used to search for keyword.
   */
  public TableSearcherComponent(JTable table) {
    _table = table;
    _userActions = new ArrayList<TableSearcherComponentAction>();
    buildGUI();
  }

  /**
   * Reset the component.
   */
  public void resetSearch() {
    _lastMatchRow = -1;
    _pattern = null;
    _indices = null;
    _sf.setTextForeground(LOCATE_MATCH_FG_COLOR);
    _sf.setTextBackground(LOCATE_MATCH_BG_COLOR);
  }

  /**
   * Set a list of columns that have to used to scan for the keyword. By
   * default, this component will scan all columns of the table.
   * 
   * @param colsIdx
   *          an array of column indices
   */
  public void setColumnsToScan(int[] colsIdx) {
    _colsIdx = colsIdx;
  }

  public JButton addUserAction(ImageIcon icon,
      TableSearcherComponentAction act) {
    JButton btn = _sf.addUserAction(icon, act);
    act.setEnabled(false);
    act.setTableSearcherComponentActionGateway(this);
    _userActions.add(act);
    return btn;
  }

  private void buildGUI() {

    _sf = new SearchField();
    _sf.setHelperText(_helper);
    _searchPrev = _sf.addUserAction(EZEnvironment.getImageIcon("run_back.png"),<commit_msg>update API<commit_after>/* Copyright (C) 2003-2019 <NAME>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  You may obtain a copy of the License at
 *
 *     https://www.gnu.org/licenses/agpl-3.0.txt
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 */
package bzh.plealog.bioinfo.ui.util;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;

import com.plealog.genericapp.api.EZEnvironment;

/**
 * This is a utility class aims at providing a search component. It can be used
 * to hightlight rows containing a keyword. By default the component scans the
 * entire table, but the method setColumnsToScan() can be used to set the list
 * of columns to scan.
 * 
 * @author <NAME>
 */
@SuppressWarnings("serial")
public class TableSearcherComponent extends JPanel
    implements TableSearcherComponentAPI {

  private JButton                                 _searchNext;
  private JButton                                 _searchPrev;
  private int                                     _lastMatchRow            = -1;
  private String                                  _helper                  = "Enter a pattern to search for";
  private SearchField                             _sf;
  private JTable                                  _table;
  private int[]                                   _colsIdx;
  private int[]                                   _indices;
  private Pattern                                 _pattern;
  private ArrayList<TableSearcherComponentAction> _userActions;
  private boolean                                 _searching;

  private static final Color                      LOCATE_NO_MATCH_BG_COLOR = new Color(
      255, 102, 102);
  private static final Color                      LOCATE_NO_MATCH_FG_COLOR = Color.white;
  private static final Color                      LOCATE_MATCH_FG_COLOR    = Color.black;
  private static final Color                      LOCATE_MATCH_BG_COLOR    = Color.white;

  @SuppressWarnings("unused")
  private TableSearcherComponent() {
  }

  /**
   * Constructor.
   * 
   * @param table
   *          the table used to search for keyword.
   */
  public TableSearcherComponent(JTable table) {
    _table = table;
    _userActions = new ArrayList<TableSearcherComponentAction>();
    buildGUI();
  }

  /**
   * Reset the component.
   */
  public void resetSearch() {
    _lastMatchRow = -1;
    _pattern = null;
    _indices = null;
    _sf.setTextForeground(LOCATE_MATCH_FG_COLOR);
    _sf.setTextBackground(LOCATE_MATCH_BG_COLOR);
  }

  /**
   * Set a list of columns that have to used to scan for the keyword. By
   * default, this component will scan all columns of the table.
   * 
   * @param colsIdx
   *          an array of column indices
   */
  public void setColumnsToScan(int[] colsIdx) {
    _colsIdx = colsIdx;
  }

  public JButton addUserAction(ImageIcon icon,
      TableSearcherComponentAction act) {
    JButton btn = _sf.addUserAction(icon, act);
    act.setEnabled(false);
    act.setTableSearcherComponentActionGateway(this);
    _userActions.add(act);
    return btn;
  }

  public void setSearchText(String txt) {
    _sf.setText(txt);
  }
  
  public String getSearchText() {
    return _sf.getText();
  }
  
  private void buildGUI() {

    _sf = new SearchField();
    _sf.setHelperText(_helper);
    _searchPrev = _sf.addUserAction(EZEnvironment.getImageIcon("run_back.png"),
{'fraction_non_alphanumeric': 0.07437329214684567, 'fraction_numerical': 0.005227515741950814, 'mean_word_length': 2.421951219512195, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3400235', 'n_tokens_mistral': 2368, 'n_tokens_neox': 2297, 'n_words': 718}