<commit_before>package net.sf.jabref.gui.maintable;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

import net.sf.jabref.Globals;
import net.sf.jabref.JabRefExecutorService;
import net.sf.jabref.JabRefGUI;
import net.sf.jabref.gui.BasePanel;
import net.sf.jabref.gui.BasePanelMode;
import net.sf.jabref.gui.GUIGlobals;
import net.sf.jabref.gui.IconTheme;
import net.sf.jabref.gui.PreviewPanel;
import net.sf.jabref.gui.actions.CopyDoiUrlAction;
import net.sf.jabref.gui.desktop.JabRefDesktop;
import net.sf.jabref.gui.entryeditor.EntryEditor;
import net.sf.jabref.gui.externalfiletype.ExternalFileMenuItem;
import net.sf.jabref.gui.externalfiletype.ExternalFileType;
import net.sf.jabref.gui.filelist.FileListEntry;
import net.sf.jabref.gui.filelist.FileListTableModel;
import net.sf.jabref.gui.menus.RightClickMenu;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.OS;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.preferences.PreviewPreferences;
import net.sf.jabref.specialfields.SpecialFieldValue;
import net.sf.jabref.specialfields.SpecialFieldsUtils;

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.event.ListEvent;
import ca.odell.glazedlists.event.ListEventListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * List event, mouse, key and focus listener for the main table that makes up the
 * most part of the BasePanel for a single BIB database.
 */
public class MainTableSelectionListener implements ListEventListener<BibEntry>, MouseListener,
        KeyListener, FocusListener {

    private final MainTable table;
    private final BasePanel panel;
    private final EventList<BibEntry> tableRows;

    private PreviewPanel preview;
    private boolean previewActive = Globals.prefs.getPreviewPreferences().isPreviewPanelEnabled();
    private boolean workingOnPreview;

    private boolean enabled = true;

    // Register the last character pressed to quick jump in the table. Together
    // with storing the last row number jumped to, this is used to let multiple
    // key strokes cycle between all entries starting with the same letter:
    private final int[] lastPressed = new int[20];
    private int lastPressedCount;
    private long lastPressedTime;

    private static final Log LOGGER = LogFactory.getLog(MainTableSelectionListener.class);

    public MainTableSelectionListener(BasePanel panel, MainTable table) {
        this.table = table;
        this.panel = panel;
        this.tableRows = table.getTableModel().getTableRows();
        PreviewPanel previewPanel = panel.getPreviewPanel();
        if (previewPanel != null){
            preview = previewPanel;
        } else {
            preview = new PreviewPanel(panel.getBibDatabaseContext(), null, panel);
            panel.frame().getGlobalSearchBar().getSearchQueryHighlightObservable().addSearchListener(preview);
        }
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    @Override
    public void listChanged(ListEvent<BibEntry> e) {
        if (!enabled) {
            return;
        }

        EventList<BibEntry> selected = e.getSourceList();
        if (selected.isEmpty()){
            return;
        }

        final BibEntry newSelected = selected.get(0);
        if (Objects.nonNull(panel.getCurrentEditor()) && (newSelected == panel.getCurrentEditor().getEntry())) {
            // is already selected
            return;
        }

        if (newSelected != null) {
            final BasePanelMode mode = panel.getMode(); // What is the panel already showing?
            if ((mode == BasePanelMode.WILL_SHOW_EDITOR) || (mode == BasePanelMode.SHOWING_EDITOR)) {
                // An entry is currently being edited.
                EntryEditor oldEditor = panel.getCurrentEditor();
                String visName = null;
                if (oldEditor != null) {
                    visName = oldEditor.getVisiblePanelName();
                }
                // Get an old or new editor for the entry to edit:
                EntryEditor newEditor = panel.getEntryEditor(newSelected);

                if (oldEditor != null) {<commit_msg>fix preview update (#2125)<commit_after>package net.sf.jabref.gui.maintable;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

import net.sf.jabref.Globals;
import net.sf.jabref.JabRefExecutorService;
import net.sf.jabref.JabRefGUI;
import net.sf.jabref.gui.BasePanel;
import net.sf.jabref.gui.BasePanelMode;
import net.sf.jabref.gui.GUIGlobals;
import net.sf.jabref.gui.IconTheme;
import net.sf.jabref.gui.PreviewPanel;
import net.sf.jabref.gui.actions.CopyDoiUrlAction;
import net.sf.jabref.gui.desktop.JabRefDesktop;
import net.sf.jabref.gui.entryeditor.EntryEditor;
import net.sf.jabref.gui.externalfiletype.ExternalFileMenuItem;
import net.sf.jabref.gui.externalfiletype.ExternalFileType;
import net.sf.jabref.gui.filelist.FileListEntry;
import net.sf.jabref.gui.filelist.FileListTableModel;
import net.sf.jabref.gui.menus.RightClickMenu;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.OS;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.preferences.PreviewPreferences;
import net.sf.jabref.specialfields.SpecialFieldValue;
import net.sf.jabref.specialfields.SpecialFieldsUtils;

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.event.ListEvent;
import ca.odell.glazedlists.event.ListEventListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * List event, mouse, key and focus listener for the main table that makes up the
 * most part of the BasePanel for a single BIB database.
 */
public class MainTableSelectionListener implements ListEventListener<BibEntry>, MouseListener,
        KeyListener, FocusListener {

    private final MainTable table;
    private final BasePanel panel;
    private final EventList<BibEntry> tableRows;

    private PreviewPanel preview;
    private boolean previewActive = Globals.prefs.getPreviewPreferences().isPreviewPanelEnabled();
    private boolean workingOnPreview;

    private boolean enabled = true;

    // Register the last character pressed to quick jump in the table. Together
    // with storing the last row number jumped to, this is used to let multiple
    // key strokes cycle between all entries starting with the same letter:
    private final int[] lastPressed = new int[20];
    private int lastPressedCount;
    private long lastPressedTime;

    private static final Log LOGGER = LogFactory.getLog(MainTableSelectionListener.class);

    public MainTableSelectionListener(BasePanel panel, MainTable table) {
        this.table = table;
        this.panel = panel;
        this.tableRows = table.getTableModel().getTableRows();
        PreviewPanel previewPanel = panel.getPreviewPanel();
        if (previewPanel != null){
            preview = previewPanel;
        } else {
            preview = new PreviewPanel(panel.getBibDatabaseContext(), null, panel);
            panel.frame().getGlobalSearchBar().getSearchQueryHighlightObservable().addSearchListener(preview);
        }
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    @Override
    public void listChanged(ListEvent<BibEntry> e) {
        if (!enabled) {
            return;
        }

        EventList<BibEntry> selected = e.getSourceList();
        if (selected.isEmpty()){
            return;
        }

        final BibEntry newSelected = selected.get(0);
        if (newSelected != null) {
            final BasePanelMode mode = panel.getMode(); // What is the panel already showing?
            if ((mode == BasePanelMode.WILL_SHOW_EDITOR) || (mode == BasePanelMode.SHOWING_EDITOR)) {
                // An entry is currently being edited.
                EntryEditor oldEditor = panel.getCurrentEditor();
                String visName = null;
                if (oldEditor != null) {
                    visName = oldEditor.getVisiblePanelName();
                }
                // Get an old or new editor for the entry to edit:
                EntryEditor newEditor = panel.getEntryEditor(newSelected);

                if (oldEditor != null) {
{'fraction_non_alphanumeric': 0.09515495416848538, 'fraction_numerical': 0.0015277171540811873, 'mean_word_length': 3.3684461391801714, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1696302', 'n_tokens_mistral': 2690, 'n_tokens_neox': 2572, 'n_words': 619}