<commit_before>package project;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.InvocationTargetException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;

import project.primeCalc.PrimeCalculator;
import project.primeUsage.PrimeUsage;

// We don't call this PrimesFrame, cause it does much more than just being a frame.
public class PrimesGUI extends JFrame implements UI {
	private static final long serialVersionUID = 1L;

	protected static final String GUI_WINDOW_TITLE = "Primzahlen-Berechnung";

	protected static final NumberFormat NUMBER_FORMAT = new DecimalFormat("#,###,###,##0");

	private JPanel contentPane;

	private JTextPane textPane;
	private JComboBox cbxMethode;
	private JCheckBox chckbxPrimzahlenAusgeben;
	private JTextField textFieldBerechnetBis;
	private JSpinner spinner;
	private JButton btnCalcStart;
	private JButton btnExport;
	private JButton btnClear;

	/**
	 * Stores the available PrimeCalculators.
	 */
	private Map<String, PrimeCalculator> primeCalculators = new HashMap<String, PrimeCalculator>();

	/**
	 * Stores the result of a prime calculation, for further use by a PrimeUsage.
	 */
	private boolean[] primes;

	/**
	 * Create the frame.
	 */
	public PrimesGUI() {
		setResizable(false);
		setTitle(GUI_WINDOW_TITLE);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 466, 376);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		btnExport = new JButton("Primzahlen exportieren");
		btnExport.setToolTipText("Exportiert die Primzahlen in eine portable Datei.");
		btnExport.setEnabled(false);
		btnExport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				exportPrimes();
			}
		});
		btnExport.setBounds(311, 317, 142, 23);
		contentPane.add(btnExport);

		JPanel usePrimesPanel = new JPanel();
		usePrimesPanel.setBorder(new TitledBorder(new LineBorder(Color.BLACK, 1, true), "Primzahlen benutzen", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		usePrimesPanel.setBounds(10, 181, 207, 159);
		contentPane.add(usePrimesPanel);
		usePrimesPanel.setLayout(null);

		JPanel calcPrimesPanel = new JPanel();
		calcPrimesPanel.setBorder(new TitledBorder(new LineBorder(Color.BLACK, 1, true), "Primzahlen berechnen", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		calcPrimesPanel.setBounds(10, 11, 207, 165);
		contentPane.add(calcPrimesPanel);
		calcPrimesPanel.setLayout(null);

		JLabel lblMethode = new JLabel("Methode:");
		lblMethode.setBounds(10, 22, 46, 14);
		calcPrimesPanel.add(lblMethode);

		JLabel lblBerechnePrimzahlenBis = new JLabel("Berechne Zahlen bis");
		lblBerechnePrimzahlenBis.setBounds(10, 52, 97, 14);
		calcPrimesPanel.add(lblBerechnePrimzahlenBis);

		chckbxPrimzahlenAusgeben = new JCheckBox("Primzahlen ausgeben");
		chckbxPrimzahlenAusgeben.setToolTipText("Achtung: Nur für kleine Berechnungen benutzen, da die grafische Ausgabe viel Zeit in Anspruch nimmt.");
		chckbxPrimzahlenAusgeben.setBounds(6, 73, 131, 23);
		calcPrimesPanel.add(chckbxPrimzahlenAusgeben);

		spinner = new JSpinner();
		spinner.setToolTipText("Gibt an, bis zu welcher Zahl berechnet werden soll. Umso höher, umso zeitaufwändiger die Berechnung.");
		spinner.setModel(new SpinnerNumberModel(new Integer(10000), new Integer(1), null, new Integer(100)));
		spinner.setBounds(111, 49, 86, 20);
		calcPrimesPanel.add(spinner);

		cbxMethode = new JComboBox();
		cbxMethode.setToolTipText("Wählt die Methode aus, mit der Primzahlen berechnet werden sollen.");
		cbxMethode.setBounds(66, 19, 131, 20);
		cbxMethode.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				((SpinnerNumberModel) spinner.getModel()).setMaximum(primeCalculators.get(cbxMethode.getSelectedItem()).getHighestDeterminableNumber());
			}
		});
		calcPrimesPanel.add(cbxMethode);

		btnCalcStart = new JButton("Berechnung starten");
		btnCalcStart.setToolTipText("Startet die Berechnung.");
		btnCalcStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				startPrimeCalculation();
			}
		});
		btnCalcStart.setBounds(66, 100, 131, 23);
		calcPrimesPanel.add(btnCalcStart);

		JLabel lblVerfgbarePrimzahlen = new JLabel("Zahlen berechnet bis:");
		lblVerfgbarePrimzahlen.setBounds(10, 140, 109, 14);
		calcPrimesPanel.add(lblVerfgbarePrimzahlen);

		textFieldBerechnetBis = new JTextField();
		textFieldBerechnetBis.setToolTipText("Zeigt die Anzahl der berechneten Primzahlen.");
		textFieldBerechnetBis.setHorizontalAlignment(SwingConstants.TRAILING);
		textFieldBerechnetBis.setText("0");
		textFieldBerechnetBis.setEditable(false);<commit_msg>Almost forgot this little bug!<commit_after>package project;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.InvocationTargetException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;

import project.primeCalc.PrimeCalculator;
import project.primeUsage.PrimeUsage;

// We don't call this PrimesFrame, cause it does much more than just being a frame.
public class PrimesGUI extends JFrame implements UI {
	private static final long serialVersionUID = 1L;

	protected static final String GUI_WINDOW_TITLE = "Primzahlen-Berechnung";

	protected static final NumberFormat NUMBER_FORMAT = new DecimalFormat("#,###,###,##0");

	private JPanel contentPane;

	private JTextPane textPane;
	private JComboBox cbxMethode;
	private JCheckBox chckbxPrimzahlenAusgeben;
	private JTextField textFieldBerechnetBis;
	private JSpinner spinner;
	private JButton btnCalcStart;
	private JButton btnExport;
	private JButton btnClear;

	/**
	 * Stores the available PrimeCalculators.
	 */
	private Map<String, PrimeCalculator> primeCalculators = new HashMap<String, PrimeCalculator>();

	/**
	 * Stores the result of a prime calculation, for further use by a PrimeUsage.
	 */
	private boolean[] primes;

	/**
	 * Create the frame.
	 */
	public PrimesGUI() {
		setResizable(false);
		setTitle(GUI_WINDOW_TITLE);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 466, 376);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		btnExport = new JButton("Primzahlen exportieren");
		btnExport.setToolTipText("Exportiert die Primzahlen in eine portable Datei.");
		btnExport.setEnabled(false);
		btnExport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				exportPrimes();
			}
		});
		btnExport.setBounds(311, 317, 142, 23);
		contentPane.add(btnExport);

		JPanel usePrimesPanel = new JPanel();
		usePrimesPanel.setBorder(new TitledBorder(new LineBorder(Color.BLACK, 1, true), "Primzahlen benutzen", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		usePrimesPanel.setBounds(10, 181, 207, 159);
		contentPane.add(usePrimesPanel);
		usePrimesPanel.setLayout(null);

		JPanel calcPrimesPanel = new JPanel();
		calcPrimesPanel.setBorder(new TitledBorder(new LineBorder(Color.BLACK, 1, true), "Primzahlen berechnen", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		calcPrimesPanel.setBounds(10, 11, 207, 165);
		contentPane.add(calcPrimesPanel);
		calcPrimesPanel.setLayout(null);

		JLabel lblMethode = new JLabel("Methode:");
		lblMethode.setBounds(10, 22, 46, 14);
		calcPrimesPanel.add(lblMethode);

		JLabel lblBerechnePrimzahlenBis = new JLabel("Berechne Zahlen bis");
		lblBerechnePrimzahlenBis.setBounds(10, 52, 97, 14);
		calcPrimesPanel.add(lblBerechnePrimzahlenBis);

		chckbxPrimzahlenAusgeben = new JCheckBox("Primzahlen ausgeben");
		chckbxPrimzahlenAusgeben.setToolTipText("Achtung: Nur für kleine Berechnungen benutzen, da die grafische Ausgabe viel Zeit in Anspruch nimmt.");
		chckbxPrimzahlenAusgeben.setBounds(6, 73, 131, 23);
		calcPrimesPanel.add(chckbxPrimzahlenAusgeben);

		spinner = new JSpinner();
		spinner.setToolTipText("Gibt an, bis zu welcher Zahl berechnet werden soll. Umso höher, umso zeitaufwändiger die Berechnung.");
		spinner.setModel(new SpinnerNumberModel(new Integer(10000), new Integer(1), null, new Integer(100)));
		spinner.setBounds(111, 49, 86, 20);
		calcPrimesPanel.add(spinner);

		cbxMethode = new JComboBox();
		cbxMethode.setToolTipText("Wählt die Methode aus, mit der Primzahlen berechnet werden sollen.");
		cbxMethode.setBounds(66, 19, 131, 20);
		cbxMethode.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int highestDeteminableNumber = primeCalculators.get(cbxMethode.getSelectedItem()).getHighestDeterminableNumber();
				SpinnerNumberModel spinnerModel = (SpinnerNumberModel) spinner.getModel();
				
				spinnerModel.setMaximum(highestDeteminableNumber);
				if ((Integer) spinnerModel.getNumber() > highestDeteminableNumber)
					spinnerModel.setValue(highestDeteminableNumber - (highestDeteminableNumber % (Integer) spinnerModel.getStepSize()));
			}
		});
		calcPrimesPanel.add(cbxMethode);

		btnCalcStart = new JButton("Berechnung starten");
		btnCalcStart.setToolTipText("Startet die Berechnung.");
		btnCalcStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				startPrimeCalculation();
			}
		});
		btnCalcStart.setBounds(66, 100, 131, 23);
		calcPrimesPanel.add(btnCalcStart);

		JLabel lblVerfgbarePrimzahlen = new JLabel("Zahlen berechnet bis:");
		lblVerfgbarePrimzahlen.setBounds(10, 140, 109, 14);
		calcPrimesPanel.add(lblVerfgbarePrimzahlen);

		textFieldBerechnetBis = new JTextField();
		textFieldBerechnetBis.setToolTipText("Zeigt die Anzahl der berechneten Primzahlen.");
		textFieldBerechnetBis.setHorizontalAlignment(SwingConstants.TRAILING);
		textFieldBerechnetBis.setText("0");
		textFieldBerechnetBis.setEditable(false);
{'fraction_non_alphanumeric': 0.10372157133011715, 'fraction_numerical': 0.021364576154376293, 'mean_word_length': 6.1220858895705526, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, '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': '3278536', 'n_tokens_mistral': 4351, 'n_tokens_neox': 3878, 'n_words': 813}