<commit_before>package gui;

import gui.render.ProgressCellRenderer;
import gui.render.TableModelDL;
import gui.render.TableModelSpec;

import java.awt.Font;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import main.Core;
import main.Settings;
import main.Utils;
import blocks.BlockedFile;

@SuppressWarnings("serial")
public class MainWindow extends JFrame {

	protected JPanel contentPane;
	public JTextField searchInput;
	protected JTable searchRes;
	protected JTable downloadList;
	public DefaultTableModel tableModel;
	public DefaultTableModel downloadModel;
	protected DefaultTableCellRenderer betterRenderer;
	protected CountDownLatch resLatch;
	public CountDownLatch debugLatch;
	protected JScrollPane searchResScrollPane;
	protected JScrollPane downloadScrollPane;
	protected JSeparator separator;
	protected JLabel lblPeers;
	public String debugHost;
	protected JMenuItem mntmAbout;
	protected JMenuBar menuBar;
	protected JMenu mnFile;
	private JPopupMenu downloadPopupMenu;
	
	protected boolean searchMode;
	private JMenuItem downloadPopupMenuRemoveFromList;

	/**
	 * Create the frame.
	 */
	public MainWindow() {
		setResizable(false);
		searchMode = false;
		setTitle("XNet v" + Settings.version);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 550, 470);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		//Set title icon
		try {
			Image iconImage = ImageIO.read(getClass().getResourceAsStream("/res/imgres/titleicon.png"));
			setIconImage(iconImage);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		tableModel = new TableModelSpec();
		tableModel.addColumn("Status");
		
		downloadModel = new TableModelDL();
		downloadModel.addColumn("Filename");
		downloadModel.addColumn("Progress");
		
		resLatch = new CountDownLatch(1);
		
		searchInput = new JTextField();
		searchInput.setBounds(12, 39, 520, 25);
		searchInput.setColumns(10);
		searchInput.setFocusable(false);
		searchInput.setEditable(false);
		contentPane.add(searchInput);
		
		contentPane.setLayout(null);
		
		menuBar = new JMenuBar();
		menuBar.setBounds(0, -4, 546, 30);
		contentPane.add(menuBar);
		
		mnFile = new JMenu("Help");
		menuBar.add(mnFile);

		mntmAbout = new JMenuItem("About");
		try {
			ImageIcon imageIcon = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("/res/imgres/glasses.png")));
			mntmAbout.setIcon(imageIcon);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		mntmAbout.setHorizontalAlignment(SwingConstants.LEFT);
		mnFile.add(mntmAbout);
		
		searchResScrollPane = new JScrollPane();
		searchResScrollPane.setBounds(12, 76, 520, 220);
		contentPane.add(searchResScrollPane);
		
		downloadScrollPane = new JScrollPane();
		downloadScrollPane.setBounds(12, 306, 520, 102);
		contentPane.add(downloadScrollPane);
		
		separator = new JSeparator();
		separator.setBounds(0, 419, 546, 2);
		contentPane.add(separator);
		
		lblPeers = new JLabel("");
		lblPeers.setToolTipText("[0|0]");
		lblPeers.setIcon(new ImageIcon(MainWindow.class.getResource("/res/imgres/0bars.png")));
		lblPeers.setBounds(520, 419, 24, 25);
		lblPeers.setFont(new Font("Tahoma", Font.PLAIN, 11));
		contentPane.add(lblPeers);
		
		searchRes = new JTable(tableModel);
		betterRenderer = new DefaultTableCellRenderer();
		betterRenderer.setHorizontalAlignment(SwingConstants.CENTER);
		searchRes.setDefaultRenderer(Object.class, betterRenderer);
		//.getColumn(0).setCellRenderer(betterRenderer);
		searchRes.getTableHeader().setReorderingAllowed(false);
		searchRes.getTableHeader().setResizingAllowed(false);
		searchResScrollPane.setViewportView(searchRes);
		searchRes.setCellSelectionEnabled(true);
		searchRes.setColumnSelectionAllowed(true);
		
		downloadPopupMenu = new JPopupMenu();
		downloadPopupMenuRemoveFromList = new JMenuItem("Remove from list");
		downloadPopupMenu.add(downloadPopupMenuRemoveFromList);

		downloadList = new JTable(downloadModel);
		downloadList.getColumnModel().getColumn(0).setCellRenderer(betterRenderer);
		downloadList.getColumnModel().getColumn(1).setCellRenderer(betterRenderer);
		downloadList.getTableHeader().setReorderingAllowed(false);
		downloadList.getTableHeader().setResizingAllowed(false);
		downloadScrollPane.setViewportView(downloadList);
	}
	
	public void registerListeners() {
		downloadPopupMenuRemoveFromList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int[] selectedRows = downloadList.getSelectedRows();
				for(Integer i : selectedRows) {
					downloadModel.removeRow(i);
				}
			}
		});
		
		downloadList.addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent arg0) {
				if(arg0.isPopupTrigger()) {
					Point clickPoint = arg0.getPoint();
					int tableRow = downloadList.rowAtPoint(clickPoint);
					if(!downloadList.isRowSelected(tableRow)) {
						downloadList.changeSelection(tableRow, 0, false, false);
					}
					downloadPopupMenu.show(arg0.getComponent(), arg0.getX(), arg0.getY());
				}
			}
		});
		
		mntmAbout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				aboutWindow();
			}
		});
		
		searchInput.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				int key = arg0.getKeyCode();
				if(key == KeyEvent.VK_ENTER) {
					//clear any previous res
					clearTable();
					//clear core index
					Core.index.clear();
					if(Core.debugServer) {
						debugHost = searchInput.getText();
						if(debugHost.equals("")) {
							out("You cannot enter a blank IP.");
						} else {
							out("Debug host set!");
							debugLatch.countDown();
						}
						Core.debugServer = false;
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						Core.mainWindow.resetTable();
					} else {
						if(Core.peerList.size() == 0) {
							out("No peers connected. Query is not possible.");
						} else {
							String input = searchInput.getText();
							if(input.equals("")) {
								out("You cannot search for a blank query.");
							} else if(input.length() < 4) {
								out("You cannot search for a query shorter than 4 characters.");
							} else {
								if(!searchMode) {
									removeColumnAndData(searchRes, 0);
									tableModel.addColumn("Filename");
									tableModel.addColumn("Checksum");
									searchMode = true;
								}
								Utils.doSearch(input);
							}
							//dump results
						}
					}
					searchInput.setText("");
				}
			}
		});
		
		searchRes.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if(searchMode) {
					Point clickPoint = arg0.getPoint();
					int tableRow = searchRes.rowAtPoint(clickPoint);<commit_msg>Adjusted minimum query length<commit_after>package gui;

import gui.render.ProgressCellRenderer;
import gui.render.TableModelDL;
import gui.render.TableModelSpec;

import java.awt.Font;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import main.Core;
import main.Settings;
import main.Utils;
import blocks.BlockedFile;

@SuppressWarnings("serial")
public class MainWindow extends JFrame {

	protected JPanel contentPane;
	public JTextField searchInput;
	protected JTable searchRes;
	protected JTable downloadList;
	public DefaultTableModel tableModel;
	public DefaultTableModel downloadModel;
	protected DefaultTableCellRenderer betterRenderer;
	protected CountDownLatch resLatch;
	public CountDownLatch debugLatch;
	protected JScrollPane searchResScrollPane;
	protected JScrollPane downloadScrollPane;
	protected JSeparator separator;
	protected JLabel lblPeers;
	public String debugHost;
	protected JMenuItem mntmAbout;
	protected JMenuBar menuBar;
	protected JMenu mnFile;
	private JPopupMenu downloadPopupMenu;
	
	protected boolean searchMode;
	private JMenuItem downloadPopupMenuRemoveFromList;

	/**
	 * Create the frame.
	 */
	public MainWindow() {
		setResizable(false);
		searchMode = false;
		setTitle("XNet v" + Settings.version);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 550, 470);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		//Set title icon
		try {
			Image iconImage = ImageIO.read(getClass().getResourceAsStream("/res/imgres/titleicon.png"));
			setIconImage(iconImage);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		tableModel = new TableModelSpec();
		tableModel.addColumn("Status");
		
		downloadModel = new TableModelDL();
		downloadModel.addColumn("Filename");
		downloadModel.addColumn("Progress");
		
		resLatch = new CountDownLatch(1);
		
		searchInput = new JTextField();
		searchInput.setBounds(12, 39, 520, 25);
		searchInput.setColumns(10);
		searchInput.setFocusable(false);
		searchInput.setEditable(false);
		contentPane.add(searchInput);
		
		contentPane.setLayout(null);
		
		menuBar = new JMenuBar();
		menuBar.setBounds(0, -4, 546, 30);
		contentPane.add(menuBar);
		
		mnFile = new JMenu("Help");
		menuBar.add(mnFile);

		mntmAbout = new JMenuItem("About");
		try {
			ImageIcon imageIcon = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("/res/imgres/glasses.png")));
			mntmAbout.setIcon(imageIcon);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		mntmAbout.setHorizontalAlignment(SwingConstants.LEFT);
		mnFile.add(mntmAbout);
		
		searchResScrollPane = new JScrollPane();
		searchResScrollPane.setBounds(12, 76, 520, 220);
		contentPane.add(searchResScrollPane);
		
		downloadScrollPane = new JScrollPane();
		downloadScrollPane.setBounds(12, 306, 520, 102);
		contentPane.add(downloadScrollPane);
		
		separator = new JSeparator();
		separator.setBounds(0, 419, 546, 2);
		contentPane.add(separator);
		
		lblPeers = new JLabel("");
		lblPeers.setToolTipText("[0|0]");
		lblPeers.setIcon(new ImageIcon(MainWindow.class.getResource("/res/imgres/0bars.png")));
		lblPeers.setBounds(520, 419, 24, 25);
		lblPeers.setFont(new Font("Tahoma", Font.PLAIN, 11));
		contentPane.add(lblPeers);
		
		searchRes = new JTable(tableModel);
		betterRenderer = new DefaultTableCellRenderer();
		betterRenderer.setHorizontalAlignment(SwingConstants.CENTER);
		searchRes.setDefaultRenderer(Object.class, betterRenderer);
		//.getColumn(0).setCellRenderer(betterRenderer);
		searchRes.getTableHeader().setReorderingAllowed(false);
		searchRes.getTableHeader().setResizingAllowed(false);
		searchResScrollPane.setViewportView(searchRes);
		searchRes.setCellSelectionEnabled(true);
		searchRes.setColumnSelectionAllowed(true);
		
		downloadPopupMenu = new JPopupMenu();
		downloadPopupMenuRemoveFromList = new JMenuItem("Remove from list");
		downloadPopupMenu.add(downloadPopupMenuRemoveFromList);

		downloadList = new JTable(downloadModel);
		downloadList.getColumnModel().getColumn(0).setCellRenderer(betterRenderer);
		downloadList.getColumnModel().getColumn(1).setCellRenderer(betterRenderer);
		downloadList.getTableHeader().setReorderingAllowed(false);
		downloadList.getTableHeader().setResizingAllowed(false);
		downloadScrollPane.setViewportView(downloadList);
	}
	
	public void registerListeners() {
		downloadPopupMenuRemoveFromList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int[] selectedRows = downloadList.getSelectedRows();
				for(Integer i : selectedRows) {
					downloadModel.removeRow(i);
				}
			}
		});
		
		downloadList.addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent arg0) {
				if(arg0.isPopupTrigger()) {
					Point clickPoint = arg0.getPoint();
					int tableRow = downloadList.rowAtPoint(clickPoint);
					if(!downloadList.isRowSelected(tableRow)) {
						downloadList.changeSelection(tableRow, 0, false, false);
					}
					downloadPopupMenu.show(arg0.getComponent(), arg0.getX(), arg0.getY());
				}
			}
		});
		
		mntmAbout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				aboutWindow();
			}
		});
		
		searchInput.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				int key = arg0.getKeyCode();
				if(key == KeyEvent.VK_ENTER) {
					//clear any previous res
					clearTable();
					//clear core index
					Core.index.clear();
					if(Core.debugServer) {
						debugHost = searchInput.getText();
						if(debugHost.equals("")) {
							out("You cannot enter a blank IP.");
						} else {
							out("Debug host set!");
							debugLatch.countDown();
						}
						Core.debugServer = false;
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						Core.mainWindow.resetTable();
					} else {
						if(Core.peerList.size() == 0) {
							out("No peers connected. Query is not possible.");
						} else {
							String input = searchInput.getText();
							if(input.equals("")) {
								out("You cannot search for a blank query.");
							} else if(input.length() < 3) {
								out("You cannot search for a query shorter than 3 characters.");
							} else {
								if(!searchMode) {
									removeColumnAndData(searchRes, 0);
									tableModel.addColumn("Filename");
									tableModel.addColumn("Checksum");
									searchMode = true;
								}
								Utils.doSearch(input);
							}
							//dump results
						}
					}
					searchInput.setText("");
				}
			}
		});
		
		searchRes.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if(searchMode) {
					Point clickPoint = arg0.getPoint();
					int tableRow = searchRes.rowAtPoint(clickPoint);
{'fraction_non_alphanumeric': 0.11453013278855975, 'fraction_numerical': 0.013023493360572012, 'mean_word_length': 4.401724137931034, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 62, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4404931', 'n_tokens_mistral': 5795, 'n_tokens_neox': 4748, 'n_words': 926}