Commit 260ad7dc authored by Leon Zeyu Long's avatar Leon Zeyu Long
Browse files

MainApp.java - Swing GUI

parent c6bda859
Loading
Loading
Loading
Loading
+128 −0
Original line number Diff line number Diff line
package edu.bu.LanguageCorrection;

import javax.swing.*;
import java.awt.*;
// import java.awt.event.ActionEvent;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class MainApp extends JFrame {
    private final JTextField urlField;
    private final JTextArea resultArea;
    private final JButton runButton;
    private final JComboBox<String> moduleSelector;

    public MainApp() {
        super("Language Correction Tool");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());

        // Module selector
        String[] modules = {"Select Module", "Crawler", "Checker", "Corrector"};
        moduleSelector = new JComboBox<>(modules);

        // User inout entry field and run button
        urlField = new JTextField();
        runButton = new JButton("Run");
        runButton.addActionListener(e -> {
            String selectedModule = (String) moduleSelector.getSelectedItem();
            String input = urlField.getText().trim();

            switch (selectedModule) {
                case "Crawler":
                    runCrawler(input);
                    break;
                case "Checker":
                    runChecker(input);
                    break;
                case "Corrector":
                    runCorrector(input);
                    break;
                default:
                    JOptionPane.showMessageDialog(this, "Select a valid module");
            }
        });
        JPanel northPanel = new JPanel(new BorderLayout());
        northPanel.add(moduleSelector, BorderLayout.WEST);
        northPanel.add(urlField, BorderLayout.CENTER);
        northPanel.add(runButton, BorderLayout.EAST);

        // Result area
        resultArea = new JTextArea();
        resultArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(resultArea);

        add(northPanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.CENTER);
    }

    private void runCrawler(String input) {
        crawler webCrawler = new crawler();
        if (input.startsWith("http")) {
            // Input is a single URL
            webCrawler.add_to_queue(input);
        } else {
            // Input is file path
            try {
                List<String> lines = Files.readAllLines(Paths.get(input));
                lines.forEach(webCrawler::add_to_queue);
            } catch (Exception e) {
                resultArea.setText("Error reading file: " + e.getMessage());
                return;
            }
        }

        // Redirect output to the GUI
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(outputStream);
        PrintStream oldStream = System.out;
        System.setOut(printStream);

        // Run the crawler
        webCrawler.crawl(1); // Adjust the limit (DEFAULT SET TO 1)

        // Restore standard output and update the GUI
        System.setOut(oldStream);
        resultArea.setText(outputStream.toString());
    }

    private void runChecker(String filePath) {
        try {
            String content = new String(Files.readAllBytes(Paths.get(filePath)));
            Checker checker = new Checker();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PrintStream ps = new PrintStream(baos);
            PrintStream old = System.out;
            System.setOut(ps);
            checker.analyze(content);
            System.out.flush();
            System.setOut(old);
            resultArea.setText(baos.toString());
        } catch (Exception e) {
            resultArea.setText("Error: " + e.getMessage());
        }
    }

    private void runCorrector(String filePath) {
        try {
            String content = new String(Files.readAllBytes(Paths.get(filePath)));
            Corrector corrector = new Corrector();
            String corrected = corrector.correct(content);
            resultArea.setText("Corrected Text:\n" + corrected);
        } catch (Exception e) {
            resultArea.setText("Error: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            MainApp mainApp = new MainApp();
            mainApp.setVisible(true);
        });
    }
}