<commit_before>package cz.slahora.compling.gui.ui.words;

import cz.compling.analysis.analysator.frequency.words.IWordFrequency;
import cz.slahora.compling.gui.analysis.ToggleHeader;
import cz.slahora.compling.gui.analysis.assonance.NonEditableTable;
import cz.slahora.compling.gui.analysis.words.WordTextAnalysisType;
import cz.slahora.compling.gui.model.CsvData;
import cz.slahora.compling.gui.model.WorkingText;
import cz.slahora.compling.gui.ui.*;
import cz.slahora.compling.gui.utils.GridBagConstraintBuilder;
import cz.slahora.compling.gui.utils.HtmlLabelBuilder;
import org.jdesktop.swingx.JXCollapsiblePane;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.Plot;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;

public class WordFrequencyResultsPanel<T> extends AbstractResultsPanel implements ResultsPanel {

	private final IWordFrequenciesModel<T> model;
	private final WordFrequencyChartFactory<T> chartFactory;
	private final WordTextAnalysisType analysisType;

	private JComponent allWordsChartComponent;
	private JComponent compareChartComponent;
	private final JPanel allWordsChartParent = new JPanel(new BorderLayout());
	private final JPanel compareChartParent  = new JPanel(new BorderLayout());


	private final JSpinner lowerFreqBound;
	private ChangeListener boundChangedListener;

	public WordFrequencyResultsPanel(WordTextAnalysisType analysisType, Map<WorkingText, IWordFrequency> wordFrequencies, Factory<T> factory) {
		super(new ResultsScrollablePanel());
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

		this.analysisType = analysisType;
		switch (analysisType) {
			case WORD:
				this.model = factory.createModel(wordFrequencies);
				this.chartFactory = factory.createChartFactory(model);
				break;
			case WORD_LENGHT:
				this.model = factory.createModel(wordFrequencies);
				this.chartFactory = factory.createChartFactory(model);
				break;
			default:
				throw new IllegalStateException("Cannot create model for type " + analysisType);
		}


		SpinnerModel spinnerModel = new SpinnerNumberModel(1, 1, model.getFilterMaxValue(), 1);
		lowerFreqBound = new JSpinner(spinnerModel);
	}

	@Override
	public JPanel getPanel() {
		panel.removeAll();

		JLabel headline1 = new HtmlLabelBuilder().hx(1, "Frekvence výskytu slov").build();
		headline1.setAlignmentX(Component.LEFT_ALIGNMENT);
		panel.add(headline1);

		MultipleLinesLabel text = new MultipleLinesLabel(model.getMainParagraphText());
		text.setAlignmentX(Component.LEFT_ALIGNMENT);
		panel.add(text);

		JLabel tableHeadline;
		if (analysisType == WordTextAnalysisType.WORD) {
			tableHeadline = new HtmlLabelBuilder().hx(1, "Tabulka četností výskytu slov").build();
		} else {
			tableHeadline = new HtmlLabelBuilder().hx(1, "Výskyt slov dle délky slova").build();
		}
		tableHeadline.setAlignmentX(Component.LEFT_ALIGNMENT);
		panel.add(createVerticalSpace());
		panel.add(tableHeadline);
		//table with words occurrences
		JTable table = new NonEditableTable(model.getTableModel());
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setRowSelectionAllowed(true);
		table.setColumnSelectionAllowed(false);
		table.setAutoCreateRowSorter(true);
		table.setBackground(Color.WHITE);
		table.invalidate();

		final JTableHeader tableHeader = table.getTableHeader();
		tableHeader.setAlignmentX(Component.LEFT_ALIGNMENT);
		panel.add(tableHeader);

		table.setAlignmentX(Component.LEFT_ALIGNMENT);
		panel.add(table);

		if (analysisType == WordTextAnalysisType.WORD_LENGHT) {

			final JXCollapsiblePane chiSquareTestsPanel = new JXCollapsiblePane();
			chiSquareTestsPanel.setBackground(Color.white);
			chiSquareTestsPanel.setLayout(new BoxLayout(chiSquareTestsPanel.getContentPane(), BoxLayout.Y_AXIS));

			chiSquareTestsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);

			ToggleHeader toggle = new ToggleHeader(chiSquareTestsPanel,
				new HtmlLabelBuilder().hx(2, "\u03C7<sup>2</sup> test").build().getText());
			toggle.setAlignmentX(Component.LEFT_ALIGNMENT);

			panel.add(createVerticalSpace());
			panel.add(toggle);

			final Set<WorkingText> allTexts = model.getAllTexts();
			if (allTexts.size() > 1) {
				WorkingText texts[] = allTexts.toArray(new WorkingText[allTexts.size()]);
				final JPanel selectTextPanel = new JPanel();
				selectTextPanel.setAlignmentX(Component.LEFT_ALIGNMENT);

				JComboBox<WorkingText> selectTextCombo = new JComboBox<WorkingText>(texts);
				selectTextCombo.setSelectedItem(null);
				selectTextCombo.addItemListener(new ItemListener() {<commit_msg>analýza četnosti slov -- vysouvací tabulka<commit_after>package cz.slahora.compling.gui.ui.words;

import cz.compling.analysis.analysator.frequency.words.IWordFrequency;
import cz.slahora.compling.gui.analysis.ToggleHeader;
import cz.slahora.compling.gui.analysis.assonance.NonEditableTable;
import cz.slahora.compling.gui.analysis.words.WordTextAnalysisType;
import cz.slahora.compling.gui.model.CsvData;
import cz.slahora.compling.gui.model.WorkingText;
import cz.slahora.compling.gui.ui.*;
import cz.slahora.compling.gui.utils.GridBagConstraintBuilder;
import cz.slahora.compling.gui.utils.HtmlLabelBuilder;
import org.jdesktop.swingx.JXCollapsiblePane;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.Plot;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;

public class WordFrequencyResultsPanel<T> extends AbstractResultsPanel implements ResultsPanel {

	private final IWordFrequenciesModel<T> model;
	private final WordFrequencyChartFactory<T> chartFactory;
	private final WordTextAnalysisType analysisType;

	private JComponent allWordsChartComponent;
	private JComponent compareChartComponent;
	private final JPanel allWordsChartParent = new JPanel(new BorderLayout());
	private final JPanel compareChartParent  = new JPanel(new BorderLayout());


	private final JSpinner lowerFreqBound;
	private ChangeListener boundChangedListener;

	public WordFrequencyResultsPanel(WordTextAnalysisType analysisType, Map<WorkingText, IWordFrequency> wordFrequencies, Factory<T> factory) {
		super(new ResultsScrollablePanel());
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

		this.analysisType = analysisType;
		switch (analysisType) {
			case WORD:
				this.model = factory.createModel(wordFrequencies);
				this.chartFactory = factory.createChartFactory(model);
				break;
			case WORD_LENGHT:
				this.model = factory.createModel(wordFrequencies);
				this.chartFactory = factory.createChartFactory(model);
				break;
			default:
				throw new IllegalStateException("Cannot create model for type " + analysisType);
		}


		SpinnerModel spinnerModel = new SpinnerNumberModel(1, 1, model.getFilterMaxValue(), 1);
		lowerFreqBound = new JSpinner(spinnerModel);
	}

	@Override
	public JPanel getPanel() {
		panel.removeAll();

		JLabel headline1 = new HtmlLabelBuilder().hx(1, "Frekvence výskytu slov").build();
		headline1.setAlignmentX(Component.LEFT_ALIGNMENT);
		panel.add(headline1);

		MultipleLinesLabel text = new MultipleLinesLabel(model.getMainParagraphText());
		text.setAlignmentX(Component.LEFT_ALIGNMENT);
		panel.add(text);

		//table with words occurrences
		JTable table = new NonEditableTable(model.getTableModel());
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setRowSelectionAllowed(true);
		table.setColumnSelectionAllowed(false);
		table.setAutoCreateRowSorter(true);
		table.setBackground(Color.WHITE);
		table.invalidate();

		final JTableHeader tableHeader = table.getTableHeader();
		tableHeader.setAlignmentX(Component.LEFT_ALIGNMENT);

		table.setAlignmentX(Component.LEFT_ALIGNMENT);
		JXCollapsiblePane tablePanel = new JXCollapsiblePane(new BorderLayout());
		tablePanel.setAlignmentX(Component.LEFT_ALIGNMENT);
		tablePanel.setCollapsed(true);
		tablePanel.setBackground(Color.white);

		tablePanel.add(tableHeader, BorderLayout.NORTH);
		tablePanel.add(table, BorderLayout.CENTER);

		ToggleHeader tableHeadline;
		if (analysisType == WordTextAnalysisType.WORD) {
			tableHeadline = new ToggleHeader(tablePanel, new HtmlLabelBuilder().hx(2, "Tabulka četností výskytu slov").build().getText());
		} else {
			tableHeadline = new ToggleHeader(tablePanel, new HtmlLabelBuilder().hx(2, "Výskyt slov dle délky slova").build().getText());
		}
		tableHeadline.setAlignmentX(Component.LEFT_ALIGNMENT);

		panel.add(createVerticalSpace());
		panel.add(tableHeadline);
		panel.add(tablePanel);

		if (analysisType == WordTextAnalysisType.WORD_LENGHT) {

			final JXCollapsiblePane chiSquareTestsPanel = new JXCollapsiblePane();
			chiSquareTestsPanel.setBackground(Color.white);
			chiSquareTestsPanel.setLayout(new BoxLayout(chiSquareTestsPanel.getContentPane(), BoxLayout.Y_AXIS));

			chiSquareTestsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);

			ToggleHeader toggle = new ToggleHeader(chiSquareTestsPanel,
				new HtmlLabelBuilder().hx(2, "\u03C7<sup>2</sup> test").build().getText());
			toggle.setAlignmentX(Component.LEFT_ALIGNMENT);

			panel.add(createVerticalSpace());
			panel.add(toggle);

			final Set<WorkingText> allTexts = model.getAllTexts();
			if (allTexts.size() > 1) {
				WorkingText texts[] = allTexts.toArray(new WorkingText[allTexts.size()]);
				final JPanel selectTextPanel = new JPanel();
				selectTextPanel.setAlignmentX(Component.LEFT_ALIGNMENT);

				JComboBox<WorkingText> selectTextCombo = new JComboBox<WorkingText>(texts);
				selectTextCombo.setSelectedItem(null);
				selectTextCombo.addItemListener(new ItemListener() {
{'fraction_non_alphanumeric': 0.1049618320610687, 'fraction_numerical': 0.0028625954198473282, 'mean_word_length': 6.24326192121631, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, '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': '5398774', 'n_tokens_mistral': 3537, 'n_tokens_neox': 3249, 'n_words': 559}