<commit_before>package com.dawidweiss.carrot2.browser;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

import org.jdesktop.jdic.browser.WebBrowser;

import com.dawidweiss.carrot.core.local.clustering.RawDocument;
import com.dawidweiss.carrot.core.local.impl.ClustersConsumerOutputComponent;
import com.dawidweiss.carrot.core.local.impl.ClustersConsumerOutputComponent.Result;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.uif_lite.panel.SimpleInternalFrame;

/**
 * A component that displays results of a clustering process.
 *  
 * @author <NAME>
 */
public class ResultsTab extends JPanel {
    private final static String MAIN_CARD = "main";
    private final static String PROGRESS_CARD = "progress";

    private String query;
    private String processId;
    private WebBrowser browserView;
    private RawClustersTree clustersTree;
    private Result result;
    private JPanel cards;
    private JLabel progressInfo;
    
    private class SelfRemoveAction implements ActionListener {
        public void actionPerformed(ActionEvent actionEvent) {
            // find the tabbedpane we belong to.
            Component last = ResultsTab.this;
            while (last != null)
            {
                if (last.getParent() instanceof JTabbedPane)
                {
                    ((JTabbedPane) last.getParent()).remove(last);
                    break;
                }
                last = last.getParent();
            }        
            
        }
    }

    public ResultsTab(String query, String processId) {
        this.query = query;
        this.processId = processId;

        buildSplit();
    }
    
    private void buildSplit() {
        this.setLayout(new BorderLayout());

        SimpleInternalFrame all = new SimpleInternalFrame("Query: "
                + ("".equals(query) ? "<empty>" : query)
                + " (process: " + processId + ")");
        JToolBar toolbar = new JToolBar();
        ToolbarButton closeButton = new ToolbarButton(
                new ImageIcon(this.getClass().getResource("remove.gif")),
                new ImageIcon(this.getClass().getResource("remove_co.gif")));
        closeButton.addActionListener(new SelfRemoveAction());
        closeButton.setToolTipText("Close this tab");
        ToolbarButton homeButton = new ToolbarButton(
                new ImageIcon(this.getClass().getResource("home_nav.gif")),
                new ImageIcon(this.getClass().getResource("home_nav_dis.gif")));
        homeButton.setToolTipText("Display all documents (from all clusters)");
        homeButton.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        showAllDocuments();
                    }
                });
        toolbar.add(homeButton);
        toolbar.add(closeButton);
        all.setToolBar(toolbar);

        JScrollPane scrollerLeft = new JScrollPane();

        this.clustersTree = new RawClustersTree();
        scrollerLeft.getViewport().add(clustersTree);
        scrollerLeft.setBorder(BorderFactory.createEmptyBorder());
        clustersTree.addTreeSelectionListener(
                new RawClustersTreeSelectionListener(this));

        this.browserView = new WebBrowser();

        JScrollPane scrollerRight = new JScrollPane();
        scrollerRight.setBorder(BorderFactory.createEmptyBorder());
        scrollerRight.getViewport().add(browserView);

        // create 'progress' card.
        JPanel progressPane = new JPanel();
        FormLayout fm = new FormLayout(
                "pref:grow,center:min(pref;200px),pref:grow",
                "40px,default,4px,8px");
        progressPane.setLayout(fm);
        progressInfo = new JLabel("");
        CellConstraints cc = new CellConstraints();
        progressPane.add(progressInfo, cc.xy(2,2));
        JProgressBar pindic = new JProgressBar();
        pindic.setIndeterminate(true);
        progressPane.add(pindic, cc.xy(2,4));

        JSplitPane splitPane = new JSplitPane(
                JSplitPane.HORIZONTAL_SPLIT,
                scrollerLeft, scrollerRight);
        splitPane.setDividerLocation(300);
        
        this.cards = new JPanel(new CardLayout());
        cards.add(splitPane, MAIN_CARD);
        cards.add(progressPane, PROGRESS_CARD);
        all.add(cards);
        ((CardLayout)cards.getLayout()).show(cards, PROGRESS_CARD);

        this.add(all, BorderLayout.CENTER);        
    }

    /**
     * Shows error information.
     */
    public void showError(final Throwable e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.flush();
        final String stackTrace = sw.toString();

        showInfo("<html><body>"
                + "<h1>An exception occurred.</h1>"
                + "<h2>" + e.toString() + "</h2>"
                + "<br><br><pre>"<commit_msg>This should help with the embedded browser size problems.<commit_after>package com.dawidweiss.carrot2.browser;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

import org.jdesktop.jdic.browser.WebBrowser;

import com.dawidweiss.carrot.core.local.clustering.RawDocument;
import com.dawidweiss.carrot.core.local.impl.ClustersConsumerOutputComponent;
import com.dawidweiss.carrot.core.local.impl.ClustersConsumerOutputComponent.Result;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.uif_lite.panel.SimpleInternalFrame;

/**
 * A component that displays results of a clustering process.
 *  
 * @author <NAME>
 */
public class ResultsTab extends JPanel {
    private final static String MAIN_CARD = "main";
    private final static String PROGRESS_CARD = "progress";

    private String query;
    private String processId;
    private WebBrowser browserView;
    private RawClustersTree clustersTree;
    private Result result;
    private JPanel cards;
    private JLabel progressInfo;
    
    private class SelfRemoveAction implements ActionListener {
        public void actionPerformed(ActionEvent actionEvent) {
            // find the tabbedpane we belong to.
            Component last = ResultsTab.this;
            while (last != null)
            {
                if (last.getParent() instanceof JTabbedPane)
                {
                    ((JTabbedPane) last.getParent()).remove(last);
                    break;
                }
                last = last.getParent();
            }        
            
        }
    }

    public ResultsTab(String query, String processId) {
        this.query = query;
        this.processId = processId;

        buildSplit();
    }
    
    private void buildSplit() {
        this.setLayout(new BorderLayout());

        SimpleInternalFrame all = new SimpleInternalFrame("Query: "
                + ("".equals(query) ? "<empty>" : query)
                + " (process: " + processId + ")");
        JToolBar toolbar = new JToolBar();
        ToolbarButton closeButton = new ToolbarButton(
                new ImageIcon(this.getClass().getResource("remove.gif")),
                new ImageIcon(this.getClass().getResource("remove_co.gif")));
        closeButton.addActionListener(new SelfRemoveAction());
        closeButton.setToolTipText("Close this tab");
        ToolbarButton homeButton = new ToolbarButton(
                new ImageIcon(this.getClass().getResource("home_nav.gif")),
                new ImageIcon(this.getClass().getResource("home_nav_dis.gif")));
        homeButton.setToolTipText("Display all documents (from all clusters)");
        homeButton.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        showAllDocuments();
                    }
                });
        toolbar.add(homeButton);
        toolbar.add(closeButton);
        all.setToolBar(toolbar);

        JScrollPane scrollerLeft = new JScrollPane();

        this.clustersTree = new RawClustersTree();
        scrollerLeft.getViewport().add(clustersTree);
        scrollerLeft.setBorder(BorderFactory.createEmptyBorder());
        clustersTree.addTreeSelectionListener(
                new RawClustersTreeSelectionListener(this));

        this.browserView = new WebBrowser();

        // create 'progress' card.
        JPanel progressPane = new JPanel();
        FormLayout fm = new FormLayout(
                "pref:grow,center:min(pref;200px),pref:grow",
                "40px,default,4px,8px");
        progressPane.setLayout(fm);
        progressInfo = new JLabel("");
        CellConstraints cc = new CellConstraints();
        progressPane.add(progressInfo, cc.xy(2,2));
        JProgressBar pindic = new JProgressBar();
        pindic.setIndeterminate(true);
        progressPane.add(pindic, cc.xy(2,4));

        final JPanel subPanel = new JPanel(new BorderLayout()) {
            public Dimension getMinimumSize() {
                return new Dimension(0,0);
            }

            public Dimension getPreferredSize() {
                return super.getMinimumSize();
            }
        };

        subPanel.add(browserView, BorderLayout.CENTER);
        JSplitPane splitPane = new JSplitPane(
                JSplitPane.HORIZONTAL_SPLIT,
                scrollerLeft, subPanel);
        splitPane.setDividerLocation(300);
        
        this.cards = new JPanel(new CardLayout());
        cards.add(splitPane, MAIN_CARD);
        cards.add(progressPane, PROGRESS_CARD);
        all.add(cards);
        ((CardLayout)cards.getLayout()).show(cards, PROGRESS_CARD);

        this.add(all, BorderLayout.CENTER);        
    }

    /**
     * Shows error information.
     */
    public void showError(final Throwable e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.flush();
        final String stackTrace = sw.toString();

        showInfo("<html><body>"
                + "<h1>An exception occurred.</h1>"
                + "<h2>" + e.toString() + "</h2>"
                + "<br><br><pre>"
{'fraction_non_alphanumeric': 0.104162976085031, 'fraction_numerical': 0.003720106288751107, 'mean_word_length': 2.5054330953120147, 'pattern_counts': {'":': 0, '<': 25, '<?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': '5664704', 'n_tokens_mistral': 3161, 'n_tokens_neox': 3058, 'n_words': 624}