<commit_before>package com.korshyadoo.musicFileNamer.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.LookAndFeel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

import com.korshyadoo.musicFileNamer.conf.Configuration;
import com.korshyadoo.musicFileNamer.controller.FileProcessor;
import com.korshyadoo.musicFileNamer.controller.LookAndFeelController;
import com.korshyadoo.musicFileNamer.controller.ProgramLauncher;
import com.korshyadoo.musicFileNamer.model.Mode;
import com.korshyadoo.musicFileNamer.model.PrefixFormats;

@SuppressWarnings("serial")
public class MainFrame extends JFrame {
	private static final long serialVersionUID = 4884885745124721080L;

	private Mode mode;
	private JPanel contentPane;
	private static File selectedDirectory = null;
	private JTextField txtDirectory;
	private BufferedImage upArrow;
	private BufferedImage downArrow;
	private DefaultListModel<String> listModel = new DefaultListModel<>();
	private JList<String> lstFileList;
	private JButton btnSave;
	private JButton btnRestart;
	private JTextField txtNumberInput;
	private JRadioButton pattern1;
	private JRadioButton pattern2;
	private JRadioButton pattern3;
	private JScrollPane scrollPane;
	private JLabel lblClickBrowse;
	private JPanel listPanel;
	private boolean listVisible;

	public MainFrame() throws IOException {
		upArrow = ImageIO.read(getInputStream("upArrow.png"));
		downArrow = ImageIO.read(getInputStream("downArrow.png"));

		createAndShowGui();
	}

	private InputStream getInputStream(String path) {
		InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(path);
		return resourceAsStream;
	}

	private void createAndShowGui() {
		Configuration config = ProgramLauncher.config;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		setBounds(config.getMainWindowX(), config.getMainWindowY(), config.getMainWindowWidth(), config.getMainWindowHeight());
		// setBounds(100, 200, 400, 400);
		contentPane = new JPanel();
		// contentPane.setBorder(BorderFactory.createLineBorder(Color.black));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		createBrowsePanel();
		createListPanel();
		createBottomPanel();
	}

	private void createBrowsePanel() {
		JPanel browsePanel = new JPanel();
		// browsePanel.setBorder(BorderFactory.createLineBorder(Color.black));
		browsePanel.setLayout(new BorderLayout(0, 0));
		contentPane.add(browsePanel, BorderLayout.NORTH);

		txtDirectory = new JTextField("");
		txtDirectory.setEditable(false);
		browsePanel.add(txtDirectory, BorderLayout.CENTER);

		JButton btnBrowse = new JButton("Browse");
		btnBrowse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Open a file browser to choose a location
				final LookAndFeel previousLF = UIManager.getLookAndFeel();
				LookAndFeelController.setLookAndFeel(LookAndFeelController.NIMBUS);
				final JFileChooser fc = new JFileChooser();
				fc.setCurrentDirectory(ProgramLauncher.config.getDefaultDirectory());
				fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				int returnVal = fc.showDialog(MainFrame.this, "Select");
				if(returnVal == JFileChooser.APPROVE_OPTION) {
					MainFrame.setSelectedDirectory(fc.getSelectedFile());
					boolean noFiles = hasOnlyDirectories(selectedDirectory);<commit_msg>Bug Fix: selecting a file in the JFileChooser throws an exception<commit_after>package com.korshyadoo.musicFileNamer.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.LookAndFeel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

import com.korshyadoo.musicFileNamer.conf.Configuration;
import com.korshyadoo.musicFileNamer.controller.FileProcessor;
import com.korshyadoo.musicFileNamer.controller.LookAndFeelController;
import com.korshyadoo.musicFileNamer.controller.ProgramLauncher;
import com.korshyadoo.musicFileNamer.model.Mode;
import com.korshyadoo.musicFileNamer.model.PrefixFormats;

@SuppressWarnings("serial")
public class MainFrame extends JFrame {
	private static final long serialVersionUID = 4884885745124721080L;

	private Mode mode;
	private JPanel contentPane;
	private static File selectedDirectory = null;
	private JTextField txtDirectory;
	private BufferedImage upArrow;
	private BufferedImage downArrow;
	private DefaultListModel<String> listModel = new DefaultListModel<>();
	private JList<String> lstFileList;
	private JButton btnSave;
	private JButton btnRestart;
	private JTextField txtNumberInput;
	private JRadioButton pattern1;
	private JRadioButton pattern2;
	private JRadioButton pattern3;
	private JScrollPane scrollPane;
	private JLabel lblClickBrowse;
	private JPanel listPanel;
	private boolean listVisible;

	public MainFrame() throws IOException {
		upArrow = ImageIO.read(getInputStream("upArrow.png"));
		downArrow = ImageIO.read(getInputStream("downArrow.png"));

		createAndShowGui();
	}

	private InputStream getInputStream(String path) {
		InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(path);
		return resourceAsStream;
	}

	private void createAndShowGui() {
		Configuration config = ProgramLauncher.config;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		setBounds(config.getMainWindowX(), config.getMainWindowY(), config.getMainWindowWidth(), config.getMainWindowHeight());
		// setBounds(100, 200, 400, 400);
		contentPane = new JPanel();
		// contentPane.setBorder(BorderFactory.createLineBorder(Color.black));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		createBrowsePanel();
		createListPanel();
		createBottomPanel();
	}

	private void createBrowsePanel() {
		JPanel browsePanel = new JPanel();
		// browsePanel.setBorder(BorderFactory.createLineBorder(Color.black));
		browsePanel.setLayout(new BorderLayout(0, 0));
		contentPane.add(browsePanel, BorderLayout.NORTH);

		txtDirectory = new JTextField("");
		txtDirectory.setEditable(false);
		browsePanel.add(txtDirectory, BorderLayout.CENTER);

		JButton btnBrowse = new JButton("Browse");
		btnBrowse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Open a file browser to choose a location
				final LookAndFeel previousLF = UIManager.getLookAndFeel();
				LookAndFeelController.setLookAndFeel(LookAndFeelController.NIMBUS);
				final JFileChooser fc = new JFileChooser();
				fc.setCurrentDirectory(ProgramLauncher.config.getDefaultDirectory());
				fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				int returnVal = fc.showDialog(MainFrame.this, "Select");
				if(returnVal == JFileChooser.APPROVE_OPTION) {
					//If the selected File is a file, use the parent directory instead
					File selectedFile = (fc.getSelectedFile().isDirectory()) ? fc.getSelectedFile() : new File(fc.getSelectedFile().getParent());
					
					MainFrame.setSelectedDirectory(selectedFile);
					boolean noFiles = hasOnlyDirectories(selectedDirectory);
{'fraction_non_alphanumeric': 0.09683509161576902, 'fraction_numerical': 0.009328151027207108, 'mean_word_length': 6.381967213114754, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3314890', 'n_tokens_mistral': 2951, 'n_tokens_neox': 2725, 'n_words': 553}