<commit_before>package wcNaver;

import theCrawler.LabelValueTuple;
import wcNaver.bestChallenge.NaverBCCrawler;
import wcNaver.challenge.Genre;
import wcNaver.challenge.NaverCHCrawler;
import wcNaver.webtoon.*;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


/**
 * GUI for Naver Crawler.
 * TODO: Implement a method to return JPanel so that
 * TODO: NaverCrawlerGUI can be a part of WebtoonCrawler.
 * TODO: Since the initial beta version will only support
 * TODO: Naver webtoon, the current implementation is okay.
 */
public class NaverCrawlerGUI implements NaverConstants {

    private JPanel selectionPanel;
    private JPanel wtListPanel;
    private JPanel myProfilePanel;
    private JPanel saveDirPanel;

    private JScrollPane wtListScrollPane;
    private JScrollPane selSubScrollPane;
    private JScrollPane savePathScrollPane;

    private JLabel mainLabel;
    private JLabel subLabel;
    private JLabel pageLabel;
    private JLabel wtListLabel;
    private JLabel saveDirMsgLabel;
    private JLabel saveDirLabel;
    private JLabel tmpMsgLabel;

    private JList mainSelectorList;
    private JList subSelectorList;

    private JButton getListBtn;
    private JButton chseDirBtn;

    private JFileChooser saveDirChsr;

    private JProgressBar tmpProgBar;

    private JSpinner pageNumSpinner;

    final private Dimension mainDim = new Dimension(650, 780);
    final private Dimension selPanelDim = new Dimension(310, 240);
    final private Dimension myProPanelDim = new Dimension(310, 240);
    final private Dimension wtListPanelDim = new Dimension(622, 500);


    public NaverCrawlerGUI() {

        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
            System.out.println("Failed to set cross-platform look and feel");
            e.printStackTrace();
        }

        JFrame jfrm = new JFrame("네이버 크롤러");
        jfrm.setLayout(new FlowLayout(FlowLayout.CENTER, 2, 2));
        jfrm.setSize(mainDim);
        jfrm.setResizable(false); // Make it not resizable for now.
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setupSelectionPanel();
        setupMyProfilePanel();
        setupWtListScrollPane();
        setupGetListBtn(); // Set up "웹툰 목록 가져오기~" button

        // Add the selectionPanel to the main frame.
        jfrm.add(selectionPanel);
        jfrm.add(myProfilePanel);
        jfrm.add(wtListScrollPane);

        jfrm.setVisible(true);
    }

    private void setupSelectionPanel() {
        // Create a selection panel where users will select cartoons
        selectionPanel = new JPanel();
        selectionPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 2, 2));
        selectionPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        selectionPanel.setPreferredSize(selPanelDim);

        // Create the main selector with a label
        mainLabel = new JLabel("메인 카테고리");
        mainLabel.setPreferredSize(new Dimension(150, 10));
        mainSelectorList = new JList(MAIN_CAT.toArray());
        mainSelectorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        mainSelectorList.setSelectedIndex(0);
        mainSelectorList.setPreferredSize(new Dimension(150, 130));
        mainSelectorList.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));

        // Create the sub selector with a scroll pane.
        selSubScrollPane = new JScrollPane();
        selSubScrollPane.setPreferredSize(new Dimension(150, 130));
        selSubScrollPane.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));

        // Create the sub selector with a label.
        subLabel = new JLabel("하위 카테고리");
        subLabel.setPreferredSize(new Dimension(150, 10));
        subSelectorList = new JList(WEBTOON_CAT.toArray());
        subSelectorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        subSelectorList.setSelectedIndex(0);
        selSubScrollPane.getViewport().setView(subSelectorList);

        // Switch sub selections based on the main selection
        // Also turn page chooser on/off based on the main selection
        mainSelectorList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent le) {
                switch (((LabelValueTuple<String>) mainSelectorList.getSelectedValue()
                ).getValue()) {
                    case "webtoon":
                        subSelectorList.setListData(WEBTOON_CAT.toArray());
                        pageLabel.setEnabled(false);
                        pageNumSpinner.setEnabled(false);
                        break;

                    case "best":
                        subSelectorList.setListData(BEST_CAT.toArray());
                        pageLabel.setEnabled(true);
                        pageNumSpinner.setEnabled(true);
                        break;

                    case "challenge":
                        subSelectorList.setListData(CHALLENGE_CAT.toArray());
                        pageLabel.setEnabled(true);
                        pageNumSpinner.setEnabled(true);
                        break;
                }
                subSelectorList.setSelectedIndex(0);
            }
        });

        // Create a number field to choose a page.
        // Page number starts at 1, and increments by 1.
        SpinnerNumberModel model =
                new SpinnerNumberModel(1, 1, 1, 1);
        // We don't know the last page number, so set it to max.
        model.setMaximum(Integer.MAX_VALUE);
        pageNumSpinner = new JSpinner(model);
        pageNumSpinner.setPreferredSize(new Dimension(50, 20));
        pageNumSpinner.setEnabled(false);

        // Create a label for the page number field.
        pageLabel = new JLabel("페이지 번호 :");
        pageLabel.setEnabled(false);
        pageLabel.setPreferredSize(new Dimension(150, 20));
        pageLabel.setHorizontalAlignment(SwingConstants.RIGHT);

        // Create a dummy label to fill empty space
        JLabel dummyLabel = new JLabel();
        dummyLabel.setPreferredSize(new Dimension(90, 20));

        // Add a button that user can click to download stuff.
        getListBtn = new JButton("웹툰 목록 가져오기~");
        getListBtn.setPreferredSize(new Dimension(150, 20));

        saveDirChsr = new JFileChooser();
        saveDirChsr.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        saveDirMsgLabel = new JLabel("저장 경로를 선택하세요~!");
        saveDirLabel = new JLabel("");

        saveDirPanel = new JPanel();
        saveDirPanel.add(saveDirMsgLabel);
        saveDirPanel.add(saveDirLabel);

        savePathScrollPane = new JScrollPane();
        savePathScrollPane.setPreferredSize(new Dimension(300, 40));
        savePathScrollPane.getViewport().setView(saveDirPanel);

        chseDirBtn = new JButton("저장 경로 선택");
        chseDirBtn.setPreferredSize(new Dimension(120, 20));
        chseDirBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (saveDirChsr.showOpenDialog(selectionPanel)
                        == JFileChooser.APPROVE_OPTION) {
                    saveDirMsgLabel.setText("저장경로 : ");
                    saveDirLabel.setText(
                            saveDirChsr.getSelectedFile().getAbsolutePath());
                }
            }
        });<commit_msg>When user inputs path, it is checked if it is valid.<commit_after>package wcNaver;

import theCrawler.LabelValueTuple;
import wcNaver.bestChallenge.NaverBCCrawler;
import wcNaver.challenge.Genre;
import wcNaver.challenge.NaverCHCrawler;
import wcNaver.webtoon.*;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;


/**
 * GUI for Naver Crawler.
 * TODO: Implement a method to return JPanel so that
 * TODO: NaverCrawlerGUI can be a part of WebtoonCrawler.
 * TODO: Since the initial beta version will only support
 * TODO: Naver webtoon, the current implementation is okay.
 */
public class NaverCrawlerGUI implements NaverConstants {

    private JPanel selectionPanel;
    private JPanel wtListPanel;
    private JPanel myProfilePanel;
    private JPanel saveDirPanel;

    private JScrollPane wtListScrollPane;
    private JScrollPane selSubScrollPane;
    private JScrollPane savePathScrollPane;

    private JLabel mainLabel;
    private JLabel subLabel;
    private JLabel pageLabel;
    private JLabel wtListLabel;
    private JLabel saveDirMsgLabel;
    private JLabel saveDirLabel;
    private JLabel tmpMsgLabel;

    private JList mainSelectorList;
    private JList subSelectorList;

    private JButton getListBtn;
    private JButton chseDirBtn;

    private JFileChooser saveDirChsr;

    private JProgressBar tmpProgBar;

    private JSpinner pageNumSpinner;

    final private Dimension mainDim = new Dimension(650, 780);
    final private Dimension selPanelDim = new Dimension(310, 240);
    final private Dimension myProPanelDim = new Dimension(310, 240);
    final private Dimension wtListPanelDim = new Dimension(622, 500);


    public NaverCrawlerGUI() {

        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
            System.out.println("Failed to set cross-platform look and feel");
            e.printStackTrace();
        }

        JFrame jfrm = new JFrame("네이버 크롤러");
        jfrm.setLayout(new FlowLayout(FlowLayout.CENTER, 2, 2));
        jfrm.setSize(mainDim);
        jfrm.setResizable(false); // Make it not resizable for now.
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setupSelectionPanel();
        setupMyProfilePanel();
        setupWtListScrollPane();
        setupGetListBtn(); // Set up "웹툰 목록 가져오기~" button

        // Add the selectionPanel to the main frame.
        jfrm.add(selectionPanel);
        jfrm.add(myProfilePanel);
        jfrm.add(wtListScrollPane);

        jfrm.setVisible(true);
    }

    private void setupSelectionPanel() {
        // Create a selection panel where users will select cartoons
        selectionPanel = new JPanel();
        selectionPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 2, 2));
        selectionPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        selectionPanel.setPreferredSize(selPanelDim);

        // Create the main selector with a label
        mainLabel = new JLabel("메인 카테고리");
        mainLabel.setPreferredSize(new Dimension(150, 10));
        mainSelectorList = new JList(MAIN_CAT.toArray());
        mainSelectorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        mainSelectorList.setSelectedIndex(0);
        mainSelectorList.setPreferredSize(new Dimension(150, 130));
        mainSelectorList.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));

        // Create the sub selector with a scroll pane.
        selSubScrollPane = new JScrollPane();
        selSubScrollPane.setPreferredSize(new Dimension(150, 130));
        selSubScrollPane.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));

        // Create the sub selector with a label.
        subLabel = new JLabel("하위 카테고리");
        subLabel.setPreferredSize(new Dimension(150, 10));
        subSelectorList = new JList(WEBTOON_CAT.toArray());
        subSelectorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        subSelectorList.setSelectedIndex(0);
        selSubScrollPane.getViewport().setView(subSelectorList);

        // Switch sub selections based on the main selection
        // Also turn page chooser on/off based on the main selection
        mainSelectorList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent le) {
                switch (((LabelValueTuple<String>) mainSelectorList.getSelectedValue()
                ).getValue()) {
                    case "webtoon":
                        subSelectorList.setListData(WEBTOON_CAT.toArray());
                        pageLabel.setEnabled(false);
                        pageNumSpinner.setEnabled(false);
                        break;

                    case "best":
                        subSelectorList.setListData(BEST_CAT.toArray());
                        pageLabel.setEnabled(true);
                        pageNumSpinner.setEnabled(true);
                        break;

                    case "challenge":
                        subSelectorList.setListData(CHALLENGE_CAT.toArray());
                        pageLabel.setEnabled(true);
                        pageNumSpinner.setEnabled(true);
                        break;
                }
                subSelectorList.setSelectedIndex(0);
            }
        });

        // Create a number field to choose a page.
        // Page number starts at 1, and increments by 1.
        SpinnerNumberModel model =
                new SpinnerNumberModel(1, 1, 1, 1);
        // We don't know the last page number, so set it to max.
        model.setMaximum(Integer.MAX_VALUE);
        pageNumSpinner = new JSpinner(model);
        pageNumSpinner.setPreferredSize(new Dimension(50, 20));
        pageNumSpinner.setEnabled(false);

        // Create a label for the page number field.
        pageLabel = new JLabel("페이지 번호 :");
        pageLabel.setEnabled(false);
        pageLabel.setPreferredSize(new Dimension(150, 20));
        pageLabel.setHorizontalAlignment(SwingConstants.RIGHT);

        // Create a dummy label to fill empty space
        JLabel dummyLabel = new JLabel();
        dummyLabel.setPreferredSize(new Dimension(90, 20));

        // Add a button that user can click to download stuff.
        getListBtn = new JButton("웹툰 목록 가져오기~");
        getListBtn.setPreferredSize(new Dimension(150, 20));

        saveDirChsr = new JFileChooser();
        saveDirChsr.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        saveDirMsgLabel = new JLabel("저장 경로를 선택하세요~!");
        saveDirLabel = new JLabel("");

        saveDirPanel = new JPanel();
        saveDirPanel.add(saveDirMsgLabel);
        saveDirPanel.add(saveDirLabel);

        savePathScrollPane = new JScrollPane();
        savePathScrollPane.setPreferredSize(new Dimension(300, 40));
        savePathScrollPane.getViewport().setView(saveDirPanel);

        chseDirBtn = new JButton("저장 경로 선택");
        chseDirBtn.setPreferredSize(new Dimension(120, 20));
        chseDirBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (saveDirChsr.showOpenDialog(selectionPanel)
                        == JFileChooser.APPROVE_OPTION) {

                    // Check if the user didn't make a mistake
                    Path chosenPath
                            = Paths.get(saveDirChsr.getSelectedFile().getAbsolutePath());
                    if(!Files.exists(chosenPath)) {
                        saveDirMsgLabel.setText(chosenPath +
                                "는 존재하지않는 경로 입니다. :-p");
                        saveDirLabel.setText("");
                    } else {
                        saveDirMsgLabel.setText("저장경로 : ");
                        saveDirLabel.setText(
                                saveDirChsr.getSelectedFile().getAbsolutePath());
                    }

                }
            }
        });
{'fraction_non_alphanumeric': 0.08388241256969083, 'fraction_numerical': 0.011403953370501775, 'mean_word_length': 2.458588957055215, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '342699', 'n_tokens_mistral': 4605, 'n_tokens_neox': 4394, 'n_words': 998}