<commit_before>package org.cytoscape.view.table.internal;

import static org.cytoscape.view.table.internal.util.ViewUtil.invokeOnEDT;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.Printable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;

import org.cytoscape.equations.EquationCompiler;
import org.cytoscape.event.DebounceTimer;
import org.cytoscape.model.CyColumn;
import org.cytoscape.model.CyRow;
import org.cytoscape.model.CyTable;
import org.cytoscape.service.util.CyServiceRegistrar;
import org.cytoscape.util.swing.IconManager;
import org.cytoscape.util.swing.LookAndFeelUtil;
import org.cytoscape.util.swing.TextIcon;
import org.cytoscape.view.model.View;
import org.cytoscape.view.model.VisualLexicon;
import org.cytoscape.view.model.VisualProperty;
import org.cytoscape.view.model.events.TableViewChangedListener;
import org.cytoscape.view.model.table.CyTableView;
import org.cytoscape.view.presentation.RenderingEngine;
import org.cytoscape.view.table.internal.impl.BrowserTable;
import org.cytoscape.view.table.internal.impl.BrowserTableModel;
import org.cytoscape.view.table.internal.impl.BrowserTableRowHeader;
import org.cytoscape.view.table.internal.impl.BrowserTableRowHeaderRenderer;
import org.cytoscape.view.table.internal.impl.PopupMenuHelper;
import org.cytoscape.view.table.internal.impl.icon.VisualPropertyIconFactory;

/*
 * #%L
 * Cytoscape Table Presentation Impl (table-presentation-impl)
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2010 - 2021 The Cytoscape Consortium
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * 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 General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */

public class TableRenderingEngineImpl implements RenderingEngine<CyTable> {
	
	private static float SMALL_ICON_FONT_SIZE = 14.0f;
	private static int SMALL_ICON_SIZE = 16;
	
	private final CyTableView tableView;
	private final VisualLexicon lexicon;
	private final PopupMenuHelper popupMenuHelper;
	private final CyServiceRegistrar registrar;
	
	private BrowserTable browserTable;
	private BrowserTableRowHeader rowHeader;
	private CornerPanel cornerPanel;
	private VisualPropertyChangeListener vpChangeListener;
	
	private final Icon copyIcon;
	
	private boolean ignoreHeaderSelectionEvents;
	private boolean ignoreTableSelectionEvents;
	
	private final DebounceTimer rowSelectionTimer;
	
	public TableRenderingEngineImpl(
			CyTableView tableView,
			VisualLexicon lexicon,
			PopupMenuHelper popupMenuHelper,
			CyServiceRegistrar registrar
	) {
		this.tableView = tableView;
		this.lexicon = lexicon;
		this.popupMenuHelper = popupMenuHelper;
		this.registrar = registrar;
		
		var iconManager = registrar.getService(IconManager.class);
		var iconFont = iconManager.getIconFont(SMALL_ICON_FONT_SIZE);
		copyIcon = new TextIcon(IconManager.ICON_COPY, iconFont, SMALL_ICON_SIZE, SMALL_ICON_SIZE);
		
		rowSelectionTimer = new DebounceTimer(25);
	}
	
	public void install(JComponent component) {
		vpChangeListener = new VisualPropertyChangeListener(getBrowserTable(), tableView);
		
		var scrollPane = new JScrollPane();
		scrollPane.setViewportView(getBrowserTable());
		scrollPane.setRowHeaderView(getRowHeader());
		scrollPane.setCorner(JScrollPane.UPPER_LEADING_CORNER, getCornerPanel());
		scrollPane.getViewport().addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent evt) {
				if (!evt.isPopupTrigger())
					getBrowserTable().clearSelection();
			}
		});
		
		component.setLayout(new BorderLayout());
		component.add(scrollPane);
		
		getBrowserTable().addPropertyChangeListener("rowHeight", evt -> getRowHeader().update());
		getBrowserTable().addPropertyChangeListener("rowHeightChanged", evt -> getRowHeader().update());
		getBrowserTable().getModel().addTableModelListener(evt -> {
			// Update the row header when the table model changes (e.g. added/removed rows)
			getRowHeader().updateModel();
			getCornerPanel().update();
		});
		getBrowserTable().getColumnModel().addColumnModelListener(new TableColumnModelListener() {
			@Override
			public void columnAdded(TableColumnModelEvent evt) {
				getRowHeader().update();
				getCornerPanel().update();
			}
			@Override
			public void columnRemoved(TableColumnModelEvent evt) {
				getRowHeader().update();
				getCornerPanel().update();
			}
			@Override
			public void columnSelectionChanged(ListSelectionEvent evt) {
				if (!ignoreTableSelectionEvents)
					updateHeader();
			}
			@Override
			public void columnMoved(TableColumnModelEvent evt) {
				// Ignore...
			}
			@Override
			public void columnMarginChanged(ChangeEvent evt) {
				// Ignore...
			}
		});
		getBrowserTable().getSelectionModel().addListSelectionListener(evt -> {
			if (!ignoreTableSelectionEvents) {
				if (!rowSelectionTimer.isShutdown()) {
					rowSelectionTimer.debounce(() -> {
						invokeOnEDT(() -> {
							ignoreHeaderSelectionEvents = true;
							
							try {
								boolean allColumnsSelected = isAllTableColumnsSelected();
								var tbl = getBrowserTable();
								var rh = getRowHeader();
								int fi = evt.getFirstIndex();
								int li = evt.getLastIndex();
								
								// Only select the row header if all table columns are selected
								for (int i = fi; i <= li; i++) {
									if (allColumnsSelected && tbl.isRowSelected(i)) {
										if (!rh.isSelectedIndex(i))
											rh.addSelectionInterval(i, i);
									} else {
										if (rh.isSelectedIndex(i))<commit_msg>Rolling back one of the previous changes (we do have to iterate all the rows every time, because the DebounceTimer will ignore many row selection events in some scenarios) -- CYTOSCAPE-12894<commit_after>package org.cytoscape.view.table.internal;

import static org.cytoscape.view.table.internal.util.ViewUtil.invokeOnEDT;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.Printable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;

import org.cytoscape.equations.EquationCompiler;
import org.cytoscape.event.DebounceTimer;
import org.cytoscape.model.CyColumn;
import org.cytoscape.model.CyRow;
import org.cytoscape.model.CyTable;
import org.cytoscape.service.util.CyServiceRegistrar;
import org.cytoscape.util.swing.IconManager;
import org.cytoscape.util.swing.LookAndFeelUtil;
import org.cytoscape.util.swing.TextIcon;
import org.cytoscape.view.model.View;
import org.cytoscape.view.model.VisualLexicon;
import org.cytoscape.view.model.VisualProperty;
import org.cytoscape.view.model.events.TableViewChangedListener;
import org.cytoscape.view.model.table.CyTableView;
import org.cytoscape.view.presentation.RenderingEngine;
import org.cytoscape.view.table.internal.impl.BrowserTable;
import org.cytoscape.view.table.internal.impl.BrowserTableModel;
import org.cytoscape.view.table.internal.impl.BrowserTableRowHeader;
import org.cytoscape.view.table.internal.impl.BrowserTableRowHeaderRenderer;
import org.cytoscape.view.table.internal.impl.PopupMenuHelper;
import org.cytoscape.view.table.internal.impl.icon.VisualPropertyIconFactory;

/*
 * #%L
 * Cytoscape Table Presentation Impl (table-presentation-impl)
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2010 - 2021 The Cytoscape Consortium
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * 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 General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */

public class TableRenderingEngineImpl implements RenderingEngine<CyTable> {
	
	private static float SMALL_ICON_FONT_SIZE = 14.0f;
	private static int SMALL_ICON_SIZE = 16;
	
	private final CyTableView tableView;
	private final VisualLexicon lexicon;
	private final PopupMenuHelper popupMenuHelper;
	private final CyServiceRegistrar registrar;
	
	private BrowserTable browserTable;
	private BrowserTableRowHeader rowHeader;
	private CornerPanel cornerPanel;
	private VisualPropertyChangeListener vpChangeListener;
	
	private final Icon copyIcon;
	
	private boolean ignoreHeaderSelectionEvents;
	private boolean ignoreTableSelectionEvents;
	
	private final DebounceTimer rowSelectionTimer;
	
	public TableRenderingEngineImpl(
			CyTableView tableView,
			VisualLexicon lexicon,
			PopupMenuHelper popupMenuHelper,
			CyServiceRegistrar registrar
	) {
		this.tableView = tableView;
		this.lexicon = lexicon;
		this.popupMenuHelper = popupMenuHelper;
		this.registrar = registrar;
		
		var iconManager = registrar.getService(IconManager.class);
		var iconFont = iconManager.getIconFont(SMALL_ICON_FONT_SIZE);
		copyIcon = new TextIcon(IconManager.ICON_COPY, iconFont, SMALL_ICON_SIZE, SMALL_ICON_SIZE);
		
		rowSelectionTimer = new DebounceTimer(25);
	}
	
	public void install(JComponent component) {
		vpChangeListener = new VisualPropertyChangeListener(getBrowserTable(), tableView);
		
		var scrollPane = new JScrollPane();
		scrollPane.setViewportView(getBrowserTable());
		scrollPane.setRowHeaderView(getRowHeader());
		scrollPane.setCorner(JScrollPane.UPPER_LEADING_CORNER, getCornerPanel());
		scrollPane.getViewport().addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent evt) {
				if (!evt.isPopupTrigger())
					getBrowserTable().clearSelection();
			}
		});
		
		component.setLayout(new BorderLayout());
		component.add(scrollPane);
		
		getBrowserTable().addPropertyChangeListener("rowHeight", evt -> getRowHeader().update());
		getBrowserTable().addPropertyChangeListener("rowHeightChanged", evt -> getRowHeader().update());
		getBrowserTable().getModel().addTableModelListener(evt -> {
			// Update the row header when the table model changes (e.g. added/removed rows)
			getRowHeader().updateModel();
			getCornerPanel().update();
		});
		getBrowserTable().getColumnModel().addColumnModelListener(new TableColumnModelListener() {
			@Override
			public void columnAdded(TableColumnModelEvent evt) {
				getRowHeader().update();
				getCornerPanel().update();
			}
			@Override
			public void columnRemoved(TableColumnModelEvent evt) {
				getRowHeader().update();
				getCornerPanel().update();
			}
			@Override
			public void columnSelectionChanged(ListSelectionEvent evt) {
				if (!ignoreTableSelectionEvents)
					updateHeader();
			}
			@Override
			public void columnMoved(TableColumnModelEvent evt) {
				// Ignore...
			}
			@Override
			public void columnMarginChanged(ChangeEvent evt) {
				// Ignore...
			}
		});
		getBrowserTable().getSelectionModel().addListSelectionListener(evt -> {
			if (!ignoreTableSelectionEvents) {
				if (!rowSelectionTimer.isShutdown()) {
					rowSelectionTimer.debounce(() -> {
						invokeOnEDT(() -> {
							ignoreHeaderSelectionEvents = true;
							
							try {
								boolean allColumnsSelected = isAllTableColumnsSelected();
								var tbl = getBrowserTable();
								var rh = getRowHeader();
								int rowCount = tbl.getRowCount();
								
								// Only select the row header if all table columns are selected
								for (int i = 0; i < rowCount; i++) {
									if (allColumnsSelected && tbl.isRowSelected(i)) {
										if (!rh.isSelectedIndex(i))
											rh.addSelectionInterval(i, i);
									} else {
										if (rh.isSelectedIndex(i))
{'fraction_non_alphanumeric': 0.09745083758193736, 'fraction_numerical': 0.003350327749453751, 'mean_word_length': 4.681009515928837, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '382225', 'n_tokens_mistral': 4629, 'n_tokens_neox': 3966, 'n_words': 925}